src/readiness/index.js
/**
 * Machine state to handle the ready / update event propagation.
 */
Machine state to handle the ready / update event propagation.
Machine state to handle the ready / update event propagation .
function GateContext() {
  let splitsStatus = 0;
  const splits = new EventEmitter();
  splits.SDK_SPLITS_ARRIVED = Events.SDK_SPLITS_ARRIVED;
  let refCount = 0;
  function ReadinessGateFactory(splits, segments) {
    const gate = new EventEmitter();
    let segmentsStatus = 0;
    let status = 0;
    gate.on(Events.READINESS_GATE_CHECK_STATE, () => {
      if (status !== SDK_FIRE_UPDATE && splitsStatus + segmentsStatus === SDK_FIRE_READY) {
        status = SDK_FIRE_UPDATE;
        gate.emit(Events.SDK_READY);
      } else if (status === SDK_FIRE_UPDATE) {
        gate.emit(Events.SDK_UPDATE);
      }
    });
    splits.on(Events.SDK_SPLITS_ARRIVED, () => {
      splitsStatus = SPLITS_READY;
      gate.emit(Events.READINESS_GATE_CHECK_STATE);
    });
    segments.on(Events.SDK_SEGMENTS_ARRIVED, () => {
      segmentsStatus = SEGMENTS_READY;
      gate.emit(Events.READINESS_GATE_CHECK_STATE);
    });
    return gate;
  }
  function SDKReadinessGateFactory(timeout = 0) {
    let readinessTimeoutId = 0;
    const segments = new EventEmitter();
    segments.SDK_SEGMENTS_ARRIVED = Events.SDK_SEGMENTS_ARRIVED;
    const gate = ReadinessGateFactory(splits, segments, timeout);
    if (timeout > 0) {
      readinessTimeoutId = setTimeout(() => {
        gate.emit(Events.SDK_READY_TIMED_OUT, 'Split SDK emitted SDK_READY_TIMED_OUT event.');
      }, timeout);
      gate.once(Events.SDK_READY, () => clearTimeout(readinessTimeoutId));
    }
    gate.SDK_READY = Events.SDK_READY;
    gate.SDK_UPDATE = Events.SDK_UPDATE;
    gate.SDK_READY_TIMED_OUT = Events.SDK_READY_TIMED_OUT;
    refCount++;
    return {
      splits,
      segments,
      gate,
      destroy() {
        segments.removeAllListeners();
        gate.removeAllListeners();
        clearTimeout(readinessTimeoutId);
        if (refCount > 0) refCount--;
        if (refCount === 0) splits.removeAllListeners();
      }
    };
  }
  return SDKReadinessGateFactory;
}
function GateContext ( ) { let splitsStatus = 0 ; const splits = new EventEmitter ( ) ; splits . SDK_SPLITS_ARRIVED = Events . SDK_SPLITS_ARRIVED ; let refCount = 0 ; function ReadinessGateFactory ( splits , segments ) { const gate = new EventEmitter ( ) ; let segmentsStatus = 0 ; let status = 0 ; gate . on ( Events . READINESS_GATE_CHECK_STATE , ( ) => { if ( status !== SDK_FIRE_UPDATE && splitsStatus + segmentsStatus === SDK_FIRE_READY ) { status = SDK_FIRE_UPDATE ; gate . emit ( Events . SDK_READY ) ; } else if ( status === SDK_FIRE_UPDATE ) { gate . emit ( Events . SDK_UPDATE ) ; } } ) ; splits . on ( Events . SDK_SPLITS_ARRIVED , ( ) => { splitsStatus = SPLITS_READY ; gate . emit ( Events . READINESS_GATE_CHECK_STATE ) ; } ) ; segments . on ( Events . SDK_SEGMENTS_ARRIVED , ( ) => { segmentsStatus = SEGMENTS_READY ; gate . emit ( Events . READINESS_GATE_CHECK_STATE ) ; } ) ; return gate ; } function SDKReadinessGateFactory ( timeout = 0 ) { let readinessTimeoutId = 0 ; const segments = new EventEmitter ( ) ; segments . SDK_SEGMENTS_ARRIVED = Events . SDK_SEGMENTS_ARRIVED ; const gate = ReadinessGateFactory ( splits , segments , timeout ) ; if ( timeout > 0 ) { readinessTimeoutId = setTimeout ( ( ) => { gate . emit ( Events . SDK_READY_TIMED_OUT , 'Split SDK emitted SDK_READY_TIMED_OUT event.' ) ; } , timeout ) ; gate . once ( Events . SDK_READY , ( ) => clearTimeout ( readinessTimeoutId ) ) ; } gate . SDK_READY = Events . SDK_READY ; gate . SDK_UPDATE = Events . SDK_UPDATE ; gate . SDK_READY_TIMED_OUT = Events . SDK_READY_TIMED_OUT ; refCount ++ ; return { splits , segments , gate , destroy ( ) { segments . removeAllListeners ( ) ; gate . removeAllListeners ( ) ; clearTimeout ( readinessTimeoutId ) ; if ( refCount > 0 ) refCount -- ; if ( refCount === 0 ) splits . removeAllListeners ( ) ; } } ; } return SDKReadinessGateFactory ; }
Machine state to handle the ready / update event propagation.
Machine state to handle the ready / update event propagation .
// Splits are shared through all instances of the same SDK. // references counter: how many /**
   * SDK Readiness Gate Factory
   *
   * The ready state in the browser relay on sharing the splits ready flag across
   * all the gates, and have an extra flag for the segments which is per gate
   * instance.
   */ // Add the timeout. // Clear it if the SDK get's ready. // New Gate has been created, so increase the counter // Emitters // Cleanup listeners