[v2] A better approach to delayed IPC

This commit is contained in:
Lea\Anthony
2021-12-05 14:23:48 +11:00
parent 41507e966a
commit 38caa645e5
5 changed files with 25 additions and 836 deletions
@@ -9,7 +9,6 @@ import (
"github.com/wailsapp/wails/v2/pkg/options"
"golang.org/x/net/html"
"path/filepath"
"strings"
)
/*
@@ -98,14 +97,6 @@ func (a *BrowserAssetServer) Load(filename string) ([]byte, string, error) {
content = runtime.WebsocketIPC
default:
content, err = a.loadFileFromDisk(filename)
if strings.HasSuffix(filename, ".js") {
var buffer bytes.Buffer
buffer.WriteString("window.awaitIPC('" + filename + "', ()=>{")
buffer.Write(content)
buffer.WriteString(`
});`)
content = buffer.Bytes()
}
}
if err != nil {
return nil, "", err
+15 -13
View File
@@ -14,14 +14,17 @@ import Overlay from "./Overlay.svelte";
import {hideOverlay, showOverlay} from "./store";
let components = {};
window.ipcCallbacks = [];
window.ipcCallbackNames = [];
window.awaitIPC = (name, callback) => {
if (!window.ipcCallbacks) return callback;
log("Queuing '" + name + "' for execution once ipc ready.");
window.ipcCallbackNames.push(name);
window.ipcCallbacks.push(callback);
let wailsInvokeInternal = null;
let messageQueue = [];
window.WailsInvoke = (message) => {
if (!wailsInvokeInternal) {
console.log("Queueing: " + message);
messageQueue.push(message);
return;
}
wailsInvokeInternal(message);
};
window.addEventListener('DOMContentLoaded', () => {
@@ -47,15 +50,14 @@ window.onbeforeunload = function () {
connect();
function setupIPCBridge() {
window.WailsInvoke = (message) => {
wailsInvokeInternal = (message) => {
websocket.send(message);
};
for (let i = 0; i < window.ipcCallbacks.length; i++) {
log("Executing JS: " + window.ipcCallbackNames[i]);
window.ipcCallbacks[i]();
for (let i = 0; i < messageQueue.length; i++) {
console.log("sending queued message: " + messageQueue[i]);
window.WailsInvoke(messageQueue[i]);
}
delete window.ipcCallbacks;
delete window.ipcCallbackNames;
messageQueue = [];
}
// Handles incoming websocket connections
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long