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

Function setter

src/index.tsx:51–94  ·  view source on GitHub ↗
(state: Partial<S> | ((prevState: S) => S | void))

Source from the content-addressed store, hash-verified

49 }
50
51 const setter = async (state: Partial<S> | ((prevState: S) => S | void)) => {
52 if (typeof state === 'function') {
53 Global.mutableState[storeId][index] = produce(
54 Global.mutableState[storeId][index],
55 // @ts-ignore
56 state
57 )
58 } else {
59 if (
60 Global.mutableState[storeId][index] &&
61 state &&
62 Global.mutableState[storeId][index].constructor.name === 'Object' &&
63 state.constructor.name === 'Object'
64 ) {
65 Global.mutableState[storeId][index] = {
66 ...Global.mutableState[storeId][index],
67 ...state
68 }
69 } else {
70 Global.mutableState[storeId][index] = state
71 }
72 }
73
74 const context: InnerContext = {
75 Global,
76 action: () => {
77 return typeof state === 'function'
78 ? // @ts-ignore
79 Global.mutableState[storeId][index]
80 : state
81 },
82 actionName: 'setter',
83 consumerActions,
84 disableSelectorUpdate: true,
85 middlewareConfig: {},
86 modelName: storeId,
87 newState: {},
88 params: undefined,
89 type: 'u'
90 }
91
92 // pass update event to other components subscribe the same store
93 return await applyMiddlewares(actionMiddlewares, context)
94 }
95 return [Global.mutableState[storeId][index], setter]
96}
97

Callers

nothing calls this directly

Calls 1

applyMiddlewaresFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…