-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KvStorage.js
360 lines (329 loc) · 10 KB
/
KvStorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* KvStorage initialization options type definition
* @typedef {object} KvStorageOptions
* @property {boolean} [suppressErrors = false] - Should we suppress ALL errors
* @property {any} [universalFallback] - Default value of any "get-like" operation whose kv is uninitialized (eg $kv.get("key", "universalFallback"))
*/
/**
* KvStorage singleton class
*/
class KvStorage {
/**
* KvStorage constructor
* @param {KvStorageOptions} options KvStorage initialization options
*/
constructor(options = KvStorage.defaultOptions) {
this.NO_VALUE = 'KV_HAS_NOT_BEEN_INITIALIZED';
this.SUPPRESS_ERRORS = (options.hasOwnProperty('suppressErrors')) ? !!options.suppressErrors : !!KvStorage.defaultOptions.suppressErrors;
this.UNIVERSAL_FALLBACK = (options.hasOwnProperty('universalFallback')) ? options.universalFallback : 'UNIVERSAL_FALLBACK_NOT_PROVIDED';
/* The public release of KvStorage a Singleton, will update to Storage Buses when private release is confirmed stable */
if (KvStorage.singleton instanceof KvStorage) {
return KvStorage.singleton;
}
/* This should never happen... But, as per CB documentation, $room payload being unavailable in common should also never happen... So this is here just in case */
if (typeof $kv === 'undefined') {
if (this.SUPPRESS_ERRORS === false) {
throw new ReferenceError('$kv seems to be unavailable at the moment, please report this as a bug');
} else {
/* Our hacked together $kv fallback */
$kv = {
decr: function() { return 0 },
get: function () { return null },
incr: function() { return 0 },
remove: function () { return false },
set: function () { return false }
};
}
}
this.indexKey = 'kv-index'; /* $kv key that will hold our index / lookup table */
this.internalsKey = 'kv-internal' /* $kv key that will hold version-specific implementation data */
this.version = '1.4.1';
this.index = $kv.get(this.indexKey, this.NO_VALUE);
this.internals = $kv.get(this.internalsKey, this.NO_VALUE);
if (this.index === this.NO_VALUE) {
this.index = [ ];
$kv.set(this.indexKey, this.index);
}
if (this.internals === this.NO_VALUE) {
this.internals = {
purgePhrase: (Date.now()).toString(),
strictTyping: false,
version: this.version
};
$kv.set(this.internalsKey, this.internals);
}
/* This is where we update version-specific features */
if (this.internals.version !== this.version) {
if (this.internals.version.split('.')[1] < 2) {
if (this.internals.version.split('.')[2] < 3) {
this.internals.purgePhrase === (Date.now()).toString();
}
if (this.internals.version.split('.')[2] < 4) {
this.internals.strictTyping === false;
}
}
$kv.set(this.internalsKey, this.internals);
}
KvStorage.singleton = this;
}
/**
* Adds a key to our index
* @private
*/
addToIndex(key) {
if (this.index.includes(key) === true) {
return;
}
this.index.push(key);
$kv.set(this.indexKey, this.index);
}
/**
* Decreases the value of a key
* @param {any} key Target key, whose value will be decreased
* @param {number} quantity Amount to decrease value by
* @returns {number} Newly adjusted value
*/
decr(key, quantity = 1) {
return this.math('MINUS', key, quantity);
}
/**
* Divides a key by a factor - Beware of floating point precision and quirks in JavaScript
* @param {any} key Target key, whose value will divided
* @param {number} [factor = 2] quantity to divide by
* @returns {number} Newly adjusted value
*/
divi(key, factor = 2) {
return this.math('DIVIDE', key, factor);
}
/**
* Alias for {@link this.toggle}
* @param {any} key
*/
flip(key) {
this.toggle(key);
}
/**
* Get the value of a key. Providing a fallback is optional, but will throw an error if key doesn't exist and no fallback is provided
* @param {any} key Target key
* @param {any} [fallback] Default return value
* @returns {any} value of key or fallback if key doesn't exist
*/
get(key, fallback) {
this.addToIndex(key);
const value = $kv.get(key, this.NO_VALUE);
if (value === this.NO_VALUE) {
if (fallback === undefined) {
if (this.UNIVERSAL_FALLBACK !== 'UNIVERSAL_FALLBACK_NOT_PROVIDED') {
return this.UNIVERSAL_FALLBACK;
}
if (this.SUPPRESS_ERRORS === true) {
return null;
}
throw new TypeError(`attempted to get non-existent kv "${key}" with no fallback value`);
}
return fallback;
}
return value;
}
/**
* Retrieves all indexed $kv pairs and returns them as an object
* @returns {object} Object representing all indexed $kv pairs
*/
getAll() {
const returnObject = { };
const thisInstance = this;
this.index.forEach(function(key) {
const value = $kv.get(key, thisInstance.NO_VALUE);
if (value !== thisInstance.NO_VALUE) {
returnObject[key] = value;
}
});
return returnObject;
}
/**
* Increases the value of a key
* @param {any} key Target key, whose value will be increased
* @param {number} quantity Amount to increase value by
* @returns {number} Newly adjusted value
*/
incr(key, quantity = 1) {
return this.math('PLUS', key, quantity);
}
/**
* Retrieves a list of all indexed keys
* @returns {array} Array of indexed keys
*/
keys() {
return Array.from(this.index);
}
/**
* Update kv using Math operations
* @private
*/
math(operation, key, operand) {
const valid = !isNaN(operand);
let value = parseInt(this.get(key, 0), 10);
switch (operation.toString().toLowerCase()) {
case ('DIVIDE'): {
value = value / (valid ? operand : 1);
break;
};
case ('MINUS'): {
value = value - (valid ? operand : 0);
break;
};
case ('MULTIPLY'): {
value = value * (valid ? operand : 2);
break;
};
case ('PLUS'): {
value = value + (valid ? operand : 0);
break;
};
case ('POWER'): {
value = value ** (valid ? operand : 2);
break;
}
default: {
value = NaN;
}
}
this.set(key, value);
return value;
}
/**
* Multiplies a key by a factor
* @param {any} key Target key, whose value will multiplied
* @param {number} [factor = 2] quantity to multiply by
* @returns {number} Newly adjusted value
*/
mult(key, factor = 2) {
return this.math('MULTIPLY', key, factor);
}
/**
* Raises a key to a power
* @param {any} key Target key, whose value will be raised
* @param {number} [power = 2] exponent to raise
* @returns {number} Newly adjusted value
*/
pow(key, power = 2) {
return this.math('POWER', key, power);
}
/**
* Remove all indexed keys when provided with correct secretPhrase
* @param {string} secretPhrase string to match internally confirming removal of all indexed keys
* @returns {string | boolean} If secretPhrase is correct, returns a boolean indicating whether all keys were successfully removed, otherwise returns secret phrase
*/
purge(secretPhrase) {
const purgePhrase = this.internals.purgePhrase;
if (secretPhrase === purgePhrase) {
let allSuccessful = true;
const thisInstance = this;
this.index.forEach(function(key) {
const success = thisInstance.remove(key);
if (success === false) {
allSuccessful = false;
}
});
$kv.set(this.indexKey, [ ]);
return allSuccessful;
}
return purgePhrase;
}
/**
* Push value into key that is an array
* @param {any} key Target key that is an array
* @param {any} value Value to push into array
* @returns {boolean} Indicating if operation was successful
*/
push(key, value) {
if (key === undefined) {
return false;
}
let array = this.get(key, [ ]);
if (Array.isArray(array) === false) {
array = [array];
}
array.push(value);
return this.set(key, array);
}
/**
* Removes keys stored in $kv
* @param {...any} keys keys to remove
* @returns {boolean} Indicating if operation was successful
*/
remove(...keys) {
if (keys.length === 0) {
return false;
}
const thisInstance = this;
keys.forEach(function(key) {
thisInstance.removeFromIndex(key);
$kv.remove(key);
});
return true;
}
/**
* Removes a key from our index
* @private
*/
removeFromIndex(key) {
if (key === undefined) {
return false;
}
const indexIndex = this.index.indexOf(key);
if (indexIndex < 0) {
return false;
}
this.index.splice(indexIndex, 1);
$kv.set(this.indexKey, this.index);
return true;
}
/**
* Sets the value of given key
* @param {any} key Target key
* @param {any} value New value of key
* @returns {boolean} Indicating if operation was successful
*/
set(key, value) {
this.addToIndex(key);
return $kv.set(key, value)
}
/**
* Toggles the value of a boolean, forcefully coercing non-boolean values
* @param {any} key Target key to invert
*/
toggle(key) {
this.addToIndex(key);
const currentValue = !!this.get(key, true);
this.set(key, !currentValue);
}
/**
* Remove value from key that is an array. Only removes the last index of that value
* @param {any} key Target key that is an array
* @param {any} value Value to remove array
* @returns {boolean} Indicating if operation was successful
*/
unpush(key, value) {
if (key === undefined) {
return false;
}
let array = this.get(key, []);
if (Array.isArray(array) === false) {
array = [array];
}
const target = array.lastIndexOf(value);
if (target < 0) {
return false;
}
array.splice(target, 1);
return this.set(key, array);
}
}
KvStorage.singleton = null;
KvStorage.defaultOptions = Object.freeze({
suppressErrors: false
});
const _kv = new KvStorage({
suppressErrors: false
});