Skip to content

Commit 605100a

Browse files
authored
Merge pull request rhashimoto#225 from davidisaaclee/commit-hook
Expose commit hook
2 parents ca1c304 + dc9b8a1 commit 605100a

File tree

12 files changed

+215
-5
lines changed

12 files changed

+215
-5
lines changed

dist/wa-sqlite-async.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/wa-sqlite-async.wasm

35 Bytes
Binary file not shown.

dist/wa-sqlite-jspi.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/wa-sqlite-jspi.wasm

52 Bytes
Binary file not shown.

dist/wa-sqlite.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/wa-sqlite.wasm

35 Bytes
Binary file not shown.

src/libadapters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Method names for these signatures must be in src/asyncify_imports.json.
22
const SIGNATURES = [
3-
'ipp', // xProgress
3+
'ipp', // xProgress, xCommitHook
44
'ippp', // xClose, xSectorSize, xDeviceCharacteristics
55
'vppp', // xShmBarrier, xFinal
66
'ipppj', // xTruncate

src/libhook.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
SIGNATURE##_async(KEY, __VA_ARGS__) : \
1111
SIGNATURE(KEY, __VA_ARGS__))
1212

13+
static int libhook_xCommitHook(void* pApp) {
14+
const int asyncFlags = pApp ? *(int *)pApp : 0;
15+
return CALL_JS(ipp, pApp, pApp);
16+
}
17+
1318
static void libhook_xUpdateHook(
1419
void* pApp,
1520
int iUpdateType,
@@ -22,6 +27,10 @@ static void libhook_xUpdateHook(
2227
CALL_JS(vppippii, pApp, pApp, iUpdateType, dbName, tblName, lo32, hi32);
2328
}
2429

30+
void EMSCRIPTEN_KEEPALIVE libhook_commit_hook(sqlite3* db, int xCommitHook, void* pApp) {
31+
sqlite3_commit_hook(db, xCommitHook ? &libhook_xCommitHook : NULL, pApp);
32+
}
33+
2534
void EMSCRIPTEN_KEEPALIVE libhook_update_hook(sqlite3* db, int xUpdateHook, void* pApp) {
2635
sqlite3_update_hook(db, xUpdateHook ? &libhook_xUpdateHook : NULL, pApp);
2736
}

src/libhook.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,31 @@
2626
});
2727
}
2828
};
29-
})();
29+
})();
30+
31+
(function() {
32+
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
33+
let pAsyncFlags = 0;
34+
35+
Module['commit_hook'] = function(db, xCommitHook) {
36+
if (pAsyncFlags) {
37+
Module['deleteCallback'](pAsyncFlags);
38+
Module['_sqlite3_free'](pAsyncFlags);
39+
pAsyncFlags = 0;
40+
}
41+
42+
pAsyncFlags = Module['_sqlite3_malloc'](4);
43+
setValue(pAsyncFlags, xCommitHook instanceof AsyncFunction ? 1 : 0, 'i32');
44+
45+
ccall(
46+
'libhook_commit_hook',
47+
'void',
48+
['number', 'number', 'number'],
49+
[db, xCommitHook ? 1 : 0, pAsyncFlags]);
50+
if (xCommitHook) {
51+
Module['setCallback'](pAsyncFlags, (_) => {
52+
return xCommitHook();
53+
});
54+
}
55+
};
56+
})();

src/sqlite-api.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,11 @@ export function Factory(Module) {
735735
};
736736
})();
737737

738+
sqlite3.commit_hook = function(db, xCommitHook) {
739+
verifyDatabase(db);
740+
Module.commit_hook(db, xCommitHook);
741+
};
742+
738743
sqlite3.update_hook = function(db, xUpdateHook) {
739744
verifyDatabase(db);
740745

0 commit comments

Comments
 (0)