Skip to content

Instantly share code, notes, and snippets.

@deepak1556
Created June 21, 2021 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepak1556/1a5f694b5361a71de33a34ceba38deab to your computer and use it in GitHub Desktop.
Save deepak1556/1a5f694b5361a71de33a34ceba38deab to your computer and use it in GitHub Desktop.
OOP shared worker
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
<script>
// https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker
var myWorker = new SharedWorker("worker.js");
// Message ports allow IPC between the OOP worker and this document
myWorker.port.onmessage = function(e) {
console.log('Message received from worker');
console.log(e.data)
}
myWorker.port.postMessage('start');
</script>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, protocol} = require('electron')
const path = require('path')
protocol.registerSchemesAsPrivileged([
{
scheme: 'foo',
privileges: {
standard: true,
secure: true
}
}
])
function createWindow () {
protocol.registerFileProtocol('foo', (request, cb) => {
const url = new URL(request.url)
console.log(`Loading : ${__dirname + url.pathname}`)
if (url.pathname === "/worker.js") {
cb({
path: __dirname + url.pathname
})
} else {
// The headers below force the shared worker to be Out-Of-Process
cb({
path: __dirname + url.pathname,
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp"
}
})
}
})
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
}
})
// and load the index.html of the app.
mainWindow.loadURL('foo://custom/index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
onconnect = function(e) {
var port = e.ports[0];
port.onmessage = function(e) {
port.postMessage('worker connected');
}
}
@bpasero
Copy link

bpasero commented Jun 23, 2021

I like the idea of exploring shared workers with node.js integration for file services specifically because I do not think this service benefits from running in the background from the shared process. It still needs to be isolated process wise given file watch events can be expensive. One thing that would be nice to have is if the shared worker could also have a direct communication channel to the extension host to deliver file events, but that is maybe P2.

My main worry for file service adoption is how expensive / slow it is to create such a shared worker. The file service is obviously one of those things we need right from the beginning because many things get resolved on startup. We can try to offload some of that work into the initial config object we send over but it might become unpractical very quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment