Skip to content

Commit 104e3de

Browse files
authored
Add emscripten_idb_clear API (#20182)
This patch adds missing `clear` IndexedDB API (both sync and async versions).
1 parent 5da546c commit 104e3de

File tree

9 files changed

+87
-3
lines changed

9 files changed

+87
-3
lines changed

site/source/docs/api_reference/emscripten.h.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,21 @@ Asynchronous IndexedDB API
865865
866866
- *(void*)* : Equal to ``arg`` (user defined data).
867867
868+
.. c:function:: void emscripten_idb_async_clear(const char *db_name, void* arg, em_arg_callback_func onclear, em_arg_callback_func onerror)
869+
870+
Clears all data from local IndexedDB storage asynchronously.
871+
872+
When the storage has been cleared then the ``onclear`` callback will be called. If any error occurred ``onerror`` will be called.
873+
874+
:param db_name: The IndexedDB database.
875+
:param void* arg: User-defined data that is passed to the callbacks, untouched by the API itself. This may be used by a callback to identify the associated call.
876+
:param em_arg_callback_func onclear: Callback on successful clear. The callback function parameter is:
877+
878+
- *(void*)* : Equal to ``arg`` (user defined data).
879+
880+
:param em_arg_callback_func onerror: Callback in the event of failure. The callback function parameter is:
881+
882+
- *(void*)* : Equal to ``arg`` (user defined data).
868883
869884
870885
.. c:function:: int emscripten_run_preload_plugins(const char* file, em_str_callback_func onload, em_str_callback_func onerror)
@@ -1362,6 +1377,13 @@ IndexedDB
13621377
:param pexists: An out parameter that will be filled with a non-zero value if the file exists in that database.
13631378
:param perror: An out parameter that will be filled with a non-zero value if an error occurred.
13641379
1380+
.. c:function:: void emscripten_idb_clear(const char *db_name, int *perror);
1381+
1382+
Synchronously clears all data from IndexedDB.
1383+
1384+
:param db_name: The name of the database to clear
1385+
:param perror: An out parameter that will be filled with a non-zero value if an error occurred.
1386+
13651387
13661388
Upstream Asyncify functions
13671389
===========================

src/IDBStore.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ var IDBStore = {
102102
req.onerror = (error) => callback(error);
103103
});
104104
},
105+
clearStore(dbName, callback) {
106+
IDBStore.getStore(dbName, 'readwrite', (err, store) => {
107+
if (err) return callback(err);
108+
var req = store.clear();
109+
req.onsuccess = (event) => callback();
110+
req.onerror = (error) => callback(error);
111+
});
112+
},
105113
};

src/library_idbstore.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ var LibraryIDBStore = {
7676
});
7777
});
7878
},
79+
emscripten_idb_async_clear__deps: ['$UTF8ToString', '$callUserCallback'],
80+
emscripten_idb_async_clear: (db, arg, onclear, onerror) => {
81+
{{{ runtimeKeepalivePush() }}};
82+
IDBStore.clearStore(UTF8ToString(db), (error) => {
83+
{{{ runtimeKeepalivePop() }}}
84+
callUserCallback(() => {
85+
if (error) {
86+
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg);
87+
return;
88+
}
89+
if (onclear) {{{ makeDynCall('vp', 'onclear') }}}(arg);
90+
});
91+
});
92+
},
7993

