From 97817711d3eebaacc354a77aa83bc9544cc87d42 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 14 Jan 2023 11:26:56 +0530 Subject: feat: adds bindto with scopes --- src/context.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/context.ts') diff --git a/src/context.ts b/src/context.ts index 06fe041..adc0be5 100644 --- a/src/context.ts +++ b/src/context.ts @@ -76,6 +76,8 @@ export const createContext = (options: CtxOptions): Ctx => { let currentEnv = 'node' const setEnv = (e: string) => (currentEnv = e) + const environment: Array> = [] + const ctx: Ctx = { sourceFile, typeChecker, @@ -112,6 +114,27 @@ export const createContext = (options: CtxOptions): Ctx => { hasCustomEffect: (name) => customEffects[name] !== undefined, evaluateType, + + newScope() { + environment.unshift(new Map) + }, + addToScope(name, resKey) { + if (environment.length === 0) ctx.newScope() + const curScope = environment[0] + curScope?.set(name, resKey) + }, + clearScope() { + environment.shift() + }, + getKeyInScope(name) { + return environment.find(scope => scope.has(name))?.get(name) + }, + withScope: (fn) => async () => { + ctx.newScope() + const res = await fn() + ctx.clearScope() + return res + }, } return ctx -- cgit v1.3.1