Files

60 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2019-07-12 10:12:15 +10:00
/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The lightweight framework for web-like apps
(c) Lea Anthony 2019-present
*/
2019-07-13 15:35:50 +10:00
/* jshint esversion: 6 */
2019-07-12 10:12:15 +10:00
2019-11-27 22:55:19 +11:00
// IPC Listeners
var listeners = [];
/**
* Adds a listener to IPC messages
* @param {function} callback
*/
export function AddIPCListener(callback) {
listeners.push(callback);
}
2019-07-20 19:32:30 +10:00
/**
* Invoke sends the given message to the backend
*
* @param {string} message
*/
2019-07-13 15:30:52 +10:00
function Invoke(message) {
2019-11-27 22:55:19 +11:00
if (window.wailsbridge) {
2019-11-02 21:06:02 +11:00
window.wailsbridge.websocket.send(message);
} else {
window.external.invoke(message);
}
2019-11-27 22:55:19 +11:00
// Also send to listeners
if (listeners.length > 0) {
for (var i = 0; i < listeners.length; i++) {
listeners[i](message);
}
}
2019-07-12 10:12:15 +10:00
}
2019-07-20 19:32:30 +10:00
/**
* Sends a message to the backend based on the given type, payload and callbackID
*
* @export
* @param {string} type
2021-01-25 21:05:20 +11:00
* @param {Object} payload
2019-07-20 19:32:30 +10:00
* @param {string=} callbackID
*/
2019-07-12 10:12:15 +10:00
export function SendMessage(type, payload, callbackID) {
2019-07-13 22:12:49 +10:00
const message = {
type,
callbackID,
payload
};
2019-07-13 15:30:52 +10:00
2019-07-13 22:12:49 +10:00
Invoke(JSON.stringify(message));
2019-07-12 10:12:15 +10:00
}