MessagesForMacintosh/JS/node_modules/@wry/context/lib/context.esm.js.map

1 line
12 KiB
Plaintext

{"version":3,"file":"context.esm.js","sources":["../src/slot.ts","../src/context.ts"],"sourcesContent":["type Context = {\n parent: Context | null;\n slots: { [slotId: string]: any };\n}\n\n// This currentContext variable will only be used if the makeSlotClass\n// function is called, which happens only if this is the first copy of the\n// @wry/context package to be imported.\nlet currentContext: Context | null = null;\n\n// This unique internal object is used to denote the absence of a value\n// for a given Slot, and is never exposed to outside code.\nconst MISSING_VALUE: any = {};\n\nlet idCounter = 1;\n\n// Although we can't do anything about the cost of duplicated code from\n// accidentally bundling multiple copies of the @wry/context package, we can\n// avoid creating the Slot class more than once using makeSlotClass.\nconst makeSlotClass = () => class Slot<TValue> {\n // If you have a Slot object, you can find out its slot.id, but you cannot\n // guess the slot.id of a Slot you don't have access to, thanks to the\n // randomized suffix.\n public readonly id = [\n \"slot\",\n idCounter++,\n Date.now(),\n Math.random().toString(36).slice(2),\n ].join(\":\");\n\n public hasValue() {\n for (let context = currentContext; context; context = context.parent) {\n // We use the Slot object iself as a key to its value, which means the\n // value cannot be obtained without a reference to the Slot object.\n if (this.id in context.slots) {\n const value = context.slots[this.id];\n if (value === MISSING_VALUE) break;\n if (context !== currentContext) {\n // Cache the value in currentContext.slots so the next lookup will\n // be faster. This caching is safe because the tree of contexts and\n // the values of the slots are logically immutable.\n currentContext!.slots[this.id] = value;\n }\n return true;\n }\n }\n if (currentContext) {\n // If a value was not found for this Slot, it's never going to be found\n // no matter how many times we look it up, so we might as well cache\n // the absence of the value, too.\n currentContext.slots[this.id] = MISSING_VALUE;\n }\n return false;\n }\n\n public getValue(): TValue | undefined {\n if (this.hasValue()) {\n return currentContext!.slots[this.id] as TValue;\n }\n }\n\n public withValue<TResult, TArgs extends any[], TThis = any>(\n value: TValue,\n callback: (this: TThis, ...args: TArgs) => TResult,\n // Given the prevalence of arrow functions, specifying arguments is likely\n // to be much more common than specifying `this`, hence this ordering:\n args?: TArgs,\n thisArg?: TThis,\n ): TResult {\n const slots = {\n __proto__: null,\n [this.id]: value,\n };\n const parent = currentContext;\n currentContext = { parent, slots };\n try {\n // Function.prototype.apply allows the arguments array argument to be\n // omitted or undefined, so args! is fine here.\n return callback.apply(thisArg!, args!);\n } finally {\n currentContext = parent;\n }\n }\n\n // Capture the current context and wrap a callback function so that it\n // reestablishes the captured context when called.\n static bind<TArgs extends any[], TResult>(\n callback: (...args: TArgs) => TResult,\n ) {\n const context = currentContext;\n return function (this: any) {\n const saved = currentContext;\n try {\n currentContext = context;\n return callback.apply(this, arguments as any);\n } finally {\n currentContext = saved;\n }\n } as typeof callback;\n }\n\n // Immediately run a callback function without any captured context.\n static noContext<TResult, TArgs extends any[], TThis = any>(\n callback: (this: TThis, ...args: TArgs) => TResult,\n // Given the prevalence of arrow functions, specifying arguments is likely\n // to be much more common than specifying `this`, hence this ordering:\n args?: TArgs,\n thisArg?: TThis,\n ) {\n if (currentContext) {\n const saved = currentContext;\n try {\n currentContext = null;\n // Function.prototype.apply allows the arguments array argument to be\n // omitted or undefined, so args! is fine here.\n return callback.apply(thisArg!, args!);\n } finally {\n currentContext = saved;\n }\n } else {\n return callback.apply(thisArg!, args!);\n }\n }\n};\n\n// We store a single global implementation of the Slot class as a permanent\n// non-enumerable symbol property of the Array constructor. This obfuscation\n// does nothing to prevent access to the Slot class, but at least it ensures\n// the implementation (i.e. currentContext) cannot be tampered with, and all\n// copies of the @wry/context package (hopefully just one) will share the\n// same Slot implementation. Since the first copy of the @wry/context package\n// to be imported wins, this technique imposes a very high cost for any\n// future breaking changes to the Slot class.\nconst globalKey = \"@wry/context:Slot\";\nconst host = Array as any;\n\nexport const Slot: ReturnType<typeof makeSlotClass> = host[globalKey] || function () {\n const Slot = makeSlotClass();\n try {\n Object.defineProperty(host, globalKey, {\n value: host[globalKey] = Slot,\n enumerable: false,\n writable: false,\n configurable: false,\n });\n } finally {\n return Slot;\n }\n}();\n","import { Slot } from \"./slot\";\nexport { Slot }\nexport const { bind, noContext } = Slot;\n\n// Like global.setTimeout, except the callback runs with captured context.\nexport { setTimeoutWithContext as setTimeout };\nfunction setTimeoutWithContext(callback: () => any, delay: number) {\n return setTimeout(bind(callback), delay);\n}\n\n// Turn any generator function into an async function (using yield instead\n// of await), with context automatically preserved across yields.\nexport function asyncFromGen<TArgs extends any[], TResult>(\n genFn: (...args: TArgs) => IterableIterator<TResult>,\n) {\n return function (this: any) {\n const gen = genFn.apply(this, arguments as any);\n const boundNext = bind(gen.next);\n const boundThrow = bind(gen.throw!);\n type Method = typeof boundNext | typeof boundThrow;\n\n return new Promise<TResult>((resolve, reject) => {\n function invoke(method: Method, argument: any) {\n try {\n var result = method.call(gen, argument);\n } catch (error) {\n return reject(error);\n }\n const next = result.done ? resolve : invokeNext;\n if (isPromiseLike(result.value)) {\n result.value.then(next, result.done ? reject : invokeThrow);\n } else {\n next(result.value);\n }\n }\n const invokeNext = (value?: any) => invoke(boundNext, value);\n const invokeThrow = (error: any) => invoke(boundThrow, error);\n invokeNext();\n });\n } as (...args: TArgs) => Promise<TResult>;\n}\n\nfunction isPromiseLike(value: any): value is PromiseLike<any> {\n return value && typeof value.then === \"function\";\n}\n\n// If you use the fibers npm package to implement coroutines in Node.js,\n// you should call this function at least once to ensure context management\n// remains coherent across any yields.\nconst wrappedFibers: Function[] = [];\nexport function wrapYieldingFiberMethods<F extends Function>(Fiber: F): F {\n // There can be only one implementation of Fiber per process, so this array\n // should never grow longer than one element.\n if (wrappedFibers.indexOf(Fiber) < 0) {\n const wrap = (obj: any, method: string) => {\n const fn = obj[method];\n obj[method] = function () {\n return noContext(fn, arguments as any, this);\n };\n }\n // These methods can yield, according to\n // https://github.com/laverdet/node-fibers/blob/ddebed9b8ae3883e57f822e2108e6943e5c8d2a8/fibers.js#L97-L100\n wrap(Fiber, \"yield\");\n wrap(Fiber.prototype, \"run\");\n wrap(Fiber.prototype, \"throwInto\");\n wrappedFibers.push(Fiber);\n }\n return Fiber;\n}\n"],"names":[],"mappings":"AA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