Skip to content

Commit e7592e5

Browse files
committed
[store] setContent can take a function
1 parent 72d01de commit e7592e5

File tree

8 files changed

+41
-11
lines changed

8 files changed

+41
-11
lines changed

src/@types/mergeable-store/docs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,11 @@
757757
* The method is generally intended to be used internally within TinyBase
758758
* itself and the return type is assumed to be opaque to applications that use
759759
* it.
760+
*
761+
* Since v5.4.2, this method can also take a function that returns the
762+
* content.
760763
* @param content An array containing the tabular and keyed-value data to be
761-
* set.
764+
* set, or a function that returns the array.
762765
* @returns A reference to the MergeableStore.
763766
* @example
764767
* This example creates a new MergeableStore with default data, and

src/@types/mergeable-store/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export interface MergeableStore extends Store {
124124
setMergeableContent(mergeableContent: MergeableContent): MergeableStore;
125125

126126
/// MergeableStore.setDefaultContent
127-
setDefaultContent(content: Content): MergeableStore;
127+
setDefaultContent(content: Content | (() => Content)): MergeableStore;
128128

129129
/// MergeableStore.getTransactionMergeableChanges
130130
getTransactionMergeableChanges(withHashes?: boolean): MergeableChanges<true>;

src/@types/mergeable-store/with-schemas/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ export interface MergeableStore<Schemas extends OptionalSchemas>
200200
): MergeableStore<Schemas>;
201201

202202
/// MergeableStore.setDefaultContent
203-
setDefaultContent(content: Content<Schemas>): MergeableStore<Schemas>;
203+
setDefaultContent(
204+
content: Content<Schemas> | (() => Content<Schemas>),
205+
): MergeableStore<Schemas>;
204206

205207
/// MergeableStore.getTransactionMergeableChanges
206208
getTransactionMergeableChanges(

src/@types/store/docs.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2603,8 +2603,11 @@
26032603
*
26042604
* The method returns a reference to the Store so that subsequent operations
26052605
* can be chained in a fluent style.
2606+
*
2607+
* Since v5.4.2, this method can also take a function that returns the
2608+
* content.
26062609
* @param content An array containing the tabular and keyed-value data of the
2607-
* Store to be set.
2610+
* Store to be set, or a function that returns the array.
26082611
* @returns A reference to the Store.
26092612
* @example
26102613
* This example sets the data of a Store.
@@ -2622,6 +2625,21 @@
26222625
* // -> {open: true, employees: 3}
26232626
* ```
26242627
* @example
2628+
* This example sets the data of a Store with the results of a function.
2629+
*
2630+
* ```js
2631+
* import {createStore} from 'tinybase';
2632+
*
2633+
* const store = createStore().setContent(() => [
2634+
* {pets: {fido: {species: 'dog'}}},
2635+
* {open: true, employees: 3},
2636+
* ]);
2637+
* console.log(store.getTables());
2638+
* // -> {pets: {fido: {species: 'dog'}}}
2639+
* console.log(store.getValues());
2640+
* // -> {open: true, employees: 3}
2641+
* ```
2642+
* @example
26252643
* This example attempts to set the data of an existing Store with partly
26262644
* invalid, and then completely invalid objects.
26272645
*

src/@types/store/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ export interface Store {
509509
hasValuesSchema(): boolean;
510510

511511
/// Store.setContent
512-
setContent(content: Content): this;
512+
setContent(content: Content | (() => Content)): this;
513513

514514
/// Store.setTables
515515
setTables(tables: Tables): this;

src/@types/store/with-schemas/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,9 @@ export interface Store<in out Schemas extends OptionalSchemas> {
10281028
hasValuesSchema(): boolean;
10291029

10301030
/// Store.setContent
1031-
setContent(content: Content<Schemas, true>): this;
1031+
setContent(
1032+
content: Content<Schemas, true> | (() => Content<Schemas, true>),
1033+
): this;
10321034

10331035
/// Store.setTables
10341036
setTables(tables: Tables<Schemas[0], true>): this;

src/mergeable-store/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,9 @@ export const createMergeableStore = ((uniqueId?: Id): MergeableStore => {
526526
: 0,
527527
);
528528

529-
const setDefaultContent = (content: Content): MergeableStore => {
529+
const setDefaultContent = (
530+
content: Content | (() => Content),
531+
): MergeableStore => {
530532
store.transaction(() => {
531533
defaultingContent = 1;
532534
store.setContent(content);

src/store/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,13 @@ export const createStore: typeof createStoreDecl = (): Store => {
10811081
const getSchemaJson = (): Json =>
10821082
jsonStringWithMap([tablesSchemaMap, valuesSchemaMap]);
10831083

1084-
const setContent = (content: Content): Store =>
1085-
fluentTransaction(() =>
1086-
validateContent(content) ? setValidContent(content) : 0,
1087-
);
1084+
const setContent = (content: Content | (() => Content)): Store =>
1085+
fluentTransaction(() => {
1086+
const content2 = isFunction(content) ? content() : content;
1087+
if (validateContent(content2)) {
1088+
setValidContent(content2);
1089+
}
1090+
});
10881091

10891092
const setTables = (tables: Tables): Store =>
10901093
fluentTransaction(() =>

0 commit comments

Comments
 (0)