@launchdarkly/js-sdk-common
Version:
LaunchDarkly SDK for JavaScript - common code
75 lines • 2.38 kB
TypeScript
import { LDLogger } from '../../api';
import { FDv2Event } from './proto';
/**
* Defines object processing between deserialization and payload emission.
* Can be used to provide object sanitization logic per kind.
*/
export interface ObjProcessors {
[kind: string]: (object: any) => any;
}
export interface Update {
kind: string;
key: string;
version: number;
object?: any;
deleted?: boolean;
}
export type PayloadType = 'full' | 'partial' | 'none';
/**
* A collection of updates from the FDv2 services.
*
* - `full`: the updates represent the complete state and replace everything.
* - `partial`: the updates are incremental changes to apply.
* - `none`: no changes are needed; the SDK is up-to-date.
*/
export interface Payload {
version: number;
state?: string;
type: PayloadType;
updates: Update[];
}
export type PayloadListener = (payload: Payload) => void;
/**
* - `inactive`: No server intent has been expressed (initial state).
* - `changes`: Currently receiving incremental changes.
* - `full`: Currently receiving a full transfer.
*/
export type ProtocolState = 'inactive' | 'changes' | 'full';
export type ProtocolErrorKind = 'UNKNOWN_EVENT' | 'MISSING_PAYLOAD' | 'PROTOCOL_ERROR';
/**
* - `none`: No special action should be taken.
* - `payload`: A changeset should be applied.
* - `error`: An internal protocol error was encountered.
* - `goodbye`: The server intends to disconnect.
* - `serverError`: A server-side application error was encountered.
*/
export type ProtocolAction = {
type: 'none';
} | {
type: 'payload';
payload: Payload;
} | {
type: 'error';
kind: ProtocolErrorKind;
message: string;
} | {
type: 'goodbye';
reason: string;
} | {
type: 'serverError';
id?: string;
reason: string;
};
/**
* Pure FDv2 protocol state machine. Processes a single event at a time and
* returns an action describing what the caller should do. Contains no I/O
* or callbacks.
*/
export interface ProtocolHandler {
readonly state: ProtocolState;
processEvent(event: FDv2Event): ProtocolAction;
/** Resets the handler to inactive. Should be called when a connection is reset. */
reset(): void;
}
export declare function createProtocolHandler(objProcessors: ObjProcessors, logger?: LDLogger): ProtocolHandler;
//# sourceMappingURL=protocolHandler.d.ts.map