@legendapp/state 3.0.0-alpha.39 → 3.0.0-alpha.40
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/package.json +1 -1
- package/persist-plugins/indexeddb.js +24 -10
- package/persist-plugins/indexeddb.mjs +24 -10
- package/sync-plugins/crud.js +8 -7
- package/sync-plugins/crud.mjs +8 -7
- package/sync.d.mts +2 -0
- package/sync.d.ts +2 -0
package/package.json
CHANGED
|
@@ -29,18 +29,27 @@ var ObservablePersistIndexedDB = class {
|
|
|
29
29
|
const { databaseName, version, tableNames } = config;
|
|
30
30
|
const openRequest = indexedDB.open(databaseName, version);
|
|
31
31
|
openRequest.onerror = () => {
|
|
32
|
-
console.error("
|
|
32
|
+
console.error("[legend-state] ObservablePersistIndexedDB load error", openRequest.error);
|
|
33
33
|
};
|
|
34
|
-
openRequest.onupgradeneeded = () => {
|
|
34
|
+
openRequest.onupgradeneeded = (event) => {
|
|
35
35
|
const db = openRequest.result;
|
|
36
|
-
const { tableNames: tableNames2 } = config;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
const { tableNames: tableNames2, deleteTableNames, onUpgradeNeeded } = config;
|
|
37
|
+
if (onUpgradeNeeded) {
|
|
38
|
+
onUpgradeNeeded(event);
|
|
39
|
+
} else {
|
|
40
|
+
deleteTableNames == null ? void 0 : deleteTableNames.forEach((table) => {
|
|
41
|
+
if (db.objectStoreNames.contains(table)) {
|
|
42
|
+
db.deleteObjectStore(table);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
tableNames2.forEach((table) => {
|
|
46
|
+
if (!db.objectStoreNames.contains(table)) {
|
|
47
|
+
db.createObjectStore(table, {
|
|
48
|
+
keyPath: "id"
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
44
53
|
};
|
|
45
54
|
return new Promise((resolve) => {
|
|
46
55
|
openRequest.onsuccess = async () => {
|
|
@@ -59,6 +68,11 @@ var ObservablePersistIndexedDB = class {
|
|
|
59
68
|
}
|
|
60
69
|
loadTable(table, config) {
|
|
61
70
|
var _a;
|
|
71
|
+
if (!this.db) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
"[legend-state] ObservablePersistIndexedDB loading without being initialized. This may happen when running outside of a browser."
|
|
74
|
+
);
|
|
75
|
+
}
|
|
62
76
|
if (!this.tableData[table]) {
|
|
63
77
|
const transaction = this.db.transaction(table, "readonly");
|
|
64
78
|
return this.initTable(table, transaction).then(() => this.loadTable(table, config));
|
|
@@ -27,18 +27,27 @@ var ObservablePersistIndexedDB = class {
|
|
|
27
27
|
const { databaseName, version, tableNames } = config;
|
|
28
28
|
const openRequest = indexedDB.open(databaseName, version);
|
|
29
29
|
openRequest.onerror = () => {
|
|
30
|
-
console.error("
|
|
30
|
+
console.error("[legend-state] ObservablePersistIndexedDB load error", openRequest.error);
|
|
31
31
|
};
|
|
32
|
-
openRequest.onupgradeneeded = () => {
|
|
32
|
+
openRequest.onupgradeneeded = (event) => {
|
|
33
33
|
const db = openRequest.result;
|
|
34
|
-
const { tableNames: tableNames2 } = config;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
const { tableNames: tableNames2, deleteTableNames, onUpgradeNeeded } = config;
|
|
35
|
+
if (onUpgradeNeeded) {
|
|
36
|
+
onUpgradeNeeded(event);
|
|
37
|
+
} else {
|
|
38
|
+
deleteTableNames == null ? void 0 : deleteTableNames.forEach((table) => {
|
|
39
|
+
if (db.objectStoreNames.contains(table)) {
|
|
40
|
+
db.deleteObjectStore(table);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
tableNames2.forEach((table) => {
|
|
44
|
+
if (!db.objectStoreNames.contains(table)) {
|
|
45
|
+
db.createObjectStore(table, {
|
|
46
|
+
keyPath: "id"
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
42
51
|
};
|
|
43
52
|
return new Promise((resolve) => {
|
|
44
53
|
openRequest.onsuccess = async () => {
|
|
@@ -57,6 +66,11 @@ var ObservablePersistIndexedDB = class {
|
|
|
57
66
|
}
|
|
58
67
|
loadTable(table, config) {
|
|
59
68
|
var _a;
|
|
69
|
+
if (!this.db) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"[legend-state] ObservablePersistIndexedDB loading without being initialized. This may happen when running outside of a browser."
|
|
72
|
+
);
|
|
73
|
+
}
|
|
60
74
|
if (!this.tableData[table]) {
|
|
61
75
|
const transaction = this.db.transaction(table, "readonly");
|
|
62
76
|
return this.initTable(table, transaction).then(() => this.loadTable(table, config));
|
package/sync-plugins/crud.js
CHANGED
|
@@ -226,25 +226,25 @@ function syncedCrud(props) {
|
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
228
|
itemsChanged == null ? void 0 : itemsChanged.forEach(([item, prev]) => {
|
|
229
|
-
const isCreate = !pendingCreates.has(item
|
|
229
|
+
const isCreate = !pendingCreates.has(item[fieldId]) && (fieldCreatedAt ? !item[fieldCreatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : fieldUpdatedAt ? !item[fieldUpdatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : state.isNullOrUndefined(prev));
|
|
230
230
|
if (isCreate) {
|
|
231
231
|
if (generateId) {
|
|
232
232
|
ensureId(item, fieldId, generateId);
|
|
233
233
|
}
|
|
234
|
-
if (!item
|
|
234
|
+
if (!item[fieldId]) {
|
|
235
235
|
console.error("[legend-state]: added item without an id");
|
|
236
236
|
}
|
|
237
237
|
if (createFn) {
|
|
238
|
-
pendingCreates.add(item
|
|
239
|
-
creates.set(item
|
|
238
|
+
pendingCreates.add(item[fieldId]);
|
|
239
|
+
creates.set(item[fieldId], item);
|
|
240
240
|
} else {
|
|
241
241
|
console.warn("[legend-state] missing create function");
|
|
242
242
|
}
|
|
243
243
|
} else {
|
|
244
244
|
if (updateFn) {
|
|
245
245
|
updates.set(
|
|
246
|
-
item
|
|
247
|
-
updates.has(item
|
|
246
|
+
item[fieldId],
|
|
247
|
+
updates.has(item[fieldId]) ? Object.assign(updates.get(item[fieldId]), item) : item
|
|
248
248
|
);
|
|
249
249
|
} else {
|
|
250
250
|
console.warn("[legend-state] missing update function");
|
|
@@ -254,11 +254,12 @@ function syncedCrud(props) {
|
|
|
254
254
|
}
|
|
255
255
|
});
|
|
256
256
|
const saveResult = async (itemKey, input, data, isCreate) => {
|
|
257
|
+
var _a;
|
|
257
258
|
if (data) {
|
|
258
259
|
const saved = (transform == null ? void 0 : transform.load) ? await transform.load(data, "set") : data;
|
|
259
260
|
const isChild = itemKey !== "undefined" && asType !== "value";
|
|
260
261
|
const currentPeeked = state.getNodeValue(node);
|
|
261
|
-
const currentValue = isChild ?
|
|
262
|
+
const currentValue = isChild ? (_a = asType === "array" && state.isArray(currentPeeked) ? currentPeeked.find((v) => v[fieldId] === itemKey) : void 0) != null ? _a : currentPeeked[itemKey] : currentPeeked;
|
|
262
263
|
const dataOnSaved = {
|
|
263
264
|
saved,
|
|
264
265
|
input,
|
package/sync-plugins/crud.mjs
CHANGED
|
@@ -224,25 +224,25 @@ function syncedCrud(props) {
|
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
itemsChanged == null ? void 0 : itemsChanged.forEach(([item, prev]) => {
|
|
227
|
-
const isCreate = !pendingCreates.has(item
|
|
227
|
+
const isCreate = !pendingCreates.has(item[fieldId]) && (fieldCreatedAt ? !item[fieldCreatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : fieldUpdatedAt ? !item[fieldUpdatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : isNullOrUndefined(prev));
|
|
228
228
|
if (isCreate) {
|
|
229
229
|
if (generateId) {
|
|
230
230
|
ensureId(item, fieldId, generateId);
|
|
231
231
|
}
|
|
232
|
-
if (!item
|
|
232
|
+
if (!item[fieldId]) {
|
|
233
233
|
console.error("[legend-state]: added item without an id");
|
|
234
234
|
}
|
|
235
235
|
if (createFn) {
|
|
236
|
-
pendingCreates.add(item
|
|
237
|
-
creates.set(item
|
|
236
|
+
pendingCreates.add(item[fieldId]);
|
|
237
|
+
creates.set(item[fieldId], item);
|
|
238
238
|
} else {
|
|
239
239
|
console.warn("[legend-state] missing create function");
|
|
240
240
|
}
|
|
241
241
|
} else {
|
|
242
242
|
if (updateFn) {
|
|
243
243
|
updates.set(
|
|
244
|
-
item
|
|
245
|
-
updates.has(item
|
|
244
|
+
item[fieldId],
|
|
245
|
+
updates.has(item[fieldId]) ? Object.assign(updates.get(item[fieldId]), item) : item
|
|
246
246
|
);
|
|
247
247
|
} else {
|
|
248
248
|
console.warn("[legend-state] missing update function");
|
|
@@ -252,11 +252,12 @@ function syncedCrud(props) {
|
|
|
252
252
|
}
|
|
253
253
|
});
|
|
254
254
|
const saveResult = async (itemKey, input, data, isCreate) => {
|
|
255
|
+
var _a;
|
|
255
256
|
if (data) {
|
|
256
257
|
const saved = (transform == null ? void 0 : transform.load) ? await transform.load(data, "set") : data;
|
|
257
258
|
const isChild = itemKey !== "undefined" && asType !== "value";
|
|
258
259
|
const currentPeeked = getNodeValue(node);
|
|
259
|
-
const currentValue = isChild ?
|
|
260
|
+
const currentValue = isChild ? (_a = asType === "array" && isArray(currentPeeked) ? currentPeeked.find((v) => v[fieldId] === itemKey) : void 0) != null ? _a : currentPeeked[itemKey] : currentPeeked;
|
|
260
261
|
const dataOnSaved = {
|
|
261
262
|
saved,
|
|
262
263
|
input,
|
package/sync.d.mts
CHANGED
|
@@ -83,6 +83,8 @@ interface ObservablePersistIndexedDBPluginOptions {
|
|
|
83
83
|
databaseName: string;
|
|
84
84
|
version: number;
|
|
85
85
|
tableNames: string[];
|
|
86
|
+
deleteTableNames?: string[];
|
|
87
|
+
onUpgradeNeeded?: (event: IDBVersionChangeEvent) => void;
|
|
86
88
|
}
|
|
87
89
|
interface ObservablePersistAsyncStoragePluginOptions {
|
|
88
90
|
AsyncStorage: AsyncStorageStatic;
|
package/sync.d.ts
CHANGED
|
@@ -83,6 +83,8 @@ interface ObservablePersistIndexedDBPluginOptions {
|
|
|
83
83
|
databaseName: string;
|
|
84
84
|
version: number;
|
|
85
85
|
tableNames: string[];
|
|
86
|
+
deleteTableNames?: string[];
|
|
87
|
+
onUpgradeNeeded?: (event: IDBVersionChangeEvent) => void;
|
|
86
88
|
}
|
|
87
89
|
interface ObservablePersistAsyncStoragePluginOptions {
|
|
88
90
|
AsyncStorage: AsyncStorageStatic;
|