-
-
Notifications
You must be signed in to change notification settings - Fork 709
Expand file tree
/
Copy pathcontexts.ts
More file actions
35 lines (29 loc) · 1021 Bytes
/
contexts.ts
File metadata and controls
35 lines (29 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { createContext } from 'react'
import type { Context } from 'react'
import type { Atom, Scope } from './atom'
import { VERSION_OBJECT, createStore } from './store'
import type { Store } from './store'
type GetVersion = () => object | undefined
export type ScopeContainer = {
s: Store
v: GetVersion
}
export const createScopeContainer = (
initialValues?: Iterable<readonly [Atom<unknown>, unknown]>
): ScopeContainer => {
const store = createStore(initialValues)
const getVersion =
typeof process === 'object' &&
process.env.JOTAI_EXPERIMENTAL_VERSION_OBJECT === 'true'
? store[VERSION_OBJECT]
: () => undefined
return { s: store, v: getVersion }
}
type ScopeContext = Context<ScopeContainer>
const ScopeContextMap = new Map<Scope | undefined, ScopeContext>()
export const getScopeContext = (scope?: Scope) => {
if (!ScopeContextMap.has(scope)) {
ScopeContextMap.set(scope, createContext(createScopeContainer()))
}
return ScopeContextMap.get(scope) as ScopeContext
}