More fun with web workers...
This commit is contained in:
parent
94de17938e
commit
ebf4f39331
|
@ -1,6 +1,10 @@
|
|||
import _ from 'lodash';
|
||||
import PouchDB from 'pouchdb';
|
||||
import PouchDBFind from 'pouchdb-find';
|
||||
import uri from '../lib/ids';
|
||||
|
||||
PouchDB.plugin(PouchDBFind);
|
||||
|
||||
const dbDefinitionId = uri('dbdef', {});
|
||||
|
||||
const currentDbDefinition = {
|
||||
|
@ -9,7 +13,19 @@ const currentDbDefinition = {
|
|||
def: { version: '0.000001' },
|
||||
};
|
||||
|
||||
export const initDb = async (db: any, setDbReady?: any) => {
|
||||
declare global {
|
||||
var db: any;
|
||||
var dbReady: boolean;
|
||||
}
|
||||
|
||||
export const initDb = async (params: any) => {
|
||||
if (globalThis.db === undefined) {
|
||||
globalThis.db = new PouchDB('dyomedea', {
|
||||
auto_compaction: true,
|
||||
revs_limit: 10,
|
||||
});
|
||||
}
|
||||
const db = globalThis.db;
|
||||
var previousDbDefinition = {
|
||||
_id: dbDefinitionId,
|
||||
type: dbDefinitionId,
|
||||
|
@ -81,7 +97,7 @@ export const initDb = async (db: any, setDbReady?: any) => {
|
|||
}
|
||||
}
|
||||
|
||||
if (setDbReady !== undefined) setDbReady(true);
|
||||
globalThis.dbReady = true;
|
||||
|
||||
/* const indexes = await db.getIndexes();
|
||||
console.log(`indexes: ${JSON.stringify(indexes)}`);
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import dispatch, { init, worker } from './dispatcher-main';
|
||||
|
||||
var mockPostMessage = jest.fn();
|
||||
import dispatch, { init, worker } from './dispatcher-main'
|
||||
|
||||
jest.mock('./get-worker', () => ({
|
||||
getWorker: () => ({
|
||||
|
|
|
@ -11,7 +11,7 @@ export const init = () => {
|
|||
|
||||
worker = getWorker();
|
||||
|
||||
console.log(`worker: ${worker}`)
|
||||
console.log(`worker: ${worker}`);
|
||||
|
||||
worker.port.onmessage = (event: any) => {
|
||||
const { id, payload } = event.data;
|
||||
|
|
|
@ -14,6 +14,9 @@ describe('The dispatcher-worker ', () => {
|
|||
});
|
||||
test('receives a ping and sends a pong', () => {
|
||||
port.onmessage({ data: { id: 5, payload: 'ping' } });
|
||||
expect(port.postMessage).toBeCalledWith({ id: 5, payload: '"ping" pong' });
|
||||
expect(port.postMessage).toBeCalledWith({
|
||||
id: 5,
|
||||
payload: 'unknownAction',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,12 +1,23 @@
|
|||
import { initDb } from '../db';
|
||||
|
||||
const self = globalThis as unknown as SharedWorkerGlobalScope;
|
||||
|
||||
const actions = { initDb };
|
||||
|
||||
self.onconnect = function (e) {
|
||||
var port = e.ports[0];
|
||||
|
||||
port.onmessage = function (e) {
|
||||
console.log(`Worker received ${JSON.stringify(e)}`);
|
||||
port.onmessage = async function (e) {
|
||||
console.log(`Worker received ${JSON.stringify(e.data)}`);
|
||||
const { id, payload } = e.data;
|
||||
port.postMessage({ id: id, payload: `${JSON.stringify(payload)} pong` });
|
||||
console.log(`payload.action in actions: ${payload.action in actions}`);
|
||||
var returnValue: any = 'unknownAction';
|
||||
if (payload.action in actions) {
|
||||
returnValue = await actions[<keyof typeof actions>payload.action](
|
||||
payload.params
|
||||
);
|
||||
}
|
||||
port.postMessage({ id: id, payload: returnValue });
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue