@@ -4,11 +4,12 @@ const { identity } = require("lodash");
4
4
/**
5
5
* Cross-packages synchronization context.
6
6
* @typedef Synchronizer
7
- * @param {EventEmitter } ee Shared signal bus
7
+ * @param {EventEmitter } ee Shared event emitter class.
8
8
* @param {Function } todo Gets the list of packages which are still todo
9
- * @params {Function} waitForAll return a promise that waits until all the packages have the same target probe value.
10
- * @params {Function} announce Attach expected state promise to target package.
11
- * @params {Function} announceForAll Attach expected state promise to all packages.
9
+ * @param {Function } once Memoized event subscriber.
10
+ * @param {Function } emit Memoized event emitter.
11
+ * @params {Function} waitFor Function returns a promise that waits until the package has target probe value.
12
+ * @params {Function} waitForAll Function returns a promise that waits until all the packages have the same target probe value.
12
13
*/
13
14
14
15
/**
@@ -28,17 +29,22 @@ const getSynchronizer = (packages) => {
28
29
evt : { } ,
29
30
subscr : { } ,
30
31
} ;
32
+
31
33
const emit = ( probe , pkg ) => {
32
34
const name = getEventName ( probe , pkg ) ;
33
-
34
35
return store . evt [ name ] || ( store . evt [ name ] = ee . emit ( name ) ) ;
35
36
} ;
37
+
36
38
const once = ( probe , pkg ) => {
37
39
const name = getEventName ( probe , pkg ) ;
38
-
39
40
return store . evt [ name ] || store . subscr [ name ] || ( store . subscr [ name ] = ee . once ( name ) ) ;
40
41
} ;
41
42
43
+ const waitFor = ( probe , pkg ) => {
44
+ const name = getEventName ( probe , pkg ) ;
45
+ return pkg [ name ] || ( pkg [ name ] = once ( probe , pkg ) ) ;
46
+ } ;
47
+
42
48
// Status sync point.
43
49
const waitForAll = ( probe , filter = identity ) => {
44
50
const promise = once ( probe ) ;
@@ -54,9 +60,6 @@ const getSynchronizer = (packages) => {
54
60
return promise ;
55
61
} ;
56
62
57
- const announce = ( probe , pkg ) => ( pkg [ probe ] = once ( probe , pkg ) ) ;
58
- const announceForAll = ( probe ) => todo ( ) . forEach ( ( p ) => announce ( probe , p ) ) ;
59
-
60
63
// Only the first lucky package passes the probe.
61
64
const getLucky = ( probe , pkg ) => {
62
65
if ( getLucky [ probe ] ) {
@@ -71,8 +74,7 @@ const getSynchronizer = (packages) => {
71
74
emit,
72
75
once,
73
76
todo,
74
- announce,
75
- announceForAll,
77
+ waitFor,
76
78
waitForAll,
77
79
getLucky,
78
80
} ;
0 commit comments