@@ -17,6 +17,7 @@ const messages = require('./messages');
1717const { checkContext, getContextKeys } = require ( './context' ) ;
1818const { InspectorTypes, InspectorManager } = require ( './InspectorManager' ) ;
1919const timedPromise = require ( './timedPromise' ) ;
20+ const HookRunner = require ( './HookRunner' ) ;
2021
2122const changeEvent = 'change' ;
2223const internalChangeEvent = 'internal-change' ;
@@ -40,6 +41,7 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
4041 const sendEvents = options . sendEvents ;
4142 let environment = env ;
4243 let hash = options . hash ;
44+ const hookRunner = new HookRunner ( logger , options . hooks ) ;
4345
4446 const persistentStorage = PersistentStorage ( platform . localStorage , logger ) ;
4547
@@ -256,11 +258,16 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
256258 logger . warn ( messages . identifyDisabled ( ) ) ;
257259 return utils . wrapPromiseCallback ( Promise . resolve ( utils . transformVersionedValuesToValues ( flags ) ) , onDone ) ;
258260 }
261+ let afterIdentify ;
259262 const clearFirst = useLocalStorage && persistentFlagStore ? persistentFlagStore . clearFlags ( ) : Promise . resolve ( ) ;
260263 return utils . wrapPromiseCallback (
261264 clearFirst
262265 . then ( ( ) => anonymousContextProcessor . processContext ( context ) )
263266 . then ( verifyContext )
267+ . then ( context => {
268+ afterIdentify = hookRunner . identify ( context , undefined ) ;
269+ return context ;
270+ } )
264271 . then ( validatedContext =>
265272 requestor
266273 . fetchFlagSettings ( validatedContext , newHash )
@@ -277,12 +284,14 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
277284 } )
278285 )
279286 . then ( flagValueMap => {
287+ afterIdentify ?. ( { status : 'completed' } ) ;
280288 if ( streamActive ) {
281289 connectStream ( ) ;
282290 }
283291 return flagValueMap ;
284292 } )
285293 . catch ( err => {
294+ afterIdentify ?. ( { status : 'error' } ) ;
286295 emitter . maybeReportError ( err ) ;
287296 return Promise . reject ( err ) ;
288297 } ) ,
@@ -299,11 +308,16 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
299308 }
300309
301310 function variation ( key , defaultValue ) {
302- return variationDetailInternal ( key , defaultValue , true , false , false , true ) . value ;
311+ const { value } = hookRunner . withEvaluation ( key , ident . getContext ( ) , defaultValue , ( ) =>
312+ variationDetailInternal ( key , defaultValue , true , false , false , true )
313+ ) ;
314+ return value ;
303315 }
304316
305317 function variationDetail ( key , defaultValue ) {
306- return variationDetailInternal ( key , defaultValue , true , true , false , true ) ;
318+ return hookRunner . withEvaluation ( key , ident . getContext ( ) , defaultValue , ( ) =>
319+ variationDetailInternal ( key , defaultValue , true , true , false , true )
320+ ) ;
307321 }
308322
309323 function variationDetailInternal ( key , defaultValue , sendEvent , includeReasonInEvent , isAllFlags , notifyInspection ) {
@@ -826,6 +840,10 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
826840 return initializationStateTracker . getInitializationPromise ( ) ;
827841 }
828842
843+ function addHook ( hook ) {
844+ hookRunner . addHook ( hook ) ;
845+ }
846+
829847 const client = {
830848 waitForInitialization,
831849 waitUntilReady : ( ) => initializationStateTracker . getReadyPromise ( ) ,
@@ -840,6 +858,7 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
840858 flush : flush ,
841859 allFlags : allFlags ,
842860 close : close ,
861+ addHook : addHook ,
843862 } ;
844863
845864 return {
0 commit comments