5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 19:19:15 +08:00
wails/v2/internal/frontend/runtime/desktop/events.js
Lea Anthony 583b3cd1c3
New Crowdin updates (#1800)
* New translations markdown-page.md (Acholi)
[ci skip]

* New translations stats.mdx (Acholi)
[ci skip]

* New translations code.json (Acholi)
[ci skip]

* New translations options.json (Acholi)
[ci skip]

* New translations current.json (Acholi)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Acholi)
[ci skip]

* New translations helloworld.mdx (Acholi)
[ci skip]

* New translations footer.json (Acholi)
[ci skip]

* New translations navbar.json (Acholi)
[ci skip]

* New translations emailit.mdx (Acholi)
[ci skip]

* New translations modalfilemanager.mdx (Acholi)
[ci skip]

* New translations linux.mdx (Acholi)
[ci skip]

* New translations scriptbar.mdx (Acholi)
[ci skip]

* New translations minecraftupdater.mdx (Acholi)
[ci skip]

* New translations faq.mdx (Acholi)
[ci skip]

* New translations community-guide.mdx (Acholi)
[ci skip]

* New translations vscode.mdx (Acholi)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations templates.mdx (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations troubleshooting.mdx (Acholi)
[ci skip]

* New translations windows-installer.mdx (Acholi)
[ci skip]

* New translations windows.mdx (Acholi)
[ci skip]

* New translations howdoesitwork.mdx (Acholi)
[ci skip]

* New translations introduction.mdx (Acholi)
[ci skip]

* New translations cli.mdx (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations window.mdx (Acholi)
[ci skip]

* New translations menus.mdx (Acholi)
[ci skip]

* New translations options.mdx (Acholi)
[ci skip]

* New translations project-config.mdx (Acholi)
[ci skip]

* New translations _category_.json (Acholi)
[ci skip]

* New translations browser.mdx (Acholi)
[ci skip]

* New translations dialog.mdx (Acholi)
[ci skip]

* New translations events.mdx (Acholi)
[ci skip]

* New translations intro.mdx (Acholi)
[ci skip]

* New translations log.mdx (Acholi)
[ci skip]

* New translations menu.mdx (Acholi)
[ci skip]

* New translations dogsapi.mdx (Acholi)
[ci skip]

* New translations templates.mdx (Chinese Simplified)
[ci skip]

* New translations templates.mdx (Russian)
[ci skip]

* New translations templates.mdx (Japanese)
[ci skip]

* New translations templates.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations templates.mdx (Acholi)
[ci skip]

* New translations encrypteasy.mdx (Japanese)
[ci skip]

* New translations wally.mdx (Korean)
[ci skip]

* New translations wally.mdx (Japanese)
[ci skip]

* New translations wally.mdx (Russian)
[ci skip]

* New translations wally.mdx (Chinese Simplified)
[ci skip]

* New translations wombat.mdx (Japanese)
[ci skip]

* New translations wombat.mdx (Korean)
[ci skip]

* New translations wombat.mdx (Russian)
[ci skip]

* New translations wombat.mdx (Chinese Simplified)
[ci skip]

* New translations ytd.mdx (Japanese)
[ci skip]

* New translations ytd.mdx (Korean)
[ci skip]

* New translations ytd.mdx (Russian)
[ci skip]

* New translations ytd.mdx (Chinese Simplified)
[ci skip]

* New translations encrypteasy.mdx (Korean)
[ci skip]

* New translations october.mdx (Japanese)
[ci skip]

* New translations encrypteasy.mdx (Russian)
[ci skip]

* New translations encrypteasy.mdx (Chinese Simplified)
[ci skip]

* New translations filehound.mdx (Japanese)
[ci skip]

* New translations filehound.mdx (Korean)
[ci skip]

* New translations filehound.mdx (Russian)
[ci skip]

* New translations filehound.mdx (Chinese Simplified)
[ci skip]

* New translations mollywallet.mdx (Japanese)
[ci skip]

* New translations mollywallet.mdx (Korean)
[ci skip]

* New translations mollywallet.mdx (Russian)
[ci skip]

* New translations mollywallet.mdx (Chinese Simplified)
[ci skip]

* New translations october.mdx (Korean)
[ci skip]

* New translations october.mdx (Russian)
[ci skip]

* New translations october.mdx (Chinese Simplified)
[ci skip]

* New translations optimus.mdx (Japanese)
[ci skip]

* New translations optimus.mdx (Korean)
[ci skip]

* New translations optimus.mdx (Russian)
[ci skip]

* New translations optimus.mdx (Chinese Simplified)
[ci skip]

* New translations portfall.mdx (Japanese)
[ci skip]

* New translations portfall.mdx (Korean)
[ci skip]

* New translations portfall.mdx (Russian)
[ci skip]

* New translations portfall.mdx (Chinese Simplified)
[ci skip]

* New translations scriptbar.mdx (Russian)
[ci skip]

* New translations emailit.mdx (Japanese)
[ci skip]

* New translations emailit.mdx (Korean)
[ci skip]

* New translations emailit.mdx (Russian)
[ci skip]

* New translations emailit.mdx (Chinese Simplified)
[ci skip]

* New translations modalfilemanager.mdx (Japanese)
[ci skip]

* New translations modalfilemanager.mdx (Korean)
[ci skip]

* New translations modalfilemanager.mdx (Russian)
[ci skip]

* New translations modalfilemanager.mdx (Chinese Simplified)
[ci skip]

* New translations scriptbar.mdx (Japanese)
[ci skip]

* New translations scriptbar.mdx (Korean)
[ci skip]

* New translations scriptbar.mdx (Chinese Simplified)
[ci skip]

* New translations minecraftupdater.mdx (Japanese)
[ci skip]

* New translations minecraftupdater.mdx (Korean)
[ci skip]

* New translations minecraftupdater.mdx (Russian)
[ci skip]

* New translations minecraftupdater.mdx (Chinese Simplified)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations optimus.mdx (Acholi)
[ci skip]

* New translations portfall.mdx (Acholi)
[ci skip]

* New translations wally.mdx (Acholi)
[ci skip]

* New translations october.mdx (Acholi)
[ci skip]

* New translations mollywallet.mdx (Acholi)
[ci skip]

* New translations filehound.mdx (Acholi)
[ci skip]

* New translations encrypteasy.mdx (Acholi)
[ci skip]

* New translations wombat.mdx (Acholi)
[ci skip]

* New translations ytd.mdx (Acholi)
[ci skip]

* New translations scriptbar.mdx (Acholi)
[ci skip]

* New translations minecraftupdater.mdx (Acholi)
[ci skip]

* New translations modalfilemanager.mdx (Acholi)
[ci skip]

* New translations emailit.mdx (Acholi)
[ci skip]

* New translations troubleshooting.mdx (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Russian)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations troubleshooting.mdx (Acholi)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations building.mdx (Japanese)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Japanese)
[ci skip]

* New translations howdoesitwork.mdx (Chinese Simplified)
[ci skip]

* New translations introduction.mdx (Japanese)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations introduction.mdx (Russian)
[ci skip]

* New translations introduction.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Japanese)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Korean)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Russian)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Korean)
[ci skip]

