1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

Initial work at getting sqlite3Worker1Promiser.v2() to return a Promise instead of using an onready() callback, and also creating an ESM build for promiser1 per user request. It seems to work but requires more testing.

FossilOrigin-Name: 0e272123ace55ed63fe86632671cca48e8965a28fc3625324984028729fc203f
This commit is contained in:
stephan
2024-03-07 16:04:43 +00:00
parent a64342ee9c
commit 61405c463d
5 changed files with 82 additions and 31 deletions

View File

@@ -42,9 +42,13 @@
- `onready` (optional, but...): this callback is called with no
arguments when the worker fires its initial
'sqlite3-api'/'worker1-ready' message, which it does when
sqlite3.initWorker1API() completes its initialization. This is
the simplest way to tell the worker to kick off work at the
earliest opportunity.
sqlite3.initWorker1API() completes its initialization. This is the
simplest way to tell the worker to kick off work at the earliest
opportunity, and the only way to know when the worker module has
completed loading. The irony of using a callback for this, instead
of returning a promise from sqlite3Worker1Promiser() is not lost on
the developers, but initial attempts to return a promise resulted
in a much clumsier interface.
- `onunhandled` (optional): a callback which gets passed the
message event object for any worker.onmessage() events which
@@ -277,7 +281,45 @@ globalThis.sqlite3Worker1Promiser.defaultConfig = {
//#endif
,
onerror: (...args)=>console.error('worker1 promiser error',...args)
}/*defaultConfig*/;
/**
sqlite3Worker1Promiser.v2() works identically to
sqlite3Worker1Promiser() except that it returns a promise instead
of relying an an onready callback in the config object.
*/
sqlite3Worker1Promiser.v2 = function(config){
const x = Object.create(null);
let oldFunc;
if( 'function' == typeof config ){
oldFunc = config;
config = {};
}else if('function'===typeof config?.onready){
oldFunc = config.onready;
delete config.onready;
}
config = Object.assign((config || Object.create(null)),{
onready: function(func){
try {
if( oldFunc ){
oldFunc(func);
}
x.resolve(func);
}
catch(e){x.reject(e)}
}
});
const p = new Promise(function(resolve,reject){
x.resolve = resolve;
x.reject = reject;
});
sqlite3Worker1Promiser(config);
return p;
};
//#if target=es6-module
export default sqlite3Worker1Promiser.v2;
//#endif /* target=es6-module */
//#else
/* Built with the omit-oo1 flag. */
//#endif ifnot omit-oo1