MCPcopy Create free account
hub / github.com/byte-fe/react-model / createStore

Function createStore

src/index.tsx:100–141  ·  view source on GitHub ↗
(n: any, u?: any)

Source from the content-addressed store, hash-verified

98function createStore<S>(useHook: CustomModelHook<S>): LaneAPI<S>
99function createStore<S>(name: string, useHook: CustomModelHook<S>): LaneAPI<S>
100function createStore<S>(n: any, u?: any): LaneAPI<S> {
101 const hasName = typeof n === 'string'
102 Global.storeId += hasName ? 0 : 1
103 const storeId = hasName ? n : Global.storeId.toString()
104 if (!Global.Actions[storeId]) {
105 Global.Actions[storeId] = {}
106 }
107 if (!Global.mutableState[storeId]) {
108 Global.mutableState[storeId] = { count: 0 }
109 }
110 // Global.currentStoreId = storeId
111 // const state = useHook()
112 // Global.State = produce(Global.State, (s) => {
113 // s[hash] = state
114 // })
115 const selector = () => {
116 Global.mutableState[storeId].count = 0
117 Global.currentStoreId = storeId
118 const res = u ? u() : n()
119 return res
120 }
121 Global.mutableState[storeId].selector = selector
122 return {
123 // TODO: support selector
124 useStore: () => useStore(storeId, selector),
125 getState: () => selector(),
126 subscribe: (callback: () => void) => {
127 if (!Global.subscriptions[storeId]) {
128 Global.subscriptions[storeId] = []
129 }
130 Global.subscriptions[storeId].push(callback)
131 },
132 unsubscribe: (callback?: () => void) => {
133 if (Global.subscriptions[storeId]) {
134 if (callback) {
135 const idx = Global.subscriptions[storeId].indexOf(callback)
136 if (idx >= 0) Global.subscriptions[storeId].splice(idx, 1)
137 }
138 }
139 }
140 }
141}
142
143function Model<E, Ctx extends {}, MT extends ModelType<any, any, {}>>(
144 models: MT,

Callers 4

react.spec.tsFile · 0.90
migrate.spec.tsFile · 0.90
lane.spec.tsFile · 0.90

Calls 2

useStoreFunction · 0.85
selectorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…