* New translations howdoesitwork.mdx (Korean)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Russian)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Japanese)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Korean)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Russian)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations dogsapi.mdx (Japanese)
[ci skip]

* New translations dogsapi.mdx (Korean)
[ci skip]

* New translations dogsapi.mdx (Russian)
[ci skip]

* New translations dogsapi.mdx (Chinese Simplified)
[ci skip]

* New translations howdoesitwork.mdx (Russian)
[ci skip]

* New translations howdoesitwork.mdx (Japanese)
[ci skip]

* New translations building.mdx (Korean)
[ci skip]

* New translations ides.mdx (Chinese Simplified)
[ci skip]

* New translations building.mdx (Russian)
[ci skip]

* New translations building.mdx (Chinese Simplified)
[ci skip]

* New translations installation.mdx (Korean)
[ci skip]

* New translations dynamic-assets.mdx (Japanese)
[ci skip]

* New translations dynamic-assets.mdx (Korean)
[ci skip]

* New translations dynamic-assets.mdx (Russian)
[ci skip]

* New translations dynamic-assets.mdx (Chinese Simplified)
[ci skip]

* New translations ides.mdx (Japanese)
[ci skip]

* New translations ides.mdx (Korean)
[ci skip]

* New translations ides.mdx (Russian)
[ci skip]