8094
#if ASYNCIFY
8195
emscripten_idb_load__async: true,
@@ -125,6 +139,15 @@ var LibraryIDBStore = {
125139
});
126140
});
127141
},
142+
emscripten_idb_clear__async: true,
143+
emscripten_idb_clear: (db, perror) => {
144+
return Asyncify.handleSleep((wakeUp) => {
145+
IDBStore.clearStore(UTF8ToString(db), (error) => {
146+
{{{ makeSetValue('perror', 0, '!!error', 'i32') }}};
147+
wakeUp();
148+
});
149+
});
150+
},
128151
// extra worker methods - proxied
129152
emscripten_idb_load_blob__async: true,
130153
emscripten_idb_load_blob: (db, id, pblob, perror) => {
@@ -195,6 +218,9 @@ var LibraryIDBStore = {
195218
emscripten_idb_exists: (db, id, pexists, perror) => {
196219
throw 'Please compile your program with async support in order to use synchronous operations like emscripten_idb_exists, etc.';
197220
},
221+
emscripten_idb_clear: (db, perror) => {
222+
throw 'Please compile your program with async support in order to use synchronous operations like emscripten_idb_clear, etc.';
223+
},
198224
#endif // ASYNCIFY
199225
};
200226

src/library_sigs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,12 @@ sigs = {
650650
emscripten_has_threading_support__sig: 'i',
651651
emscripten_hide_mouse__sig: 'v',
652652
emscripten_html5_remove_all_event_listeners__sig: 'v',
653+
emscripten_idb_async_clear__sig: 'vpppp',
653654
emscripten_idb_async_delete__sig: 'vppppp',
654655
emscripten_idb_async_exists__sig: 'vppppp',
655656
emscripten_idb_async_load__sig: 'vppppp',
656657
emscripten_idb_async_store__sig: 'vpppippp',
658+
emscripten_idb_clear__sig: 'vpp',
657659
emscripten_idb_delete__sig: 'vppp',
658660
emscripten_idb_exists__sig: 'vpppp',
659661
emscripten_idb_load__sig: 'vppppp',

system/include/emscripten/emscripten.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ void emscripten_idb_async_store(const char *db_name __attribute__((nonnull)), co
9595
void emscripten_idb_async_delete(const char *db_name __attribute__((nonnull)), const char *file_id __attribute__((nonnull)), void* arg, em_arg_callback_func ondelete, em_arg_callback_func onerror);
9696
typedef void (*em_idb_exists_func)(void*, int);
9797
void emscripten_idb_async_exists(const char *db_name __attribute__((nonnull)), const char *file_id __attribute__((nonnull)), void* arg, em_idb_exists_func oncheck, em_arg_callback_func onerror);
98+
void emscripten_idb_async_clear(const char *db_name __attribute__((nonnull)), void* arg, em_arg_callback_func onclear, em_arg_callback_func onerror);
9899

99100
// IDB "sync"
100101

101102
void emscripten_idb_load(const char *db_name, const char *file_id, void** pbuffer, int* pnum, int *perror);
102103
void emscripten_idb_store(const char *db_name, const char *file_id, void* buffer, int num, int *perror);
103104
void emscripten_idb_delete(const char *db_name, const char *file_id, int *perror);
104105
void emscripten_idb_exists(const char *db_name, const char *file_id, int* pexists, int *perror);
106+
void emscripten_idb_clear(const char *db_name, int *perror);
105107

106108
void emscripten_idb_load_blob(const char *db_name, const char *file_id, int* pblob, int *perror);
107109
void emscripten_idb_store_blob(const char *db_name, const char *file_id, void* buffer, int num, int *perror);

test/browser/test_idbstore.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ int main() {
7777
#elif STAGE == 5
7878
expected = 77;
7979
emscripten_idb_async_exists(DB, "the_secret", (void*)expected, onchecknope, onerror);
80+
#elif STAGE == 6
81+
expected = 88;
82+
printf("clearing\n");
83+
emscripten_idb_async_clear(DB, (void*)expected, ok, onerror);
8084
#else
8185
assert(0);
8286
#endif

test/browser/test_idbstore_sync.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ void test() {
5252
assert(error); // expected error!
5353
sum++;
5454

55+
printf("storing %s again\n", SECRET);
56+
emscripten_idb_store(DB, "the_secret", SECRET, strlen(SECRET)+1, &error);
57+
assert(!error);
58+
sum++;
59+
60+
printf("clearing the store\n");
61+
emscripten_idb_clear(DB, &error);
62+
assert(!error);
63+
sum++;
64+
5565
printf("last checking\n");
5666
emscripten_idb_exists(DB, "the_secret", &exists, &error);
5767
assert(!error);

test/browser/test_idbstore_sync_worker.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ int main() {
5050
assert(error); // expected error!
5151
sum++;
5252

53+
printf("storing %s again\n", SECRET);
54+
emscripten_idb_store(DB, "the_secret", SECRET, strlen(SECRET)+1, &error);
55+
assert(!error);
56+
sum++;
57+
58+
printf("clearing the store\n");
59+
emscripten_idb_clear(DB, &error);
60+
assert(!error);
61+
sum++;
62+
5363
printf("last checking\n");
5464
emscripten_idb_exists(DB, "the_secret", &exists, &error);
5565
assert(!error);
@@ -93,7 +103,7 @@ int main() {
93103

94104
// finish up
95105

96-
assert(sum == 6);
106+
assert(sum == 8);
97107
REPORT_RESULT(0);
98108
return 0;
99109
}

test/test_browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ def test_separate_metadata_later(self):
15051505
@also_with_wasm64
15061506
def test_idbstore(self):
15071507
secret = str(time.time())
1508-
for stage in [0, 1, 2, 3, 0, 1, 2, 0, 0, 1, 4, 2, 5]:
1508+
for stage in [0, 1, 2, 3, 0, 1, 2, 0, 0, 1, 4, 2, 5, 0, 4, 6, 5]:
15091509
print(stage)
15101510
self.btest_exit('test_idbstore.c',
15111511
args=['-lidbstore.js', f'-DSTAGE={stage}', f'-DSECRET="{secret}"'],
@@ -1524,7 +1524,7 @@ def test_idbstore_sync(self, asyncify, wasm64):
15241524
if asyncify == 2:
15251525
self.require_jspi()
15261526
secret = str(time.time())
1527-
self.btest('test_idbstore_sync.c', '6', args=['-lidbstore.js', f'-DSECRET="{secret}"', '-O3', '-g2', '-sASYNCIFY=' + str(asyncify)])
1527+
self.btest('test_idbstore_sync.c', '8', args=['-lidbstore.js', f'-DSECRET="{secret}"', '-O3', '-g2', '-sASYNCIFY=' + str(asyncify)])
15281528

15291529
def test_idbstore_sync_worker(self):
15301530
secret = str(time.time())

0 commit comments

Comments
 (0)