1
- import type { IPFS } from 'ipfs-core'
1
+ import { IPFS } from 'ipfs-core-types '
2
2
import type { libp2p as Libp2p } from 'ipfs-core/src/components/network'
3
+ import { CID } from 'multiformats/cid'
4
+ import { Store } from '.'
3
5
import { PubSub } from './pubsub'
4
- import { Store } from './store'
5
6
6
7
export interface Peer {
7
8
id : string
8
9
address : string
9
10
}
10
11
11
- export class Stack < TMessage = any > {
12
+ export interface PeerAnnouncement {
13
+ kind : 'announcement'
14
+ peer : Peer
15
+ }
16
+
17
+ export type StackPubSubMessage = PeerAnnouncement
18
+
19
+ export class Stack {
20
+ public pubsub : PubSub < StackPubSubMessage >
12
21
public store : Store
13
- public pubsub : PubSub < TMessage >
14
22
15
- private constructor ( public namespace : string , public ipfs : IPFS , public id : string ) {
16
- this . pubsub = new PubSub ( ipfs , namespace )
17
- this . store = new Store ( ipfs , namespace , this . pubsub )
23
+ private announceInterval ?: ReturnType < typeof setTimeout >
24
+ public announce = false
25
+
26
+ constructor ( public namespace : CID , public ipfs : IPFS ) {
27
+ this . pubsub = new PubSub ( ipfs , namespace . toString ( ) )
28
+ this . store = new Store ( this )
29
+ }
30
+
31
+ /**
32
+ * get this peer info
33
+ */
34
+ public async id ( ) : Promise < Peer > {
35
+ const result = await this . ipfs . id ( )
36
+
37
+ return {
38
+ id : result . id ,
39
+ address : result . addresses [ 0 ] . toString ( )
40
+ }
18
41
}
19
42
20
43
private get libp2p ( ) {
21
44
return ( this . ipfs as any ) . libp2p as Libp2p
22
45
}
23
46
47
+ public async start ( ) : Promise < void > {
48
+ this . announceInterval = setInterval ( async ( ) => {
49
+ if ( ! this . announce ) return
50
+ await this . pubsub . publish ( 'announce' , {
51
+ kind : 'announcement' ,
52
+ peer : await this . id ( )
53
+ } )
54
+ } , 250 )
55
+
56
+ await this . store . start ( )
57
+ }
58
+
59
+ public async stop ( ) : Promise < void > {
60
+ if ( this . announceInterval ) clearInterval ( this . announceInterval )
61
+ await this . store . stop ( )
62
+ await this . pubsub . stop ( )
63
+ }
64
+
24
65
/**
25
66
* Create stack
26
67
*
@@ -29,10 +70,11 @@ export class Stack<TMessage = any> {
29
70
* @returns Stack instance
30
71
*/
31
72
public static async create ( namespace : string , ipfs : IPFS ) {
32
- const { id } = await ipfs . id ( )
33
- const stack = new Stack ( namespace , ipfs , id )
73
+ const cid = await ipfs . dag . put ( { namespace } )
74
+
75
+ const stack = new Stack ( cid , ipfs )
76
+ await stack . start ( )
34
77
35
- await stack . store . start ( )
36
78
return stack
37
79
}
38
80
@@ -88,12 +130,4 @@ export class Stack<TMessage = any> {
88
130
} )
89
131
} )
90
132
}
91
-
92
- /**
93
- * Start logging debug events
94
- */
95
- public debug ( ) : void {
96
- this . onPeerConnect ( ( peer ) => console . log ( 'New peer connected' , peer . id , peer . address ) )
97
- this . onPeerDisconnected ( ( peer ) => console . log ( 'Peer disconnected' , peer . id , peer . address ) )
98
- }
99
133
}
0 commit comments