MCPcopy
hub / github.com/mobxjs/mobx-state-tree / createStore

Function createStore

__tests__/core/reference-onInvalidated.test.ts:25–64  ·  view source on GitHub ↗
(
    partialSnapshot: any,
    onInvalidated?: OnReferenceInvalidated<Instance<typeof Todo>>,
    customRef = false
)

Source from the content-addressed store, hash-verified

23})
24
25const createStore = (
26 partialSnapshot: any,
27 onInvalidated?: OnReferenceInvalidated<Instance<typeof Todo>>,
28 customRef = false
29) => {
30 const refOptions = {
31 onInvalidated,
32 get(identifier: ReferenceIdentifier, parent: IAnyStateTreeNode | null) {
33 return (parent as Instance<typeof Store>).todos.find(t => t.id === identifier)
34 },
35 set(value: Instance<typeof Todo>): ReferenceIdentifier {
36 return value.id
37 }
38 }
39
40 if (!customRef) {
41 // @ts-ignore
42 delete refOptions.get
43 // @ts-ignore
44 delete refOptions.set
45 }
46
47 const Store = types.model({
48 todos: types.array(Todo),
49 onInv: types.maybe(types.reference(Todo, refOptions as any)),
50 single: types.safeReference(Todo),
51 deep: types.optional(
52 types.model({
53 single: types.safeReference(Todo)
54 }),
55 {}
56 ),
57 arr: types.array(types.safeReference(Todo)),
58 map: types.map(types.safeReference(Todo))
59 })
60
61 const s = Store.create(createSnapshot(partialSnapshot))
62 unprotect(s)
63 return s
64}
65
66for (const customRef of [false, true]) {
67 describe(`onInvalidated - customRef: ${customRef}`, () => {

Callers 1

Calls 3

unprotectFunction · 0.90
createSnapshotFunction · 0.85
createMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…