-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathidb-iegap.js
1162 lines (1087 loc) · 53.6 KB
/
idb-iegap.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if (navigator.userAgent.indexOf("Trident/") !== -1) (function (idb, undefined) {
/* IndexedDB IE Gap polyfill (idb-iegap.js)
*
* VERSION: $Format:%d$
*
* Gaps if IE10 and IE11:
* * The lack of support for compound indexes
* * The lack of support for compound primary keys
* * The lack of support for multiEntry indexes
* * Always returning false from property IDBObjectStore.autoIncrement
*
*
* Where to inject?
*
* Everything that is implemented is marked with a "V" below:
*
* V IDBObjectStore.createIndex(name, [keyPath1, keypath2], { unique: true/false, multiEntry: true });
* What to do?
* 1) If keyPath is an array, create a new table ($iegap-<table>-<indexName> with autoinc, key "key" (unique?) and value "primKey" of this table
* If multiEntry is true, create a new table ($iegap-<table>-<indexName> with autoinc, key "key" and value "primKey" of this table.
* 2) Dont create the real index but store the index in localStorage key ("$iegap-<table>")
* { indexes: [{name: "", keyPath, ...
* V IDBObjectStore.deleteIndex()
* V IDBObjectStore.index("name")
* V If the name corresponds to a special index, return a fake IDBIndex with its own version of openCursor()
* V IDBObjectStore.add():
* V If we have compound indexes, make sure to also add an item in its table if add was successful. Return a fake request that resolves when both requests are resolved
V Ignore but log error events occurring when adding index keys
* V If we have multiEntry indexes, make sure to also interpret the array and add all its items. Same rule as above.
* V IDBObjectStore.put():
* V First delete all indexed-items bound to this primary key, then do the add.
* V IDBObjectStore.delete(): Delete all indexed items bound to this primary key.
* V IDBObjectStore.clear(): Clear all meta stores bound to this object store.
* V IDBKeyRange.*: Allow arrays and store them in range.compound value.
* V IEGAPIndex.openKeyCursor():
* V compound: Iterate the own table, use a fake cursor object to point out correct primary key
* V IEGAPIndex.openCursor():
* V compound: Iterate the own table, use a fake cursor object to point out correct primary key and value
* V IEGapCursor.delete(): delete the object itself along with the index objects pointing to it. HOW WILL IE REACT WHEN SAWING OF ITS OWN BRANCH IN AN ITERATION. We might have to restart the query with another upperBound/lowerBound request.
* V IEGapCursor.update(): Do a put() on the object itself. WHAT HAPPENS IF PUT/DELETE results in deleting next-coming iterations? We might have to restart the quer with another upperBound/lowerBound request.
* V Support nextunique and prevunique by just using it on the index store.
* V IDBDatabase.transaction(): Make sure to include all meta-tables for included object stores.
* V IDBDatabase.deleteObjectStore(): Delete the meta-indexe object stores and update the meta-table.
* V Detect IE10/IE11.
*
* Over-course:
* V IDBObjectStore.indexNames: Return the compound result of real indexNames and the ones in $iegap-<table>-indexes
* V IDBDatabase.objectStoreNames: Filter away those names that contain metadata
* V indexedDB.open(): extend the returned request and override onupgradeneeded so that main meta-table is created
* V " onsuccess so that the main meta-table is read into a var stored onto db.
* V IDBTransaction.objectStore(): Populate the "autoIncrement" property onto returned objectStore. Need to have that stored if so.
* V readyState in IEGAPRequest
*/
function extend(obj, extension) {
if (typeof extension !== 'object') extension = extension(); // Allow to supply a function returning the extension. Useful for simplifying private scopes.
Object.keys(extension).forEach(function(key) {
obj[key] = extension[key];
});
return obj;
}
function derive(Child) {
return {
from: function(Parent) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
return {
extend: function(extension) {
extend(Child.prototype, typeof extension !== 'object' ? extension(Parent.prototype) : extension);
}
};
}
};
}
function override(orig, overrider) {
if (typeof orig === 'object')
// map of properties to override
Object.keys(overrider).forEach(function (prop) {
var pd = Object.getOwnPropertyDescriptor(orig, prop);
var newPd = overrider[prop](pd);
if (newPd.hasOwnProperty('value') && newPd.writable !== false) newPd.writable = true;
Object.defineProperty(orig, prop, extend({ configurable: true, enumerable: true }, newPd));
});
else
// simple function
return overrider(orig);
}
function getByKeyPath(obj, keyPath) {
// http://www.w3.org/TR/IndexedDB/#steps-for-extracting-a-key-from-a-value-using-a-key-path
if (obj.hasOwnProperty(keyPath)) return obj[keyPath]; // This line is moved from last to first for optimization purpose.
if (!keyPath) return obj;
if (typeof keyPath !== 'string') {
var rv = [];
for (var i = 0, l = keyPath.length; i < l; ++i) {
var val = getByKeyPath(obj, keyPath[i]);
rv.push(val);
}
return rv;
}
var period = keyPath.indexOf('.');
if (period !== -1) {
var innerObj = obj[keyPath.substr(0, period)];
return innerObj === undefined ? undefined : getByKeyPath(innerObj, keyPath.substr(period + 1));
}
return undefined;
}
function setByKeyPath(obj, keyPath, value) {
if (!obj || keyPath === undefined) return;
if (Array.isArray(keyPath)) {
for (var i = 0, l = keyPath.length; i < l; ++i) {
setByKeyPath(obj, keyPath[i], value[i]);
}
} else {
var period = keyPath.indexOf('.');
if (period !== -1) {
var currentKeyPath = keyPath.substr(0, period);
var remainingKeyPath = keyPath.substr(period + 1);
if (remainingKeyPath === "")
if (value === undefined) delete obj[currentKeyPath]; else obj[currentKeyPath] = value;
else {
var innerObj = obj[currentKeyPath];
if (!innerObj) innerObj = (obj[currentKeyPath] = {});
setByKeyPath(innerObj, remainingKeyPath, value);
}
} else {
if (value === undefined) delete obj[keyPath]; else obj[keyPath] = value;
}
}
}
function delByKeyPath(obj, keyPath) {
setByKeyPath(obj, keyPath, undefined);
}
function ignore(op, cb) {
return function (ev) {
console.log("Warning: IEGap polyfill failed to " + (op.call ? op() : op) + ": " + ev.target.error);
ev.stopPropagation();
ev.preventDefault();
cb();
return false;
}
}
function addCompoundIndexKey(idxStore, indexSpec, value, primKey, rollbacks, onfinally) {
/// <param name="idxStore" type="IDBObjectStore">The object store for meta-indexes</param>
try {
var idxKeys = getByKeyPath(value, indexSpec.keyPath);
if (idxKeys === undefined) return onfinally(); // no key to add index for
var req = idxStore.add({ fk: primKey, k: compoundToString(idxKeys) });
req.onerror = ignore("add compound index", onfinally);
req.onsuccess = function (ev) {
if (rollbacks) rollbacks.push({store: idxStore, del: true, pk: ev.target.result});
onfinally();
};
} catch (ex) {
console.log("IEGap polyfill exception when adding compound index key");
onfinally();
}
}
function addMultiEntryIndexKeys(idxStore, indexSpec, value, primKey, rollbacks, onfinally) {
/// <param name="idxStore" type="IDBObjectStore">The object store for meta-indexes</param>
try {
var idxKeys = getByKeyPath(value, indexSpec.keyPath);
if (idxKeys === undefined) return onfinally(); // no key to add index for.
if (!Array.isArray(idxKeys)) {
// the result of evaluating the index's key path doesn't yield an Array
var req = idxStore.add({ fk: primKey, k: idxKeys });
req.onerror = ignore("add index", onfinally);
req.onsuccess = function(ev) {
if (rollbacks) rollbacks.push({store: idxStore, del: true, pk: ev.target.result});
onfinally();
}
} else {
// the result of evaluating the index's key path yields an Array
idxKeys.forEach(function(idxKey) {
var req2 = idxStore.add({ fk: primKey, k: idxKey });
req2.onerror = ignore(function() { return "add multiEntry index " + idxKey + " for " + indexSpec.storeName + "." + indexSpec.keyPath ; }, checkComplete);
req2.onsuccess = function(ev) {
if (rollbacks) rollbacks.push({ store: idxStore, del: true, pk: ev.target.result });
checkComplete();
}
});
var nRequests = idxKeys.length;
function checkComplete() {
if (--nRequests === 0) onfinally();
}
}
} catch (ex) {
console.log("IEGap polyfill exception when adding multientry key");
}
}
function bulkDelete(index, key, onfinally) {
/// <param name="index" type="IDBIndex"></param>
/// <param name="key"></param>
var cursorReq = index.openKeyCursor(key);
var primKeys = [];
cursorReq.onerror = ignore("list indexed references", onfinally);
cursorReq.onsuccess = function (ev) {
var cursor = cursorReq.result;
if (!cursor) return doDelete();
primKeys.push(cursor.primaryKey);
cursor.continue();
}
function doDelete() {
var store = index.objectStore;
primKeys.forEach(function (primKey) {
var req = store.delete(primKey);
req.onerror = ignore("delete meta index", checkComplete);
req.onsuccess = checkComplete;
});
var nRequests = primKeys.length;
if (nRequests === 0) onfinally();
function checkComplete() {
if (--nRequests === 0) onfinally();
}
}
}
function bulk(operations, cb, log) {
/// <summary>
/// Execute given array of operations and the call given callback
/// </summary>
/// <param name="operations" value="[{store: IDBObjectStore.prototype, del:false, pk: null, obj: null}]">Operations to execute</param>
/// <param name="cb" type="Function"></param>
var nRequests = operations.length;
operations.forEach(function (item) {
var req = (item.del ? item.store.delete(item.pk) : (item.pk ? item.store.add (item.obj, item.pk) : item.store.add (item.obj)));
req.onerror = ignore(log || "executing bulk", checkComplete);
req.onsuccess = checkComplete;
});
function checkComplete() {
if (--nRequests === 0 && cb) cb();
}
}
//
// Constants and imports
//
var POWTABLE = {};
var IDBKeyRange = window.IDBKeyRange,
IDBObjectStore = window.IDBObjectStore,
IDBDatabase = window.IDBDatabase;
function initPowTable() {
for (var i = 4; i >= -4; --i) {
POWTABLE[i] = Math.pow(32768, i);
}
}
function unipack(number, intChars, floatChars) {
/// <summary>
/// Represent the number as a unicode string keeping the sort
/// order of the Number instance intact when comparing the
/// resulting string.
/// </summary>
/// <param name="number" type="Number">Number to represent as sort-order kept unicode string</param>
/// <param name="intChars" type="Number">Number of unicode chars that should represent the integer part of given Number.
/// Each unicode char will hold 15 bits (16 - 1, since 0000 is an unusable char making us lose one bit of info per char.</param>
/// <param name="floatChars" type="Number">Number of unicode chars that should represent the floating point decimals of
/// given Number instance. Each char will hold 15 bits.</param>
var currPow = intChars - 1,
xor = number < 0 ? 32767 : 0,
unsignedNumber = number < 0 ? -number : number,
rv = "";
while (currPow >= -floatChars) {
var val = ((unsignedNumber / POWTABLE[currPow]) & 32767) ^ xor;
var charCode = (val << 1) | 1;
rv += String.fromCharCode(charCode);
--currPow;
}
return rv;
}
function uniback(unicode, intChars, floatChars, negate) {
/// <summary>
///
/// </summary>
/// <param name="unicode" type="String"></param>
/// <param name="intChars"></param>
/// <param name="floatChars"></param>
var rv = 0,
currPow = intChars - 1,
l = unicode.length;
if (intChars + floatChars != l) return undefined;
for (var i = 0; i < l; ++i) {
var val = ((unicode.charCodeAt(i) - 1) >> 1);
if (negate)
rv -= POWTABLE[currPow] * (val ^ 32767);
else
rv += POWTABLE[currPow] * val;
--currPow;
}
return rv;
}
function compoundToString(a) {
/// <param name="a" type="Array"></param>
var l = a.length,
rv = new Array(l);
for (var i = 0; i < l; ++i) {
var part = a[i];
if (part instanceof Date)
rv[i] = "D" + unipack(part.getTime(), 4, 0);
else if (typeof part === 'string')
rv[i] = "S" + part;
else if (isNaN(part))
if (part)
rv[i] = "J" + JSON.stringify(part);
else if (typeof part === 'undefined')
rv[i] = "u" // undefined
else
rv[i] = "0" // null
else
rv[i] = (part < 0 ? "N" : "n") + unipack(part, 5, 4);
}
return JSON.stringify(rv);
}
function stringToCompound(s) {
var a = JSON.parse(s),
l = a.length,
rv = new Array(l);
for (var i = 0; i < l; ++i) {
var item = a[i];
var type = item[0];
var encoded = item.substr(1);
var value = undefined;
if (type === "D")
value = new Date(uniback(encoded, 4, 0));
else if (type === "J")
value = JSON.parse(encoded);
else if (type === "S")
value = encoded;
else if (type === "N")
value = uniback(encoded, 5, 4, true);
else if (type === "n")
value = uniback(encoded, 5, 4, false);
else if (type === "u")
value = undefined;
else if (type === "0")
value = null
rv[i] = value;
}
return rv;
}
function getMeta(db) {
/// <param name="db" type="IDBDatabase"></param>
/// <returns value="{stores: {storeName: {metaStores: [], indexes: {indexName: {name:'',keyPath:null,multiEntry:false,unique:false,compound:false,idxStoreName:'',storeName:''}}, compound: false, keyPath: ['a','b.c']}}}"></returns>
return db._iegapmeta;
}
function setMeta(db, transaction, value) {
/// <param name="db" type="IDBDatabase"></param>
/// <param name="transaction" type="IDBTransaction"></param>
db._iegapmeta = value;
transaction.objectStore('$iegapmeta').put(value, 1);
}
function parseKeyParam(key) {
if (Array.isArray(key)) return compoundToString(key);
if (key instanceof IEGAPKeyRange) {
var upper = Array.isArray(key.upper) ? compoundToString(key.upper) : key.upper,
lower = Array.isArray(key.lower) ? compoundToString(key.lower) : key.lower;
if (key.lower === null)
return IDBKeyRange.upperBound(upper, key.upperOpen);
if (key.upper === null)
return IDBKeyRange.lowerBound(lower, key.lowerOpen);
return IDBKeyRange.bound(
lower,
upper,
!!key.lowerOpen,
!!key.upperOpen);
}
return key;
}
function IEGAPIndex(idbIndex, idbStore, name, keyPath, multiEntry) {
this._idx = idbIndex;
this._store = idbStore;
this._compound = Array.isArray(keyPath);
this._multiEntry = multiEntry;
this.keyPath = keyPath;
this.name = name;
this.objectStore = idbStore;
this.unique = idbIndex.unique;
}
derive(IEGAPIndex).from(Object).extend(function() {
function openCursor(iegIndex, range, dir, includeValue) {
/// <param name="iegIndex" type="IEGAPIndex"></param>
/// <param name="range" type="IDBKeyRange"></param>
/// <param name="dir" type="String"></param>
return new IEGAPRequest(iegIndex, iegIndex.objectStore.transaction, function (success, error) {
var compound = iegIndex._compound,
compoundPrimKey = Array.isArray(iegIndex.objectStore.keyPath);
if (compound && Array.isArray(range)) range = new IEGAPKeyRange(range, range);
var idbRange = compound && range ?
IDBKeyRange.bound(compoundToString(range.lower), compoundToString(range.upper), range.lowerOpen, range.upperOpen) :
range;
if (typeof idbRange === 'undefined') idbRange = null;
var req = iegIndex._idx.openCursor(idbRange, dir);
req.onerror = error;
if (includeValue) {
req.onsuccess = function(ev) {
var cursor = ev.target.result;
if (cursor) {
var getreq = iegIndex._store.get(cursor.value.fk);
getreq.onerror = error;
getreq.onsuccess = function () {
if (!getreq.result) return cursor.continue(); // An index is about to be deleted but it hasnt happened yet.
var key = compound ? stringToCompound(cursor.key) : cursor.key;
var primKey = compoundPrimKey ? stringToCompound(cursor.value.fk) : cursor.value.fk;
success(ev, new IEGAPCursor(cursor, iegIndex, iegIndex.objectStore, primKey, key, getreq.result));
}
} else {
success(ev, null);
}
}
} else {
req.onsuccess = function(ev) {
var cursor = ev.target.result;
var key = compound ? stringToCompound(cursor.key) : cursor.key;
var primKey = compoundPrimKey ? stringToCompound(cursor.value.fk) : cursor.value.fk;
success(ev, cursor && new IEGAPCursor(cursor, iegIndex, iegIndex.objectStore, primKey, key));
}
}
});
}
return {
count: function (key) {
if (arguments.length > 0) arguments[0] = parseKeyParam(key);
return this._idx.count.apply(this._idx, arguments);
},
get: function(key) {
var thiz = this;
var req = this._idx.get(parseKeyParam(key));
return new IEGAPRequest(this, this.objectStore.transaction, function(success, error) {
// First request the meta-store for this index
req.onsuccess = function(ev) {
// Check if key was found.
var foreignKey = req.result && req.result.fk;
if (foreignKey) {
// Key found. Do a new request on the origin store based on the foreignKey found in meta-store.
req = thiz.objectStore.get(foreignKey);
req.onsuccess = function () {
success(ev, req.result);
}
req.onerror = error;
} else {
// Key not found. Just forward the undefined-found index.
success(ev);
}
}
req.onerror = error;
});
},
getKey: function(key) {
var thiz = this;
var req = this._idx.get(parseKeyParam(key));
return new IEGAPRequest(this, this.objectStore.transaction, function (success, error) {
req.onsuccess = function (ev) {
var res = ev.target.result;
success(ev, res && res.fk);
}
req.onerror = error;
});
},
openKeyCursor: function(range, dir) {
return openCursor(this, range, dir);
},
openCursor: function(range, dir) {
return openCursor(this, range, dir, true);
}
};
});
function IEGAPCursor(idbCursor, source, store, primaryKey, key, value) {
this._cursor = idbCursor;
this._store = store;
this.direction = idbCursor.direction;
this.key = key;
this.primaryKey = primaryKey;
this.source = source;
if (arguments.length >= 6) this.value = value;
}
extend(IEGAPCursor.prototype, function() {
return {
advance: function(n) {
this._cursor.advance(n);
},
"continue": function(key) {
/// <param name="key" optional="true"></param>
if (!key) return this._cursor.continue();
if (Array.isArray(key)) return this._cursor.continue(compoundToString(key));
return this._cursor.continue(key);
},
"delete": function () {
return this._store.delete(this.primaryKey);// Will automatically delete and iegap index items as well.
// req.target will be the object store and not the cursor. Let it be so for now.
// TODO: Låtsas som det regnar och fortsätt här. Testa sedan vad som händer om man deletar
// ett objekt som resulterar i att en massa multiValue indexes deletas och därmed att
// cursor.continue falierar eller beter sig märkligt. Kanske är det upp till implementatören att vänta på delete() requestet eller?
// Hur beter sig IDB i detta läge (de som har stöd för multiValue index)?
},
update: function(newValue) {
// Samma eventuella problem här som med delete(). Frågan är var ansvaret ligger för att inte fortsätta jobba med
// en collection som håller på att manipuleras. API usern eller IDB?
return this._store.keyPath ? this._store.put(newValue) : this._store.put(newValue, this.primaryKey);
}
}
});
function IEGAPEventTarget() {
this._el = {};
}
extend(IEGAPEventTarget.prototype, function() {
return {
addEventListener: function (type, listener) {
this._el[type] ? this._el[type].push(listener) : this._el[type] = [listener];
},
removeEventListener: function (type, listener) {
var listeners = this._el[type];
if (listeners) {
var pos = listeners.indexOf(listener);
if (pos !== -1) listeners.splice(pos, 1);
}
},
dispatchEvent: function (event) {
var listener = this["on" + event.type];
if (listener && listener(event) === false) return false;
var listeners = this._el[event.type];
if (listeners) {
for (var i = 0, l = listeners.length; i < l; ++i) {
listener = listeners[i];
if ((listener.handleEvent || listener)(event) === false) return false;
}
return true;
}
}
}
});
//
// IEGAP version of IDBRequest
//
function IEGAPRequest(source, transaction, deferred) {
this._el = {};
this.source = source;
this.transaction = transaction;
this.readyState = "pending";
var thiz = this;
var eventTargetProp = { get: function () { return thiz; } };
deferred(function (e, result) {
thiz.result = result;
Object.defineProperty(e, "target", eventTargetProp);
thiz.readyState = "done";
thiz.dispatchEvent(e);
}, function (e, err) {
thiz.error = err || e.target.error;
Object.defineProperty(e, "target", eventTargetProp);
thiz.readyState = "done";
if (e.type != "error") {
Object.defineProperty(e, "type", { get: function() { return "error"; } });
}
thiz.dispatchEvent(e);
}, this);
}
derive(IEGAPRequest).from(IEGAPEventTarget).extend({
onsuccess: null,
onerror: null,
});
//
// IDBOpenRequest
//
function IEGAPOpenRequest() {
IEGAPRequest.apply(this, arguments);
}
derive(IEGAPOpenRequest).from(IEGAPRequest).extend({
onblocked: null,
onupgradeneeded: null
});
//
// Our IDBKeyRange
//
function IEGAPKeyRange(lower, upper, lowerOpen, upperOpen) {
this.lower = lower;
this.upper = upper;
this.lowerOpen = lowerOpen;
this.upperOpen = upperOpen;
}
//
// Our DOMStringList
//
function IEGAPStringList(a) {
Object.defineProperties(a, {
contains: {
configurable: true, writable: true, value: function (str) {
return a.indexOf(str) !== -1;
}
},
item: {
configurable: true, writable: true, value: function (index) {
return a[index];
}
}
});
return a;
}
function Constructor() {
var getObjectStoreNames = Object.getOwnPropertyDescriptor(IDBDatabase.prototype, "objectStoreNames").get;
//var getIndexNames = Object.getOwnPropertyDescriptor(IDBObjectStore.prototype, "indexNames").get;
var deleteObjectStore = IDBDatabase.prototype.deleteObjectStore;
var createObjectStore = IDBDatabase.prototype.createObjectStore;
initPowTable();
//
// Inject into onupgradeneeded and onsuccess in indexedDB.open()
//
idb.open = override(idb.open, function(orig) {
return function (name, version) {
var req = orig.apply(this, arguments);
return new IEGAPOpenRequest(this, null, function (success, error, iegReq) {
req.onerror = error;
req.onblocked = function(ev) { iegReq.dispatchEvent(ev); };
req.onupgradeneeded = function (ev) {
iegReq.transaction = req.transaction;
var db = (iegReq.result = req.result);
db._upgradeTransaction = req.transaction; // Needed in IDBDatabase.prototype.deleteObjectStore(). Save to set like this because during upgrade transaction, no other transactions may live concurrently.
db._iegapmeta = { stores: {} };
if (!getObjectStoreNames.apply(db).contains("$iegapmeta")) {
var store = createObjectStore.call(db, "$iegapmeta");
store.add(db._iegapmeta, 1);
}
ev.target = ev.currentTarget = iegReq;
iegReq.dispatchEvent(ev);
}
req.onsuccess = function(ev) {
var db = req.result;
delete db._upgradeTransaction;
db._iegapmeta = { stores: {} }; // Until we have loaded the correct value, we need db.transaction() to work.
try {
var trans = db.transaction(["$iegapmeta"], 'readonly');
var req2 = trans.objectStore("$iegapmeta").get(1);
req2.onerror = error;
req2.onsuccess = function() {
db._iegapmeta = req2.result;
success(ev, db);
}
} catch (e) {
error(ev, e);
}
}
});
}
});
//
// Inject into window.IDBKeyRange
//
IDBKeyRange.bound = override(IDBKeyRange.bound, function(orig) {
return function bound(lower, upper, lopen, oopen) {
if (!Array.isArray(lower)) return orig.apply(this, arguments);
return new IEGAPKeyRange(lower, upper, lopen, oopen);
}
});
IDBKeyRange.lowerBound = override(IDBKeyRange.lowerBound, function(orig) {
return function lowerBound(bound, open) {
if (!Array.isArray(bound)) return orig.apply(this, arguments);
return new IEGAPKeyRange(bound, null, open, null);
}
});
IDBKeyRange.upperBound = override(IDBKeyRange.upperBound, function(orig) {
return function upperBound(bound, open) {
if (!Array.isArray(bound)) return orig.apply(this, arguments);
return new IEGAPKeyRange(null, bound, null, open);
}
});
IDBKeyRange.only = override(IDBKeyRange.only, function(orig) {
return function only(val) {
if (!Array.isArray(val)) return orig.apply(this, arguments);
return new IEGAPKeyRange(val, val);
}
});
//
// Inject into window.IDBObjectStore
//
IDBObjectStore.prototype.count = override(IDBObjectStore.prototype.count, function(orig) {
return function (key) {
if (arguments.length > 0) arguments[0] = parseKeyParam(key);
return orig.apply(this, arguments);
}
});
IDBObjectStore.prototype.get = override(IDBObjectStore.prototype.get, function(orig) {
return function(key) {
if (arguments.length > 0) arguments[0] = parseKeyParam(key);
return orig.apply(this, arguments);
}
});
IDBObjectStore.prototype.openCursor = override(IDBObjectStore.prototype.openCursor, function(orig) {
return function (range, dir) {
/// <param name="range" type="IDBKeyRange"></param>
/// <param name="dir" type="String"></param>
var meta = this.transaction.db._iegapmeta.stores[this.name];
if (!meta) return orig.apply(this, arguments);
if (Array.isArray(range)) range = new IEGAPKeyRange(range, range);
var compound = meta.compound;
if (compound && range && !(range instanceof IEGAPKeyRange)) throw new RangeError("Primary key is compound but given range is not.");
var idbRange = compound && range ?
IDBKeyRange.bound(compoundToString(range.lower), compoundToString(range.upper), range.lowerOpen, range.upperOpen) :
range;
arguments[0] = idbRange;
var req = orig.apply(this, arguments);
var store = this;
return new IEGAPRequest(this, this.transaction, function(success, error) {
req.onerror = error;
req.onsuccess = function (ev) {
var cursor = ev.target.result;
if (cursor) {
var key = compound ? stringToCompound(cursor.key) : cursor.key;
var iegapCursor = new IEGAPCursor(cursor, store, store, key, key, cursor.value);
success(ev, iegapCursor);
} else {
success(ev, null);
}
}
});
}
});
IDBObjectStore.prototype.createIndex = override(IDBObjectStore.prototype.createIndex, function (origFunc) {
function createIndex(store, name, keyPath, props) {
/// <summary>
///
/// </summary>
/// <param name="store" type="IDBObjectStore"></param>
/// <param name="name"></param>
/// <param name="keyPath"></param>
/// <param name="props" value="{unique: true, multiEntry: true}"></param>
var db = store.transaction.db;
var idxStoreName = "$iegap-" + store.name + "-" + name;
var meta = db._iegapmeta;
if (props.multiEntry && Array.isArray(keyPath)) {
// IDB spec require us to throw DOMException.INVALID_ACCESS_ERR
createObjectStore.call(db, "dummy", { keyPath: "", autoIncrement: true }); // Will throw DOMException.INVALID_ACCESS_ERR
throw "invalid access"; // fallback.
}
var idxStore = createObjectStore.call(db, idxStoreName, { autoIncrement: true });
var storeMeta = meta.stores[store.name] || (meta.stores[store.name] = {indexes: {}, metaStores: [] });
var indexMeta = {
name: name,
keyPath: keyPath,
multiEntry: props.multiEntry || false,
unique: props.unique || false,
compound: Array.isArray(keyPath),
storeName: store.name,
idxStoreName: idxStoreName
};
storeMeta.indexes[name] = indexMeta;
storeMeta.metaStores.push(idxStoreName);
idxStore.createIndex("fk", "fk", { unique: false });
var keyIndex = idxStore.createIndex("k", "k", { unique: props.unique || false });
setMeta(db, store.transaction, meta);
// Reindexing existing data:
if (!store._reindexing) {
store._reindexing = true;
store.openCursor().onsuccess = function (e) {
delete store._reindexing;
var cursor = e.target.result;
if (cursor) {
cursor.update(cursor.value); // Will call out version of IDBObjectStore.put() that will re-index all items!
cursor.continue();
}
}
}
return new IEGAPIndex(keyIndex, store, name, keyPath, props.multiEntry);
}
return function (name, keyPath, props) {
if (Array.isArray(keyPath) || (props && props.multiEntry))
return createIndex(this, name, keyPath, props || {});
return origFunc.apply(this, arguments);
}
});
IDBObjectStore.prototype.deleteIndex = override(IDBObjectStore.prototype.deleteIndex, function(origFunc) {
return function(name) {
var db = this.transaction.db;
var meta = db._iegapmeta;
var storeMeta = meta.stores[this.name];
if (!storeMeta) return origFunc.apply(this, arguments);
var indexMeta = storeMeta.indexes[name];
if (!indexMeta) return origFunc.apply(this, arguments);
deleteObjectStore.call(db, indexMeta.idxStoreName);
delete storeMeta.indexes[name];
setMeta(db, this.transaction, meta);
}
});
IDBObjectStore.prototype.index = override(IDBObjectStore.prototype.index, function(origFunc) {
return function (indexName) {
var meta = this.transaction.db._iegapmeta.stores[this.name];
if (!meta) return origFunc.apply(this, arguments);
var idx = meta.indexes[indexName];
return idx ?
new IEGAPIndex(this.transaction.objectStore(idx.idxStoreName).index("k"), this, idx.name, idx.keyPath, idx.multiEntry) :
origFunc.apply(this, arguments);
};
});
IDBObjectStore.prototype.add = override(IDBObjectStore.prototype.add, function (origFunc) {
return function(value, key) {
var meta = this.transaction.db._iegapmeta.stores[this.name];
if (!meta) return origFunc.apply(this, arguments);
var addReq;
if (meta.compound) {
// Compound primary key
// Key must not be provided when having inbound keys (compound is inbound)
if (key) return this.add(null); // Trigger DOMException(DataError)!
key = compoundToString(getByKeyPath(value, meta.keyPath));
addReq = origFunc.call(this, value, key);
} else {
addReq = origFunc.apply(this, arguments);
}
var indexes = Object.keys(meta.indexes);
if (!meta.compound && indexes.length === 0) return addReq; // No indexes to deal with
var store = this;
return new IEGAPRequest(this, this.transaction, function(success, error) {
var addEvent = null, errorEvent = null, indexAddFinished = false, rollbacks = [];
var primKey = key || (store.keyPath && getByKeyPath(value, store.keyPath));
if (!primKey) {
addReq.onerror = error;
addReq.onsuccess = function(ev) {
addEvent = ev;
primKey = addReq.result;
addIndexKeys();
}
} else {
// Caller provided primKey - we can start adding indexes at once! No need waiting for onsuccess. However, this means we may need to rollback our stuff...
// So, why do it in such a complex way? Because otherwise we fail on a W3C web-platform-test where an item is added and then expected its index to be there the line after.
addIndexKeys();
addReq.onerror = function(ev) {
errorEvent = ev;
ev.preventDefault(); // Dont abort transaction quite yet. First roll-back our added indexes, then when done, call the real error eventhandler and check if it wants to preventDefault or not.
checkFinally();
}
addReq.onsuccess = function(ev) {
addEvent = ev;
checkFinally();
}
}
function checkFinally() {
if (indexAddFinished && (addEvent || errorEvent))
if (errorEvent) {
var defaultPrevented = false;
errorEvent.preventDefault = function () { defaultPrevented = true; };
error(errorEvent);
if (!defaultPrevented) store.transaction.abort(); // We prevented default in the first place. Now we must manually abort when having called the event handler
} else
success(addEvent, meta.compound ? stringToCompound(addReq.result) : addReq.result);
}
function addIndexKeys() {
var nRequests = indexes.length;
indexes.forEach(function (indexName) {
var indexSpec = meta.indexes[indexName];
var idxStore = store.transaction.objectStore(indexSpec.idxStoreName);
if (indexSpec.multiEntry) {
addMultiEntryIndexKeys(idxStore, indexSpec, value, primKey, rollbacks, checkComplete);
} else if (indexSpec.compound) {
addCompoundIndexKey(idxStore, indexSpec, value, primKey, rollbacks, checkComplete);
} else {
throw "IEGap assert error";
}
});
function checkComplete() {
if (--nRequests === 0) {
if (!errorEvent) {
indexAddFinished = true;
checkFinally();
} else {
bulk(rollbacks, function() {
indexAddFinished = true;
checkFinally();
}, "rolling back index additions");
}
}
}
}
});
}
});
IDBObjectStore.prototype.put = override(IDBObjectStore.prototype.put, function (origFunc) {
return function (value, key) {
var meta = this.transaction.db._iegapmeta.stores[this.name];
if (!meta) return origFunc.apply(this, arguments);
var putReq;
if (meta.compound) {
// Compound primary key
// Key must not be provided when having inbound keys (compound is inbound)
if (key) return this.add(null); // Trigger DOMException(DataError)!
key = compoundToString(getByKeyPath(value, meta.keyPath));
putReq = origFunc.call(this, value, key);
} else {
putReq = origFunc.apply(this, arguments);
}
var indexes = Object.keys(meta.indexes);
if (!meta.compound && indexes.length === 0) return putReq;
var store = this;
return new IEGAPRequest(this, this.transaction, function (success, error) {
var putEvent = null;
var primKey;
putReq.onerror = error;
putReq.onsuccess = function (ev) {
putEvent = ev;
primKey = putReq.result;
replaceIndexKeys();
}
function replaceIndexKeys() {
var nRequests = indexes.length;
indexes.forEach(function(indexName) {
var indexSpec = meta.indexes[indexName];
var idxStore = store.transaction.objectStore(indexSpec.idxStoreName);
bulkDelete(idxStore.index("fk"), primKey, function () {
// then, when deleted, add entries:
if (indexSpec.multiEntry) {
addMultiEntryIndexKeys(idxStore, indexSpec, value, primKey, null, checkComplete);
} else if (indexSpec.compound) {
addCompoundIndexKey(idxStore, indexSpec, value, primKey, null, checkComplete);
} else {
checkComplete();
throw "IEGap assert error";
}
});
});
function checkComplete() {
if (--nRequests === 0) success(putEvent, meta.compound ? stringToCompound(primKey) : primKey);
}
}
});
}
});
IDBObjectStore.prototype.delete = override(IDBObjectStore.prototype.delete, function (origFunc) {
return function (key) {
var meta = this.transaction.db._iegapmeta.stores[this.name];
if (!meta) return origFunc.apply(this, arguments);
if (meta.compound) {
// Compound primary key
key = compoundToString(key);
}
var delReq = origFunc.call(this, key);
var indexes = Object.keys(meta.indexes);
if (indexes.length == 0) return delReq;
var store = this;
return new IEGAPRequest(this, this.transaction, function (success, error) {
var delEvent = null;
delReq.onerror = error;
delReq.onsuccess = function(ev) {
delEvent = ev;