-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpowersync-update.patch
More file actions
34 lines (34 loc) · 1.07 KB
/
Copy pathpowersync-update.patch
File metadata and controls
34 lines (34 loc) · 1.07 KB
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
diff --git a/src/update.c b/src/update.c
new file mode 100644
index 0000000..c7e8a23
--- /dev/null
+++ b/src/update.c
@@ -0,0 +1,28 @@
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+
+#include <emscripten.h>
+#include <stdio.h>
+#include <stdint.h>
+
+EM_JS(void, call_js_update_hook, (int db_p, int type, const char* db_name, const char* table_name), {
+ if (typeof onSqliteUpdate === "function") {
+ onSqliteUpdate(db_p, type, UTF8ToString(db_name), UTF8ToString(table_name));
+ } else {
+ console.error("onSqliteUpdate is not defined");
+ }
+});
+
+static void sqlite_update_callback(void* pArg, int type, const char* db, const char* table, sqlite3_int64 rowid) {
+ call_js_update_hook((int)(intptr_t)pArg, type, db, table);
+}
+
+static int register_update_hook_on_open(sqlite3* db, const char** pzErrMsg, const sqlite3_api_routines* pApi) {
+ SQLITE_EXTENSION_INIT2(pApi);
+ sqlite3_update_hook(db, sqlite_update_callback, db);
+ return SQLITE_OK;
+}
+
+void register_update_hook_extension(void) {
+ sqlite3_auto_extension((void(*)(void))register_update_hook_on_open);
+}