@@ -22,6 +22,9 @@ function InspectorManager(inspectors, logger) {
2222
2323 /**
2424 * Collection of inspectors keyed by type.
25+ *
26+ * Inspectors are async by default.
27+ *
2528 * @type {{[type: string]: object[]} }
2629 */
2730 const inspectorsByType = {
@@ -30,14 +33,30 @@ function InspectorManager(inspectors, logger) {
3033 [ InspectorTypes . flagDetailChanged ] : [ ] ,
3134 [ InspectorTypes . clientIdentityChanged ] : [ ] ,
3235 } ;
36+ /**
37+ * Collection synchronous of inspectors keyed by type.
38+ *
39+ * @type {{[type: string]: object[]} }
40+ */
41+ const synchronousInspectorsByType = {
42+ [ InspectorTypes . flagUsed ] : [ ] ,
43+ [ InspectorTypes . flagDetailsChanged ] : [ ] ,
44+ [ InspectorTypes . flagDetailChanged ] : [ ] ,
45+ [ InspectorTypes . clientIdentityChanged ] : [ ] ,
46+ } ;
3347
3448 const safeInspectors = inspectors && inspectors . map ( inspector => SafeInspector ( inspector , logger ) ) ;
3549
3650 safeInspectors &&
3751 safeInspectors . forEach ( safeInspector => {
3852 // Only add inspectors of supported types.
39- if ( Object . prototype . hasOwnProperty . call ( inspectorsByType , safeInspector . type ) ) {
53+ if ( Object . prototype . hasOwnProperty . call ( inspectorsByType , safeInspector . type ) && ! safeInspector . synchronous ) {
4054 inspectorsByType [ safeInspector . type ] . push ( safeInspector ) ;
55+ } else if (
56+ Object . prototype . hasOwnProperty . call ( synchronousInspectorsByType , safeInspector . type ) &&
57+ safeInspector . synchronous
58+ ) {
59+ synchronousInspectorsByType [ safeInspector . type ] . push ( safeInspector ) ;
4160 } else {
4261 logger . warn ( messages . invalidInspector ( safeInspector . type , safeInspector . name ) ) ;
4362 }
@@ -49,7 +68,9 @@ function InspectorManager(inspectors, logger) {
4968 * @param {string } type The type of the inspector to check.
5069 * @returns True if there are any inspectors of that type registered.
5170 */
52- manager . hasListeners = type => inspectorsByType [ type ] && inspectorsByType [ type ] . length ;
71+ manager . hasListeners = type =>
72+ ( inspectorsByType [ type ] && inspectorsByType [ type ] . length ) ||
73+ ( synchronousInspectorsByType [ type ] && synchronousInspectorsByType [ type ] . length ) ;
5374
5475 /**
5576 * Notify registered inspectors of a flag being used.
@@ -61,9 +82,13 @@ function InspectorManager(inspectors, logger) {
6182 * @param {Object } context The LDContext for the flag.
6283 */
6384 manager . onFlagUsed = ( flagKey , detail , context ) => {
64- if ( inspectorsByType [ InspectorTypes . flagUsed ] . length ) {
85+ const type = InspectorTypes . flagUsed ;
86+ if ( synchronousInspectorsByType [ type ] . length ) {
87+ synchronousInspectorsByType [ type ] . forEach ( inspector => inspector . method ( flagKey , detail , context ) ) ;
88+ }
89+ if ( inspectorsByType [ type ] . length ) {
6590 onNextTick ( ( ) => {
66- inspectorsByType [ InspectorTypes . flagUsed ] . forEach ( inspector => inspector . method ( flagKey , detail , context ) ) ;
91+ inspectorsByType [ type ] . forEach ( inspector => inspector . method ( flagKey , detail , context ) ) ;
6792 } ) ;
6893 }
6994 } ;
@@ -76,9 +101,13 @@ function InspectorManager(inspectors, logger) {
76101 * @param {Record<string, Object> } flags The current flags as a Record<string, LDEvaluationDetail>.
77102 */
78103 manager . onFlags = flags => {
79- if ( inspectorsByType [ InspectorTypes . flagDetailsChanged ] . length ) {
104+ const type = InspectorTypes . flagDetailsChanged ;
105+ if ( synchronousInspectorsByType [ type ] . length ) {
106+ synchronousInspectorsByType [ type ] . forEach ( inspector => inspector . method ( flags ) ) ;
107+ }
108+ if ( inspectorsByType [ type ] . length ) {
80109 onNextTick ( ( ) => {
81- inspectorsByType [ InspectorTypes . flagDetailsChanged ] . forEach ( inspector => inspector . method ( flags ) ) ;
110+ inspectorsByType [ type ] . forEach ( inspector => inspector . method ( flags ) ) ;
82111 } ) ;
83112 }
84113 } ;
@@ -92,9 +121,13 @@ function InspectorManager(inspectors, logger) {
92121 * @param {Object } flag An `LDEvaluationDetail` for the flag.
93122 */
94123 manager . onFlagChanged = ( flagKey , flag ) => {
95- if ( inspectorsByType [ InspectorTypes . flagDetailChanged ] . length ) {
124+ const type = InspectorTypes . flagDetailChanged ;
125+ if ( synchronousInspectorsByType [ type ] . length ) {
126+ synchronousInspectorsByType [ type ] . forEach ( inspector => inspector . method ( flagKey , flag ) ) ;
127+ }
128+ if ( inspectorsByType [ type ] . length ) {
96129 onNextTick ( ( ) => {
97- inspectorsByType [ InspectorTypes . flagDetailChanged ] . forEach ( inspector => inspector . method ( flagKey , flag ) ) ;
130+ inspectorsByType [ type ] . forEach ( inspector => inspector . method ( flagKey , flag ) ) ;
98131 } ) ;
99132 }
100133 } ;
@@ -107,9 +140,13 @@ function InspectorManager(inspectors, logger) {
107140 * @param {Object } context The `LDContext` which is now identified.
108141 */
109142 manager . onIdentityChanged = context => {
110- if ( inspectorsByType [ InspectorTypes . clientIdentityChanged ] . length ) {
143+ const type = InspectorTypes . clientIdentityChanged ;
144+ if ( synchronousInspectorsByType [ type ] . length ) {
145+ synchronousInspectorsByType [ type ] . forEach ( inspector => inspector . method ( context ) ) ;
146+ }
147+ if ( inspectorsByType [ type ] . length ) {
111148 onNextTick ( ( ) => {
112- inspectorsByType [ InspectorTypes . clientIdentityChanged ] . forEach ( inspector => inspector . method ( context ) ) ;
149+ inspectorsByType [ type ] . forEach ( inspector => inspector . method ( context ) ) ;
113150 } ) ;
114151 }
115152 } ;
0 commit comments