From 94de17938e9e49e35dc1b842bd96a1ece0367174 Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 6 Nov 2022 22:29:33 +0100 Subject: [PATCH] More work with web workers (pffff!) --- src/App.tsx | 17 +++++++++++------ src/workers/dispatcher-main.test.js | 22 ++++++++++++---------- src/workers/dispatcher-main.ts | 6 +++++- src/workers/get-worker.ts | 4 ++++ 4 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 src/workers/get-worker.ts diff --git a/src/App.tsx b/src/App.tsx index 43dec32..acf75a8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -26,6 +26,9 @@ import '@ionic/react/css/display.css'; /* Theme variables */ import './theme/variables.css'; + +import dispatch from './workers/dispatcher-main'; + import LiveMap from './components/map/LiveMap'; import { atom, useAtom } from 'jotai'; @@ -37,10 +40,10 @@ import { geoPoint } from './components/map/types'; import Back from './components/buttons/Back'; import Forward from './components/buttons/Forward'; import CurrentLocation from './components/map/CurrentLocation'; -import { initDb } from './db'; -import PouchDB from 'pouchdb'; -import PouchDBFind from 'pouchdb-find'; -PouchDB.plugin(PouchDBFind); +// import { initDb } from './db'; +// import PouchDB from 'pouchdb'; +// import PouchDBFind from 'pouchdb-find'; +// PouchDB.plugin(PouchDBFind); setupIonicReact(); @@ -88,8 +91,10 @@ export const setCenterAtom = atom(null, (get, set, center: geoPoint) => { set(scopeAtom, newScope); }); -const db = new PouchDB('dyomedea', { auto_compaction: true, revs_limit: 10 }); -initDb(db); +// const db = new PouchDB('dyomedea', { auto_compaction: true, revs_limit: 10 }); +// initDb(db); + +dispatch({ action: 'initDb' }); /** * diff --git a/src/workers/dispatcher-main.test.js b/src/workers/dispatcher-main.test.js index f80a883..0b9504c 100644 --- a/src/workers/dispatcher-main.test.js +++ b/src/workers/dispatcher-main.test.js @@ -1,19 +1,21 @@ import dispatch, { init, worker } from './dispatcher-main'; +var mockPostMessage = jest.fn(); + +jest.mock('./get-worker', () => ({ + getWorker: () => ({ + port: { + postMessage: jest.fn(), + }, + }), +})); + describe('The dispatcher-main', () => { - var postMessage = jest.fn(); beforeEach(() => { - global.SharedWorker = jest.fn(function (source) { - this.source = source; - this.port = { - postMessage, - }; - return this; - }); init(); }); test('should create a new shared web worker', () => { - expect(SharedWorker).toBeCalled(); + expect(worker).toBeDefined(); }); test('should create a onmessage function', () => { expect(worker.port.onmessage).toBeDefined(); @@ -23,6 +25,6 @@ describe('The dispatcher-main', () => { }); test('should send back the message', () => { dispatch('ping'); - expect(postMessage).toBeCalledWith({ id: 0, payload: 'ping' }); + expect(worker.port.postMessage).toBeCalledWith({ id: 0, payload: 'ping' }); }); }); diff --git a/src/workers/dispatcher-main.ts b/src/workers/dispatcher-main.ts index db0a7a3..57f9ca3 100644 --- a/src/workers/dispatcher-main.ts +++ b/src/workers/dispatcher-main.ts @@ -1,3 +1,5 @@ +import { getWorker } from './get-worker'; + declare global { var dispatcherQueue: { index: number; queue: Map }; } @@ -7,7 +9,9 @@ export var worker: any; export const init = () => { globalThis.dispatcherQueue = { index: 0, queue: new Map() }; - worker = new SharedWorker('./dispatcher-worker'); + worker = getWorker(); + + console.log(`worker: ${worker}`) worker.port.onmessage = (event: any) => { const { id, payload } = event.data; diff --git a/src/workers/get-worker.ts b/src/workers/get-worker.ts new file mode 100644 index 0000000..fff7835 --- /dev/null +++ b/src/workers/get-worker.ts @@ -0,0 +1,4 @@ +export const getWorker = () => + new SharedWorker(new URL('./dispatcher-worker', import.meta.url)); + +export default getWorker;