@instantdb/core 0.22.97 → 0.22.98-experimental.drewh-ts-target.20762041587.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/dist/commonjs/Connection.js +50 -51
- package/dist/commonjs/Connection.js.map +1 -1
- package/dist/commonjs/InMemoryStorage.js +13 -32
- package/dist/commonjs/InMemoryStorage.js.map +1 -1
- package/dist/commonjs/IndexedDBStorage.js +193 -217
- package/dist/commonjs/IndexedDBStorage.js.map +1 -1
- package/dist/commonjs/InstantError.js +1 -0
- package/dist/commonjs/InstantError.js.map +1 -1
- package/dist/commonjs/Reactor.js +566 -610
- package/dist/commonjs/Reactor.js.map +1 -1
- package/dist/commonjs/StorageAPI.js +51 -70
- package/dist/commonjs/StorageAPI.js.map +1 -1
- package/dist/commonjs/SyncTable.js +68 -81
- package/dist/commonjs/SyncTable.js.map +1 -1
- package/dist/commonjs/WindowNetworkListener.js +2 -13
- package/dist/commonjs/WindowNetworkListener.js.map +1 -1
- package/dist/commonjs/__types__/fieldsTypeTest.js +8 -16
- package/dist/commonjs/__types__/fieldsTypeTest.js.map +1 -1
- package/dist/commonjs/__types__/useDatesTypeTest.js +3 -6
- package/dist/commonjs/__types__/useDatesTypeTest.js.map +1 -1
- package/dist/commonjs/authAPI.js +62 -79
- package/dist/commonjs/authAPI.js.map +1 -1
- package/dist/commonjs/createRouteHandler.js +5 -15
- package/dist/commonjs/createRouteHandler.js.map +1 -1
- package/dist/commonjs/datalog.js +1 -1
- package/dist/commonjs/datalog.js.map +1 -1
- package/dist/commonjs/devtool.js +26 -8
- package/dist/commonjs/devtool.js.map +1 -1
- package/dist/commonjs/framework.d.ts.map +1 -1
- package/dist/commonjs/framework.js +142 -152
- package/dist/commonjs/framework.js.map +1 -1
- package/dist/commonjs/index.js +204 -190
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/instaml.js +44 -30
- package/dist/commonjs/instaml.js.map +1 -1
- package/dist/commonjs/instaql.js +25 -33
- package/dist/commonjs/instaql.js.map +1 -1
- package/dist/commonjs/parseSchemaFromJSON.js +6 -7
- package/dist/commonjs/parseSchemaFromJSON.js.map +1 -1
- package/dist/commonjs/presence.js +7 -8
- package/dist/commonjs/presence.js.map +1 -1
- package/dist/commonjs/queryValidation.js +1 -2
- package/dist/commonjs/queryValidation.js.map +1 -1
- package/dist/commonjs/schema.js +8 -6
- package/dist/commonjs/schema.js.map +1 -1
- package/dist/commonjs/schemaTypes.js +22 -3
- package/dist/commonjs/schemaTypes.js.map +1 -1
- package/dist/commonjs/store.js +29 -38
- package/dist/commonjs/store.js.map +1 -1
- package/dist/commonjs/transactionValidation.js +1 -2
- package/dist/commonjs/transactionValidation.js.map +1 -1
- package/dist/commonjs/utils/Deferred.js +3 -0
- package/dist/commonjs/utils/Deferred.js.map +1 -1
- package/dist/commonjs/utils/PersistedObject.js +216 -233
- package/dist/commonjs/utils/PersistedObject.js.map +1 -1
- package/dist/commonjs/utils/fetch.js +9 -19
- package/dist/commonjs/utils/fetch.js.map +1 -1
- package/dist/commonjs/utils/linkIndex.js +2 -4
- package/dist/commonjs/utils/linkIndex.js.map +1 -1
- package/dist/commonjs/utils/object.js +1 -1
- package/dist/commonjs/utils/object.js.map +1 -1
- package/dist/esm/Connection.js +50 -51
- package/dist/esm/Connection.js.map +1 -1
- package/dist/esm/InMemoryStorage.js +13 -32
- package/dist/esm/InMemoryStorage.js.map +1 -1
- package/dist/esm/IndexedDBStorage.js +193 -217
- package/dist/esm/IndexedDBStorage.js.map +1 -1
- package/dist/esm/InstantError.js +1 -0
- package/dist/esm/InstantError.js.map +1 -1
- package/dist/esm/Reactor.js +566 -610
- package/dist/esm/Reactor.js.map +1 -1
- package/dist/esm/StorageAPI.js +51 -70
- package/dist/esm/StorageAPI.js.map +1 -1
- package/dist/esm/SyncTable.js +68 -81
- package/dist/esm/SyncTable.js.map +1 -1
- package/dist/esm/WindowNetworkListener.js +2 -13
- package/dist/esm/WindowNetworkListener.js.map +1 -1
- package/dist/esm/__types__/fieldsTypeTest.js +8 -16
- package/dist/esm/__types__/fieldsTypeTest.js.map +1 -1
- package/dist/esm/__types__/useDatesTypeTest.js +3 -6
- package/dist/esm/__types__/useDatesTypeTest.js.map +1 -1
- package/dist/esm/authAPI.js +62 -79
- package/dist/esm/authAPI.js.map +1 -1
- package/dist/esm/createRouteHandler.js +5 -15
- package/dist/esm/createRouteHandler.js.map +1 -1
- package/dist/esm/datalog.js +1 -1
- package/dist/esm/datalog.js.map +1 -1
- package/dist/esm/devtool.js +26 -8
- package/dist/esm/devtool.js.map +1 -1
- package/dist/esm/framework.d.ts.map +1 -1
- package/dist/esm/framework.js +142 -152
- package/dist/esm/framework.js.map +1 -1
- package/dist/esm/index.js +204 -190
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/instaml.js +44 -30
- package/dist/esm/instaml.js.map +1 -1
- package/dist/esm/instaql.js +25 -33
- package/dist/esm/instaql.js.map +1 -1
- package/dist/esm/parseSchemaFromJSON.js +6 -7
- package/dist/esm/parseSchemaFromJSON.js.map +1 -1
- package/dist/esm/presence.js +7 -8
- package/dist/esm/presence.js.map +1 -1
- package/dist/esm/queryValidation.js +1 -2
- package/dist/esm/queryValidation.js.map +1 -1
- package/dist/esm/schema.js +8 -6
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/schemaTypes.js +22 -3
- package/dist/esm/schemaTypes.js.map +1 -1
- package/dist/esm/store.js +29 -38
- package/dist/esm/store.js.map +1 -1
- package/dist/esm/transactionValidation.js +1 -2
- package/dist/esm/transactionValidation.js.map +1 -1
- package/dist/esm/utils/Deferred.js +3 -0
- package/dist/esm/utils/Deferred.js.map +1 -1
- package/dist/esm/utils/PersistedObject.js +216 -233
- package/dist/esm/utils/PersistedObject.js.map +1 -1
- package/dist/esm/utils/fetch.js +9 -19
- package/dist/esm/utils/fetch.js.map +1 -1
- package/dist/esm/utils/linkIndex.js +2 -4
- package/dist/esm/utils/linkIndex.js.map +1 -1
- package/dist/esm/utils/object.js +1 -1
- package/dist/esm/utils/object.js.map +1 -1
- package/dist/standalone/index.js +2610 -2367
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/framework.ts +0 -1
package/dist/esm/StorageAPI.js
CHANGED
|
@@ -1,84 +1,65 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { jsonFetch } from './utils/fetch.js';
|
|
11
|
-
export function uploadFile(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
body: file,
|
|
26
|
-
});
|
|
27
|
-
return data;
|
|
2
|
+
export async function uploadFile({ apiURI, appId, path, file, refreshToken, contentType, contentDisposition, }) {
|
|
3
|
+
const headers = {
|
|
4
|
+
app_id: appId,
|
|
5
|
+
path,
|
|
6
|
+
authorization: `Bearer ${refreshToken}`,
|
|
7
|
+
'content-type': contentType || file.type,
|
|
8
|
+
};
|
|
9
|
+
if (contentDisposition) {
|
|
10
|
+
headers['content-disposition'] = contentDisposition;
|
|
11
|
+
}
|
|
12
|
+
const data = await jsonFetch(`${apiURI}/storage/upload`, {
|
|
13
|
+
method: 'PUT',
|
|
14
|
+
headers,
|
|
15
|
+
body: file,
|
|
28
16
|
});
|
|
17
|
+
return data;
|
|
29
18
|
}
|
|
30
|
-
export function deleteFile(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
},
|
|
38
|
-
});
|
|
39
|
-
return data;
|
|
19
|
+
export async function deleteFile({ apiURI, appId, path, refreshToken, }) {
|
|
20
|
+
const { data } = await jsonFetch(`${apiURI}/storage/files?app_id=${appId}&filename=${encodeURIComponent(path)}`, {
|
|
21
|
+
method: 'DELETE',
|
|
22
|
+
headers: {
|
|
23
|
+
'content-type': 'application/json',
|
|
24
|
+
authorization: `Bearer ${refreshToken}`,
|
|
25
|
+
},
|
|
40
26
|
});
|
|
27
|
+
return data;
|
|
41
28
|
}
|
|
42
29
|
// Deprecated Storage API (Jan 2025)
|
|
43
30
|
// ---------------------------------
|
|
44
|
-
export function getSignedUploadUrl(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}),
|
|
56
|
-
});
|
|
57
|
-
return data;
|
|
31
|
+
export async function getSignedUploadUrl({ apiURI, appId, fileName, refreshToken, metadata = {}, }) {
|
|
32
|
+
const { data } = await jsonFetch(`${apiURI}/storage/signed-upload-url`, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: {
|
|
35
|
+
'content-type': 'application/json',
|
|
36
|
+
authorization: `Bearer ${refreshToken}`,
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify({
|
|
39
|
+
app_id: appId,
|
|
40
|
+
filename: fileName,
|
|
41
|
+
}),
|
|
58
42
|
});
|
|
43
|
+
return data;
|
|
59
44
|
}
|
|
60
|
-
export function upload(presignedUrl, file) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
return response.ok;
|
|
45
|
+
export async function upload(presignedUrl, file) {
|
|
46
|
+
const response = await fetch(presignedUrl, {
|
|
47
|
+
method: 'PUT',
|
|
48
|
+
body: file,
|
|
49
|
+
headers: {
|
|
50
|
+
'Content-Type': file.type,
|
|
51
|
+
},
|
|
70
52
|
});
|
|
53
|
+
return response.ok;
|
|
71
54
|
}
|
|
72
|
-
export function getDownloadUrl(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
return data;
|
|
55
|
+
export async function getDownloadUrl({ apiURI, appId, path, refreshToken, }) {
|
|
56
|
+
const { data } = await jsonFetch(`${apiURI}/storage/signed-download-url?app_id=${appId}&filename=${encodeURIComponent(path)}`, {
|
|
57
|
+
method: 'GET',
|
|
58
|
+
headers: {
|
|
59
|
+
'content-type': 'application/json',
|
|
60
|
+
authorization: `Bearer ${refreshToken}`,
|
|
61
|
+
},
|
|
82
62
|
});
|
|
63
|
+
return data;
|
|
83
64
|
}
|
|
84
65
|
//# sourceMappingURL=StorageAPI.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StorageAPI.js","sourceRoot":"","sources":["../../src/StorageAPI.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"StorageAPI.js","sourceRoot":"","sources":["../../src/StorageAPI.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAc7C,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,EAC/B,MAAM,EACN,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,YAAY,EACZ,WAAW,EACX,kBAAkB,GASnB;IACC,MAAM,OAAO,GAAG;QACd,MAAM,EAAE,KAAK;QACb,IAAI;QACJ,aAAa,EAAE,UAAU,YAAY,EAAE;QACvC,cAAc,EAAE,WAAW,IAAI,IAAI,CAAC,IAAI;KACzC,CAAC;IACF,IAAI,kBAAkB,EAAE,CAAC;QACvB,OAAO,CAAC,qBAAqB,CAAC,GAAG,kBAAkB,CAAC;IACtD,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,SAAS,CAAC,GAAG,MAAM,iBAAiB,EAAE;QACvD,MAAM,EAAE,KAAK;QACb,OAAO;QACP,IAAI,EAAE,IAAI;KACX,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,EAC/B,MAAM,EACN,KAAK,EACL,IAAI,EACJ,YAAY,GAMb;IACC,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,SAAS,CAC9B,GAAG,MAAM,yBAAyB,KAAK,aAAa,kBAAkB,CAAC,IAAI,CAAC,EAAE,EAC9E;QACE,MAAM,EAAE,QAAQ;QAChB,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,YAAY,EAAE;SACxC;KACF,CACF,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC;AAED,oCAAoC;AACpC,oCAAoC;AAEpC,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,EACvC,MAAM,EACN,KAAK,EACL,QAAQ,EACR,YAAY,EACZ,QAAQ,GAAG,EAAE,GAOd;IACC,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,SAAS,CAAC,GAAG,MAAM,4BAA4B,EAAE;QACtE,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,YAAY,EAAE;SACxC;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;YACnB,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,QAAQ;SACnB,CAAC;KACH,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,YAAY,EAAE,IAAI;IAC7C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,YAAY,EAAE;QACzC,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,IAAI;QACV,OAAO,EAAE;YACP,cAAc,EAAE,IAAI,CAAC,IAAI;SAC1B;KACF,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC,EAAE,CAAC;AACrB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,EACnC,MAAM,EACN,KAAK,EACL,IAAI,EACJ,YAAY,GAMb;IACC,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,SAAS,CAC9B,GAAG,MAAM,uCAAuC,KAAK,aAAa,kBAAkB,CAClF,IAAI,CACL,EAAE,EACH;QACE,MAAM,EAAE,KAAK;QACb,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,YAAY,EAAE;SACxC;KACF,CACF,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { jsonFetch } from './utils/fetch.js';\n\nexport type UploadFileResponse = {\n data: {\n id: string;\n };\n};\n\nexport type DeleteFileResponse = {\n data: {\n id: string | null;\n };\n};\n\nexport async function uploadFile({\n apiURI,\n appId,\n path,\n file,\n refreshToken,\n contentType,\n contentDisposition,\n}: {\n apiURI: string;\n appId: string;\n path: string;\n file: File | Blob;\n refreshToken?: string;\n contentType?: string;\n contentDisposition?: string;\n}): Promise<UploadFileResponse> {\n const headers = {\n app_id: appId,\n path,\n authorization: `Bearer ${refreshToken}`,\n 'content-type': contentType || file.type,\n };\n if (contentDisposition) {\n headers['content-disposition'] = contentDisposition;\n }\n\n const data = await jsonFetch(`${apiURI}/storage/upload`, {\n method: 'PUT',\n headers,\n body: file,\n });\n\n return data;\n}\n\nexport async function deleteFile({\n apiURI,\n appId,\n path,\n refreshToken,\n}: {\n apiURI: string;\n appId: string;\n path: string;\n refreshToken?: string;\n}): Promise<DeleteFileResponse> {\n const { data } = await jsonFetch(\n `${apiURI}/storage/files?app_id=${appId}&filename=${encodeURIComponent(path)}`,\n {\n method: 'DELETE',\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${refreshToken}`,\n },\n },\n );\n\n return data;\n}\n\n// Deprecated Storage API (Jan 2025)\n// ---------------------------------\n\nexport async function getSignedUploadUrl({\n apiURI,\n appId,\n fileName,\n refreshToken,\n metadata = {},\n}: {\n apiURI: string;\n appId: string;\n fileName: string;\n refreshToken?: string;\n metadata?: Record<string, any>;\n}) {\n const { data } = await jsonFetch(`${apiURI}/storage/signed-upload-url`, {\n method: 'POST',\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${refreshToken}`,\n },\n body: JSON.stringify({\n app_id: appId,\n filename: fileName,\n }),\n });\n\n return data;\n}\n\nexport async function upload(presignedUrl, file) {\n const response = await fetch(presignedUrl, {\n method: 'PUT',\n body: file,\n headers: {\n 'Content-Type': file.type,\n },\n });\n\n return response.ok;\n}\n\nexport async function getDownloadUrl({\n apiURI,\n appId,\n path,\n refreshToken,\n}: {\n apiURI: string;\n appId: string;\n path: string;\n refreshToken?: string;\n}) {\n const { data } = await jsonFetch(\n `${apiURI}/storage/signed-download-url?app_id=${appId}&filename=${encodeURIComponent(\n path,\n )}`,\n {\n method: 'GET',\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${refreshToken}`,\n },\n },\n );\n\n return data;\n}\n"]}
|
package/dist/esm/SyncTable.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { PersistedObject } from "./utils/PersistedObject.js";
|
|
11
2
|
import * as s from "./store.js";
|
|
12
3
|
import weakHash from "./utils/weakHash.js";
|
|
@@ -28,23 +19,24 @@ function syncSubFromStorage(sub, useDateObjects) {
|
|
|
28
19
|
return sub;
|
|
29
20
|
}
|
|
30
21
|
function syncSubToStorage(_k, sub) {
|
|
31
|
-
var _a;
|
|
32
22
|
if (sub.values) {
|
|
33
23
|
const entities = [];
|
|
34
|
-
for (const e of
|
|
24
|
+
for (const e of sub.values?.entities) {
|
|
35
25
|
const store = s.toJSON(e.store);
|
|
36
|
-
entities.push(
|
|
26
|
+
entities.push({ ...e, store });
|
|
37
27
|
}
|
|
38
|
-
return
|
|
28
|
+
return {
|
|
29
|
+
...sub,
|
|
30
|
+
values: { attrsStore: sub.values.attrsStore.toJSON(), entities },
|
|
31
|
+
};
|
|
39
32
|
}
|
|
40
33
|
else {
|
|
41
34
|
return sub;
|
|
42
35
|
}
|
|
43
36
|
}
|
|
44
37
|
function onMergeSub(_key, storageSub, inMemorySub) {
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
const memoryTxId = (_b = inMemorySub === null || inMemorySub === void 0 ? void 0 : inMemorySub.state) === null || _b === void 0 ? void 0 : _b.txId;
|
|
38
|
+
const storageTxId = storageSub?.state?.txId;
|
|
39
|
+
const memoryTxId = inMemorySub?.state?.txId;
|
|
48
40
|
if (storageTxId && (!memoryTxId || storageTxId > memoryTxId)) {
|
|
49
41
|
return storageSub;
|
|
50
42
|
}
|
|
@@ -58,8 +50,7 @@ function queryEntity(sub, store, attrsStore) {
|
|
|
58
50
|
return res.data[sub.table][0];
|
|
59
51
|
}
|
|
60
52
|
function getServerCreatedAt(sub, store, attrsStore, entityId) {
|
|
61
|
-
|
|
62
|
-
const aid = (_a = s.getAttrByFwdIdentName(attrsStore, sub.table, 'id')) === null || _a === void 0 ? void 0 : _a.id;
|
|
53
|
+
const aid = s.getAttrByFwdIdentName(attrsStore, sub.table, 'id')?.id;
|
|
63
54
|
if (!aid) {
|
|
64
55
|
return -1;
|
|
65
56
|
}
|
|
@@ -82,18 +73,17 @@ function applyChangesToStore(store, attrsStore, changes) {
|
|
|
82
73
|
}
|
|
83
74
|
}
|
|
84
75
|
function changedFieldsOfChanges(store, attrsStore, changes) {
|
|
85
|
-
var _a, _b, _c, _d;
|
|
86
76
|
// This will be more complicated when we include links, we can either add a
|
|
87
77
|
// changedLinks field or we can have something like 'bookshelves.title`
|
|
88
78
|
const changedFields = {};
|
|
89
79
|
for (const { action, triple } of changes) {
|
|
90
80
|
const [e, a, v] = triple;
|
|
91
|
-
const field =
|
|
81
|
+
const field = attrsStore.getAttr(a)?.['forward-identity']?.[2];
|
|
92
82
|
if (!field)
|
|
93
83
|
continue;
|
|
94
|
-
const fields =
|
|
84
|
+
const fields = changedFields[e] ?? {};
|
|
95
85
|
changedFields[e] = fields;
|
|
96
|
-
const oldNew =
|
|
86
|
+
const oldNew = fields[field] ?? {};
|
|
97
87
|
switch (action) {
|
|
98
88
|
case 'added':
|
|
99
89
|
oldNew.newValue = v;
|
|
@@ -123,13 +113,12 @@ function subData(sub, entities) {
|
|
|
123
113
|
// and returns the type. We have to wait until the attrs
|
|
124
114
|
// are loaded before we can determine the type.
|
|
125
115
|
function orderFieldTypeMutative(sub, getAttrs) {
|
|
126
|
-
var _a;
|
|
127
116
|
if (sub.orderFieldType) {
|
|
128
117
|
return sub.orderFieldType;
|
|
129
118
|
}
|
|
130
119
|
const orderFieldType = sub.orderField === 'serverCreatedAt'
|
|
131
120
|
? 'number'
|
|
132
|
-
:
|
|
121
|
+
: s.getAttrByFwdIdentName(getAttrs(), sub.table, sub.orderField)?.['checked-data-type'];
|
|
133
122
|
sub.orderFieldType = orderFieldType;
|
|
134
123
|
return orderFieldType;
|
|
135
124
|
}
|
|
@@ -163,10 +152,16 @@ export var CallbackEventType;
|
|
|
163
152
|
CallbackEventType["Error"] = "Error";
|
|
164
153
|
})(CallbackEventType || (CallbackEventType = {}));
|
|
165
154
|
export class SyncTable {
|
|
155
|
+
trySend;
|
|
156
|
+
subs;
|
|
157
|
+
// Using any for the SyncCallback because we'd need Reactor to be typed
|
|
158
|
+
callbacks = {};
|
|
159
|
+
config;
|
|
160
|
+
idToHash = {};
|
|
161
|
+
log;
|
|
162
|
+
createStore;
|
|
163
|
+
getAttrs;
|
|
166
164
|
constructor(trySend, storage, config, log, createStore, getAttrs) {
|
|
167
|
-
// Using any for the SyncCallback because we'd need Reactor to be typed
|
|
168
|
-
this.callbacks = {};
|
|
169
|
-
this.idToHash = {};
|
|
170
165
|
this.trySend = trySend;
|
|
171
166
|
this.config = config;
|
|
172
167
|
this.log = log;
|
|
@@ -177,13 +172,13 @@ export class SyncTable {
|
|
|
177
172
|
merge: onMergeSub,
|
|
178
173
|
serialize: syncSubToStorage,
|
|
179
174
|
parse: (_key, x) => syncSubFromStorage(x, this.config.useDateObjects),
|
|
180
|
-
objectSize: (sub) =>
|
|
175
|
+
objectSize: (sub) => sub.values?.entities.length || 0,
|
|
181
176
|
logger: log,
|
|
182
177
|
gc: {
|
|
183
178
|
maxAgeMs: 1000 * 60 * 60 * 24 * 7 * 52, // 1 year
|
|
184
179
|
maxEntries: 1000,
|
|
185
180
|
// Size of each sub is the number of entity
|
|
186
|
-
maxSize:
|
|
181
|
+
maxSize: 1_000_000, // 1 million entities
|
|
187
182
|
},
|
|
188
183
|
});
|
|
189
184
|
}
|
|
@@ -196,7 +191,7 @@ export class SyncTable {
|
|
|
196
191
|
this.callbacks[hash].push(cb);
|
|
197
192
|
this.initSubscription(q, hash, cb);
|
|
198
193
|
return (opts) => {
|
|
199
|
-
this.unsubscribe(hash, cb, opts
|
|
194
|
+
this.unsubscribe(hash, cb, opts?.keepSubscription);
|
|
200
195
|
};
|
|
201
196
|
}
|
|
202
197
|
unsubscribe(hash, cb, keepSubscription) {
|
|
@@ -205,7 +200,7 @@ export class SyncTable {
|
|
|
205
200
|
if (!cbs.length) {
|
|
206
201
|
delete this.callbacks[hash];
|
|
207
202
|
const sub = this.subs.currentValue[hash];
|
|
208
|
-
if (sub
|
|
203
|
+
if (sub?.state) {
|
|
209
204
|
this.clearSubscriptionData(sub.state.subscriptionId, !!keepSubscription);
|
|
210
205
|
}
|
|
211
206
|
if (!keepSubscription) {
|
|
@@ -238,52 +233,47 @@ export class SyncTable {
|
|
|
238
233
|
'keep-subscription': keepSubscription,
|
|
239
234
|
});
|
|
240
235
|
}
|
|
241
|
-
initSubscription(query, hash, cb) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
if (existingSub
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
data: subData(existingSub, (_b = existingSub.values) === null || _b === void 0 ? void 0 : _b.entities),
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
return;
|
|
236
|
+
async initSubscription(query, hash, cb) {
|
|
237
|
+
// Wait for storage to load so that we know if we already have an existing subscription
|
|
238
|
+
await this.subs.waitForKeyToLoad(hash);
|
|
239
|
+
const existingSub = this.subs.currentValue[hash];
|
|
240
|
+
if (existingSub && existingSub.state && existingSub.state.txId) {
|
|
241
|
+
this.sendResync(existingSub, existingSub.state, existingSub.state.txId);
|
|
242
|
+
if (existingSub.values?.entities && cb) {
|
|
243
|
+
cb({
|
|
244
|
+
type: CallbackEventType.LoadFromStorage,
|
|
245
|
+
data: subData(existingSub, existingSub.values?.entities),
|
|
246
|
+
});
|
|
256
247
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const table = Object.keys(query)[0];
|
|
251
|
+
const orderBy = query[table]?.$?.order || { serverCreatedAt: 'asc' };
|
|
252
|
+
const [orderField, orderDirection] = Object.entries(orderBy)[0];
|
|
253
|
+
this.subs.updateInPlace((prev) => {
|
|
254
|
+
prev[hash] = {
|
|
255
|
+
query,
|
|
256
|
+
hash: hash,
|
|
257
|
+
table,
|
|
258
|
+
orderDirection,
|
|
259
|
+
orderField,
|
|
260
|
+
createdAt: Date.now(),
|
|
261
|
+
updatedAt: Date.now(),
|
|
262
|
+
};
|
|
272
263
|
});
|
|
264
|
+
this.sendStart(query);
|
|
273
265
|
}
|
|
274
|
-
flushPending() {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
yield this.initSubscription(sub.query, sub.hash);
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
this.log.error('Missing sub for hash in flushPending', hash);
|
|
284
|
-
}
|
|
266
|
+
async flushPending() {
|
|
267
|
+
for (const hash of Object.keys(this.callbacks)) {
|
|
268
|
+
await this.subs.waitForKeyToLoad(hash);
|
|
269
|
+
const sub = this.subs.currentValue[hash];
|
|
270
|
+
if (sub) {
|
|
271
|
+
await this.initSubscription(sub.query, sub.hash);
|
|
285
272
|
}
|
|
286
|
-
|
|
273
|
+
else {
|
|
274
|
+
this.log.error('Missing sub for hash in flushPending', hash);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
287
277
|
}
|
|
288
278
|
onStartSyncOk(msg) {
|
|
289
279
|
const subscriptionId = msg['subscription-id'];
|
|
@@ -308,7 +298,6 @@ export class SyncTable {
|
|
|
308
298
|
}
|
|
309
299
|
}
|
|
310
300
|
onSyncLoadBatch(msg) {
|
|
311
|
-
var _a;
|
|
312
301
|
const subscriptionId = msg['subscription-id'];
|
|
313
302
|
const joinRows = msg['join-rows'];
|
|
314
303
|
const hash = this.idToHash[subscriptionId];
|
|
@@ -322,7 +311,7 @@ export class SyncTable {
|
|
|
322
311
|
this.log.error('Missing sub for hash', hash, msg);
|
|
323
312
|
return;
|
|
324
313
|
}
|
|
325
|
-
const values =
|
|
314
|
+
const values = sub.values ?? {
|
|
326
315
|
entities: [],
|
|
327
316
|
attrsStore: this.getAttrs(),
|
|
328
317
|
};
|
|
@@ -353,7 +342,6 @@ export class SyncTable {
|
|
|
353
342
|
}
|
|
354
343
|
}
|
|
355
344
|
onSyncInitFinish(msg) {
|
|
356
|
-
var _a;
|
|
357
345
|
const subscriptionId = msg['subscription-id'];
|
|
358
346
|
const hash = this.idToHash[subscriptionId];
|
|
359
347
|
if (!hash) {
|
|
@@ -378,12 +366,11 @@ export class SyncTable {
|
|
|
378
366
|
if (sub) {
|
|
379
367
|
this.notifyCbs(hash, {
|
|
380
368
|
type: CallbackEventType.InitialSyncComplete,
|
|
381
|
-
data: subData(sub,
|
|
369
|
+
data: subData(sub, sub.values?.entities || []),
|
|
382
370
|
});
|
|
383
371
|
}
|
|
384
372
|
}
|
|
385
373
|
onSyncUpdateTriples(msg) {
|
|
386
|
-
var _a, _b, _c;
|
|
387
374
|
const subscriptionId = msg['subscription-id'];
|
|
388
375
|
const hash = this.idToHash[subscriptionId];
|
|
389
376
|
if (!hash) {
|
|
@@ -409,11 +396,11 @@ export class SyncTable {
|
|
|
409
396
|
// Note: this won't work as well when links are involved
|
|
410
397
|
const byEid = {};
|
|
411
398
|
for (const change of tx.changes) {
|
|
412
|
-
const eidChanges =
|
|
399
|
+
const eidChanges = byEid[change.triple[0]] ?? [];
|
|
413
400
|
byEid[change.triple[0]] = eidChanges;
|
|
414
401
|
eidChanges.push(change);
|
|
415
402
|
}
|
|
416
|
-
const values =
|
|
403
|
+
const values = sub.values ?? {
|
|
417
404
|
entities: [],
|
|
418
405
|
attrsStore: this.getAttrs(),
|
|
419
406
|
};
|
|
@@ -474,7 +461,7 @@ export class SyncTable {
|
|
|
474
461
|
sortEntitiesInPlace(sub, orderFieldType, entities);
|
|
475
462
|
this.notifyCbs(hash, {
|
|
476
463
|
type: CallbackEventType.SyncTransaction,
|
|
477
|
-
data: subData(sub,
|
|
464
|
+
data: subData(sub, sub.values?.entities),
|
|
478
465
|
added,
|
|
479
466
|
removed,
|
|
480
467
|
updated,
|