|
7 | 7 | #include "IDBStore.js"
|
8 | 8 |
|
9 | 9 | var LibraryIDBStore = {
|
10 |
| - // A simple IDB-backed storage mechanism. Suitable for saving and loading large files asynchronously. This does |
11 |
| - // *NOT* use the emscripten filesystem, intentionally, to avoid overhead. It lets you application define whatever |
12 |
| - // filesystem-like layer you want, with the overhead 100% controlled by you. At the extremes, you could either |
13 |
| - // just store large files, with almost no extra code; or you could implement a file b-tree using posix-compliant |
| 10 | + // A simple IDB-backed storage mechanism. Suitable for saving and loading |
| 11 | + // large files asynchronously. This does *NOT* use the emscripten filesystem, |
| 12 | + // intentionally, to avoid overhead. It lets you application define whatever |
| 13 | + // filesystem-like layer you want, with the overhead 100% controlled by you. |
| 14 | + // At the extremes, you could either just store large files, with almost no |
| 15 | + // extra code; or you could implement a file b-tree using posix-compliant |
14 | 16 | // filesystem on top.
|
15 | 17 | $IDBStore: IDBStore,
|
16 |
| - emscripten_idb_async_load__deps: ['$UTF8ToString', 'malloc', 'free'], |
| 18 | + emscripten_idb_async_load__deps: ['$UTF8ToString', '$callUserCallback', 'malloc', 'free'], |
17 | 19 | emscripten_idb_async_load: function(db, id, arg, onload, onerror) {
|
18 |
| - IDBStore.getFile(UTF8ToString(db), UTF8ToString(id), function(error, byteArray) { |
19 |
| - if (error) { |
20 |
| - if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg); |
21 |
| - return; |
22 |
| - } |
23 |
| - var buffer = _malloc(byteArray.length); |
24 |
| - HEAPU8.set(byteArray, buffer); |
25 |
| - {{{ makeDynCall('viii', 'onload') }}}(arg, buffer, byteArray.length); |
26 |
| - _free(buffer); |
| 20 | + {{{ runtimeKeepalivePush() }}}; |
| 21 | + IDBStore.getFile(UTF8ToString(db), UTF8ToString(id), (error, byteArray) => { |
| 22 | + {{{ runtimeKeepalivePop() }}} |
| 23 | + callUserCallback(() => { |
| 24 | + if (error) { |
| 25 | + if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg); |
| 26 | + return; |
| 27 | + } |
| 28 | + var buffer = _malloc(byteArray.length); |
| 29 | + HEAPU8.set(byteArray, buffer); |
| 30 | + {{{ makeDynCall('vppi', 'onload') }}}(arg, buffer, byteArray.length); |
| 31 | + _free(buffer); |
| 32 | + }); |
27 | 33 | });
|
28 | 34 | },
|
| 35 | + emscripten_idb_async_store__deps: ['$UTF8ToString', 'free', '$callUserCallback'], |
29 | 36 | emscripten_idb_async_store: function(db, id, ptr, num, arg, onstore, onerror) {
|
30 |
| - // note that we copy the data here, as these are async operatins - changes to HEAPU8 meanwhile should not affect us! |
31 |
| - IDBStore.setFile(UTF8ToString(db), UTF8ToString(id), new Uint8Array(HEAPU8.subarray(ptr, ptr+num)), function(error) { |
32 |
| - if (error) { |
33 |
| - if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg); |
34 |
| - return; |
35 |
| - } |
36 |
| - if (onstore) {{{ makeDynCall('vi', 'onstore') }}}(arg); |
| 37 | + // note that we copy the data here, as these are async operatins - changes |
| 38 | + // to HEAPU8 meanwhile should not affect us! |
| 39 | + {{{ runtimeKeepalivePush() }}}; |
| 40 | + IDBStore.setFile(UTF8ToString(db), UTF8ToString(id), new Uint8Array(HEAPU8.subarray(ptr, ptr+num)), (error) => { |
| 41 | + {{{ runtimeKeepalivePop() }}} |
| 42 | + callUserCallback(() => { |
| 43 | + if (error) { |
| 44 | + if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg); |
| 45 | + return; |
| 46 | + } |
| 47 | + if (onstore) {{{ makeDynCall('vp', 'onstore') }}}(arg); |
| 48 | + }); |
37 | 49 | });
|
38 | 50 | },
|
| 51 | + emscripten_idb_async_delete__deps: ['$UTF8ToString', '$callUserCallback'], |
39 | 52 | emscripten_idb_async_delete: function(db, id, arg, ondelete, onerror) {
|
40 |
| - IDBStore.deleteFile(UTF8ToString(db), UTF8ToString(id), function(error) { |
41 |
| - if (error) { |
42 |
| - if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg); |
43 |
| - return; |
44 |
| - } |
45 |
| - if (ondelete) {{{ makeDynCall('vi', 'ondelete') }}}(arg); |
| 53 | + {{{ runtimeKeepalivePush() }}}; |
| 54 | + IDBStore.deleteFile(UTF8ToString(db), UTF8ToString(id), (error) => { |
| 55 | + {{{ runtimeKeepalivePop() }}} |
| 56 | + callUserCallback(() => { |
| 57 | + if (error) { |
| 58 | + if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg); |
| 59 | + return; |
| 60 | + } |
| 61 | + if (ondelete) {{{ makeDynCall('vp', 'ondelete') }}}(arg); |
| 62 | + }); |
46 | 63 | });
|
47 | 64 | },
|
| 65 | + emscripten_idb_async_exists__deps: ['$UTF8ToString', '$callUserCallback'], |
48 | 66 | emscripten_idb_async_exists: function(db, id, arg, oncheck, onerror) {
|
49 |
| - IDBStore.existsFile(UTF8ToString(db), UTF8ToString(id), function(error, exists) { |
50 |
| - if (error) { |
51 |
| - if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg); |
52 |
| - return; |
53 |
| - } |
54 |
| - if (oncheck) {{{ makeDynCall('vii', 'oncheck') }}}(arg, exists); |
| 67 | + {{{ runtimeKeepalivePush() }}}; |
| 68 | + IDBStore.existsFile(UTF8ToString(db), UTF8ToString(id), (error, exists) => { |
| 69 | + {{{ runtimeKeepalivePop() }}} |
| 70 | + callUserCallback(() => { |
| 71 | + if (error) { |
| 72 | + if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg); |
| 73 | + return; |
| 74 | + } |
| 75 | + if (oncheck) {{{ makeDynCall('vpi', 'oncheck') }}}(arg, exists); |
| 76 | + }); |
55 | 77 | });
|
56 | 78 | },
|
57 | 79 |
|
|
0 commit comments