|
| 1 | +import { effect as rawEffect, reactive, ReactiveEffect } from '@vue/reactivity' |
| 2 | +import { Block } from './block' |
| 3 | +import { Directive } from './directives' |
| 4 | +import { queueJob } from './scheduler' |
| 5 | +import { inOnce } from './walk' |
| 6 | + |
| 7 | +export interface Context { |
| 8 | + key?: any |
| 9 | + scope: Record<string, any> |
| 10 | + dirs: Record<string, Directive> |
| 11 | + blocks: Block[] |
| 12 | + effect: typeof rawEffect |
| 13 | + effects: ReactiveEffect[] |
| 14 | + cleanups: (() => void)[] |
| 15 | +} |
| 16 | + |
| 17 | +export const createContext = (parent?: Context): Context => { |
| 18 | + const ctx: Context = { |
| 19 | + ...parent, |
| 20 | + scope: parent ? parent.scope : reactive({}), |
| 21 | + dirs: parent ? parent.dirs : {}, |
| 22 | + effects: [], |
| 23 | + blocks: [], |
| 24 | + cleanups: [], |
| 25 | + effect: (fn) => { |
| 26 | + if (inOnce) { |
| 27 | + queueJob(fn) |
| 28 | + return fn as any |
| 29 | + } |
| 30 | + const e: ReactiveEffect = rawEffect(fn, { |
| 31 | + scheduler: () => queueJob(e) |
| 32 | + }) |
| 33 | + ctx.effects.push(e) |
| 34 | + return e |
| 35 | + } |
| 36 | + } |
| 37 | + return ctx |
| 38 | +} |
| 39 | + |
| 40 | +export const createScopedContext = (ctx: Context, data = {}): Context => { |
| 41 | + const parentScope = ctx.scope |
| 42 | + const mergedScope = Object.create(parentScope) |
| 43 | + Object.defineProperties(mergedScope, Object.getOwnPropertyDescriptors(data)) |
| 44 | + mergedScope.$refs = Object.create(parentScope.$refs) |
| 45 | + const reactiveProxy = reactive( |
| 46 | + new Proxy(mergedScope, { |
| 47 | + set(target, key, val, receiver) { |
| 48 | + // when setting a property that doesn't exist on current scope, |
| 49 | + // do not create it on the current scope and fallback to parent scope. |
| 50 | + if (receiver === reactiveProxy && !target.hasOwnProperty(key)) { |
| 51 | + return Reflect.set(parentScope, key, val) |
| 52 | + } |
| 53 | + return Reflect.set(target, key, val, receiver) |
| 54 | + } |
| 55 | + }) |
| 56 | + ) |
| 57 | + return { |
| 58 | + ...ctx, |
| 59 | + scope: reactiveProxy |
| 60 | + } |
| 61 | +} |
0 commit comments