-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* store context * fix
- Loading branch information
Showing
13 changed files
with
168 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"derived-field": "DerivedField", | ||
"effect": "Effect", | ||
"key": "Key", | ||
"ref": "Ref", | ||
"slice": "Slice", | ||
"state-field": "StateField", | ||
"store-state": "StoreState", | ||
"store": "Store", | ||
"transaction": "Transaction" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
### `.createStore` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# React API | ||
|
||
|
||
### `createContextStore` | ||
|
||
The same as [createStore](/docs/api/store/#createstore) with the difference that it accepts the following additional properties: | ||
|
||
- `context`: The react context object created using `React.createContext()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Common errors | ||
|
||
|
||
## Store Context | ||
|
||
#### `Cannot create a context store with a slice that is already associated with another store` | ||
|
||
This error occurs if you have multiple stores in your app and you are using the `createContextStore` method that uses React Context to share NSM store behind the scenes. | ||
|
||
- Solution1: If you want to use *multiple stores* then you will have to use the [createStore](/docs/api/store/#createstore) function to create store and share it on by your self to all components that need it. | ||
- Solution2: Use a single store for your entire app. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export { cleanup } from '@nalanda/core'; | ||
export { createKey } from '@nalanda/core'; | ||
// NO STORE EXPORTS, as we want to use the React-specific store | ||
// export { createStore, _storeSlicesInspect } from '@nalanda/core'; | ||
export { DerivedField, StateField } from '@nalanda/core'; | ||
export { ref } from '@nalanda/core'; | ||
export { Slice } from '@nalanda/core'; | ||
|
||
export type { BaseField } from '@nalanda/core'; | ||
export type { Effect, EffectStore, EffectScheduler } from '@nalanda/core'; | ||
export type { IfSubset } from '@nalanda/core'; | ||
export type { Key } from '@nalanda/core'; | ||
export type { Store } from '@nalanda/core'; | ||
export type { StoreState } from '@nalanda/core'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from '@nalanda/core'; | ||
export * from './core-exports'; | ||
export * from './react'; | ||
export { createContextStore } from './store'; | ||
export type { ContextStoreOptions } from './store'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
createStore as createVanillaStore, | ||
Store, | ||
StoreOptions, | ||
} from '@nalanda/core'; | ||
|
||
export interface ContextStoreOptions<TSliceName extends string> | ||
extends StoreOptions<TSliceName> { | ||
/** | ||
* The react context object created using React.createContext() | ||
*/ | ||
context: React.Context<Store<any> | null>; | ||
} | ||
|
||
export const StoreContextSymbol = Symbol('StoreContextKey'); | ||
|
||
export function createContextStore<TSliceName extends string = any>( | ||
options: ContextStoreOptions<TSliceName>, | ||
): Store<TSliceName> { | ||
options.slices.forEach((slice) => { | ||
// @ts-expect-error - this is a private symbol | ||
if (slice[StoreContextSymbol]) { | ||
throw new Error( | ||
`Cannot create a context store with a slice that is already associated with another store. Please see https://nalanda.bangle.io/docs/react/common-errors/#store-context`, | ||
); | ||
} | ||
// @ts-expect-error - this is a private symbol | ||
slice[StoreContextSymbol] = options.context; | ||
}); | ||
|
||
const store = createVanillaStore(options); | ||
|
||
store.destroySignal.addEventListener( | ||
'abort', | ||
() => { | ||
options.slices.forEach((slice) => { | ||
// @ts-expect-error - this is a private symbol | ||
delete slice[StoreContextSymbol]; | ||
}); | ||
}, | ||
{ once: true }, | ||
); | ||
|
||
return store; | ||
} |