MCPcopy Create free account
hub / github.com/drcmda/react-contextual / constructor

Method constructor

src/store.js:56–98  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

54 }
55
56 constructor(props) {
57 super()
58 const { store, children, id, ...state } = props
59 this.store = store || createStore(state, id)
60 // When no store is given, create context by id or refer to the default context
61 if (!store)
62 this.store.context = id ? createNamedContext(id) : ProviderContext
63 // Overwrite the functions in store.state to update the state of this Provider
64 const actions = getStateUpdateFunctions(this.store.initialState)
65 Object.assign(
66 this.store.state,
67 Object.keys(actions).reduce(
68 (acc, name) => ({
69 ...acc,
70 [name]: (...args) => {
71 let result = actions[name](...args)
72 let isFunc = typeof result === 'function'
73 if (isFunc) result = result(this.state)
74 if (result.then) {
75 return new Promise(res =>
76 Promise.resolve(result).then(state => {
77 // Update store
78 this.store.state = { ...this.store.state, ...state }
79 // Call subscribers
80 this.store.subscriptions.forEach(callback => callback(state))
81 // Update local state
82 this.setState(state, res)
83 })
84 )
85 } else {
86 // Update store in sync
87 this.store.state = { ...this.store.state, ...result }
88 this.store.subscriptions.forEach(callback => callback(result))
89 this.setState(result)
90 return true
91 }
92 },
93 }),
94 {}
95 )
96 )
97 this.state = this.store.state
98 }
99
100 componentWillUnmount() {
101 if (this.props.id) removeNamedContext(this.props.id)

Callers

nothing calls this directly

Calls 3

createNamedContextFunction · 0.90
createStoreFunction · 0.85
getStateUpdateFunctionsFunction · 0.85

Tested by

no test coverage detected