tinybase 6.2.0-beta.0 → 6.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@types/ui-react/index.d.ts +455 -7
- package/@types/ui-react/with-schemas/index.d.ts +5 -3
- package/package.json +79 -79
- package/readme.md +13 -13
- package/releases.md +28 -28
|
@@ -6736,7 +6736,81 @@ export function useIndexesOrIndexesById(
|
|
|
6736
6736
|
indexesOrIndexesId?: IndexesOrIndexesId,
|
|
6737
6737
|
): Indexes | undefined;
|
|
6738
6738
|
|
|
6739
|
-
|
|
6739
|
+
/**
|
|
6740
|
+
* The useProvideIndexes hook is used to add an Indexes object by Id to a
|
|
6741
|
+
* Provider component, but imperatively from a component within it.
|
|
6742
|
+
*
|
|
6743
|
+
* Normally you will register an Indexes object by Id in a context by using the
|
|
6744
|
+
* `indexesById` prop of the top-level Provider component. This hook, however,
|
|
6745
|
+
* lets you dynamically add a new Indexes object to the context, from within a
|
|
6746
|
+
* descendent component. This is useful for applications where the set of
|
|
6747
|
+
* Indexes objects is not known at the time of the first render of the root
|
|
6748
|
+
* Provider.
|
|
6749
|
+
*
|
|
6750
|
+
* A Indexes object added to the Provider context in this way will be available
|
|
6751
|
+
* to other components within the context (using the useIndexes hook and so on).
|
|
6752
|
+
* If you use the same Id as an existing Indexes object registration, the new
|
|
6753
|
+
* one will take priority over one provided by the `indexesById` prop.
|
|
6754
|
+
*
|
|
6755
|
+
* Note that other components that consume an Indexes object registered like
|
|
6756
|
+
* this should defend against it being undefined at first. On the first render,
|
|
6757
|
+
* the other component will likely not yet have completed the registration. In
|
|
6758
|
+
* the example below, we use the null-safe `useIndexes('petIndexes')?` to do
|
|
6759
|
+
* this.
|
|
6760
|
+
* @param indexesId The Id of the Indexes object to be registered with the
|
|
6761
|
+
* Provider.
|
|
6762
|
+
* @param indexes The Indexes object to be registered.
|
|
6763
|
+
* @example
|
|
6764
|
+
* This example creates a Provider context. A child component registers an
|
|
6765
|
+
* Indexes object into it which is then consumable by a peer child component.
|
|
6766
|
+
*
|
|
6767
|
+
* ```jsx
|
|
6768
|
+
* import React from 'react';
|
|
6769
|
+
* import {createRoot} from 'react-dom/client';
|
|
6770
|
+
* import {createIndexes, createStore} from 'tinybase';
|
|
6771
|
+
* import {
|
|
6772
|
+
* Provider,
|
|
6773
|
+
* useCreateIndexes,
|
|
6774
|
+
* useCreateStore,
|
|
6775
|
+
* useIndexes,
|
|
6776
|
+
* useProvideIndexes,
|
|
6777
|
+
* } from 'tinybase/ui-react';
|
|
6778
|
+
*
|
|
6779
|
+
* const App = () => (
|
|
6780
|
+
* <Provider>
|
|
6781
|
+
* <RegisterIndexes />
|
|
6782
|
+
* <ConsumeIndexes />
|
|
6783
|
+
* </Provider>
|
|
6784
|
+
* );
|
|
6785
|
+
* const RegisterIndexes = () => {
|
|
6786
|
+
* const store = useCreateStore(() =>
|
|
6787
|
+
* createStore().setCell('pets', 'fido', 'color', 'brown'),
|
|
6788
|
+
* );
|
|
6789
|
+
* const indexes = useCreateIndexes(store, (store) =>
|
|
6790
|
+
* createIndexes(store).setIndexDefinition(
|
|
6791
|
+
* 'petsByColor',
|
|
6792
|
+
* 'pets',
|
|
6793
|
+
* 'color',
|
|
6794
|
+
* ),
|
|
6795
|
+
* );
|
|
6796
|
+
* useProvideIndexes('petIndexes', indexes);
|
|
6797
|
+
* return null;
|
|
6798
|
+
* };
|
|
6799
|
+
* const ConsumeIndexes = () => (
|
|
6800
|
+
* <span>
|
|
6801
|
+
* {JSON.stringify(useIndexes('petIndexes')?.getSliceIds('petsByColor'))}
|
|
6802
|
+
* </span>
|
|
6803
|
+
* );
|
|
6804
|
+
*
|
|
6805
|
+
* const app = document.createElement('div');
|
|
6806
|
+
* const root = createRoot(app);
|
|
6807
|
+
* root.render(<App />); // !act
|
|
6808
|
+
* console.log(app.innerHTML);
|
|
6809
|
+
* // -> '<span>["brown"]</span>'
|
|
6810
|
+
* ```
|
|
6811
|
+
* @category Indexes hooks
|
|
6812
|
+
* @since v5.3.0
|
|
6813
|
+
*/
|
|
6740
6814
|
export function useProvideIndexes(indexesId: Id, indexes: Indexes): void;
|
|
6741
6815
|
|
|
6742
6816
|
/**
|
|
@@ -7543,7 +7617,89 @@ export function useRelationshipsOrRelationshipsById(
|
|
|
7543
7617
|
relationshipsOrRelationshipsId?: RelationshipsOrRelationshipsId,
|
|
7544
7618
|
): Relationships | undefined;
|
|
7545
7619
|
|
|
7546
|
-
|
|
7620
|
+
/**
|
|
7621
|
+
* The useProvideRelationships hook is used to add a Relationships object by Id
|
|
7622
|
+
* to a Provider component, but imperatively from a component within it.
|
|
7623
|
+
*
|
|
7624
|
+
* Normally you will register a Relationships object by Id in a context by using
|
|
7625
|
+
* the `relationshipsById` prop of the top-level Provider component. This hook,
|
|
7626
|
+
* however, lets you dynamically add a new Relationships object to the context,
|
|
7627
|
+
* from within a component. This is useful for applications where the set of
|
|
7628
|
+
* Relationships objects is not known at the time of the first render of the
|
|
7629
|
+
* root Provider.
|
|
7630
|
+
*
|
|
7631
|
+
* A Relationships object added to the Provider context in this way will be
|
|
7632
|
+
* available to other components within the context (using the useRelationships
|
|
7633
|
+
* hook and so on). If you use the same Id as an existing Relationships object
|
|
7634
|
+
* registration, the new one will take priority over one provided by the
|
|
7635
|
+
* `relationshipsById` prop.
|
|
7636
|
+
*
|
|
7637
|
+
* Note that other components that consume a Relationships object registered
|
|
7638
|
+
* like this should defend against it being undefined at first. On the first
|
|
7639
|
+
* render, the other component will likely not yet have completed the
|
|
7640
|
+
* registration. In the example below, we use the null-safe
|
|
7641
|
+
* `useRelationships('petRelationships')?` to do this.
|
|
7642
|
+
* @param relationshipsId The Id of the Relationships object to be registered
|
|
7643
|
+
* with the Provider.
|
|
7644
|
+
* @param relationships The Relationships object to be registered.
|
|
7645
|
+
* @example
|
|
7646
|
+
* This example creates a Provider context. A child component registers a
|
|
7647
|
+
* Relationships object into it which is then consumable by a peer child
|
|
7648
|
+
* component.
|
|
7649
|
+
*
|
|
7650
|
+
* ```jsx
|
|
7651
|
+
* import React from 'react';
|
|
7652
|
+
* import {createRoot} from 'react-dom/client';
|
|
7653
|
+
* import {createRelationships, createStore} from 'tinybase';
|
|
7654
|
+
* import {
|
|
7655
|
+
* Provider,
|
|
7656
|
+
* useCreateRelationships,
|
|
7657
|
+
* useCreateStore,
|
|
7658
|
+
* useProvideRelationships,
|
|
7659
|
+
* useRelationships,
|
|
7660
|
+
* } from 'tinybase/ui-react';
|
|
7661
|
+
*
|
|
7662
|
+
* const App = () => (
|
|
7663
|
+
* <Provider>
|
|
7664
|
+
* <RegisterRelationships />
|
|
7665
|
+
* <ConsumeRelationships />
|
|
7666
|
+
* </Provider>
|
|
7667
|
+
* );
|
|
7668
|
+
* const RegisterRelationships = () => {
|
|
7669
|
+
* const store = useCreateStore(() =>
|
|
7670
|
+
* createStore()
|
|
7671
|
+
* .setTable('pets', {fido: {species: 'dog'}})
|
|
7672
|
+
* .setTable('species', {dog: {price: 5}}),
|
|
7673
|
+
* );
|
|
7674
|
+
* const relationships = useCreateRelationships(store, (store) =>
|
|
7675
|
+
* createRelationships(store).setRelationshipDefinition(
|
|
7676
|
+
* 'petSpecies',
|
|
7677
|
+
* 'pets',
|
|
7678
|
+
* 'species',
|
|
7679
|
+
* 'species',
|
|
7680
|
+
* ),
|
|
7681
|
+
* );
|
|
7682
|
+
* useProvideRelationships('petRelationships', relationships);
|
|
7683
|
+
* return null;
|
|
7684
|
+
* };
|
|
7685
|
+
* const ConsumeRelationships = () => (
|
|
7686
|
+
* <span>
|
|
7687
|
+
* {useRelationships('petRelationships')?.getRemoteRowId(
|
|
7688
|
+
* 'petSpecies',
|
|
7689
|
+
* 'fido',
|
|
7690
|
+
* )}
|
|
7691
|
+
* </span>
|
|
7692
|
+
* );
|
|
7693
|
+
*
|
|
7694
|
+
* const app = document.createElement('div');
|
|
7695
|
+
* const root = createRoot(app);
|
|
7696
|
+
* root.render(<App />); // !act
|
|
7697
|
+
* console.log(app.innerHTML);
|
|
7698
|
+
* // -> '<span>dog</span>'
|
|
7699
|
+
* ```
|
|
7700
|
+
* @category Relationships hooks
|
|
7701
|
+
* @since v5.3.0
|
|
7702
|
+
*/
|
|
7547
7703
|
export function useProvideRelationships(
|
|
7548
7704
|
relationshipsId: Id,
|
|
7549
7705
|
relationships: Relationships,
|
|
@@ -8597,7 +8753,82 @@ export function useQueriesOrQueriesById(
|
|
|
8597
8753
|
queriesOrQueriesId?: QueriesOrQueriesId,
|
|
8598
8754
|
): Queries | undefined;
|
|
8599
8755
|
|
|
8600
|
-
|
|
8756
|
+
/**
|
|
8757
|
+
* The useProvideQueries hook is used to add a Queries object by Id to a
|
|
8758
|
+
* Provider component, but imperatively from a component within it.
|
|
8759
|
+
*
|
|
8760
|
+
* Normally you will register a Queries object by Id in a context by using the
|
|
8761
|
+
* `queriesById` prop of the top-level Provider component. This hook, however,
|
|
8762
|
+
* lets you dynamically add a new Queries object to the context, from within a
|
|
8763
|
+
* component. This is useful for applications where the set of Queries objects
|
|
8764
|
+
* is not known at the time of the first render of the root Provider.
|
|
8765
|
+
*
|
|
8766
|
+
* A Queries object added to the Provider context in this way will be available
|
|
8767
|
+
* to other components within the context (using the useQueries hook and so on).
|
|
8768
|
+
* If you use the same Id as an existing Queries object registration, the new
|
|
8769
|
+
* one will take priority over one provided by the `queriesById` prop.
|
|
8770
|
+
*
|
|
8771
|
+
* Note that other components that consume a Queries object registered like this
|
|
8772
|
+
* should defend against it being undefined at first. On the first render, the
|
|
8773
|
+
* other component will likely not yet have completed the registration. In the
|
|
8774
|
+
* example below, we use the null-safe `useQueries('petQueries')?` to do this.
|
|
8775
|
+
* @param queriesId The Id of the Queries object to be registered with the
|
|
8776
|
+
* Provider.
|
|
8777
|
+
* @param queries The Queries object to be registered.
|
|
8778
|
+
* @example
|
|
8779
|
+
* This example creates a Provider context. A child component registers a
|
|
8780
|
+
* Queries object into it which is then consumable by a peer child component.
|
|
8781
|
+
*
|
|
8782
|
+
* ```jsx
|
|
8783
|
+
* import React from 'react';
|
|
8784
|
+
* import {createRoot} from 'react-dom/client';
|
|
8785
|
+
* import {createQueries, createStore} from 'tinybase';
|
|
8786
|
+
* import {
|
|
8787
|
+
* Provider,
|
|
8788
|
+
* useCreateQueries,
|
|
8789
|
+
* useCreateStore,
|
|
8790
|
+
* useProvideQueries,
|
|
8791
|
+
* useQueries,
|
|
8792
|
+
* } from 'tinybase/ui-react';
|
|
8793
|
+
*
|
|
8794
|
+
* const App = () => (
|
|
8795
|
+
* <Provider>
|
|
8796
|
+
* <RegisterQueries />
|
|
8797
|
+
* <ConsumeQueries />
|
|
8798
|
+
* </Provider>
|
|
8799
|
+
* );
|
|
8800
|
+
* const RegisterQueries = () => {
|
|
8801
|
+
* const store = useCreateStore(() =>
|
|
8802
|
+
* createStore().setRow('pets', 'fido', {color: 'brown', legs: 4}),
|
|
8803
|
+
* );
|
|
8804
|
+
* const queries = useCreateQueries(store, (store) =>
|
|
8805
|
+
* createQueries(store).setQueryDefinition(
|
|
8806
|
+
* 'brownLegs',
|
|
8807
|
+
* 'pets',
|
|
8808
|
+
* ({select, where}) => {
|
|
8809
|
+
* select('legs');
|
|
8810
|
+
* where('color', 'brown');
|
|
8811
|
+
* },
|
|
8812
|
+
* ),
|
|
8813
|
+
* );
|
|
8814
|
+
* useProvideQueries('petQueries', queries);
|
|
8815
|
+
* return null;
|
|
8816
|
+
* };
|
|
8817
|
+
* const ConsumeQueries = () => (
|
|
8818
|
+
* <span>
|
|
8819
|
+
* {JSON.stringify(useQueries('petQueries')?.getResultTable('brownLegs'))}
|
|
8820
|
+
* </span>
|
|
8821
|
+
* );
|
|
8822
|
+
*
|
|
8823
|
+
* const app = document.createElement('div');
|
|
8824
|
+
* const root = createRoot(app);
|
|
8825
|
+
* root.render(<App />); // !act
|
|
8826
|
+
* console.log(app.innerHTML);
|
|
8827
|
+
* // -> '<span>{\"fido\":{\"legs\":4}}</span>'
|
|
8828
|
+
* ```
|
|
8829
|
+
* @category Queries hooks
|
|
8830
|
+
* @since v5.3.0
|
|
8831
|
+
*/
|
|
8601
8832
|
export function useProvideQueries(queriesId: Id, queries: Queries): void;
|
|
8602
8833
|
|
|
8603
8834
|
/**
|
|
@@ -10784,7 +11015,77 @@ export function useCheckpointsOrCheckpointsById(
|
|
|
10784
11015
|
checkpointsOrCheckpointsId?: CheckpointsOrCheckpointsId,
|
|
10785
11016
|
): Checkpoints | undefined;
|
|
10786
11017
|
|
|
10787
|
-
|
|
11018
|
+
/**
|
|
11019
|
+
* The useProvideCheckpoints hook is used to add a Checkpoints object by Id to a
|
|
11020
|
+
* Provider component, but imperatively from a component within it.
|
|
11021
|
+
*
|
|
11022
|
+
* Normally you will register a Checkpoints object by Id in a context by using
|
|
11023
|
+
* the `checkpointsById` prop of the top-level Provider component. This hook,
|
|
11024
|
+
* however, lets you dynamically add a new Checkpoints object to the context,
|
|
11025
|
+
* from within a component. This is useful for applications where the set of
|
|
11026
|
+
* Checkpoints objects is not known at the time of the first render of the root
|
|
11027
|
+
* Provider.
|
|
11028
|
+
*
|
|
11029
|
+
* A Checkpoints object added to the Provider context in this way will be
|
|
11030
|
+
* available to other components within the context (using the useCheckpoints
|
|
11031
|
+
* hook and so on). If you use the same Id as an existing Checkpoints object
|
|
11032
|
+
* registration, the new one will take priority over one provided by the
|
|
11033
|
+
* `checkpointsById` prop.
|
|
11034
|
+
*
|
|
11035
|
+
* Note that other components that consume a Checkpoints object registered like
|
|
11036
|
+
* this should defend against it being undefined at first. On the first render,
|
|
11037
|
+
* the other component will likely not yet have completed the registration. In
|
|
11038
|
+
* the example below, we use the null-safe `useCheckpoints('petCheckpoints')?`
|
|
11039
|
+
* to do this.
|
|
11040
|
+
* @param checkpointsId The Id of the Checkpoints object to be registered with
|
|
11041
|
+
* the Provider.
|
|
11042
|
+
* @param checkpoints The Checkpoints object to be registered.
|
|
11043
|
+
* @example
|
|
11044
|
+
* This example creates a Provider context. A child component registers a
|
|
11045
|
+
* Checkpoints object into it which is then consumable by a peer child
|
|
11046
|
+
* component.
|
|
11047
|
+
*
|
|
11048
|
+
* ```jsx
|
|
11049
|
+
* import React from 'react';
|
|
11050
|
+
* import {createRoot} from 'react-dom/client';
|
|
11051
|
+
* import {createCheckpoints, createStore} from 'tinybase';
|
|
11052
|
+
* import {
|
|
11053
|
+
* Provider,
|
|
11054
|
+
* useCheckpoints,
|
|
11055
|
+
* useCreateCheckpoints,
|
|
11056
|
+
* useCreateStore,
|
|
11057
|
+
* useProvideCheckpoints,
|
|
11058
|
+
* } from 'tinybase/ui-react';
|
|
11059
|
+
*
|
|
11060
|
+
* const App = () => (
|
|
11061
|
+
* <Provider>
|
|
11062
|
+
* <RegisterCheckpoints />
|
|
11063
|
+
* <ConsumeCheckpoints />
|
|
11064
|
+
* </Provider>
|
|
11065
|
+
* );
|
|
11066
|
+
* const RegisterCheckpoints = () => {
|
|
11067
|
+
* const store = useCreateStore(() =>
|
|
11068
|
+
* createStore().setCell('pets', 'fido', 'color', 'brown'),
|
|
11069
|
+
* );
|
|
11070
|
+
* const checkpoints = useCreateCheckpoints(store, createCheckpoints);
|
|
11071
|
+
* useProvideCheckpoints('petCheckpoints', checkpoints);
|
|
11072
|
+
* return null;
|
|
11073
|
+
* };
|
|
11074
|
+
* const ConsumeCheckpoints = () => (
|
|
11075
|
+
* <span>
|
|
11076
|
+
* {JSON.stringify(useCheckpoints('petCheckpoints')?.getCheckpointIds())}
|
|
11077
|
+
* </span>
|
|
11078
|
+
* );
|
|
11079
|
+
*
|
|
11080
|
+
* const app = document.createElement('div');
|
|
11081
|
+
* const root = createRoot(app);
|
|
11082
|
+
* root.render(<App />); // !act
|
|
11083
|
+
* console.log(app.innerHTML);
|
|
11084
|
+
* // -> '<span>[[],"0",[]]</span>'
|
|
11085
|
+
* ```
|
|
11086
|
+
* @category Checkpoints hooks
|
|
11087
|
+
* @since v5.3.0
|
|
11088
|
+
*/
|
|
10788
11089
|
export function useProvideCheckpoints(
|
|
10789
11090
|
checkpointsId: Id,
|
|
10790
11091
|
checkpoints: Checkpoints,
|
|
@@ -11747,7 +12048,7 @@ export function useCreatePersister<
|
|
|
11747
12048
|
store: PersistedStore<Persist>,
|
|
11748
12049
|
) => PersisterOrUndefined | Promise<PersisterOrUndefined>,
|
|
11749
12050
|
createDeps?: React.DependencyList,
|
|
11750
|
-
then?: (persister: Persister<Persist>) => Promise<
|
|
12051
|
+
then?: (persister: Persister<Persist>) => Promise<any>,
|
|
11751
12052
|
thenDeps?: React.DependencyList,
|
|
11752
12053
|
destroy?: (persister: Persister<Persist>) => void,
|
|
11753
12054
|
destroyDeps?: React.DependencyList,
|
|
@@ -11932,7 +12233,80 @@ export function usePersisterOrPersisterById(
|
|
|
11932
12233
|
persisterOrPersisterId?: PersisterOrPersisterId,
|
|
11933
12234
|
): AnyPersister | undefined;
|
|
11934
12235
|
|
|
11935
|
-
|
|
12236
|
+
/**
|
|
12237
|
+
* The useProvidePersister hook is used to add a Persister object by Id to a
|
|
12238
|
+
* Provider component, but imperatively from a component within it.
|
|
12239
|
+
*
|
|
12240
|
+
* Normally you will register a Persister object by Id in a context by using the
|
|
12241
|
+
* `persistersById` prop of the top-level Provider component. This hook,
|
|
12242
|
+
* however, lets you dynamically add a new Persister object to the context, from
|
|
12243
|
+
* within a component. This is useful for applications where the set of
|
|
12244
|
+
* Persister objects is not known at the time of the first render of the root
|
|
12245
|
+
* Provider.
|
|
12246
|
+
*
|
|
12247
|
+
* A Persister object added to the Provider context in this way will be
|
|
12248
|
+
* available to other components within the context (using the usePersister hook
|
|
12249
|
+
* and so on). If you use the same Id as an existing Persister object
|
|
12250
|
+
* registration, the new one will take priority over one provided by the
|
|
12251
|
+
* `persistersById` prop.
|
|
12252
|
+
*
|
|
12253
|
+
* Note that other components that consume a Persister object registered like
|
|
12254
|
+
* this should defend against it being undefined at first. On the first render,
|
|
12255
|
+
* the other component will likely not yet have completed the registration. In
|
|
12256
|
+
* the example below, we use the null-safe `usePersister('petPersister')?` to do
|
|
12257
|
+
* this.
|
|
12258
|
+
* @param persisterId The Id of the Persister object to be registered with the
|
|
12259
|
+
* Provider.
|
|
12260
|
+
* @param persister The Persister object to be registered.
|
|
12261
|
+
* @example
|
|
12262
|
+
* This example creates a Provider context. A child component registers a
|
|
12263
|
+
* Persister object into it which is then consumable by a peer child
|
|
12264
|
+
* component.
|
|
12265
|
+
*
|
|
12266
|
+
* ```jsx
|
|
12267
|
+
* import React from 'react';
|
|
12268
|
+
* import {createRoot} from 'react-dom/client';
|
|
12269
|
+
* import {createStore} from 'tinybase';
|
|
12270
|
+
* import {createSessionPersister} from 'tinybase/persisters/persister-browser';
|
|
12271
|
+
* import {
|
|
12272
|
+
* Provider,
|
|
12273
|
+
* useCreatePersister,
|
|
12274
|
+
* useCreateStore,
|
|
12275
|
+
* usePersister,
|
|
12276
|
+
* useProvidePersister,
|
|
12277
|
+
* } from 'tinybase/ui-react';
|
|
12278
|
+
*
|
|
12279
|
+
* const App = () => (
|
|
12280
|
+
* <Provider>
|
|
12281
|
+
* <RegisterPersister />
|
|
12282
|
+
* <ConsumePersister />
|
|
12283
|
+
* </Provider>
|
|
12284
|
+
* );
|
|
12285
|
+
* const RegisterPersister = () => {
|
|
12286
|
+
* const store = useCreateStore(() =>
|
|
12287
|
+
* createStore().setCell('pets', 'fido', 'color', 'brown'),
|
|
12288
|
+
* );
|
|
12289
|
+
* const persister = useCreatePersister(store, (store) =>
|
|
12290
|
+
* createSessionPersister(store, 'pets'),
|
|
12291
|
+
* );
|
|
12292
|
+
* useProvidePersister('petPersister', persister);
|
|
12293
|
+
* return null;
|
|
12294
|
+
* };
|
|
12295
|
+
* const ConsumePersister = () => (
|
|
12296
|
+
* <span>{usePersister('petPersister')?.getStatus()}</span>
|
|
12297
|
+
* );
|
|
12298
|
+
*
|
|
12299
|
+
* const app = document.createElement('div');
|
|
12300
|
+
* const root = createRoot(app);
|
|
12301
|
+
* root.render(<App />); // !act
|
|
12302
|
+
*
|
|
12303
|
+
* // ... // !act
|
|
12304
|
+
* console.log(app.innerHTML);
|
|
12305
|
+
* // -> '<span>0</span>'
|
|
12306
|
+
* ```
|
|
12307
|
+
* @category Persister hooks
|
|
12308
|
+
* @since v5.3.0
|
|
12309
|
+
*/
|
|
11936
12310
|
export function useProvidePersister(
|
|
11937
12311
|
persisterId: Id,
|
|
11938
12312
|
persister: AnyPersister,
|
|
@@ -12435,7 +12809,81 @@ export function useSynchronizerOrSynchronizerById(
|
|
|
12435
12809
|
synchronizerOrSynchronizerId?: SynchronizerOrSynchronizerId,
|
|
12436
12810
|
): Synchronizer | undefined;
|
|
12437
12811
|
|
|
12438
|
-
|
|
12812
|
+
/**
|
|
12813
|
+
* The useProvideSynchronizer hook is used to add a Synchronizer object by Id to
|
|
12814
|
+
* a Provider component, but imperatively from a component within it.
|
|
12815
|
+
*
|
|
12816
|
+
* Normally you will register a Synchronizer object by Id in a context by using
|
|
12817
|
+
* the `synchronizersById` prop of the top-level Provider component. This hook,
|
|
12818
|
+
* however, lets you dynamically add a new Synchronizer object to the context,
|
|
12819
|
+
* from within a component. This is useful for applications where the set of
|
|
12820
|
+
* Synchronizer objects is not known at the time of the first render of the root
|
|
12821
|
+
* Provider.
|
|
12822
|
+
*
|
|
12823
|
+
* A Synchronizer object added to the Provider context in this way will be
|
|
12824
|
+
* available to other components within the context (using the useSynchronizer
|
|
12825
|
+
* hook and so on). If you use the same Id as an existing Synchronizer object
|
|
12826
|
+
* registration, the new one will take priority over one provided by the
|
|
12827
|
+
* `synchronizersById` prop.
|
|
12828
|
+
*
|
|
12829
|
+
* Note that other components that consume a Synchronizer object registered like
|
|
12830
|
+
* this should defend against it being undefined at first. On the first render,
|
|
12831
|
+
* the other component will likely not yet have completed the registration. In
|
|
12832
|
+
* the example below, we use the null-safe `useSynchronizer('petSynchronizer')?`
|
|
12833
|
+
* to do this.
|
|
12834
|
+
* @param synchronizerId The Id of the Synchronizer object to be registered with
|
|
12835
|
+
* the Provider.
|
|
12836
|
+
* @param synchronizer The Synchronizer object to be registered.
|
|
12837
|
+
* @example
|
|
12838
|
+
* This example creates a Provider context. A child component registers a
|
|
12839
|
+
* Synchronizer object into it which is then consumable by a peer child
|
|
12840
|
+
* component.
|
|
12841
|
+
*
|
|
12842
|
+
* ```jsx
|
|
12843
|
+
* import React from 'react';
|
|
12844
|
+
* import {createRoot} from 'react-dom/client';
|
|
12845
|
+
* import {createMergeableStore} from 'tinybase';
|
|
12846
|
+
* import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
|
|
12847
|
+
* import {
|
|
12848
|
+
* Provider,
|
|
12849
|
+
* useCreateStore,
|
|
12850
|
+
* useCreateSynchronizer,
|
|
12851
|
+
* useProvideSynchronizer,
|
|
12852
|
+
* useSynchronizer,
|
|
12853
|
+
* } from 'tinybase/ui-react';
|
|
12854
|
+
*
|
|
12855
|
+
* const App = () => (
|
|
12856
|
+
* <Provider>
|
|
12857
|
+
* <RegisterSynchronizer />
|
|
12858
|
+
* <ConsumeSynchronizer />
|
|
12859
|
+
* </Provider>
|
|
12860
|
+
* );
|
|
12861
|
+
* const RegisterSynchronizer = () => {
|
|
12862
|
+
* const store = useCreateStore(() =>
|
|
12863
|
+
* createMergeableStore().setCell('pets', 'fido', 'color', 'brown'),
|
|
12864
|
+
* );
|
|
12865
|
+
* const synchronizer = useCreateSynchronizer(
|
|
12866
|
+
* store,
|
|
12867
|
+
* createLocalSynchronizer,
|
|
12868
|
+
* );
|
|
12869
|
+
* useProvideSynchronizer('petSynchronizer', synchronizer);
|
|
12870
|
+
* return null;
|
|
12871
|
+
* };
|
|
12872
|
+
* const ConsumeSynchronizer = () => (
|
|
12873
|
+
* <span>{useSynchronizer('petSynchronizer')?.getStatus()}</span>
|
|
12874
|
+
* );
|
|
12875
|
+
*
|
|
12876
|
+
* const app = document.createElement('div');
|
|
12877
|
+
* const root = createRoot(app);
|
|
12878
|
+
* root.render(<App />); // !act
|
|
12879
|
+
*
|
|
12880
|
+
* // ... // !act
|
|
12881
|
+
* console.log(app.innerHTML);
|
|
12882
|
+
* // -> '<span>0</span>'
|
|
12883
|
+
* ```
|
|
12884
|
+
* @category Synchronizer hooks
|
|
12885
|
+
* @since v5.3.0
|
|
12886
|
+
*/
|
|
12439
12887
|
export function useProvideSynchronizer(
|
|
12440
12888
|
synchronizerId: Id,
|
|
12441
12889
|
synchronizer: Synchronizer,
|
|
@@ -12964,7 +12964,7 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
|
|
|
12964
12964
|
* store: PersistedStore<Persist>,
|
|
12965
12965
|
* ) => PersisterOrUndefined | Promise<PersisterOrUndefined>,
|
|
12966
12966
|
* createDeps?: React.DependencyList,
|
|
12967
|
-
* then?: (persister: Persister<Persist>) => Promise<
|
|
12967
|
+
* then?: (persister: Persister<Persist>) => Promise<any>,
|
|
12968
12968
|
* thenDeps?: React.DependencyList,
|
|
12969
12969
|
* destroy?: (persister: Persister<Persist>) => void,
|
|
12970
12970
|
* destroyDeps?: React.DependencyList,
|
|
@@ -13161,9 +13161,11 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
|
|
|
13161
13161
|
PersisterOrUndefined extends Persister<Schemas, Persist> | undefined,
|
|
13162
13162
|
>(
|
|
13163
13163
|
store: PersistedStore<Schemas, Persist> | undefined,
|
|
13164
|
-
create: (
|
|
13164
|
+
create: (
|
|
13165
|
+
store: PersistedStore<Schemas, Persist>,
|
|
13166
|
+
) => PersisterOrUndefined | Promise<PersisterOrUndefined>,
|
|
13165
13167
|
createDeps?: React.DependencyList,
|
|
13166
|
-
then?: (persister: Persister<Schemas, Persist>) => Promise<
|
|
13168
|
+
then?: (persister: Persister<Schemas, Persist>) => Promise<any>,
|
|
13167
13169
|
thenDeps?: React.DependencyList,
|
|
13168
13170
|
destroy?: (persister: Persister<Schemas, Persist>) => void,
|
|
13169
13171
|
destroyDeps?: React.DependencyList,
|