@devcycle/js-client-sdk
Version:
The Javascript Client SDK for DevCycle
187 lines (186 loc) • 8.28 kB
TypeScript
import { DVCFeatureSet, DevCycleOptions, DVCVariableSet, DVCVariableValue, DVCCustomDataJSON, DevCycleEvent, DevCycleUser, ErrorCallback, DVCFeature } from './types';
import { DVCVariable } from './Variable';
import { DVCPopulatedUser } from './User';
import { EventEmitter } from './EventEmitter';
import type { BucketedUserConfig, VariableDefinitions, VariableTypeAlias } from '@devcycle/types';
import type { DVCLogger } from '@devcycle/types';
import { EvalHook } from './hooks/EvalHook';
type variableUpdatedHandler = (key: string, variable: DVCVariable<DVCVariableValue> | null) => void;
type featureUpdatedHandler = (key: string, feature: DVCFeature | null) => void;
type newVariablesHandler = () => void;
type errorHandler = (error: unknown) => void;
type initializedHandler = (success: boolean) => void;
type configUpdatedHandler = (newVars: DVCVariableSet) => void;
type variableEvaluatedHandler = (key: string, variable: DVCVariable<DVCVariableValue>) => void;
export type DevCycleOptionsWithDeferredInitialization = DevCycleOptions & {
deferInitialization: true;
bootstrapConfig?: never;
};
export declare const isDeferredOptions: (arg: DevCycleUser | DevCycleOptionsWithDeferredInitialization) => arg is DevCycleOptionsWithDeferredInitialization;
export declare class DevCycleClient<Variables extends VariableDefinitions = VariableDefinitions, CustomData extends DVCCustomDataJSON = DVCCustomDataJSON> {
logger: DVCLogger;
config?: BucketedUserConfig;
user?: DVCPopulatedUser;
_isInitialized: boolean;
get isInitialized(): boolean;
private sdkKey;
private readonly options;
private onInitialized;
private settleOnInitialized;
private userSaved;
private _closing;
private isConfigCached;
private initializeTriggered;
private variableDefaultMap;
private store;
private eventQueue?;
private requestConsolidator;
eventEmitter: EventEmitter;
private streamingConnection?;
private pageVisibilityHandler?;
private inactivityHandlerId?;
private windowMessageHandler?;
private windowPageHideHandler?;
private configRefetchHandler;
private evalHooksRunner;
constructor(sdkKey: string, user: undefined, options: DevCycleOptionsWithDeferredInitialization);
constructor(sdkKey: string, user: DevCycleUser<CustomData>, options?: DevCycleOptions);
/**
* Logic to initialize the client with the appropriate user and configuration data by making requests to DevCycle
* and loading from local storage. This either happens immediately on client initialization, or when the user is
* first identified (in deferred mode)
* @param initialUser
*/
private clientInitialization;
/**
* Complete initialization process without config so that we can return default values
*/
private initializeOnConfigFailure;
/**
* Notify the user when configuration data has been loaded from the server.
* An optional callback can be passed in, and will return
* a promise if no callback has been passed in.
*/
onClientInitialized(): Promise<DevCycleClient<Variables, CustomData>>;
onClientInitialized(onInitialized: ErrorCallback<DevCycleClient<Variables, CustomData>>): void;
/**
* Get variable object associated with Features. Use the variable's key to fetch the DVCVariable object.
* If the user does not receive the feature, the default value is used in the returned DVCVariable object.
* DVCVariable is returned, which has a `value` property that is used to grab the variable value,
* and a convenience method to pass in a callback to notify the user when the value has changed from the server.
*
* @param key
* @param defaultValue
*/
variable<K extends string & keyof Variables, T extends DVCVariableValue & Variables[K]>(key: K, defaultValue: T): DVCVariable<T>;
private _variable;
private trackVariableEvaluated;
/**
* Get a variable's value associated with a Feature. Use the variable's key to fetch the variable's value.
* If the user is not segmented into the feature, the default value is returned.
*
* @param key
* @param defaultValue
*/
variableValue<K extends string & keyof Variables, T extends DVCVariableValue & Variables[K]>(key: K, defaultValue: T): VariableTypeAlias<T>;
/**
* Update user data after SDK initialization, this will trigger updates to variable values.
* The `callback` parameter or returned `promise` can be used to return the set of variables
* for the new user.
*
* @param user
* @param callback
*/
identifyUser(user: DevCycleUser<CustomData>): Promise<DVCVariableSet>;
identifyUser(user: DevCycleUser<CustomData>, callback?: ErrorCallback<DVCVariableSet>): void;
private _identifyUser;
/**
* Resets the user to an Anonymous user. `callback` or `Promise` can be used to return
* the set of variables for the new user.
*
* @param callback
*/
resetUser(): Promise<DVCVariableSet>;
resetUser(callback: ErrorCallback<DVCVariableSet>): void;
/**
* Retrieve data on all Features, Object mapped by feature `key`.
* Use the `DVCFeature.segmented` value to determine if the user was segmented into a
* feature's audience.
*/
allFeatures(): DVCFeatureSet;
/**
* Retrieve data on all Variables, Object mapped by variable `key`.
*/
allVariables(): DVCVariableSet;
/**
* Subscribe to events emitted by the SDK, `onUpdate` will be called everytime an
* event is emitted by the SDK.
*
* Events:
* - `initialized` -> (initialized: boolean)
* - `error` -> (error: Error)
* - `variableUpdated:*` -> (key: string, variable: DVCVariable)
* - `variableUpdated:<variable.key>` -> (key: string, variable: DVCVariable)
* - `featureUpdated:*` -> (key: string, feature: DVCFeature)
* - `featureUpdated:<feature.key>` -> (key: string, feature: DVCFeature)
* - `variableEvaluated:*` -> (key: string, variable: DVCVariable)
* - `variableEvaluated:<variable.key>` -> (key: string, variable: DVCVariable)
*
* @param key
* @param handler
*/
subscribe(key: `variableUpdated:${string}`, handler: variableUpdatedHandler): void;
subscribe(key: `newVariables:${string}`, handler: newVariablesHandler): void;
subscribe(key: `featureUpdated:${string}`, handler: featureUpdatedHandler): void;
subscribe(key: `variableEvaluated:${string}`, handler: variableEvaluatedHandler): void;
subscribe(key: 'error', handler: errorHandler): void;
subscribe(key: 'initialized', handler: initializedHandler): void;
subscribe(key: 'configUpdated', handler: configUpdatedHandler): void;
/**
* Unsubscribe to remove existing event emitter subscription.
*
* @param key
* @param handler
*/
unsubscribe(key: string, handler?: (...args: any[]) => void): void;
/**
* Track Event to DevCycle
*
* @param event
*/
track(event: DevCycleEvent): void;
/**
* Flush all queued events to DevCycle
*
* @param callback
*/
flushEvents(callback?: () => void): Promise<void>;
/**
* Close all open connections to DevCycle, flush any pending events and
* stop any running timers and event handlers. Use to clean up a client instance
* that is no longer needed.
*/
close(): Promise<void>;
/**
* Reflects whether `close()` has been called on the client instance.
*/
get closing(): boolean;
/**
* Method to be called by the Isomorphic SDKs to update the bootstrapped config and user data when the server's
* representation has changed.
* NOTE: It is not recommended to call this yourself.
* @param config
* @param user
* @param userAgent
*/
synchronizeBootstrapData(config: BucketedUserConfig | null, user: DevCycleUser<CustomData>, userAgent?: string): void;
private refetchConfig;
addHook(hook: EvalHook<DVCVariableValue>): void;
clearHooks(): void;
private handleConfigReceived;
private setUser;
private onSSEMessage;
private registerVisibilityChangeHandler;
private getConfigCache;
}
export {};