All files / src index.js

68.18% Statements 30/44
65.63% Branches 21/32
61.54% Functions 8/13
69.77% Lines 30/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117        2x 2x       2x       3x                         3x       3x 3x   3x         3x   3x 3x             1x   2x     2x 2x         2x 2x                       2x 1x 1x   1x         18x               2x       18x                   2x 1x 1x 1x   1x         2x      
import {setLogger} from './logger'
import Airship from './airship'
import Core from './core'
 
const defaultEnv = new Core()
defaultEnv.configure({})
 
export class FlaggerBase {
  constructor() {
    this.gatingInfoListeners = []
  }
 
  static _isDict(obj) {
    return obj !== undefined && obj !== null && obj.constructor === Object
  }
 
  async publish(objs) {
    if (this.environment) {
      await this.environment.publish(objs)
    } else {
      throw 'Airship must be configured first before `publish` can be called'
    }
  }
 
  // This will allow for async/await
  async configure(options) {
    Iif (!FlaggerBase._isDict(options)) {
      throw '<options> must be dictionary'
    }
 
    const envKey = options.envKey
    const flagConfig = options.flagConfig
 
    Iif (!envKey && !flagConfig) {
      throw '<options> must contain envKey corresponding to an environment key or a flagConfig dictionary to configure locally'
    }
 
    const subscribeToUpdates =
      options.subscribeToUpdates === false ? false : true
 
    Eif (envKey) {
      if (
        this.environment &&
        this.environment.envKey === envKey &&
        this.environment.subscribeToUpdates === subscribeToUpdates &&
        this.environment.environmentPromise &&
        !this.environment.failed
      ) {
        await this.environment.environmentPromise
      } else {
        Iif (this.environment) {
          await this.environment.shutdown()
        }
        this.environment = new Airship(this.handleGatingInfoUpdate.bind(this))
        const promise = this.environment.configure(
          envKey,
          options.subscribeToUpdates,
          options.apiDomain
        )
        this.environment.environmentPromise = promise
        await promise
      }
    } else {
      if (this.environment) {
        await this.environment.shutdown()
      }
      this.environment = new Core()
      await this.environment.configure(flagConfig)
    }
  }
 
  async shutdown() {
    if (this.environment) {
      await this.environment.shutdown()
      delete this.environment
    } else {
      throw 'Airship must be configured first before `shutdown` can be called'
    }
  }
 
  flag(flagName) {
    return (this.environment || defaultEnv).flag(flagName)
  }
 
  setErrorListener(fn) {
    setLogger(fn)
  }
 
  handleGatingInfoUpdate(gatingInfo) {
    this.gatingInfoListeners.forEach(listener => listener(gatingInfo))
  }
 
  addGatingInfoListener(listener) {
    this.gatingInfoListeners.push(listener)
  }
 
  removeGatingInfoListener(listener) {
    this.gatingInfoListeners = this.gatingInfoListeners.filter(
      l => l !== listener
    )
  }
 
  identify(obj) {
    if (this.environment) {
      this.environment.identify(obj)
      this.environment._identifyObject(obj)
      this.environment.maybeIngest(true)
    } else {
      throw 'Airship must be configured first before `identify` can be called'
    }
  }
}
 
const Flagger = new FlaggerBase()
 
export default Flagger