@aegis-framework/artemis 0.3.29 → 0.4.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/LICENSE +1 -1
- package/dist/artemis.browser.js +4 -0
- package/dist/artemis.browser.js.map +24 -0
- package/dist/artemis.js +3 -1
- package/dist/artemis.js.map +23 -1
- package/dist/types/DOM.d.ts +383 -0
- package/dist/types/DOM.d.ts.map +1 -0
- package/dist/types/Debug.d.ts +118 -0
- package/dist/types/Debug.d.ts.map +1 -0
- package/dist/types/FileSystem.d.ts +69 -0
- package/dist/types/FileSystem.d.ts.map +1 -0
- package/dist/types/Form.d.ts +32 -0
- package/dist/types/Form.d.ts.map +1 -0
- package/dist/types/Platform.d.ts +93 -0
- package/dist/types/Platform.d.ts.map +1 -0
- package/dist/types/Preload.d.ts +26 -0
- package/dist/types/Preload.d.ts.map +1 -0
- package/dist/types/Request.d.ts +86 -0
- package/dist/types/Request.d.ts.map +1 -0
- package/dist/types/Space.d.ts +205 -0
- package/dist/types/Space.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/IndexedDB.d.ts +130 -0
- package/dist/types/SpaceAdapter/IndexedDB.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/LocalStorage.d.ts +125 -0
- package/dist/types/SpaceAdapter/LocalStorage.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/RemoteStorage.d.ts +122 -0
- package/dist/types/SpaceAdapter/RemoteStorage.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/SessionStorage.d.ts +30 -0
- package/dist/types/SpaceAdapter/SessionStorage.d.ts.map +1 -0
- package/dist/types/SpaceAdapter/types.d.ts +76 -0
- package/dist/types/SpaceAdapter/types.d.ts.map +1 -0
- package/dist/types/Text.d.ts +47 -0
- package/dist/types/Text.d.ts.map +1 -0
- package/dist/types/Util.d.ts +31 -0
- package/dist/types/Util.d.ts.map +1 -0
- package/dist/types/browser.d.ts +7 -0
- package/dist/types/browser.d.ts.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +42 -53
- package/dist/artemis.min.js +0 -2
- package/dist/artemis.min.js.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/index.js +0 -10
- package/src/DOM.js +0 -993
- package/src/Debug.js +0 -233
- package/src/FileSystem.js +0 -109
- package/src/Form.js +0 -73
- package/src/Platform.js +0 -166
- package/src/Preload.js +0 -48
- package/src/Request.js +0 -163
- package/src/Space.js +0 -334
- package/src/SpaceAdapter/IndexedDB.js +0 -345
- package/src/SpaceAdapter/LocalStorage.js +0 -395
- package/src/SpaceAdapter/RemoteStorage.js +0 -225
- package/src/SpaceAdapter/SessionStorage.js +0 -46
- package/src/Text.js +0 -133
- package/src/Util.js +0 -55
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ==============================
|
|
3
|
-
* IndexedDB Adapter
|
|
4
|
-
* ==============================
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The IndexedDB Adapter provides the Space Class the ability to interact
|
|
9
|
-
* with the IndexedDB API found in most modern browsers.
|
|
10
|
-
*
|
|
11
|
-
* @class
|
|
12
|
-
*/
|
|
13
|
-
export class IndexedDB {
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Create a new IndexedDB. Differently from Local and Session Storages, the
|
|
17
|
-
* IndexedDB Adapter requires a mandatory name, version and store name.
|
|
18
|
-
*
|
|
19
|
-
* @constructor
|
|
20
|
-
* @param {Object} [configuration={name = '', version = '', store = '', props = {}, index = {}}] - Configuration Object for the Adapter
|
|
21
|
-
* @param {string} configuration.name - Name of the Space
|
|
22
|
-
* @param {string} configuration.version - Version of the Space in Semantic versioning syntax
|
|
23
|
-
* @param {string} configuration.store - Name of the Object Store to use
|
|
24
|
-
* @param {Object} configuration.props - Optional Parameters for the Object Store
|
|
25
|
-
* @param {Object} configuration.index - Object of the indexes to declare for
|
|
26
|
-
* the Object Store. Each index is a JSON object with the following properties:
|
|
27
|
-
* @param {String} configuration.index[...].name - Name for the Index
|
|
28
|
-
* @param {String} configuration.index[...].field - Field on the store to apply the index to
|
|
29
|
-
* @param {Object} configuration.index[...].props - Index properties object
|
|
30
|
-
*/
|
|
31
|
-
constructor ({name = '', version = '', store = '', props = {}, index = {}}) {
|
|
32
|
-
this.name = name;
|
|
33
|
-
this.version = version;
|
|
34
|
-
this.store = store;
|
|
35
|
-
this.props = props || {};
|
|
36
|
-
this.index = index;
|
|
37
|
-
|
|
38
|
-
this.keyPath = props.keyPath || 'id';
|
|
39
|
-
|
|
40
|
-
this.upgrades = {};
|
|
41
|
-
|
|
42
|
-
if (this.version === '') {
|
|
43
|
-
this.numericVersion = 0;
|
|
44
|
-
} else {
|
|
45
|
-
this.numericVersion = parseInt (version.replace (/\./g, ''));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Open the Storage Object
|
|
51
|
-
*
|
|
52
|
-
* @return {Promise}
|
|
53
|
-
*/
|
|
54
|
-
open () {
|
|
55
|
-
|
|
56
|
-
if (this.name === '') {
|
|
57
|
-
console.error ('No name has been defined for IndexedDB space.');
|
|
58
|
-
return Promise.reject ();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (this.store === '') {
|
|
62
|
-
console.error ('No store has been defined for IndexedDB space.');
|
|
63
|
-
return Promise.reject ();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (this.storage instanceof IDBDatabase) {
|
|
67
|
-
return Promise.resolve (this);
|
|
68
|
-
} else if (this.storage instanceof Promise) {
|
|
69
|
-
return this.storage;
|
|
70
|
-
} else {
|
|
71
|
-
this.storage = new Promise ((resolve, reject) => {
|
|
72
|
-
let upgradesToApply = [];
|
|
73
|
-
const storage = window.indexedDB.open (this.name, this.numericVersion);
|
|
74
|
-
|
|
75
|
-
storage.onerror = (event) => {
|
|
76
|
-
reject (event);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
storage.onsuccess = (event) => {
|
|
80
|
-
resolve ({ storage: event.target.result, upgrades: upgradesToApply });
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
storage.onupgradeneeded = (event) => {
|
|
84
|
-
// If the previous version is less than one, it means that
|
|
85
|
-
// the database needs to be created first
|
|
86
|
-
if (event.oldVersion < 1) {
|
|
87
|
-
// Create all the needed Stores
|
|
88
|
-
const store = event.target.result.createObjectStore (this.store, this.props);
|
|
89
|
-
for (const index of Object.keys (this.index)) {
|
|
90
|
-
store.createIndex (this.index[index].name, this.index[index].field, this.index[index].props);
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
// Check what upgrade functions have been declared in their respective order
|
|
94
|
-
const availableUpgrades = Object.keys (this.upgrades).sort ();
|
|
95
|
-
|
|
96
|
-
// Find the first update that needs to be applied to the database given
|
|
97
|
-
// the old version it currently has.
|
|
98
|
-
const startFrom = availableUpgrades.findIndex (u => {
|
|
99
|
-
const [old, ] = u.split ('::');
|
|
100
|
-
return parseInt (old) === event.oldVersion;
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
if (startFrom > -1) {
|
|
104
|
-
upgradesToApply = availableUpgrades.slice (startFrom).filter ((u) => {
|
|
105
|
-
const [old, next] = u.split ('::');
|
|
106
|
-
return parseInt (old) < this.numericVersion && parseInt (next) <= this.numericVersion;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Once the transaction is done, resolve the storage object
|
|
112
|
-
const transaction = event.target.transaction;
|
|
113
|
-
transaction.addEventListener ('success', () => {
|
|
114
|
-
resolve ({ storage: event.target.result, upgrades: upgradesToApply });
|
|
115
|
-
});
|
|
116
|
-
};
|
|
117
|
-
}).then (({ storage, upgrades }) => {
|
|
118
|
-
this.storage = storage;
|
|
119
|
-
return new Promise ((resolve) => {
|
|
120
|
-
const res = () => resolve (storage);
|
|
121
|
-
this._upgrade (upgrades, res, event);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
return this.storage;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Store a key-value pair. Because of the nature of a IndexedDB Database, the
|
|
130
|
-
* stored values must be JSON objects.
|
|
131
|
-
*
|
|
132
|
-
* @param {string} key - Key with which this value will be saved
|
|
133
|
-
* @param {Object} - Value to save
|
|
134
|
-
* @return {Promise<Object>} - When resolved, a {key, value} object is handed
|
|
135
|
-
* down, when it's rejected, the event is handed down.
|
|
136
|
-
*/
|
|
137
|
-
set (key = null, value) {
|
|
138
|
-
return this.open ().then (() => {
|
|
139
|
-
return new Promise ((resolve, reject) => {
|
|
140
|
-
const transaction = this.storage.transaction (this.store, 'readwrite').objectStore (this.store);
|
|
141
|
-
let op;
|
|
142
|
-
if (key !== null) {
|
|
143
|
-
const temp = {};
|
|
144
|
-
temp[this.keyPath] = key;
|
|
145
|
-
op = transaction.put (Object.assign ({}, temp, value));
|
|
146
|
-
} else {
|
|
147
|
-
op = transaction.add (value);
|
|
148
|
-
}
|
|
149
|
-
op.addEventListener ('success', (event) => { resolve ({key: event.target.result, value: value});});
|
|
150
|
-
op.addEventListener ('error', (event) => {reject (event);});
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Update a key-value pair. In difference with the set () method, the update
|
|
157
|
-
* method will use an Object.assign () in the case of objects so no value is
|
|
158
|
-
* lost.
|
|
159
|
-
*
|
|
160
|
-
* @param {string} key - Key with which this value will be saved
|
|
161
|
-
* @param {Object} - Value to save
|
|
162
|
-
* @return {Promise<Object>} - When resolved, a {key, value} object is handed
|
|
163
|
-
* down, when it's rejected, the event is handed down.
|
|
164
|
-
*/
|
|
165
|
-
update (key, value) {
|
|
166
|
-
return this.get (key).then ((currentValue) => {
|
|
167
|
-
// If this key did not exist on the storage, then create it using the
|
|
168
|
-
// set method
|
|
169
|
-
if (typeof currentValue === 'undefined') {
|
|
170
|
-
return this.set (key, value);
|
|
171
|
-
}
|
|
172
|
-
return new Promise ((resolve, reject) => {
|
|
173
|
-
const transaction = this.storage.transaction (this.store, 'readwrite').objectStore (this.store);
|
|
174
|
-
const op = transaction.put (Object.assign ({}, currentValue, value));
|
|
175
|
-
op.addEventListener ('success', (event) => {resolve ({key: event.target.result, value: value});});
|
|
176
|
-
op.addEventListener ('error', (event) => {reject (event);});
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Retrieves a value from storage given it's key
|
|
183
|
-
*
|
|
184
|
-
* @param {string} - Key with which the value was saved
|
|
185
|
-
* @return {Promise<Object>} - Resolves to the retreived value or its rejected
|
|
186
|
-
* if it doesn't exist
|
|
187
|
-
*/
|
|
188
|
-
get (key) {
|
|
189
|
-
return this.open ().then (() => {
|
|
190
|
-
return new Promise ((resolve, reject) => {
|
|
191
|
-
const transaction = this.storage.transaction (this.store).objectStore (this.store);
|
|
192
|
-
const op = transaction.get (key);
|
|
193
|
-
|
|
194
|
-
op.addEventListener ('success', (event) => {
|
|
195
|
-
const value = event.target.result;
|
|
196
|
-
if (typeof value !== 'undefined' && value !== null) {
|
|
197
|
-
resolve (value);
|
|
198
|
-
} else {
|
|
199
|
-
reject ();
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
op.addEventListener ('error', (event) => {reject (event);});
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Retrieves all the values in the space in a key-value JSON object
|
|
209
|
-
*
|
|
210
|
-
* @return {Promise<Object>} - Resolves to the retreived values
|
|
211
|
-
*/
|
|
212
|
-
getAll () {
|
|
213
|
-
return this.open ().then (() => {
|
|
214
|
-
return new Promise ((resolve, reject) => {
|
|
215
|
-
const transaction = this.storage.transaction (this.store).objectStore (this.store);
|
|
216
|
-
const op = transaction.getAll ();
|
|
217
|
-
|
|
218
|
-
op.addEventListener ('success', (event) => {
|
|
219
|
-
const results = {};
|
|
220
|
-
event.target.result.forEach((item) => {
|
|
221
|
-
const id = item[this.keyPath];
|
|
222
|
-
delete item[this.keyPath];
|
|
223
|
-
|
|
224
|
-
results[id] = item;
|
|
225
|
-
});
|
|
226
|
-
resolve (results);
|
|
227
|
-
});
|
|
228
|
-
op.addEventListener ('error', (event) => {reject (event);});
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Check if the space contains a given key.
|
|
235
|
-
*
|
|
236
|
-
* @param {string} key - Key to look for.
|
|
237
|
-
* @return {Promise} - Promise gets resolved if it exists and rejected if it
|
|
238
|
-
* doesn't
|
|
239
|
-
*/
|
|
240
|
-
contains (key) {
|
|
241
|
-
return this.get (key).then ((keys) => {
|
|
242
|
-
if (keys.includes (key)) {
|
|
243
|
-
Promise.resolve ();
|
|
244
|
-
} else {
|
|
245
|
-
return Promise.reject ();
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Upgrade a Space Version. Upgrades must be declared before the open ()
|
|
252
|
-
* method is executed.
|
|
253
|
-
*
|
|
254
|
-
* @param {string} oldVersion - The version to be upgraded
|
|
255
|
-
* @param {string} newVersion - The version to be upgraded to
|
|
256
|
-
* @param {function} callback - Function to transform the old stored values to the new version's format
|
|
257
|
-
* @returns {Promise}
|
|
258
|
-
*/
|
|
259
|
-
upgrade (oldVersion, newVersion, callback) {
|
|
260
|
-
this.upgrades[`${parseInt (oldVersion.replace (/\./g, ''))}::${parseInt (newVersion.replace (/\./g, ''))}`] = callback;
|
|
261
|
-
return Promise.resolve ();
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// This function acts as a helper for the upgrade progress by executing the
|
|
265
|
-
// needed upgrade callbacks in the correct order and sychronously.
|
|
266
|
-
_upgrade (upgradesToApply, resolve, event) {
|
|
267
|
-
// Check if there are still upgrades to apply
|
|
268
|
-
if (upgradesToApply.length > 0) {
|
|
269
|
-
this.upgrades[upgradesToApply[0]].call (this, this, event).then (() => {
|
|
270
|
-
this._upgrade (upgradesToApply.slice (1), resolve, event);
|
|
271
|
-
}).catch ((e) => console.error (e));
|
|
272
|
-
} else {
|
|
273
|
-
resolve ();
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Renaming the space is not possible with the IndexedDB adapter therefore
|
|
279
|
-
* this function always gets a rejection.
|
|
280
|
-
*
|
|
281
|
-
* @returns {Promise} - Result of the rename operation
|
|
282
|
-
*/
|
|
283
|
-
rename () {
|
|
284
|
-
return Promise.reject ();
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Getting a key by its index is not possible in this adapter, therefore this
|
|
289
|
-
* function always gets rejected.
|
|
290
|
-
*
|
|
291
|
-
* @return {Promise} - Promise Rejection
|
|
292
|
-
*/
|
|
293
|
-
key () {
|
|
294
|
-
return Promise.reject ();
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Return all keys stored in the space.
|
|
299
|
-
*
|
|
300
|
-
* @return {Promise<string[]>} - Array of keys
|
|
301
|
-
*/
|
|
302
|
-
keys () {
|
|
303
|
-
return this.open ().then (() => {
|
|
304
|
-
return new Promise ((resolve, reject) => {
|
|
305
|
-
const transaction = this.storage.transaction (this.store, 'readwrite').objectStore (this.store);
|
|
306
|
-
const op = transaction.getAllKeys ();
|
|
307
|
-
op.addEventListener ('success', (event) => {resolve (event.target.result);}, false);
|
|
308
|
-
op.addEventListener ('error', (event) => {reject (event);}, false);
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Delete a value from the space given its key
|
|
315
|
-
*
|
|
316
|
-
* @param {string} key - Key of the item to delete
|
|
317
|
-
* @return {Promise<key, value>} - Resolves to the key and value of the deleted object
|
|
318
|
-
*/
|
|
319
|
-
remove (key) {
|
|
320
|
-
return this.get (key).then ((value) => {
|
|
321
|
-
return new Promise ((resolve, reject) => {
|
|
322
|
-
const transaction = this.storage.transaction (this.store, 'readwrite').objectStore (this.store);
|
|
323
|
-
const op = transaction.delete (key);
|
|
324
|
-
op.addEventListener ('success', () => {resolve (value);}, false);
|
|
325
|
-
op.addEventListener ('error', (event) => {reject (event);}, false);
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Clear the entire space
|
|
332
|
-
*
|
|
333
|
-
* @return {Promise} - Result of the clear operation
|
|
334
|
-
*/
|
|
335
|
-
clear () {
|
|
336
|
-
return this.open ().then (() => {
|
|
337
|
-
return new Promise ((resolve, reject) => {
|
|
338
|
-
const transaction = this.storage.transaction (this.store, 'readwrite').objectStore (this.store);
|
|
339
|
-
const op = transaction.clear ();
|
|
340
|
-
op.addEventListener ('success', () => {resolve ();}, false);
|
|
341
|
-
op.addEventListener ('error', (event) => {reject (event);}, false);
|
|
342
|
-
});
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
}
|