UNPKG

@aws-amplify/interactions

Version:

Interactions category of aws-amplify

37 lines (35 loc) 1.54 kB
/** * Creates a per-context callback registry. Keyed on the *resolved* * `AmplifyContext` object identity so a callback registered against one * context never fires for another (context isolation) — previously this was * a single process-global map keyed only by bot name, which leaked callbacks * across independent contexts. * * NOTE (re-configure semantics): `Amplify.configure()` publishes a NEW frozen * global context object on every call. A callback registered against the * global context before a re-configure is therefore keyed on the OLD context * object and will NOT be found after re-configure (the new global context is * a different object). Callers relying on the global context must re-register * their callback after reconfiguring; callers passing an explicit context keep * their callbacks for that context's lifetime. Using a `WeakMap` lets the * per-context records be garbage-collected once a context is unreferenced. */ function createPerContextCallbackRegistry() { const callbacksByCtx = new WeakMap(); return { set(ctx, name, callback) { const existing = callbacksByCtx.get(ctx); if (existing) { existing[name] = callback; } else { callbacksByCtx.set(ctx, { [name]: callback }); } }, get(ctx, name) { return callbacksByCtx.get(ctx)?.[name]; }, }; } export { createPerContextCallbackRegistry }; //# sourceMappingURL=perContextCallbackRegistry.mjs.map