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;