* New translations windows-installer.mdx (Japanese)
[ci skip]

* New translations helloworld.mdx (Chinese Simplified)
[ci skip]

* New translations windows-installer.mdx (Korean)
[ci skip]

* New translations windows-installer.mdx (Russian)
[ci skip]

* New translations windows-installer.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Japanese)
[ci skip]

* New translations options.mdx (Korean)
[ci skip]

* New translations options.mdx (Russian)
[ci skip]

* New translations options.mdx (Chinese Simplified)
[ci skip]

* New translations helloworld.mdx (Japanese)
[ci skip]

* New translations helloworld.mdx (Korean)
[ci skip]

* New translations helloworld.mdx (Russian)
[ci skip]

* New translations building.mdx (Acholi)
[ci skip]

* New translations dynamic-assets.mdx (Acholi)
[ci skip]

* New translations ides.mdx (Acholi)
[ci skip]

* New translations windows-installer.mdx (Acholi)
[ci skip]

* New translations howdoesitwork.mdx (Acholi)
[ci skip]

* New translations introduction.mdx (Acholi)
[ci skip]

* New translations options.mdx (Acholi)
[ci skip]

* New translations helloworld.mdx (Acholi)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Acholi)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Acholi)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Acholi)
[ci skip]

* New translations dogsapi.mdx (Acholi)
[ci skip]

* New translations troubleshooting.mdx (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Russian)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations window.mdx (Japanese)
[ci skip]

* New translations window.mdx (Korean)
[ci skip]

* New translations window.mdx (Russian)
[ci skip]

* New translations window.mdx (Chinese Simplified)
[ci skip]

* New translations troubleshooting.mdx (Acholi)
[ci skip]

* New translations window.mdx (Acholi)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations installation.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Russian)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations window.mdx (Japanese)
[ci skip]

* New translations window.mdx (Korean)
[ci skip]

* New translations window.mdx (Russian)
[ci skip]

* New translations window.mdx (Chinese Simplified)
[ci skip]

* New translations navbar.json (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Acholi)
[ci skip]

* New translations navbar.json (Chinese Simplified)
[ci skip]

* New translations navbar.json (Russian)
[ci skip]

* New translations navbar.json (Korean)
[ci skip]

* New translations navbar.json (Acholi)
[ci skip]

* New translations events.mdx (Japanese)
[ci skip]

* New translations events.mdx (Korean)
[ci skip]

* New translations events.mdx (Russian)
[ci skip]

* New translations events.mdx (Chinese Simplified)
[ci skip]

* New translations events.mdx (Acholi)
[ci skip]

* New translations howdoesitwork.mdx (Japanese)
[ci skip]

* New translations building.mdx (Korean)
[ci skip]

* New translations development.mdx (Korean)
[ci skip]

* New translations overscroll.mdx (Japanese)
[ci skip]

* New translations overscroll.mdx (Korean)
[ci skip]

* New translations overscroll.mdx (Russian)
[ci skip]

* New translations overscroll.mdx (Chinese Simplified)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations overscroll.mdx (Acholi)
[ci skip]

* New translations bleeding-edge.mdx (Japanese)
[ci skip]

* New translations bleeding-edge.mdx (Korean)
[ci skip]

* New translations bleeding-edge.mdx (Russian)
[ci skip]

* New translations bleeding-edge.mdx (Chinese Simplified)
[ci skip]

* New translations bleeding-edge.mdx (Acholi)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Russian)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations howdoesitwork.mdx (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Acholi)
[ci skip]

* New translations frameless.mdx (Japanese)
[ci skip]

* New translations frameless.mdx (Korean)
[ci skip]

* New translations frameless.mdx (Russian)
[ci skip]

* New translations frameless.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Japanese)
[ci skip]

* New translations options.mdx (Korean)
[ci skip]

* New translations options.mdx (Russian)
[ci skip]

* New translations options.mdx (Chinese Simplified)
[ci skip]

* New translations frameless.mdx (Acholi)
[ci skip]

* New translations options.mdx (Acholi)
[ci skip]

* Fix issues again
2022-09-12 21:56:43 +10:00

176 lines
4.6 KiB
JavaScript

/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/
/* jshint esversion: 6 */
// Defines a single listener with a maximum number of times to callback
/**
* The Listener class defines a listener! :-)
*
* @class Listener
*/
class Listener {
/**
* Creates an instance of Listener.
* @param {function} callback
* @param {number} maxCallbacks
* @memberof Listener
*/
constructor(callback, maxCallbacks) {
// Default of -1 means infinite
maxCallbacks = maxCallbacks || -1;
// Callback invokes the callback with the given data
// Returns true if this listener should be destroyed
this.Callback = (data) => {
callback.apply(null, data);
// If maxCallbacks is infinite, return false (do not destroy)
if (maxCallbacks === -1) {
return false;
}
// Decrement maxCallbacks. Return true if now 0, otherwise false
maxCallbacks -= 1;
return maxCallbacks === 0;
};
}
}
export const eventListeners = {};
/**
* Registers an event listener that will be invoked `maxCallbacks` times before being destroyed
*
* @export
* @param {string} eventName
* @param {function} callback
* @param {number} maxCallbacks
*/
export function EventsOnMultiple(eventName, callback, maxCallbacks) {
eventListeners[eventName] = eventListeners[eventName] || [];
const thisListener = new Listener(callback, maxCallbacks);
eventListeners[eventName].push(thisListener);
}
/**
* Registers an event listener that will be invoked every time the event is emitted
*
* @export
* @param {string} eventName
* @param {function} callback
*/
export function EventsOn(eventName, callback) {
EventsOnMultiple(eventName, callback, -1);
}
/**
* Registers an event listener that will be invoked once then destroyed
*
* @export
* @param {string} eventName
* @param {function} callback
*/
export function EventsOnce(eventName, callback) {
EventsOnMultiple(eventName, callback, 1);
}
function notifyListeners(eventData) {
// Get the event name
let eventName = eventData.name;
// Check if we have any listeners for this event
if (eventListeners[eventName]) {
// Keep a list of listener indexes to destroy
const newEventListenerList = eventListeners[eventName].slice();
// Iterate listeners
for (let count = 0; count < eventListeners[eventName].length; count += 1) {
// Get next listener
const listener = eventListeners[eventName][count];
let data = eventData.data;
// Do the callback
const destroy = listener.Callback(data);
if (destroy) {
// if the listener indicated to destroy itself, add it to the destroy list
newEventListenerList.splice(count, 1);
}
}
// Update callbacks with new list of listeners
eventListeners[eventName] = newEventListenerList;
}
}
/**
* Notify informs frontend listeners that an event was emitted with the given data
*
* @export
* @param {string} notifyMessage - encoded notification message
*/
export function EventsNotify(notifyMessage) {
// Parse the message
let message;
try {
message = JSON.parse(notifyMessage);
} catch (e) {
const error = 'Invalid JSON passed to Notify: ' + notifyMessage;
throw new Error(error);
}
notifyListeners(message);
}
/**
* Emit an event with the given name and data
*
* @export
* @param {string} eventName
*/
export function EventsEmit(eventName) {
const payload = {
name: eventName,
data: [].slice.apply(arguments).slice(1),
};
// Notify JS listeners
notifyListeners(payload);
// Notify Go listeners
window.WailsInvoke('EE' + JSON.stringify(payload));
}
function removeListener(eventName) {
// Remove local listeners
delete eventListeners[eventName];
// Notify Go listeners
window.WailsInvoke('EX' + eventName);
}
/**
* Off unregisters a listener previously registered with On,
* optionally multiple listeneres can be unregistered via `additionalEventNames`
*
* @param {string} eventName
* @param {...string} additionalEventNames
*/
export function EventsOff(eventName, ...additionalEventNames) {
removeListener(eventName)
if (additionalEventNames.length > 0) {
additionalEventNames.forEach(eventName => {
removeListener(eventName)
})
}
}