Skip to content

Instantly share code, notes, and snippets.

@dfahlander
Created February 27, 2023 00:05
Show Gist options
  • Save dfahlander/7b87e78c6ea4486b5359e4822565b490 to your computer and use it in GitHub Desktop.
Save dfahlander/7b87e78c6ea4486b5359e4822565b490 to your computer and use it in GitHub Desktop.
// This module shall be imported from BrowserWindow's main modules. The node main thread should instead import "workaround-ipc".
// NOTE: This gist shall not be used if Electron solves issue https://github.com/electron/electron/issues/37417
import Dexie from "dexie";
import { ipcRenderer } from "electron";
if ("unref" in BroadcastChannel.prototype) {
// Node's BroadcastChannel has the `unref()` method.
// Seems we got the Node- and not the DOM version of BroadcastChannel.
// Only activate this workaround when we have the Node-version of BroadcastChannel
// in the BrowserWindow, since the issue seems to be connected to that trait.
let propagatingLocally = false;
Dexie.on("storagemutated", (changedParts) => {
if (!propagatingLocally)
ipcRenderer.send(
"dexie-storagemutated",
JSON.stringify(changedParts)
);
});
ipcRenderer.on("dexie-storagemutated", (event, json) => {
const changedParts = JSON.parse(json);
propagatingLocally = true;
try {
Dexie.on.storagemutated.fire(changedParts);
} finally {
propagatingLocally = false;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment