telefunc
Version:
Remote functions. Instead of API.
26 lines (25 loc) • 1.42 kB
JavaScript
export { getContext_async };
export { provideTelefuncContext_async };
import { AsyncLocalStorage } from 'node:async_hooks';
import { assert, assertWarning, isObject, getGlobalObject, assertUsage } from '../../utils.js';
import { installAsyncMode } from '../getContext.js';
installAsyncMode({ getContext_async, provideTelefuncContext_async, restoreContext_async });
const globalObject = getGlobalObject('getContext/async.ts', {});
function getContext_async() {
const errMsg = '[getContext()] Make sure to call provideTelefuncContext() before calling getContext()';
assertUsage(globalObject.asyncStore, errMsg);
const context = globalObject.asyncStore.getStore();
assert(context === undefined || isObject(context));
assertUsage(context, errMsg);
return context;
}
function provideTelefuncContext_async(context) {
var _a;
assertUsage(isObject(context), '[provideTelefuncContext(context)] Argument `context` should be an object');
globalObject.asyncStore = (_a = globalObject.asyncStore) !== null && _a !== void 0 ? _a : new AsyncLocalStorage();
globalObject.asyncStore.enterWith(context);
}
function restoreContext_async(context) {
assert(context === null || isObject(context));
assertWarning(!context, 'When using `provideTelefuncContext()` (i.e. Async Hooks), then providing the `context` object to the server middleware `telefunc()` has no effect.', { onlyOnce: true });
}