3
3
const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
4
4
const uint8ArrayToString = require ( 'uint8arrays/to-string' )
5
5
const log = require ( 'debug' ) ( 'ipfs-http-client:pubsub:subscribe' )
6
- const SubscriptionTracker = require ( './subscription-tracker' )
7
6
const configure = require ( '../lib/configure' )
8
7
const toUrlSearchParams = require ( '../lib/to-url-search-params' )
9
8
@@ -12,70 +11,75 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
12
11
* @typedef {import('ipfs-core-types/src/pubsub').Message } Message
13
12
* @typedef {(err: Error, fatal: boolean, msg?: Message) => void } ErrorHandlerFn
14
13
* @typedef {import('ipfs-core-types/src/pubsub').API<HTTPClientExtraOptions & { onError?: ErrorHandlerFn }> } PubsubAPI
14
+ * @typedef {import('../types').Options } Options
15
15
*/
16
16
17
- module . exports = configure ( ( api , options ) => {
18
- const subsTracker = SubscriptionTracker . singleton ( )
19
-
20
- /**
21
- * @type {PubsubAPI["subscribe"] }
22
- */
23
- async function subscribe ( topic , handler , options = { } ) { // eslint-disable-line require-await
24
- options . signal = subsTracker . subscribe ( topic , handler , options . signal )
25
-
26
- /** @type {(value?: any) => void } */
27
- let done
28
- /** @type {(error: Error) => void } */
29
- let fail
30
-
31
- const result = new Promise ( ( resolve , reject ) => {
32
- done = resolve
33
- fail = reject
34
- } )
35
-
36
- // In Firefox, the initial call to fetch does not resolve until some data
37
- // is received. If this doesn't happen within 1 second assume success
38
- const ffWorkaround = setTimeout ( ( ) => done ( ) , 1000 )
39
-
40
- // Do this async to not block Firefox
41
- setTimeout ( ( ) => {
42
- api . post ( 'pubsub/sub' , {
43
- timeout : options . timeout ,
44
- signal : options . signal ,
45
- searchParams : toUrlSearchParams ( {
46
- arg : topic ,
47
- ...options
48
- } ) ,
49
- headers : options . headers
17
+ /**
18
+ * @param {Options } options
19
+ * @param {import('./subscription-tracker') } subsTracker
20
+ */
21
+ module . exports = ( options , subsTracker ) => {
22
+ return configure ( ( api ) => {
23
+ /**
24
+ * @type {PubsubAPI["subscribe"] }
25
+ */
26
+ async function subscribe ( topic , handler , options = { } ) { // eslint-disable-line require-await
27
+ options . signal = subsTracker . subscribe ( topic , handler , options . signal )
28
+
29
+ /** @type {(value?: any) => void } */
30
+ let done
31
+ /** @type {(error: Error) => void } */
32
+ let fail
33
+
34
+ const result = new Promise ( ( resolve , reject ) => {
35
+ done = resolve
36
+ fail = reject
50
37
} )
51
- . catch ( ( err ) => {
52
- // Initial subscribe fail, ensure we clean up
53
- subsTracker . unsubscribe ( topic , handler )
54
38
55
- fail ( err )
39
+ // In Firefox, the initial call to fetch does not resolve until some data
40
+ // is received. If this doesn't happen within 1 second assume success
41
+ const ffWorkaround = setTimeout ( ( ) => done ( ) , 1000 )
42
+
43
+ // Do this async to not block Firefox
44
+ setTimeout ( ( ) => {
45
+ api . post ( 'pubsub/sub' , {
46
+ timeout : options . timeout ,
47
+ signal : options . signal ,
48
+ searchParams : toUrlSearchParams ( {
49
+ arg : topic ,
50
+ ...options
51
+ } ) ,
52
+ headers : options . headers
56
53
} )
57
- . then ( ( response ) => {
58
- clearTimeout ( ffWorkaround )
59
-
60
- if ( ! response ) {
61
- // if there was no response, the subscribe failed
62
- return
63
- }
64
-
65
- readMessages ( response , {
66
- onMessage : handler ,
67
- onEnd : ( ) => subsTracker . unsubscribe ( topic , handler ) ,
68
- onError : options . onError
54
+ . catch ( ( err ) => {
55
+ // Initial subscribe fail, ensure we clean up
56
+ subsTracker . unsubscribe ( topic , handler )
57
+
58
+ fail ( err )
69
59
} )
60
+ . then ( ( response ) => {
61
+ clearTimeout ( ffWorkaround )
70
62
71
- done ( )
72
- } )
73
- } , 0 )
63
+ if ( ! response ) {
64
+ // if there was no response, the subscribe failed
65
+ return
66
+ }
74
67
75
- return result
76
- }
77
- return subscribe
78
- } )
68
+ readMessages ( response , {
69
+ onMessage : handler ,
70
+ onEnd : ( ) => subsTracker . unsubscribe ( topic , handler ) ,
71
+ onError : options . onError
72
+ } )
73
+
74
+ done ( )
75
+ } )
76
+ } , 0 )
77
+
78
+ return result
79
+ }
80
+ return subscribe
81
+ } ) ( options )
82
+ }
79
83
80
84
/**
81
85
* @param {import('ipfs-utils/src/types').ExtendedResponse } response
0 commit comments