Skip to content

Instantly share code, notes, and snippets.

@Sveagruva
Created February 5, 2022 18:59
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 Sveagruva/04d02de3a61c8561e37566d355cc8876 to your computer and use it in GitHub Desktop.
Save Sveagruva/04d02de3a61c8561e37566d355cc8876 to your computer and use it in GitHub Desktop.
stream content asynchronously in your electron app without http server. stream html as you build it.
const electron = require('electron');
const PassThrough = require('stream').PassThrough;
// why. idk
electron.app.whenReady().then(() => {
electron.protocol.interceptStreamProtocol('file', async (req, callback) => {
const stream = new PassThrough();
callback({
data: stream
});
for (let i = 0; i < 100; i++) {
await wait(100);
stream.push('hi\n');
}
stream.push(null);
});
const window = new electron.BrowserWindow();
window.webContents.openDevTools();
window.loadFile("index.html");
});
const wait = async (ms) => new Promise(r => setTimeout(() => r(), ms));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment