|
| 1 | +// @flow |
| 2 | + |
| 3 | +import type { |
| 4 | + AbstractContext, |
| 5 | + UserElement, |
| 6 | + ContextMap, |
| 7 | + ContextStore |
| 8 | +} from '../types' |
| 9 | + |
| 10 | +type ContextEntry = [AbstractContext, mixed] |
| 11 | + |
| 12 | +let currentContextStore: ContextStore = new Map() |
| 13 | +let currentContextMap: ContextMap = {} |
| 14 | + |
| 15 | +let prevContextMap: void | ContextMap = undefined |
| 16 | +let prevContextEntry: void | ContextEntry = undefined |
| 17 | + |
| 18 | +export const getCurrentContextMap = (): ContextMap => currentContextMap |
| 19 | +export const getCurrentContextStore = (): ContextStore => |
| 20 | + new Map(currentContextStore) |
| 21 | + |
| 22 | +export const flushPrevContextMap = (): void | ContextMap => { |
| 23 | + const prev = prevContextMap |
| 24 | + prevContextMap = undefined |
| 25 | + return prev |
| 26 | +} |
| 27 | + |
| 28 | +export const flushPrevContextStore = (): void | ContextEntry => { |
| 29 | + const prev = prevContextEntry |
| 30 | + prevContextEntry = undefined |
| 31 | + return prev |
| 32 | +} |
| 33 | + |
| 34 | +export const restoreContextMap = (prev: ContextMap) => { |
| 35 | + currentContextMap = prev |
| 36 | +} |
| 37 | + |
| 38 | +export const restoreContextStore = (prev: ContextEntry) => { |
| 39 | + currentContextStore.set(prev[0], prev[1]) |
| 40 | +} |
| 41 | + |
| 42 | +export const setCurrentContextMap = (map: ContextMap) => { |
| 43 | + prevContextMap = undefined |
| 44 | + currentContextMap = map |
| 45 | +} |
| 46 | + |
| 47 | +export const setCurrentContextStore = (store: ContextStore) => { |
| 48 | + prevContextEntry = undefined |
| 49 | + currentContextStore = store |
| 50 | +} |
| 51 | + |
| 52 | +export const assignContextMap = (map: ContextMap) => { |
| 53 | + prevContextMap = currentContextMap |
| 54 | + currentContextMap = Object.assign({}, currentContextMap, map) |
| 55 | +} |
| 56 | + |
| 57 | +export const setContextValue = (context: AbstractContext, value: mixed) => { |
| 58 | + prevContextEntry = [context, currentContextStore.get(context)] |
| 59 | + currentContextStore.set(context, value) |
| 60 | +} |
| 61 | + |
| 62 | +export const readContextValue = (context: AbstractContext) => { |
| 63 | + const value = currentContextStore.get(context) |
| 64 | + if (value !== undefined) { |
| 65 | + return value |
| 66 | + } |
| 67 | + |
| 68 | + // Return default if context has no value yet |
| 69 | + return context._currentValue |
| 70 | +} |
| 71 | + |
| 72 | +const emptyContext = {} |
| 73 | + |
| 74 | +export const maskContext = (type: $PropertyType<UserElement, 'type'>) => { |
| 75 | + const { contextType, contextTypes } = type |
| 76 | + |
| 77 | + if (contextType) { |
| 78 | + return readContextValue(contextType) |
| 79 | + } else if (!contextTypes) { |
| 80 | + return emptyContext |
| 81 | + } |
| 82 | + |
| 83 | + const maskedContext = {} |
| 84 | + for (const name in contextTypes) { |
| 85 | + maskedContext[name] = currentContextMap[name] |
| 86 | + } |
| 87 | + |
| 88 | + return maskedContext |
| 89 | +} |
0 commit comments