@openfeature/js-sdk
Advanced tools
Comparing version
@@ -74,2 +74,3 @@ "use strict"; | ||
GeneralError: () => GeneralError, | ||
InternalEventEmitter: () => InternalEventEmitter, | ||
InvalidContextError: () => InvalidContextError, | ||
@@ -280,5 +281,6 @@ LOG_LEVELS: () => LOG_LEVELS, | ||
var import_events = __toESM(require("events")); | ||
var OpenFeatureEventEmitter = class { | ||
var GenericEventEmitter = class { | ||
constructor(globalLogger) { | ||
this.globalLogger = globalLogger; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._handlers = /* @__PURE__ */ new WeakMap(); | ||
@@ -323,6 +325,10 @@ this.eventEmitter = new import_events.default({ captureRejections: true }); | ||
get _logger() { | ||
var _a; | ||
return this._eventLogger || ((_a = this.globalLogger) == null ? void 0 : _a.call(this)); | ||
var _a, _b; | ||
return (_b = this._eventLogger) != null ? _b : (_a = this.globalLogger) == null ? void 0 : _a.call(this); | ||
} | ||
}; | ||
var OpenFeatureEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
var InternalEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
@@ -374,3 +380,3 @@ // ../shared/src/provider/provider.ts | ||
this._logger = new DefaultLogger(); | ||
this._events = new OpenFeatureEventEmitter(() => this._logger); | ||
this._events = new InternalEventEmitter(() => this._logger); | ||
this._clientEventHandlers = /* @__PURE__ */ new Map(); | ||
@@ -439,7 +445,7 @@ this._clientProviders = /* @__PURE__ */ new Map(); | ||
} | ||
const emitters = clientName ? [this.getAndCacheEventEmitterForClient(clientName)] : this.getUnboundEmitters(); | ||
const emitters = this.getAssociatedEventEmitters(clientName); | ||
if (typeof provider.initialize === "function") { | ||
(_d = (_c = (_b = provider.initialize) == null ? void 0 : _b.call(provider, this._context)) == null ? void 0 : _c.then(() => { | ||
var _a2; | ||
emitters.forEach((emitter) => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName }); | ||
@@ -450,3 +456,3 @@ }); | ||
var _a2; | ||
emitters.forEach((emitter) => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_ERROR" /* Error */, { clientName, message: error.message }); | ||
@@ -480,3 +486,3 @@ }); | ||
} | ||
getAndCacheEventEmitterForClient(name) { | ||
buildAndCacheEventEmitterForClient(name) { | ||
const emitter = this._clientEvents.get(name); | ||
@@ -486,3 +492,3 @@ if (emitter) { | ||
} | ||
const newEmitter = new OpenFeatureEventEmitter(() => this._logger); | ||
const newEmitter = new InternalEventEmitter(() => this._logger); | ||
this._clientEvents.set(name, newEmitter); | ||
@@ -511,2 +517,5 @@ const clientProvider = this.getProviderForClient(name); | ||
} | ||
getAssociatedEventEmitters(clientName) { | ||
return clientName ? [this.buildAndCacheEventEmitterForClient(clientName)] : this.getUnboundEmitters(); | ||
} | ||
transferListeners(oldProvider, newProvider, clientName, emitters) { | ||
@@ -518,13 +527,11 @@ var _a; | ||
}); | ||
const newClientHandlers = Object.values(ProviderEvents).map( | ||
(eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
}); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
const newClientHandlers = Object.values(ProviderEvents).map((eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
}); | ||
return [eventType, handler]; | ||
} | ||
); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
}); | ||
return [eventType, handler]; | ||
}); | ||
this._clientEventHandlers.set(clientName, newClientHandlers); | ||
@@ -656,3 +663,3 @@ newClientHandlers.forEach((eventHandler) => { | ||
() => this.getProviderForClient(name), | ||
() => this.getAndCacheEventEmitterForClient(name), | ||
() => this.buildAndCacheEventEmitterForClient(name), | ||
() => this._logger, | ||
@@ -887,2 +894,3 @@ { name, version }, | ||
GeneralError, | ||
InternalEventEmitter, | ||
InvalidContextError, | ||
@@ -889,0 +897,0 @@ LOG_LEVELS, |
@@ -222,5 +222,6 @@ var __defProp = Object.defineProperty; | ||
import EventEmitter from "events"; | ||
var OpenFeatureEventEmitter = class { | ||
var GenericEventEmitter = class { | ||
constructor(globalLogger) { | ||
this.globalLogger = globalLogger; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._handlers = /* @__PURE__ */ new WeakMap(); | ||
@@ -265,6 +266,10 @@ this.eventEmitter = new EventEmitter({ captureRejections: true }); | ||
get _logger() { | ||
var _a; | ||
return this._eventLogger || ((_a = this.globalLogger) == null ? void 0 : _a.call(this)); | ||
var _a, _b; | ||
return (_b = this._eventLogger) != null ? _b : (_a = this.globalLogger) == null ? void 0 : _a.call(this); | ||
} | ||
}; | ||
var OpenFeatureEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
var InternalEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
@@ -316,3 +321,3 @@ // ../shared/src/provider/provider.ts | ||
this._logger = new DefaultLogger(); | ||
this._events = new OpenFeatureEventEmitter(() => this._logger); | ||
this._events = new InternalEventEmitter(() => this._logger); | ||
this._clientEventHandlers = /* @__PURE__ */ new Map(); | ||
@@ -381,7 +386,7 @@ this._clientProviders = /* @__PURE__ */ new Map(); | ||
} | ||
const emitters = clientName ? [this.getAndCacheEventEmitterForClient(clientName)] : this.getUnboundEmitters(); | ||
const emitters = this.getAssociatedEventEmitters(clientName); | ||
if (typeof provider.initialize === "function") { | ||
(_d = (_c = (_b = provider.initialize) == null ? void 0 : _b.call(provider, this._context)) == null ? void 0 : _c.then(() => { | ||
var _a2; | ||
emitters.forEach((emitter) => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName }); | ||
@@ -392,3 +397,3 @@ }); | ||
var _a2; | ||
emitters.forEach((emitter) => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_ERROR" /* Error */, { clientName, message: error.message }); | ||
@@ -422,3 +427,3 @@ }); | ||
} | ||
getAndCacheEventEmitterForClient(name) { | ||
buildAndCacheEventEmitterForClient(name) { | ||
const emitter = this._clientEvents.get(name); | ||
@@ -428,3 +433,3 @@ if (emitter) { | ||
} | ||
const newEmitter = new OpenFeatureEventEmitter(() => this._logger); | ||
const newEmitter = new InternalEventEmitter(() => this._logger); | ||
this._clientEvents.set(name, newEmitter); | ||
@@ -453,2 +458,5 @@ const clientProvider = this.getProviderForClient(name); | ||
} | ||
getAssociatedEventEmitters(clientName) { | ||
return clientName ? [this.buildAndCacheEventEmitterForClient(clientName)] : this.getUnboundEmitters(); | ||
} | ||
transferListeners(oldProvider, newProvider, clientName, emitters) { | ||
@@ -460,13 +468,11 @@ var _a; | ||
}); | ||
const newClientHandlers = Object.values(ProviderEvents).map( | ||
(eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
}); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
const newClientHandlers = Object.values(ProviderEvents).map((eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
}); | ||
return [eventType, handler]; | ||
} | ||
); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName })); | ||
}); | ||
return [eventType, handler]; | ||
}); | ||
this._clientEventHandlers.set(clientName, newClientHandlers); | ||
@@ -598,3 +604,3 @@ newClientHandlers.forEach((eventHandler) => { | ||
() => this.getProviderForClient(name), | ||
() => this.getAndCacheEventEmitterForClient(name), | ||
() => this.buildAndCacheEventEmitterForClient(name), | ||
() => this._logger, | ||
@@ -828,2 +834,3 @@ { name, version }, | ||
GeneralError, | ||
InternalEventEmitter, | ||
InvalidContextError, | ||
@@ -830,0 +837,0 @@ LOG_LEVELS, |
@@ -23,9 +23,24 @@ declare enum ProviderEvents { | ||
}; | ||
type EventDetails = { | ||
type CommonEventDetails = { | ||
clientName?: string; | ||
}; | ||
type CommonEventProps = { | ||
message?: string; | ||
flagsChanged?: string[]; | ||
metadata?: EventMetadata; | ||
}; | ||
type EventHandler = (eventDetails?: EventDetails) => Promise<unknown> | unknown; | ||
type ReadyEvent = CommonEventProps; | ||
type ErrorEvent = CommonEventProps; | ||
type StaleEvent = CommonEventProps; | ||
type ConfigChangeEvent = CommonEventProps & { | ||
flagsChanged?: string[]; | ||
}; | ||
type EventMap = { | ||
[ProviderEvents.Ready]: ReadyEvent; | ||
[ProviderEvents.Error]: ErrorEvent; | ||
[ProviderEvents.Stale]: StaleEvent; | ||
[ProviderEvents.ConfigurationChanged]: ConfigChangeEvent; | ||
}; | ||
type EventContext<T extends ProviderEvents, U extends Record<string, unknown> = Record<string, unknown>> = EventMap[T] & U; | ||
type EventDetails<T extends ProviderEvents> = EventContext<T> & CommonEventDetails; | ||
type EventHandler<T extends ProviderEvents> = (eventDetails?: EventDetails<T>) => Promise<unknown> | unknown; | ||
interface Eventing { | ||
@@ -38,3 +53,3 @@ /** | ||
*/ | ||
addHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
@@ -45,3 +60,3 @@ * Removes a handler for the given provider event type. | ||
*/ | ||
removeHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
@@ -52,3 +67,3 @@ * Gets the current handlers for the given provider event type. | ||
*/ | ||
getHandlers(eventType: ProviderEvents): EventHandler[]; | ||
getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[]; | ||
} | ||
@@ -94,3 +109,3 @@ | ||
declare class OpenFeatureEventEmitter implements ManageLogger<OpenFeatureEventEmitter> { | ||
declare abstract class GenericEventEmitter<AdditionalContext extends Record<string, unknown> = Record<string, unknown>> implements ManageLogger<GenericEventEmitter<AdditionalContext>> { | ||
private readonly globalLogger?; | ||
@@ -101,10 +116,26 @@ private readonly _handlers; | ||
constructor(globalLogger?: (() => Logger) | undefined); | ||
emit(eventType: ProviderEvents, context?: EventDetails): void; | ||
addHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
removeHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
emit<T extends ProviderEvents>(eventType: T, context?: EventContext<T, AdditionalContext>): void; | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
removeAllHandlers(eventType?: ProviderEvents): void; | ||
getHandlers(eventType: ProviderEvents): EventHandler[]; | ||
getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[]; | ||
setLogger(logger: Logger): this; | ||
private get _logger(); | ||
} | ||
/** | ||
* The OpenFeatureEventEmitter can be used by provider developers to emit | ||
* events at various parts of the provider lifecycle. | ||
* | ||
* NOTE: Ready and error events are automatically emitted by the SDK based on | ||
* the result of the initialize method. | ||
*/ | ||
declare class OpenFeatureEventEmitter extends GenericEventEmitter { | ||
} | ||
/** | ||
* The InternalEventEmitter should only be used within the SDK. It extends the | ||
* OpenFeatureEventEmitter to include additional properties that can be included | ||
* in the event details. | ||
*/ | ||
declare class InternalEventEmitter extends GenericEventEmitter<CommonEventDetails> { | ||
} | ||
@@ -469,3 +500,3 @@ interface Metadata { | ||
protected _clientProviders: Map<string, P>; | ||
protected _clientEvents: Map<string | undefined, OpenFeatureEventEmitter>; | ||
protected _clientEvents: Map<string | undefined, InternalEventEmitter>; | ||
addHooks(...hooks: Hook<FlagValue>[]): this; | ||
@@ -487,3 +518,3 @@ getHooks(): Hook<FlagValue>[]; | ||
*/ | ||
addHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
@@ -494,3 +525,3 @@ * Removes a handler for the given provider event type. | ||
*/ | ||
removeHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
@@ -501,3 +532,3 @@ * Gets the current handlers for the given provider event type. | ||
*/ | ||
getHandlers(eventType: ProviderEvents): EventHandler[]; | ||
getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[]; | ||
/** | ||
@@ -522,4 +553,5 @@ * Sets the default provider for flag evaluations. | ||
protected getProviderForClient(name?: string): P; | ||
protected getAndCacheEventEmitterForClient(name?: string): OpenFeatureEventEmitter; | ||
protected buildAndCacheEventEmitterForClient(name?: string): InternalEventEmitter; | ||
private getUnboundEmitters; | ||
private getAssociatedEventEmitters; | ||
private transferListeners; | ||
@@ -688,7 +720,7 @@ close(): Promise<void>; | ||
private _clientLogger?; | ||
constructor(providerAccessor: () => Provider, emitterAccessor: () => OpenFeatureEventEmitter, globalLogger: () => Logger, options: OpenFeatureClientOptions, context?: EvaluationContext); | ||
constructor(providerAccessor: () => Provider, emitterAccessor: () => InternalEventEmitter, globalLogger: () => Logger, options: OpenFeatureClientOptions, context?: EvaluationContext); | ||
get metadata(): ClientMetadata; | ||
addHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
removeHandler(eventType: ProviderEvents, handler: EventHandler): void; | ||
getHandlers(eventType: ProviderEvents): EventHandler[]; | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
getHandlers(eventType: ProviderEvents): EventHandler<ProviderEvents>[]; | ||
setLogger(logger: Logger): OpenFeatureClient; | ||
@@ -770,2 +802,2 @@ setContext(context: EvaluationContext): OpenFeatureClient; | ||
export { BeforeHookContext, Client, ClientMetadata, CommonProvider, DefaultLogger, ErrorCode, EvaluationContext, EvaluationContextValue, EvaluationDetails, EvaluationLifeCycle, EventDetails, EventHandler, EventMetadata, Eventing, Features, FlagEvaluationOptions, FlagMetadata, FlagNotFoundError, FlagValue, FlagValueType, GeneralError, Hook, HookContext, HookHints, InvalidContextError, JsonArray, JsonObject, JsonValue, LOG_LEVELS, Logger, ManageContext, ManageLogger, ManageTransactionContextPropagator, Metadata, NOOP_PROVIDER, NOOP_TRANSACTION_CONTEXT_PROPAGATOR, OpenFeature, OpenFeatureAPI, OpenFeatureClient, OpenFeatureCommonAPI, OpenFeatureError, OpenFeatureEventEmitter, ParseError, PrimitiveValue, Provider, ProviderEvents, ProviderMetadata, ProviderStatus, ResolutionDetails, ResolutionReason, SafeLogger, StandardResolutionReasons, TargetingKeyMissingError, TransactionContext, TransactionContextPropagator, TypeMismatchError, isObject, isString, objectOrUndefined, stringOrUndefined }; | ||
export { BeforeHookContext, Client, ClientMetadata, CommonEventDetails, CommonProvider, ConfigChangeEvent, DefaultLogger, ErrorCode, ErrorEvent, EvaluationContext, EvaluationContextValue, EvaluationDetails, EvaluationLifeCycle, EventContext, EventDetails, EventHandler, EventMetadata, Eventing, Features, FlagEvaluationOptions, FlagMetadata, FlagNotFoundError, FlagValue, FlagValueType, GeneralError, Hook, HookContext, HookHints, InternalEventEmitter, InvalidContextError, JsonArray, JsonObject, JsonValue, LOG_LEVELS, Logger, ManageContext, ManageLogger, ManageTransactionContextPropagator, Metadata, NOOP_PROVIDER, NOOP_TRANSACTION_CONTEXT_PROPAGATOR, OpenFeature, OpenFeatureAPI, OpenFeatureClient, OpenFeatureCommonAPI, OpenFeatureError, OpenFeatureEventEmitter, ParseError, PrimitiveValue, Provider, ProviderEvents, ProviderMetadata, ProviderStatus, ReadyEvent, ResolutionDetails, ResolutionReason, SafeLogger, StaleEvent, StandardResolutionReasons, TargetingKeyMissingError, TransactionContext, TransactionContextPropagator, TypeMismatchError, isObject, isString, objectOrUndefined, stringOrUndefined }; |
{ | ||
"name": "@openfeature/js-sdk", | ||
"version": "1.3.3", | ||
"version": "1.4.0", | ||
"description": "OpenFeature SDK for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
234419
2.3%2475
1.98%