5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 06:01:52 +08:00

[v2] feature: make EventsOff capable of removing multiple listeners (#1822)

[v2] feature: add missing wrapper
This commit is contained in:
Zámbó, Levente 2022-09-01 23:34:59 +02:00 committed by GitHub
parent 01e46f0c24
commit 7816b0b67f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 69 deletions

View File

@ -150,10 +150,27 @@ export function EventsEmit(eventName) {
window.WailsInvoke('EE' + JSON.stringify(payload));
}
export function EventsOff(eventName) {
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)
})
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -50,7 +50,7 @@ export function EventsOnce(eventName: string, callback: (...data: any) => void):
// [EventsOff](https://wails.io/docs/reference/runtime/events#eventsff)
// unregisters the listener for the given event name.
export function EventsOff(eventName: string): void;
export function EventsOff(eventName: string, ...additionalEventNames: string[]): void;
// [LogPrint](https://wails.io/docs/reference/runtime/log#logprint)
// logs the given message as a raw message

View File

@ -44,8 +44,8 @@ export function EventsOn(eventName, callback) {
EventsOnMultiple(eventName, callback, -1);
}
export function EventsOff(eventName) {
return window.runtime.EventsOff(eventName);
export function EventsOff(eventName, ...additionalEventNames) {
return window.runtime.EventsOff(eventName, ...additionalEventNames);
}
export function EventsOnce(eventName, callback) {

View File

@ -10,10 +10,16 @@ func EventsOn(ctx context.Context, eventName string, callback func(optionalData
events.On(eventName, callback)
}
// EventsOff unregisters a listener for the given event name
func EventsOff(ctx context.Context, eventName string) {
// EventsOff unregisters a listener for the given event name, optionally multiple listeneres can be unregistered via `additionalEventNames`
func EventsOff(ctx context.Context, eventName string, additionalEventNames ...string) {
events := getEvents(ctx)
events.Off(eventName)
if len(additionalEventNames) > 0 {
for _, eventName := range additionalEventNames {
events.Off(eventName)
}
}
}
// EventsOnce registers a listener for the given event name. After the first callback, the

View File

@ -17,10 +17,10 @@ JS: `EventsOn(eventName string, callback function(optionalData?: any))`
### EventsOff
This method unregisters the listener for the given event name.
This method unregisters the listener for the given event name, optionally multiple listeneres can be unregistered via `additionalEventNames`.
Go: `EventsOff(ctx context.Context, eventName string)`<br/>
JS: `EventsOff(eventName string)`
Go: `EventsOff(ctx context.Context, eventName string, additionalEventNames ...string)`<br/>
JS: `EventsOff(eventName string, ...additionalEventNames)`
### EventsOnce