@ja-platform/db 0.0.0-dev-20261028223022 → 0.0.0-stage
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/README.md +3 -0
- package/package.json +3 -52
- package/LICENSE +0 -521
- package/dist/@types/Schema.d.ts +0 -1
- package/dist/@types/app.d.ts +0 -12
- package/dist/@types/browser.d.ts +0 -31
- package/dist/@types/credentials/ApiCredentials.d.ts +0 -18
- package/dist/@types/credentials/MobileCredentials.d.ts +0 -22
- package/dist/@types/credentials/index.d.ts +0 -2
- package/dist/@types/database/Batch.d.ts +0 -61
- package/dist/@types/database/Collection.d.ts +0 -85
- package/dist/@types/database/Database.d.ts +0 -22
- package/dist/@types/database/DatabaseObject.d.ts +0 -51
- package/dist/@types/database/GenericDatabase.d.ts +0 -4
- package/dist/@types/database/JourneyAPIAdapter.d.ts +0 -45
- package/dist/@types/database/ObjectDisplayScope.d.ts +0 -22
- package/dist/@types/database/OnlineDB.d.ts +0 -8
- package/dist/@types/database/adapters/BaseAdapter.d.ts +0 -26
- package/dist/@types/database/adapters/DatabaseAdapter.d.ts +0 -22
- package/dist/@types/database/adapters/WebSQLAdapter.d.ts +0 -50
- package/dist/@types/index.d.ts +0 -2
- package/dist/@types/query/InternalQuery.d.ts +0 -17
- package/dist/@types/query/Query.d.ts +0 -133
- package/dist/@types/query/getPreloadRelationships.d.ts +0 -9
- package/dist/@types/query/queryOperations.d.ts +0 -87
- package/dist/@types/types/Attachment.d.ts +0 -104
- package/dist/@types/types/Location.d.ts +0 -18
- package/dist/@types/types/ObjectData.d.ts +0 -12
- package/dist/@types/types/TypeOperations.d.ts +0 -42
- package/dist/@types/utils/FunctionQueue.d.ts +0 -32
- package/dist/@types/utils/JourneyPromise.d.ts +0 -2
- package/dist/@types/utils/base64.d.ts +0 -4
- package/dist/@types/utils/retryableFetch.d.ts +0 -3
- package/dist/Schema.js +0 -6
- package/dist/Schema.js.map +0 -1
- package/dist/app.js +0 -30
- package/dist/app.js.map +0 -1
- package/dist/browser.js +0 -77
- package/dist/browser.js.map +0 -1
- package/dist/credentials/ApiCredentials.js +0 -61
- package/dist/credentials/ApiCredentials.js.map +0 -1
- package/dist/credentials/MobileCredentials.js +0 -73
- package/dist/credentials/MobileCredentials.js.map +0 -1
- package/dist/credentials/index.js +0 -19
- package/dist/credentials/index.js.map +0 -1
- package/dist/database/Batch.js +0 -189
- package/dist/database/Batch.js.map +0 -1
- package/dist/database/Collection.js +0 -69
- package/dist/database/Collection.js.map +0 -1
- package/dist/database/Database.js +0 -47
- package/dist/database/Database.js.map +0 -1
- package/dist/database/DatabaseObject.js +0 -646
- package/dist/database/DatabaseObject.js.map +0 -1
- package/dist/database/GenericDatabase.js +0 -3
- package/dist/database/GenericDatabase.js.map +0 -1
- package/dist/database/JourneyAPIAdapter.js +0 -357
- package/dist/database/JourneyAPIAdapter.js.map +0 -1
- package/dist/database/ObjectDisplayScope.js +0 -117
- package/dist/database/ObjectDisplayScope.js.map +0 -1
- package/dist/database/OnlineDB.js +0 -16
- package/dist/database/OnlineDB.js.map +0 -1
- package/dist/database/adapters/BaseAdapter.js +0 -66
- package/dist/database/adapters/BaseAdapter.js.map +0 -1
- package/dist/database/adapters/DatabaseAdapter.js +0 -3
- package/dist/database/adapters/DatabaseAdapter.js.map +0 -1
- package/dist/database/adapters/WebSQLAdapter.js +0 -343
- package/dist/database/adapters/WebSQLAdapter.js.map +0 -1
- package/dist/index.js +0 -20
- package/dist/index.js.map +0 -1
- package/dist/query/InternalQuery.js +0 -3
- package/dist/query/InternalQuery.js.map +0 -1
- package/dist/query/Query.js +0 -414
- package/dist/query/Query.js.map +0 -1
- package/dist/query/getPreloadRelationships.js +0 -45
- package/dist/query/getPreloadRelationships.js.map +0 -1
- package/dist/query/queryOperations.js +0 -678
- package/dist/query/queryOperations.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/Attachment.js +0 -198
- package/dist/types/Attachment.js.map +0 -1
- package/dist/types/Location.js +0 -38
- package/dist/types/Location.js.map +0 -1
- package/dist/types/ObjectData.js +0 -3
- package/dist/types/ObjectData.js.map +0 -1
- package/dist/types/TypeOperations.js +0 -267
- package/dist/types/TypeOperations.js.map +0 -1
- package/dist/utils/FunctionQueue.js +0 -91
- package/dist/utils/FunctionQueue.js.map +0 -1
- package/dist/utils/JourneyPromise.js +0 -3
- package/dist/utils/JourneyPromise.js.map +0 -1
- package/dist/utils/base64.js +0 -40
- package/dist/utils/base64.js.map +0 -1
- package/dist/utils/retryableFetch.js +0 -57
- package/dist/utils/retryableFetch.js.map +0 -1
- package/readme.md +0 -37
package/dist/types/Attachment.js
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Attachment = exports.toBackendData = void 0;
|
|
4
|
-
require("isomorphic-fetch");
|
|
5
|
-
const STATE = {
|
|
6
|
-
UPLOADED: 'uploaded',
|
|
7
|
-
PENDING: 'pending'
|
|
8
|
-
};
|
|
9
|
-
exports.toBackendData = Symbol('AttachmentToBackendData');
|
|
10
|
-
class Attachment {
|
|
11
|
-
static isAttachment(attachment) {
|
|
12
|
-
if (attachment == null) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
else if (attachment instanceof Attachment) {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
else if (typeof attachment.id == 'string' &&
|
|
19
|
-
typeof attachment.present == 'function') {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
static async create(options) {
|
|
25
|
-
const attachment = new Attachment({ id: null, state: STATE.PENDING, urls: {} });
|
|
26
|
-
attachment.data = toBuffer(options);
|
|
27
|
-
attachment.filename = options.filename;
|
|
28
|
-
return attachment;
|
|
29
|
-
}
|
|
30
|
-
constructor(attrs) {
|
|
31
|
-
attrs = attrs || {};
|
|
32
|
-
this._urlKey = 'original';
|
|
33
|
-
if (typeof attrs == 'string') {
|
|
34
|
-
this.id = attrs;
|
|
35
|
-
this.state = STATE.PENDING;
|
|
36
|
-
this.urls = {};
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.id = attrs.id || undefined;
|
|
40
|
-
this.state = attrs.state;
|
|
41
|
-
this.urls = attrs.urls;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
toString() {
|
|
45
|
-
return this.id;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Returns a processed version of the attachment.
|
|
49
|
-
*
|
|
50
|
-
* If the specified process does not exist, present() will return false on the returned Attachment.
|
|
51
|
-
*
|
|
52
|
-
* @param process 'original', 'fullscreen' or 'thumbnail'.
|
|
53
|
-
*/
|
|
54
|
-
processed(process) {
|
|
55
|
-
var clone = new Attachment(this);
|
|
56
|
-
clone._urlKey = process;
|
|
57
|
-
return clone;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* An attachment is present if its data is available.
|
|
61
|
-
*
|
|
62
|
-
* This can be either:
|
|
63
|
-
* 1. An attachment created and uploaded by a different client.
|
|
64
|
-
* 2. An attachment created locally, which may not be uploaded yet.
|
|
65
|
-
*
|
|
66
|
-
* @returns true if the attachment is present
|
|
67
|
-
*/
|
|
68
|
-
present() {
|
|
69
|
-
if (this.data) {
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
return this.state == STATE.UPLOADED && this.url() != null;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* @returns true if the attachment is uploaded, either locally or by a different client.
|
|
76
|
-
*/
|
|
77
|
-
uploaded() {
|
|
78
|
-
return this.state == STATE.UPLOADED && this.url() != null;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Return the object as JSON, in the same format as given by the v4 API, with an additional `id` field.
|
|
82
|
-
*/
|
|
83
|
-
toJSON() {
|
|
84
|
-
return Object.assign({
|
|
85
|
-
id: this.id,
|
|
86
|
-
state: this.state
|
|
87
|
-
}, this.urls);
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Returns a the default downloadable URL for the attachment.
|
|
91
|
-
*
|
|
92
|
-
* For different variations of image attachments, use for example:
|
|
93
|
-
* attachment.processed('thumbnail').url();
|
|
94
|
-
*
|
|
95
|
-
* @return the URL, or null if not present
|
|
96
|
-
*/
|
|
97
|
-
url() {
|
|
98
|
-
return this.urls[this._urlKey];
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Downloads the attachment data as a Buffer.
|
|
102
|
-
*
|
|
103
|
-
* Not available on mobile devices.
|
|
104
|
-
*
|
|
105
|
-
* @returns the data as a Buffer
|
|
106
|
-
* @throws an error if the attachment is not present, or a network error occurs.
|
|
107
|
-
*/
|
|
108
|
-
async toBuffer() {
|
|
109
|
-
if (this.data) {
|
|
110
|
-
return this.data;
|
|
111
|
-
}
|
|
112
|
-
if (!this.present()) {
|
|
113
|
-
throw new Error('Attachment is not present');
|
|
114
|
-
}
|
|
115
|
-
const response = await fetch(this.url());
|
|
116
|
-
if (!response.ok) {
|
|
117
|
-
throw new Error('Attachment fetch failed: ' + response.statusText);
|
|
118
|
-
}
|
|
119
|
-
if (typeof response.buffer == 'function') {
|
|
120
|
-
// buffer() is present on node-fetch Response, but not the standard fetch Response.
|
|
121
|
-
this.data = response.buffer();
|
|
122
|
-
return this.data;
|
|
123
|
-
}
|
|
124
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
125
|
-
this.data = Buffer.from(arrayBuffer);
|
|
126
|
-
return this.data;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Downloads the attachment data as a ArrayBuffer.
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* @returns the data as an ArrayBuffer.
|
|
133
|
-
* @throws an error if the attachment is not present, or a network error occurs.
|
|
134
|
-
*/
|
|
135
|
-
async toArrayBuffer() {
|
|
136
|
-
const data = await this.toBuffer();
|
|
137
|
-
const arrBuf = data.buffer.slice(data.byteOffset, data.byteOffset + data.length);
|
|
138
|
-
return arrBuf;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Downloads the attachment data as a string.
|
|
142
|
-
*
|
|
143
|
-
* Only use for text-based data, e.g. a CSV attachment.
|
|
144
|
-
*
|
|
145
|
-
* UTF-8 encoding is assumed.
|
|
146
|
-
*
|
|
147
|
-
* @returns the data as a string
|
|
148
|
-
* @throws an error if the attachment is not present, or a network error occurs.
|
|
149
|
-
*/
|
|
150
|
-
async toText() {
|
|
151
|
-
const data = await this.toBuffer();
|
|
152
|
-
return data.toString('utf8');
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Downloads the attachment data as base64-encoded string.
|
|
156
|
-
*
|
|
157
|
-
* @returns the data
|
|
158
|
-
* @throws an error if the attachment is not present, or a network error occurs.
|
|
159
|
-
*/
|
|
160
|
-
async toBase64() {
|
|
161
|
-
const data = await this.toBuffer();
|
|
162
|
-
return data.toString('base64');
|
|
163
|
-
}
|
|
164
|
-
[exports.toBackendData]() {
|
|
165
|
-
if (this.id != null) {
|
|
166
|
-
return { id: this.id };
|
|
167
|
-
}
|
|
168
|
-
else if (this.data) {
|
|
169
|
-
return {
|
|
170
|
-
base64: this.data.toString('base64'),
|
|
171
|
-
filename: this.filename
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
throw new Error('Invalid attachment');
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
exports.Attachment = Attachment;
|
|
180
|
-
function toBuffer(options) {
|
|
181
|
-
if (options.data) {
|
|
182
|
-
if (Buffer.isBuffer(options.data)) {
|
|
183
|
-
return options.data;
|
|
184
|
-
}
|
|
185
|
-
if (options.data instanceof ArrayBuffer || options.data instanceof Uint8Array) {
|
|
186
|
-
return Buffer.from(options.data);
|
|
187
|
-
}
|
|
188
|
-
throw new Error('Invalid argument for "data"');
|
|
189
|
-
}
|
|
190
|
-
if (options.text) {
|
|
191
|
-
return Buffer.from(options.text, 'utf8');
|
|
192
|
-
}
|
|
193
|
-
if (options.base64) {
|
|
194
|
-
return Buffer.from(options.base64, 'base64');
|
|
195
|
-
}
|
|
196
|
-
throw new Error('Invalid Attachment data');
|
|
197
|
-
}
|
|
198
|
-
//# sourceMappingURL=Attachment.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Attachment.js","sourceRoot":"","sources":["../../src/types/Attachment.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAE1B,MAAM,KAAK,GAAG;IACZ,QAAQ,EAAE,UAAU;IACpB,OAAO,EAAE,SAAS;CACnB,CAAC;AAEW,QAAA,aAAa,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAE/D;IACE,MAAM,CAAC,YAAY,CAAC,UAAmB;QACrC,IAAI,UAAU,IAAI,IAAI,EAAE,CAAC;YACvB,OAAO,KAAK,CAAC;QACf,CAAC;aAAM,IAAI,UAAU,YAAY,UAAU,EAAE,CAAC;YAC5C,OAAO,IAAI,CAAC;QACd,CAAC;aAAM,IACL,OAAQ,UAAyB,CAAC,EAAE,IAAI,QAAQ;YAChD,OAAQ,UAAyB,CAAC,OAAO,IAAI,UAAU,EACvD,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAMnB;QACC,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QAChF,UAAU,CAAC,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;QACpC,UAAU,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACvC,OAAO,UAAU,CAAC;IACpB,CAAC;IAWD,YAAY,KAAmF;QAC7F,KAAK,GAAG,KAAK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,CAAC;QACjB,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC3C,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACnB,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;AAC7C,CAAC","sourcesContent":["import 'isomorphic-fetch';\n\nconst STATE = {\n UPLOADED: 'uploaded',\n PENDING: 'pending'\n};\n\nexport const toBackendData = Symbol('AttachmentToBackendData');\n\nexport class Attachment {\n static isAttachment(attachment: unknown): attachment is Attachment {\n if (attachment == null) {\n return false;\n } else if (attachment instanceof Attachment) {\n return true;\n } else if (\n typeof (attachment as Attachment).id == 'string' &&\n typeof (attachment as Attachment).present == 'function'\n ) {\n return true;\n }\n return false;\n }\n\n static async create(options: {\n filename?: string;\n mediaType?: string;\n base64?: string;\n text?: string;\n data?: ArrayBuffer | Uint8Array;\n }): Promise<Attachment> {\n const attachment = new Attachment({ id: null, state: STATE.PENDING, urls: {} });\n attachment.data = toBuffer(options);\n attachment.filename = options.filename;\n return attachment;\n }\n\n id: string;\n state: string;\n urls: { [index: string]: string };\n\n private data?: Buffer;\n private filename?: string;\n\n private _urlKey: string;\n\n constructor(attrs: string | { id?: string; state?: string; urls?: { [index: string]: string } }) {\n attrs = attrs || {};\n this._urlKey = 'original';\n if (typeof attrs == 'string') {\n this.id = attrs;\n this.state = STATE.PENDING;\n this.urls = {};\n } else {\n this.id = attrs.id || undefined;\n this.state = attrs.state;\n this.urls = attrs.urls;\n }\n }\n\n toString() {\n return this.id;\n }\n\n /**\n * Returns a processed version of the attachment.\n *\n * If the specified process does not exist, present() will return false on the returned Attachment.\n *\n * @param process 'original', 'fullscreen' or 'thumbnail'.\n */\n processed(process: string) {\n var clone = new Attachment(this);\n clone._urlKey = process;\n return clone;\n }\n\n /**\n * An attachment is present if its data is available.\n *\n * This can be either:\n * 1. An attachment created and uploaded by a different client.\n * 2. An attachment created locally, which may not be uploaded yet.\n *\n * @returns true if the attachment is present\n */\n present() {\n if (this.data) {\n return true;\n }\n return this.state == STATE.UPLOADED && this.url() != null;\n }\n\n /**\n * @returns true if the attachment is uploaded, either locally or by a different client.\n */\n uploaded() {\n return this.state == STATE.UPLOADED && this.url() != null;\n }\n\n /**\n * Return the object as JSON, in the same format as given by the v4 API, with an additional `id` field.\n */\n toJSON() {\n return Object.assign(\n {\n id: this.id,\n state: this.state\n },\n this.urls\n );\n }\n\n /**\n * Returns a the default downloadable URL for the attachment.\n *\n * For different variations of image attachments, use for example:\n * attachment.processed('thumbnail').url();\n *\n * @return the URL, or null if not present\n */\n url() {\n return this.urls[this._urlKey];\n }\n\n /**\n * Downloads the attachment data as a Buffer.\n *\n * Not available on mobile devices.\n *\n * @returns the data as a Buffer\n * @throws an error if the attachment is not present, or a network error occurs.\n */\n async toBuffer(): Promise<Buffer> {\n if (this.data) {\n return this.data;\n }\n if (!this.present()) {\n throw new Error('Attachment is not present');\n }\n const response: Response = await fetch(this.url());\n if (!response.ok) {\n throw new Error('Attachment fetch failed: ' + response.statusText);\n }\n\n if (typeof (response as any).buffer == 'function') {\n // buffer() is present on node-fetch Response, but not the standard fetch Response.\n this.data = (response as any).buffer();\n return this.data;\n }\n\n const arrayBuffer = await response.arrayBuffer();\n this.data = Buffer.from(arrayBuffer);\n\n return this.data;\n }\n\n /**\n * Downloads the attachment data as a ArrayBuffer.\n *\n *\n * @returns the data as an ArrayBuffer.\n * @throws an error if the attachment is not present, or a network error occurs.\n */\n async toArrayBuffer(): Promise<ArrayBuffer> {\n const data: Buffer = await this.toBuffer();\n const arrBuf: ArrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.length) as ArrayBuffer;\n return arrBuf;\n }\n\n /**\n * Downloads the attachment data as a string.\n *\n * Only use for text-based data, e.g. a CSV attachment.\n *\n * UTF-8 encoding is assumed.\n *\n * @returns the data as a string\n * @throws an error if the attachment is not present, or a network error occurs.\n */\n async toText(): Promise<string> {\n const data = await this.toBuffer();\n return data.toString('utf8');\n }\n\n /**\n * Downloads the attachment data as base64-encoded string.\n *\n * @returns the data\n * @throws an error if the attachment is not present, or a network error occurs.\n */\n async toBase64(): Promise<string> {\n const data = await this.toBuffer();\n return data.toString('base64');\n }\n\n [toBackendData](): any {\n if (this.id != null) {\n return { id: this.id };\n } else if (this.data) {\n return {\n base64: this.data.toString('base64'),\n filename: this.filename\n };\n } else {\n throw new Error('Invalid attachment');\n }\n }\n}\n\nfunction toBuffer(options: { base64?: string; text?: string; data?: ArrayBuffer | Uint8Array }): Buffer {\n if (options.data) {\n if (Buffer.isBuffer(options.data)) {\n return options.data;\n }\n\n if (options.data instanceof ArrayBuffer || options.data instanceof Uint8Array) {\n return Buffer.from(options.data as Uint8Array);\n }\n\n throw new Error('Invalid argument for \"data\"');\n }\n\n if (options.text) {\n return Buffer.from(options.text, 'utf8');\n }\n\n if (options.base64) {\n return Buffer.from(options.base64, 'base64');\n }\n\n throw new Error('Invalid Attachment data');\n}\n"]}
|
package/dist/types/Location.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Location = void 0;
|
|
4
|
-
class Location {
|
|
5
|
-
constructor(attrs) {
|
|
6
|
-
attrs = attrs || {};
|
|
7
|
-
// We default each attribute to `undefined` if either `null` or `undefined` is passed in.
|
|
8
|
-
this.latitude = attrs.latitude || undefined;
|
|
9
|
-
this.longitude = attrs.longitude || undefined;
|
|
10
|
-
this.altitude = attrs.altitude || undefined;
|
|
11
|
-
this.horizontal_accuracy = attrs.horizontal_accuracy || undefined;
|
|
12
|
-
this.vertical_accuracy = attrs.vertical_accuracy || undefined;
|
|
13
|
-
if (typeof attrs.timestamp == 'string') {
|
|
14
|
-
// From JSON (ISO8601)
|
|
15
|
-
this.timestamp = new Date(Date.parse(attrs.timestamp));
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
this.timestamp = attrs.timestamp || new Date();
|
|
19
|
-
}
|
|
20
|
-
// Make this read-only
|
|
21
|
-
Object.freeze(this);
|
|
22
|
-
}
|
|
23
|
-
toString() {
|
|
24
|
-
return '(' + this.latitude + ',' + this.longitude + ' ~' + this.horizontal_accuracy + ')';
|
|
25
|
-
}
|
|
26
|
-
toJSON() {
|
|
27
|
-
return [
|
|
28
|
-
this.latitude,
|
|
29
|
-
this.longitude,
|
|
30
|
-
this.altitude,
|
|
31
|
-
this.horizontal_accuracy,
|
|
32
|
-
this.vertical_accuracy,
|
|
33
|
-
this.timestamp.toISOString()
|
|
34
|
-
];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
exports.Location = Location;
|
|
38
|
-
//# sourceMappingURL=Location.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Location.js","sourceRoot":"","sources":["../../src/types/Location.ts"],"names":[],"mappings":";;;AAAA;IAQE,YAAY,KAOX;QACC,KAAK,GAAG,KAAK,IAAI,EAAE,CAAC;QACpB,yFAAyF;QACzF,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,IAAI,SAAS,CAAC;QAC5C,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS,IAAI,SAAS,CAAC;QAC9C,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,IAAI,SAAS,CAAC;QAC5C,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC,mBAAmB,IAAI,SAAS,CAAC;QAClE,IAAI,CAAC,iBAAiB,GAAG,KAAK,CAAC,iBAAiB,IAAI,SAAS,CAAC;QAE9D,IAAI,OAAO,KAAK,CAAC,SAAS,IAAI,QAAQ,EAAE,CAAC;YACvC,sBAAsB;YACtB,IAAI,CAAC,SAAS,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;QACzD,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS,IAAI,IAAI,IAAI,EAAE,CAAC;QACjD,CAAC;QAED,sBAAsB;QACtB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,QAAQ;QACN,OAAO,GAAG,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,GAAG,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,IAAI,CAAC,mBAAmB,GAAG,GAAG,CAAC;IAC5F,CAAC;IAED,MAAM;QACJ,OAAO;YACL,IAAI,CAAC,QAAQ;YACb,IAAI,CAAC,SAAS;YACd,IAAI,CAAC,QAAQ;YACb,IAAI,CAAC,mBAAmB;YACxB,IAAI,CAAC,iBAAiB;YACtB,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE;SAC7B,CAAC;IACJ,CAAC;CACF","sourcesContent":["export class Location {\n latitude: number;\n longitude: number;\n altitude: number;\n horizontal_accuracy: number;\n vertical_accuracy: number;\n timestamp: Date;\n\n constructor(attrs: {\n latitude?: number;\n longitude?: number;\n altitude?: number;\n horizontal_accuracy?: number;\n vertical_accuracy?: number;\n timestamp?: string | Date;\n }) {\n attrs = attrs || {};\n // We default each attribute to `undefined` if either `null` or `undefined` is passed in.\n this.latitude = attrs.latitude || undefined;\n this.longitude = attrs.longitude || undefined;\n this.altitude = attrs.altitude || undefined;\n this.horizontal_accuracy = attrs.horizontal_accuracy || undefined;\n this.vertical_accuracy = attrs.vertical_accuracy || undefined;\n\n if (typeof attrs.timestamp == 'string') {\n // From JSON (ISO8601)\n this.timestamp = new Date(Date.parse(attrs.timestamp));\n } else {\n this.timestamp = attrs.timestamp || new Date();\n }\n\n // Make this read-only\n Object.freeze(this);\n }\n\n toString() {\n return '(' + this.latitude + ',' + this.longitude + ' ~' + this.horizontal_accuracy + ')';\n }\n\n toJSON() {\n return [\n this.latitude,\n this.longitude,\n this.altitude,\n this.horizontal_accuracy,\n this.vertical_accuracy,\n this.timestamp.toISOString()\n ];\n }\n}\n"]}
|
package/dist/types/ObjectData.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ObjectData.js","sourceRoot":"","sources":["../../src/types/ObjectData.ts"],"names":[],"mappings":"","sourcesContent":["export interface PersistedObjectData {\n attributes: { [index: string]: any };\n belongs_to: { [index: string]: string };\n}\n\nexport interface ObjectData extends PersistedObjectData {\n id: string;\n type: string;\n}\n"]}
|
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.AttachmentOperations = exports.DatetimeOperations = exports.DateOperations = exports.TypeOperations = void 0;
|
|
37
|
-
exports.operationsFor = operationsFor;
|
|
38
|
-
const parser_type_system_1 = require("@ja-platform/parser-type-system");
|
|
39
|
-
const parser_schema_1 = require("@ja-platform/parser-schema");
|
|
40
|
-
const Location_1 = require("./Location");
|
|
41
|
-
const uuid = __importStar(require("uuid"));
|
|
42
|
-
const Attachment_1 = require("./Attachment");
|
|
43
|
-
/**
|
|
44
|
-
* Operations on runtime values, independent of descriptor traversal.
|
|
45
|
-
*/
|
|
46
|
-
class TypeOperations {
|
|
47
|
-
constructor(baseType) {
|
|
48
|
-
this.baseType = baseType;
|
|
49
|
-
}
|
|
50
|
-
is(value) {
|
|
51
|
-
return this.baseType.is(value);
|
|
52
|
-
}
|
|
53
|
-
parse(value) {
|
|
54
|
-
return this.baseType.parse(value);
|
|
55
|
-
}
|
|
56
|
-
format(value, format) {
|
|
57
|
-
return this.baseType.format(value, format);
|
|
58
|
-
}
|
|
59
|
-
clone(value) {
|
|
60
|
-
return value;
|
|
61
|
-
}
|
|
62
|
-
valueFromJSON(data) {
|
|
63
|
-
return data;
|
|
64
|
-
}
|
|
65
|
-
valueToJSON(value, options) {
|
|
66
|
-
return value;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
exports.TypeOperations = TypeOperations;
|
|
70
|
-
class DateOperations extends TypeOperations {
|
|
71
|
-
clone(value) {
|
|
72
|
-
if (this.baseType.isDay) {
|
|
73
|
-
return new parser_schema_1.Day(value);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
return new Date(value.getTime());
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
valueFromJSON(value) {
|
|
80
|
-
if (typeof value != 'string') {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
var day = new parser_schema_1.Day(value.substring(0, 10));
|
|
85
|
-
if (this.baseType.isDay) {
|
|
86
|
-
return day;
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
return day.toDate();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
catch (err) {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
valueToJSON(value) {
|
|
97
|
-
if (value instanceof Date) {
|
|
98
|
-
return value.toISOString().substring(0, 10);
|
|
99
|
-
}
|
|
100
|
-
else if (parser_schema_1.Day.isDay(value)) {
|
|
101
|
-
return value.toISOString();
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
exports.DateOperations = DateOperations;
|
|
109
|
-
class DatetimeOperations extends TypeOperations {
|
|
110
|
-
clone(value) {
|
|
111
|
-
return new Date(value.getTime());
|
|
112
|
-
}
|
|
113
|
-
valueFromJSON(value) {
|
|
114
|
-
if (typeof value == 'string') {
|
|
115
|
-
const parsed = Date.parse(value);
|
|
116
|
-
if (isNaN(parsed)) {
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
return new Date(parsed);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
valueToJSON(value) {
|
|
128
|
-
if (value instanceof Date) {
|
|
129
|
-
return value.toISOString();
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
exports.DatetimeOperations = DatetimeOperations;
|
|
137
|
-
class LocationOperations extends TypeOperations {
|
|
138
|
-
is(value) {
|
|
139
|
-
return value instanceof Location_1.Location;
|
|
140
|
-
}
|
|
141
|
-
parse(value) {
|
|
142
|
-
if (value instanceof Location_1.Location)
|
|
143
|
-
return value;
|
|
144
|
-
if (typeof value === 'object')
|
|
145
|
-
return new Location_1.Location(value);
|
|
146
|
-
throw new Error(value + ' is not a valid location');
|
|
147
|
-
}
|
|
148
|
-
clone(value) {
|
|
149
|
-
return new Location_1.Location(value);
|
|
150
|
-
}
|
|
151
|
-
valueFromJSON(value) {
|
|
152
|
-
if (Array.isArray(value)) {
|
|
153
|
-
if (value.length < 6) {
|
|
154
|
-
throw new Error('Location array format must have 6 parts, namely [latitude, longitude, altitude, horizontal_accuracy, vertical_accuracy, timestamp]');
|
|
155
|
-
}
|
|
156
|
-
// Array format
|
|
157
|
-
const data = {
|
|
158
|
-
latitude: value[0],
|
|
159
|
-
longitude: value[1],
|
|
160
|
-
altitude: value[2],
|
|
161
|
-
horizontal_accuracy: value[3],
|
|
162
|
-
vertical_accuracy: value[4],
|
|
163
|
-
timestamp: value[5]
|
|
164
|
-
};
|
|
165
|
-
return new Location_1.Location(data);
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
// Object format
|
|
169
|
-
return new Location_1.Location(value);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
class SingleChoiceIntegerOperations extends TypeOperations {
|
|
174
|
-
valueFromJSON(value) {
|
|
175
|
-
if (typeof value == 'number') {
|
|
176
|
-
return value;
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
valueToJSON(value) {
|
|
183
|
-
if (typeof value == 'number') {
|
|
184
|
-
return value;
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
class ObjectOperations extends TypeOperations {
|
|
192
|
-
is(value) {
|
|
193
|
-
return (value != null &&
|
|
194
|
-
typeof value === 'object' &&
|
|
195
|
-
parser_type_system_1.ObjectType.isInstanceOf(value.type) &&
|
|
196
|
-
value.type.name === this.baseType.name &&
|
|
197
|
-
typeof value._save === 'function');
|
|
198
|
-
}
|
|
199
|
-
parse(value) {
|
|
200
|
-
if (value == null || typeof value !== 'object') {
|
|
201
|
-
throw new Error(value + ' is not an object');
|
|
202
|
-
}
|
|
203
|
-
if (this.is(value))
|
|
204
|
-
return value;
|
|
205
|
-
throw new Error('Expected ' + value + ' to have type ' + this.baseType.name);
|
|
206
|
-
}
|
|
207
|
-
clone(value) {
|
|
208
|
-
return value._clone();
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
class QueryOperations extends TypeOperations {
|
|
212
|
-
clone(value) {
|
|
213
|
-
return value._clone();
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
class AttachmentOperations extends TypeOperations {
|
|
217
|
-
is(value) {
|
|
218
|
-
return Attachment_1.Attachment.isAttachment(value);
|
|
219
|
-
}
|
|
220
|
-
createValue(value) {
|
|
221
|
-
return new Attachment_1.Attachment(value);
|
|
222
|
-
}
|
|
223
|
-
parse(value) {
|
|
224
|
-
if (this.is(value))
|
|
225
|
-
return value;
|
|
226
|
-
if ((typeof value === 'string' && uuid.validate(value)) || typeof value === 'object') {
|
|
227
|
-
return this.createValue(value);
|
|
228
|
-
}
|
|
229
|
-
throw new Error(value + ' is not a valid id');
|
|
230
|
-
}
|
|
231
|
-
valueFromJSON(value) {
|
|
232
|
-
if (value != null)
|
|
233
|
-
return this.createValue(value);
|
|
234
|
-
}
|
|
235
|
-
valueToJSON(value, options) {
|
|
236
|
-
if (!value)
|
|
237
|
-
return null;
|
|
238
|
-
if ((options === null || options === void 0 ? void 0 : options.inlineAttachments) && typeof value[Attachment_1.toBackendData] === 'function') {
|
|
239
|
-
return value[Attachment_1.toBackendData]();
|
|
240
|
-
}
|
|
241
|
-
return value.id;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
exports.AttachmentOperations = AttachmentOperations;
|
|
245
|
-
/**
|
|
246
|
-
* Resolve value checks, parsing, copying, and wire conversion without replacing the original type.
|
|
247
|
-
*/
|
|
248
|
-
function operationsFor(type) {
|
|
249
|
-
switch (type.name) {
|
|
250
|
-
case parser_schema_1.PrimitiveType.DATE:
|
|
251
|
-
return new DateOperations(type);
|
|
252
|
-
case parser_schema_1.PrimitiveType.DATETIME:
|
|
253
|
-
return new DatetimeOperations(type);
|
|
254
|
-
case parser_schema_1.PrimitiveType.LOCATION:
|
|
255
|
-
return new LocationOperations(type);
|
|
256
|
-
case parser_schema_1.PrimitiveType.SINGLE_CHOICE_INTEGER:
|
|
257
|
-
return new SingleChoiceIntegerOperations(type);
|
|
258
|
-
case parser_schema_1.PrimitiveType.ATTACHMENT:
|
|
259
|
-
return new AttachmentOperations(type);
|
|
260
|
-
}
|
|
261
|
-
if (parser_type_system_1.ObjectType.isInstanceOf(type))
|
|
262
|
-
return new ObjectOperations(type);
|
|
263
|
-
if (parser_schema_1.QueryType.isInstanceOf(type))
|
|
264
|
-
return new QueryOperations(type);
|
|
265
|
-
return new TypeOperations(type);
|
|
266
|
-
}
|
|
267
|
-
//# sourceMappingURL=TypeOperations.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TypeOperations.js","sourceRoot":"","sources":["../../src/types/TypeOperations.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wEAAmE;AACnE,8DAAqF;AAGrF,yCAAsC;AACtC,MAAY,IAAI,iCAAa;AAC7B,6CAAyD;AAMzD;;GAEG;AACH;IACE,YAAqB,QAAc;wBAAd,QAAQ;IAAS,CAAC;IAEvC,EAAE,CAAC,KAAc;QACf,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;IACjC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AC,IAAI,CAAC,CAAC;IACrE,IAAI,yBAAS,CAAC,YAAY,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,eAAe,CAAC,IAAI,CAAC,CAAC;IACnE,OAAO,IAAI,cAAc,CAAC,IAAI,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import { Type, ObjectType } from '@ja-platform/parser-type-system';\nimport { QueryType, DateType, PrimitiveType, Day } from '@ja-platform/parser-schema';\nimport type { Formatter } from '@ja-platform/parser-type-system';\n\nimport { Location } from './Location';\nimport * as uuid from 'uuid';\nimport { Attachment, toBackendData } from './Attachment';\n\nexport interface ValueSerializeOptions {\n inlineAttachments?: boolean;\n}\n\n/**\n * Operations on runtime values, independent of descriptor traversal.\n */\nexport class TypeOperations implements Formatter {\n constructor(readonly baseType: Type) {}\n\n is(value: unknown): boolean {\n return this.baseType.is(value);\n }\n\n parse(value: unknown): any {\n return this.baseType.parse(value);\n }\n\n format(value: any, format?: string): string {\n return this.baseType.format(value, format);\n }\n\n clone(value: any): any {\n return value;\n }\n valueFromJSON(data: any): any {\n return data;\n }\n valueToJSON(value: any, options?: ValueSerializeOptions): any {\n return value;\n }\n}\n\nexport class DateOperations extends TypeOperations {\n declare readonly baseType: DateType;\n clone(value: any) {\n if (this.baseType.isDay) {\n return new Day(value);\n } else {\n return new Date(value.getTime());\n }\n }\n valueFromJSON(value: any) {\n if (typeof value != 'string') {\n return null;\n }\n try {\n var day = new Day(value.substring(0, 10));\n if (this.baseType.isDay) {\n return day;\n } else {\n return day.toDate();\n }\n } catch (err) {\n return null;\n }\n }\n valueToJSON(value: any) {\n if (value instanceof Date) {\n return value.toISOString().substring(0, 10);\n } else if (Day.isDay(value)) {\n return value.toISOString();\n } else {\n return null;\n }\n }\n}\n\nexport class DatetimeOperations extends TypeOperations {\n clone(value: any) {\n return new Date(value.getTime());\n }\n valueFromJSON(value: string) {\n if (typeof value == 'string') {\n const parsed = Date.parse(value);\n if (isNaN(parsed)) {\n return null;\n } else {\n return new Date(parsed);\n }\n } else {\n return null;\n }\n }\n valueToJSON(value: any) {\n if (value instanceof Date) {\n return value.toISOString();\n } else {\n return null;\n }\n }\n}\n\nclass LocationOperations extends TypeOperations {\n is(value: unknown): boolean {\n return value instanceof Location;\n }\n\n parse(value: any): Location {\n if (value instanceof Location) return value;\n if (typeof value === 'object') return new Location(value);\n throw new Error(value + ' is not a valid location');\n }\n\n clone(value: any) {\n return new Location(value);\n }\n valueFromJSON(value: any) {\n if (Array.isArray(value)) {\n if (value.length < 6) {\n throw new Error(\n 'Location array format must have 6 parts, namely [latitude, longitude, altitude, horizontal_accuracy, vertical_accuracy, timestamp]'\n );\n }\n // Array format\n const data = {\n latitude: value[0],\n longitude: value[1],\n altitude: value[2],\n horizontal_accuracy: value[3],\n vertical_accuracy: value[4],\n timestamp: value[5]\n };\n return new Location(data);\n } else {\n // Object format\n return new Location(value);\n }\n }\n}\n\nclass SingleChoiceIntegerOperations extends TypeOperations {\n valueFromJSON(value: any) {\n if (typeof value == 'number') {\n return value;\n } else {\n return null;\n }\n }\n valueToJSON(value: any) {\n if (typeof value == 'number') {\n return value;\n } else {\n return null;\n }\n }\n}\n\nclass ObjectOperations extends TypeOperations {\n is(value: any): boolean {\n return (\n value != null &&\n typeof value === 'object' &&\n ObjectType.isInstanceOf(value.type) &&\n value.type.name === this.baseType.name &&\n typeof value._save === 'function'\n );\n }\n\n parse(value: unknown): unknown {\n if (value == null || typeof value !== 'object') {\n throw new Error(value + ' is not an object');\n }\n if (this.is(value)) return value;\n throw new Error('Expected ' + value + ' to have type ' + this.baseType.name);\n }\n\n clone(value: { _clone(): unknown }) {\n return value._clone();\n }\n}\n\nclass QueryOperations extends TypeOperations {\n clone(value: { _clone(): unknown }) {\n return value._clone();\n }\n}\n\nexport class AttachmentOperations extends TypeOperations {\n is(value: unknown): boolean {\n return Attachment.isAttachment(value);\n }\n\n protected createValue(value: any): any {\n return new Attachment(value);\n }\n\n parse(value: unknown): any {\n if (this.is(value)) return value;\n if ((typeof value === 'string' && uuid.validate(value)) || typeof value === 'object') {\n return this.createValue(value);\n }\n throw new Error(value + ' is not a valid id');\n }\n\n valueFromJSON(value: any) {\n if (value != null) return this.createValue(value);\n }\n\n valueToJSON(value: Attachment, options?: ValueSerializeOptions) {\n if (!value) return null;\n if (options?.inlineAttachments && typeof value[toBackendData] === 'function') {\n return value[toBackendData]();\n }\n return value.id;\n }\n}\n\n/**\n * Resolve value checks, parsing, copying, and wire conversion without replacing the original type.\n */\nexport function operationsFor(type: Type): TypeOperations {\n switch (type.name) {\n case PrimitiveType.DATE:\n return new DateOperations(type as DateType);\n case PrimitiveType.DATETIME:\n return new DatetimeOperations(type);\n case PrimitiveType.LOCATION:\n return new LocationOperations(type);\n case PrimitiveType.SINGLE_CHOICE_INTEGER:\n return new SingleChoiceIntegerOperations(type);\n case PrimitiveType.ATTACHMENT:\n return new AttachmentOperations(type);\n }\n if (ObjectType.isInstanceOf(type)) return new ObjectOperations(type);\n if (QueryType.isInstanceOf(type)) return new QueryOperations(type);\n return new TypeOperations(type);\n}\n"]}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FunctionQueue = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* FunctionQueue maintains a queue of Promise-returning functions that
|
|
6
|
-
* are executed sequentially (whereas normally they would execute their async code concurrently).
|
|
7
|
-
*/
|
|
8
|
-
class FunctionQueue {
|
|
9
|
-
constructor() {
|
|
10
|
-
this.queue = [];
|
|
11
|
-
this.multiCount = 0;
|
|
12
|
-
this.exclusiveLock = false;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Place a function on the queue.
|
|
16
|
-
* The function may either return a Promise or a value.
|
|
17
|
-
* Return a Promise that is resolved with the result of the function.
|
|
18
|
-
*/
|
|
19
|
-
enqueue(promiseFn, multi) {
|
|
20
|
-
var self = this;
|
|
21
|
-
function unlock() {
|
|
22
|
-
if (multi) {
|
|
23
|
-
self.multiCount -= 1;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
self.exclusiveLock = false;
|
|
27
|
-
}
|
|
28
|
-
self._tryNext();
|
|
29
|
-
}
|
|
30
|
-
return this._lockNext(multi).then(promiseFn).finally(unlock);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Place a function on the queue.
|
|
34
|
-
* This function may execute in parallel with other "multi" functions, but not with other functions on the exclusive
|
|
35
|
-
* queue.
|
|
36
|
-
*/
|
|
37
|
-
enqueueMulti(promiseFn) {
|
|
38
|
-
return this.enqueue(promiseFn, true);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Convert a normal Promise-returning function into one that is automatically enqueued.
|
|
42
|
-
* The signature of the function stays the same - only the execution is potentially delayed.
|
|
43
|
-
* The only exception is that if the function would have returned a scalar value, it now
|
|
44
|
-
* returns a Promise.
|
|
45
|
-
*/
|
|
46
|
-
qu(fn) {
|
|
47
|
-
var self = this;
|
|
48
|
-
return function () {
|
|
49
|
-
var args = arguments;
|
|
50
|
-
return self.enqueue(function () {
|
|
51
|
-
return fn.apply(null, args);
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
_lockNext(multi) {
|
|
56
|
-
var self = this;
|
|
57
|
-
return new Promise(function (resolve, reject) {
|
|
58
|
-
var task = {
|
|
59
|
-
execute: resolve,
|
|
60
|
-
multi: multi
|
|
61
|
-
};
|
|
62
|
-
self.queue.push(task);
|
|
63
|
-
self._tryNext();
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
_tryNext() {
|
|
67
|
-
if (this.queue.length == 0) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
if (this.exclusiveLock) {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
var task = this.queue[0];
|
|
74
|
-
if (task.multi) {
|
|
75
|
-
this.multiCount += 1;
|
|
76
|
-
this.queue.shift();
|
|
77
|
-
task.execute();
|
|
78
|
-
}
|
|
79
|
-
else if (this.multiCount == 0) {
|
|
80
|
-
this.exclusiveLock = true;
|
|
81
|
-
this.queue.shift();
|
|
82
|
-
task.execute();
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
exports.FunctionQueue = FunctionQueue;
|
|
91
|
-
//# sourceMappingURL=FunctionQueue.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FunctionQueue.js","sourceRoot":"","sources":["../../src/utils/FunctionQueue.ts"],"names":[],"mappings":";;;AAOA;;;GAGG;AACH;IAKE;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;QACpB,IAAI,CAAC,aAAa,GAAG,KAAK,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAI,SAA6B,EAAE,KAAe;QACvD,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,SAAS,MAAM;YACb,IAAI,KAAK,EAAE,CAAC;gBACV,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC;YACvB,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,aAAa,GAAG,KAAK,CAAC;YAC7B,CAAC;YACD,IAAI,CAAC,QAAQ,EAAE,CAAC;QAClB,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/D,CAAC;IAED;;;;OAIG;IACH,YAAY,CAAI,SAA6B;QAC3C,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACvC,CAAC;IAED;;;;;OAKG;IACH,EAAE,CAAI,EAAsB;QAC1B,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,OAAO;YACL,IAAI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ent":["export type PromiseFunction<T> = () => Promise<T>;\n\ninterface Task {\n multi: boolean;\n execute: () => void;\n}\n\n/**\n * FunctionQueue maintains a queue of Promise-returning functions that\n * are executed sequentially (whereas normally they would execute their async code concurrently).\n */\nexport class FunctionQueue {\n private queue: Task[];\n private multiCount: number;\n private exclusiveLock: boolean;\n\n constructor() {\n this.queue = [];\n this.multiCount = 0;\n this.exclusiveLock = false;\n }\n\n /**\n * Place a function on the queue.\n * The function may either return a Promise or a value.\n * Return a Promise that is resolved with the result of the function.\n */\n enqueue<T>(promiseFn: PromiseFunction<T>, multi?: boolean): Promise<T> {\n var self = this;\n function unlock() {\n if (multi) {\n self.multiCount -= 1;\n } else {\n self.exclusiveLock = false;\n }\n self._tryNext();\n }\n return this._lockNext(multi).then(promiseFn).finally(unlock);\n }\n\n /**\n * Place a function on the queue.\n * This function may execute in parallel with other \"multi\" functions, but not with other functions on the exclusive\n * queue.\n */\n enqueueMulti<T>(promiseFn: PromiseFunction<T>): Promise<T> {\n return this.enqueue(promiseFn, true);\n }\n\n /**\n * Convert a normal Promise-returning function into one that is automatically enqueued.\n * The signature of the function stays the same - only the execution is potentially delayed.\n * The only exception is that if the function would have returned a scalar value, it now\n * returns a Promise.\n */\n qu<T>(fn: PromiseFunction<T>): () => Promise<T> {\n var self = this;\n return function () {\n var args = arguments;\n return self.enqueue(function () {\n return fn.apply(null, args);\n });\n };\n }\n\n private _lockNext(multi?: boolean) {\n var self = this;\n return new Promise<void>(function (resolve, reject) {\n var task = {\n execute: resolve,\n multi: multi\n };\n self.queue.push(task);\n self._tryNext();\n });\n }\n\n private _tryNext() {\n if (this.queue.length == 0) {\n return false;\n }\n\n if (this.exclusiveLock) {\n return false;\n }\n\n var task = this.queue[0];\n if (task.multi) {\n this.multiCount += 1;\n this.queue.shift();\n task.execute();\n } else if (this.multiCount == 0) {\n this.exclusiveLock = true;\n this.queue.shift();\n task.execute();\n } else {\n return false;\n }\n\n return true;\n }\n}\n"]}
|