astro-ghostcms/.pnpm-store/v3/files/58/287713c1fde7622b8b41d805c8a...

1 line
7.2 KiB
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
{"version":3,"names":["_gensync","data","require","asyncGeneratorStep","gen","resolve","reject","_next","_throw","key","arg","info","value","error","done","Promise","then","_asyncToGenerator","fn","self","args","arguments","apply","err","undefined","runGenerator","gensync","item","isAsync","exports","sync","errback","cb","maybeAsync","message","result","isThenable","Error","async","withKind","_ref","_x","forwardAsync","action","g","kind","adapted","onFirstPause","name","arity","firstPause","completed","waitFor","x","_ref2","_x2","val"],"sources":["../../src/gensync-utils/async.ts"],"sourcesContent":["import gensync, { type Gensync, type Handler, type Callback } from \"gensync\";\n\ntype MaybePromise<T> = T | Promise<T>;\n\nconst runGenerator: {\n sync<Return>(gen: Handler<Return>): Return;\n async<Return>(gen: Handler<Return>): Promise<Return>;\n errback<Return>(gen: Handler<Return>, cb: Callback<Return>): void;\n} = gensync(function* (item: Handler<any>): Handler<any> {\n return yield* item;\n});\n\n// This Gensync returns true if the current execution context is\n// asynchronous, otherwise it returns false.\nexport const isAsync = gensync({\n sync: () => false,\n errback: cb => cb(null, true),\n});\n\n// This function wraps any functions (which could be either synchronous or\n// asynchronous) with a Gensync. If the wrapped function returns a promise\n// but the current execution context is synchronous, it will throw the\n// provided error.\n// This is used to handle user-provided functions which could be asynchronous.\nexport function maybeAsync<Args extends unknown[], Return>(\n fn: (...args: Args) => Return,\n message: string,\n): Gensync<Args, Return> {\n return gensync({\n sync(...args) {\n const result = fn.apply(this, args);\n if (isThenable(result)) throw new Error(message);\n return result;\n },\n async(...args) {\n return Promise.resolve(fn.apply(this, args));\n },\n });\n}\n\nconst withKind = gensync({\n sync: cb => cb(\"sync\"),\n async: async cb => cb(\"async\"),\n}) as <T>(cb: (kind: \"sync\" | \"async\") => MaybePromise<T>) => Handler<T>;\n\n// This function wraps a generator (or a Gensync) into another function which,\n// when called, will run the provided generator in a sync or async way, depending\n// on the execution context where this forwardAsync function is called.\n// This is useful, for example, when passing a callback to a function which isn't\n// aware of gensync, but it only knows about synchronous and asynchronous functions.\n// An example is cache.using, which being exposed to the user must be as simple as\n// possible:\n// yield* forwardAsync(gensyncFn, wrappedFn =>\n// cache.using(x => {\n// // Here we don't know about gensync. wrappedFn is a\n// // normal sync or async function\n// return wrappedFn(x);\n// })\n// )\nexport function forwardAsync<Args extends unknown[], Return>(\n action: (...args: Args) => Handler<Return>,\n cb: (\n adapted: (...args: Args) => MaybePromise<Return>,\n ) => MaybePromise<Return>,\n): Handler<Return> {\n const g = gensync(action);\n return withKind(kind => {\n const adapted = g[kind];\n return cb(adapted);\n });\n}\n\n// If the given generator is executed asynchronously, the first time that it\n// is paused (i.e. When it yields a gensync generator which can't be run\n// synchronously), call the \"firstPause\" callback.\nexport const onFirstPause = gensync<\n [gen: Handler<unknown>, firstPause: () => void],\n unknown\n>({\n name: \"onFirstPause\",\n arity: 2,\n sync: function (item) {\n return runGenerator.sync(item);\n },\n errback: function (item, firstPause, cb) {\n let completed = false;\n\n runGenerator.errback(item, (err, value) => {\n completed = true;\n cb(err, value);\n });\n\n if (!completed) {\n firstPause();\n }\n },\n}) as <T>(gen: Handler<T>, firstPause: () => void) => Handler<T>;\n\n// Wait for the given promise to be resolved\nexport const waitFor = gensync({\n sync: x => x,\n async: async x => x,\n})