feat: 🎸 Add a shared service between web workers (#2932)

* feat: 🎸 Add a shared service between web workers

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-17409
This commit is contained in:
Zhihe Li committed 2025-09-03 11:41:40 -04:00
1 parent a9a6518b23
commit 8abed1a809
7 files changed
+326 -37

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { PWBWorker } from 'promise-worker-bi';
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
import sharedServiceInit from './utils/shared-service';
const methods = {
initializeSQLite: async () => {
try {
const sqlite3 = await sqlite3InitModule({
print: console.log,
printErr: console.error,
});
console.log(`SQLite Version: ${sqlite3?.version.libVersion}`);
const poolUtil = await sqlite3.installOpfsSAHPoolVfs();
new poolUtil.OpfsSAHPoolDb('/boundary');
} catch (err) {
console.error('Initialization error:', err.name, err.message);
}
},
};
const promiseWorker = new PWBWorker();
promiseWorker.register(async ({ method, payload }) => {
await sharedService.status.ready;
return sharedService.proxy[method](payload);
});
const initializeOnProviderChange = async (isProvider) => {
if (!isProvider) {
return;
}
// Initialize the SQLite database when we become a provider
await sharedService.proxy.initializeSQLite();
};
const sharedService = sharedServiceInit(
'sqlite',
methods,
initializeOnProviderChange,
);