Options
All
  • Public
  • Public/Protected
  • All
Menu

The LaunchDarkly SDK client object.

Applications should configure the client at application startup time and reuse the same instance.

For more information, see the SDK Reference Guide.

Hierarchy

  • LDClient

Index

Constructors

  • Returns LDClient

Methods

  • alias(user: LDUser, previousUser: LDUser, environment?: string): void
  • Alias associates two users for analytics purposes by generating an alias event.

    This can be helpful in the situation where a person is represented by multiple LaunchDarkly users. This may happen, for example, when a person initially logs into an application-- the person might be represented by an anonymous user prior to logging in and a different user after logging in, as denoted by a different user key.

    Parameters

    • user: LDUser

      The new user context

    • previousUser: LDUser

      The original user context

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

  • allFlags(environment?: string): Promise<LDFlagSet>
  • Returns a map of all available flags to the current user's values.

    Parameters

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<LDFlagSet>

    A promise containing an object in which each key is a feature flag key and each value is the flag value. Note that there is no way to specify a default value for each flag as there is with the *Variation methods, so any flag that cannot be evaluated will have a null value.

  • boolVariation(flagKey: string, defaultValue: boolean, environment?: string): Promise<boolean>
  • Determines the variation of a boolean feature flag for the current user.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: boolean

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<boolean>

    A promise containing the flag's value.

  • boolVariationDetail(flagKey: string, defaultValue: boolean, environment?: string): Promise<LDEvaluationDetail<boolean>>
  • Determines the variation of a boolean feature flag for a user, along with information about how it was calculated.

    Note that this will only work if you have set evaluationReasons to true in LDConfig. Otherwise, the reason property of the result will be null.

    For more information, see the SDK reference guide.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: boolean

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<LDEvaluationDetail<boolean>>

    A promise containing an LDEvaluationDetail object containing the value and explanation.

  • close(): Promise<void>
  • Shuts down the client and releases its resources, after delivering any pending analytics events. After the client is closed, all calls to the *Variation methods will return default values, and it will not make any requests to LaunchDarkly.

    Returns Promise<void>

  • configure(config: LDConfig, user: LDUser, timeout?: number): Promise<null>
  • Initialize the SDK to work with the specified client configuration options and on behalf of the specified user. Will block for a number of seconds represented until flags are received from LaunchDarkly if the timeout parameter is passed.

    This should only be called once at application start time.

    Parameters

    • config: LDConfig

      the client configuration options

    • user: LDUser

      the user

    • Optional timeout: number

      (Optional) A number representing how long to wait for flags

    Returns Promise<null>

  • flush(): void
  • Flushes all pending analytics events.

    Normally, batches of events are delivered in the background at intervals determined by the eventsFlushIntervalMillis property of LDConfig. Calling flush triggers an immediate delivery.

    Returns void

  • Returns the current state of the connection to LaunchDarkly.

    States include:

    • 'STREAMING': The SDK is either connected to the flag stream, or is actively attempting to acquire a connection.
    • 'POLLING': The SDK was configured with streaming disabled, and is in foreground polling mode.
    • 'BACKGROUND_POLLING': (Android specific enum value) The SDK has detected the application is in the background and has transitioned to battery conscious background polling.
    • 'BACKGROUND_DISABLED': (Android specific enum value) The SDK was configured with background polling disabled. The SDK has detected the application is in the background and is not attempting to update the flag cache.
    • 'OFFLINE': The SDK has detected that the mobile device does not have an active network connection so has ceased flag update attempts until the network status changes.
    • 'SET_OFFLINE': (Android specific enum value) The SDK has been explicitly set offline, either in the initial configuration, by setOffline(), or as a result of failed authentication to LaunchDarkly. The SDK will stay offline unless setOnline() is called.
    • 'SHUTDOWN': (Android specific enum value) The shutdown state indicates the SDK has been permanently shutdown as a result of a call to close().
    • 'ESTABLISHING_STREAMING_CONNECTION': (iOS specific enum value) The SDK is attempting to connect to LaunchDarkly by streaming.

    Parameters

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns Promise<LDConnectionMode>

    A promise containing a LDConnectionMode enum value representing the status of the connection to LaunchDarkly.

  • getLastFailedConnection(environment?: string): Promise<null | number>
  • Most recent unsuccessful flag cache update attempt in millis from the epoch or null if flag update has never been attempted.

    Parameters

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns Promise<null | number>

    A promise containing a number representing the status of the connection to LaunchDarkly, or null if a failed connection has yet to occur.

  • Returns the most recent connection failure reason or null.

    Reasons include:

    • 'NONE': This indicates when no error has been recorded.
    • 'UNKNOWN_ERROR': This indicates when there is an internal error in the stream request.
    • 'UNAUTHORIZED': (iOS specific enum value) This indicates when an incorrect mobile key is provided.
    • 'HTTP_ERROR': (iOS specific enum value) This indicates when an error with an HTTP error code is present.
    • 'UNEXPECTED_RESPONSE_CODE': (Android specific enum value) This indicates the LDFailure is an instance of LDInvalidResponseCodeFailure. See Android documentation for more details.
    • 'UNEXPECTED_STREAM_ELEMENT_TYPE': (Android specific enum value) An event was received through the stream was had an unknown event name. This could indicate a newer SDK is available if new event types have become available through the flag stream since the SDKs release.
    • 'NETWORK_FAILURE': (Android specific enum value) A network request for polling, or the EventSource stream reported a failure.
    • 'INVALID_RESPONSE_BODY': (Android specific enum value) A response body received either through polling or streaming was unable to be parsed.

    Parameters

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns Promise<null | LDFailureReason>

    A promise containing a LDFailureReason enum value representing the reason for the most recently failed connection to LaunchDarkly, or null if a failed connection has yet to occur.

  • getLastSuccessfulConnection(environment?: string): Promise<null | number>
  • Returns the most recent successful flag cache update in millis from the epoch or null if flags have never been retrieved.

    Parameters

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns Promise<null | number>

    A promise containing a number representing the status of the connection to LaunchDarkly, or null if a successful connection has yet to be established.

  • getVersion(): string
  • Returns the SDK version.

    Returns string

    A string containing the SDK version.

  • identify(user: LDUser): Promise<null>
  • Sets the current user, retrieves flags for that user, then sends an Identify Event to LaunchDarkly.

    Parameters

    • user: LDUser

      The user for evaluation and event reporting

    Returns Promise<null>

    A promise indicating when this operation is complete (meaning that flags are ready for evaluation).

  • isInitialized(environment?: string): Promise<boolean>
  • Checks whether the client is ready to return feature flag values. This is true if either the client has successfully connected to LaunchDarkly and received feature flags, or the client has been put into offline mode (in which case it will return only default flag values).

    This function will return a rejected promise in case the client has not been initialized.

    Parameters

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<boolean>

    A promise containing true if the client is initialized or offline, otherwise a rejected promise

  • isOffline(): Promise<boolean>
  • Checks whether the client has been put into offline mode. This is true only if setOffline was called, or if the configuration had [[LDConfig.offline]] set to true, not if the client is simply offline due to a loss of network connectivity.

    Returns Promise<boolean>

    A promise containing true if the client is in offline mode

  • jsonVariation(flagKey: string, defaultValue: any, environment?: string): Promise<any>
  • Determines the variation of a JSON feature flag for the current user.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: any

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<any>

    A promise containing the flag's value.

  • jsonVariationDetail(flagKey: string, defaultValue: any, environment?: string): Promise<LDEvaluationDetail<any>>
  • Determines the variation of a JSON feature flag for a user, along with information about how it was calculated.

    Note that this will only work if you have set evaluationReasons to true in LDConfig. Otherwise, the reason property of the result will be null.

    For more information, see the SDK reference guide.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: any

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<LDEvaluationDetail<any>>

    A promise containing an LDEvaluationDetail object containing the value and explanation.

  • numberVariation(flagKey: string, defaultValue: number, environment?: string): Promise<number>
  • Determines the variation of a numeric feature flag for the current user.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: number

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<number>

    A promise containing the flag's value.

  • numberVariationDetail(flagKey: string, defaultValue: number, environment?: string): Promise<LDEvaluationDetail<number>>
  • Determines the variation of a numeric feature flag for a user, along with information about how it was calculated.

    Note that this will only work if you have set evaluationReasons to true in LDConfig. Otherwise, the reason property of the result will be null.

    For more information, see the SDK reference guide.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: number

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<LDEvaluationDetail<number>>

    A promise containing an LDEvaluationDetail object containing the value and explanation.

  • registerAllFlagsListener(listenerId: string, callback: (updatedFlags: string[]) => void, environment?: string): void
  • Registers a callback to be called when a flag update is processed by the SDK.

    Parameters

    • listenerId: string

      The listener to be called when a flag update is processed

    • callback: (updatedFlags: string[]) => void

      The callback to attach to the flag update

        • (updatedFlags: string[]): void
        • Parameters

          • updatedFlags: string[]

          Returns void

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

  • registerCurrentConnectionModeListener(listenerId: string, callback: (connectionMode: string) => void, environment?: string): void
  • Registers a callback to be called on connection status updates.

    Parameters

    • listenerId: string

      The listener to be called on a connection status update

    • callback: (connectionMode: string) => void

      The callback to attach to the connection status update

        • (connectionMode: string): void
        • Parameters

          • connectionMode: string

          Returns void

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

  • registerFeatureFlagListener(flagKey: string, callback: (flagKey: string) => void, environment?: string): void
  • Registers a callback to be called when the flag with key flagKey changes from its current value.

    Parameters

    • flagKey: string

      The flag key to attach the callback to

    • callback: (flagKey: string) => void

      The callback to attach to the flag key

        • (flagKey: string): void
        • Parameters

          • flagKey: string

          Returns void

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

  • setOffline(): Promise<boolean>
  • Shuts down any network connections maintained by the client and puts the client in offline mode, preventing the client from opening new network connections until setOnline is called.

    Note: The client automatically monitors the device's network connectivity and app foreground status, so calling setOffline or setOnline is normally unnecessary in most situations.

    Returns Promise<boolean>

    A promise containing true if the change was made successfully

  • setOnline(): Promise<boolean>
  • Restores network connectivity for the client, if the client was previously in offline mode. This operation may be throttled if it is called too frequently.

    Note: The client automatically monitors the device's network connectivity and app foreground status, so calling setOffline or setOnline is normally unnecessary in most situations.

    Returns Promise<boolean>

    A promise containing true if the change was made successfully

  • stringVariation(flagKey: string, defaultValue: string, environment?: string): Promise<string>
  • Determines the variation of a string feature flag for the current user.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: string

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<string>

    A promise containing the flag's value.

  • stringVariationDetail(flagKey: string, defaultValue: string, environment?: string): Promise<LDEvaluationDetail<string>>
  • Determines the variation of a string feature flag for a user, along with information about how it was calculated.

    Note that this will only work if you have set evaluationReasons to true in LDConfig. Otherwise, the reason property of the result will be null.

    For more information, see the SDK reference guide.

    Parameters

    • flagKey: string

      The unique key of the feature flag.

    • defaultValue: string

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional environment: string

      Optional environment name to obtain the result from the corresponding secondary environment

    Returns Promise<LDEvaluationDetail<string>>

    A promise containing an LDEvaluationDetail object containing the value and explanation.

  • track(eventName: string, data?: any, metricValue?: number, environment?: string): void
  • Track events to use in goals or A/B tests.

    Parameters

    • eventName: string

      The name of the event, which may correspond to a goal in A/B tests.

    • Optional data: any

      Optional additional information to associate with the event.

    • Optional metricValue: number

      Optional numeric value to attach to the tracked event

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

  • unregisterAllFlagsListener(listenerId: string, environment?: string): void
  • Unregisters a callback so it will no longer be called on flag updates.

    Parameters

    • listenerId: string

      The listener to be removed

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

  • unregisterCurrentConnectionModeListener(listenerId: string, environment?: string): void
  • Unregisters a callback so that it will no longer be called on connection status updates.

    Parameters

    • listenerId: string

      The listener to remove the callback from

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

  • unregisterFeatureFlagListener(flagKey: string, callback: (flagKey: string) => void, environment?: string): void
  • Unregisters a callback for the flag with key flagKey.

    Parameters

    • flagKey: string

      The flag key to remove the callback from

    • callback: (flagKey: string) => void

      The callback to remove from the flag key

        • (flagKey: string): void
        • Parameters

          • flagKey: string

          Returns void

    • Optional environment: string

      Optional string to execute the function in a different environment than the default.

    Returns void

Generated using TypeDoc