Upgrading to the PouchDb indexedDb adapter while testing signals in web workers...
This commit is contained in:
1 parent
e73497265f
commit
4a4ebdff50
11 files changed
+362
-5
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { createSignal, Signal } from 'solid-js';
|
||||
|
||||
declare global {
|
||||
var exportQueue: Map<string, any>;
|
||||
}
|
||||
|
||||
let worker: Worker;
|
||||
|
||||
const init = () => {
|
||||
console.log({ caller: 'solid-worker-main / init' });
|
||||
globalThis.exportQueue = new Map<
|
||||
string,
|
||||
{ count: number; signal: Signal<any> }
|
||||
>();
|
||||
|
||||
worker = new Worker(new URL('./solid-worker', import.meta.url), {
|
||||
type: 'module',
|
||||
});
|
||||
|
||||
worker.onmessage = (event: any) => {
|
||||
const { _id, _updatedValue } = event.data;
|
||||
console.log({
|
||||
caller: 'solid-worker-main / message received',
|
||||
_id,
|
||||
_updatedValue,
|
||||
});
|
||||
if (!globalThis.exportQueue.get(_id)) {
|
||||
console.error({
|
||||
caller: 'solid-worker-main / message received',
|
||||
message: 'not found',
|
||||
_id,
|
||||
_updatedValue,
|
||||
});
|
||||
}
|
||||
const signal = globalThis.exportQueue.get(_id).signal;
|
||||
signal[1](_updatedValue);
|
||||
};
|
||||
};
|
||||
|
||||
export const createWorkerSignal = (parameters: {
|
||||
provider: string;
|
||||
params?: any;
|
||||
initialValue?: any;
|
||||
}) => {
|
||||
if (!worker) {
|
||||
init();
|
||||
}
|
||||
const { provider, params, initialValue } = parameters;
|
||||
const id = JSON.stringify({ provider, params });
|
||||
if (globalThis.exportQueue.has(id)) {
|
||||
globalThis.exportQueue.get(id).count++;
|
||||
return globalThis.exportQueue.get(id).signal;
|
||||
}
|
||||
const signal = createSignal(initialValue);
|
||||
globalThis.exportQueue.set(id, { signal, count: 1 });
|
||||
worker.postMessage({ ...parameters, _id: id });
|
||||
return signal;
|
||||
};
|
||||
|
||||
export const releaseWorkerSignal = (parameters: {
|
||||
provider: string;
|
||||
params: any;
|
||||
}) => {
|
||||
const { provider, params } = parameters;
|
||||
const id = JSON.stringify({ provider, params });
|
||||
if (globalThis.exportQueue.has(id)) {
|
||||
globalThis.exportQueue.get(id).count--;
|
||||
if (globalThis.exportQueue.get(id).count === 0) {
|
||||
globalThis.exportQueue.delete(id);
|
||||
worker.postMessage({ ...parameters, _id: id, _release: true });
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
import { createEffect, createRoot, createSignal, Signal } from 'solid-js';
|
||||
import { compactDb } from '../db-admin/compact';
|
||||
|
||||
declare global {
|
||||
var importQueue: Map<string, any>;
|
||||
}
|
||||
|
||||
globalThis.importQueue = new Map<string, Signal<any>>();
|
||||
|
||||
const providers = {
|
||||
compactDb,
|
||||
};
|
||||
|
||||
onmessage = function (e) {
|
||||
createRoot((dispose) => {
|
||||
const { provider, params, initialValue, _release, _id } = e.data;
|
||||
console.log({
|
||||
caller: 'solid-worker / onmessage',
|
||||
e,
|
||||
provider,
|
||||
params,
|
||||
initialValue,
|
||||
_release,
|
||||
_id,
|
||||
});
|
||||
|
||||
if (_release) {
|
||||
dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Object.hasOwn(providers, provider)) {
|
||||
console.error({
|
||||
caller: 'solid-worker',
|
||||
message: 'Unknown provider',
|
||||
provider,
|
||||
});
|
||||
}
|
||||
|
||||
const signal = createSignal(initialValue);
|
||||
// globalThis.importQueue.set(_id, signal); // Would be needed for bidirectional signals...
|
||||
|
||||
createEffect(() => {
|
||||
console.log({
|
||||
caller: 'solid_worker / effect',
|
||||
provider,
|
||||
params,
|
||||
initialValue,
|
||||
_release,
|
||||
currentValue: signal[0](),
|
||||
_id,
|
||||
});
|
||||
postMessage({ _id, _updatedValue: signal[0]() });
|
||||
});
|
||||
|
||||
const providerFunction = providers[provider as keyof typeof providers];
|
||||
providerFunction({ params, signal });
|
||||
});
|
||||
};
|
||||
Reference in new issue
Block a user