-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathweb-plugin.ts
More file actions
147 lines (117 loc) · 4.12 KB
/
web-plugin.ts
File metadata and controls
147 lines (117 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { PluginListenerHandle, Plugin } from './definitions';
import { Capacitor } from './global';
import { ExceptionCode } from './util';
import type { CapacitorException } from './util';
/**
* Base class web plugins should extend.
*/
export class WebPlugin implements Plugin {
protected listeners: { [eventName: string]: ListenerCallback[] } = {};
protected retainedEventArguments: { [eventName: string]: any[] } = {};
protected windowListeners: { [eventName: string]: WindowListenerHandle } = {};
addListener(eventName: string, listenerFunc: ListenerCallback): Promise<PluginListenerHandle> {
let firstListener = false;
const listeners = this.listeners[eventName];
if (!listeners) {
this.listeners[eventName] = [];
firstListener = true;
}
this.listeners[eventName].push(listenerFunc);
// If we haven't added a window listener for this event and it requires one,
// go ahead and add it
const windowListener = this.windowListeners[eventName];
if (windowListener && !windowListener.registered) {
this.addWindowListener(windowListener);
}
if (firstListener) {
this.sendRetainedArgumentsForEvent(eventName);
}
const remove = async () => this.removeListener(eventName, listenerFunc);
const p: any = Promise.resolve({ remove });
return p;
}
async removeAllListeners(): Promise<void> {
this.listeners = {};
for (const listener in this.windowListeners) {
this.removeWindowListener(this.windowListeners[listener]);
}
this.windowListeners = {};
}
protected notifyListeners(eventName: string, data: any, retainUntilConsumed?: boolean): void {
const listeners = this.listeners[eventName];
if (!listeners) {
if (retainUntilConsumed) {
let args = this.retainedEventArguments[eventName];
if (!args) {
args = [];
}
args.push(data);
this.retainedEventArguments[eventName] = args;
}
return;
}
listeners.forEach((listener) => listener(data));
}
protected hasListeners(eventName: string): boolean {
return !!this.listeners[eventName]?.length;
}
protected registerWindowListener(windowEventName: string, pluginEventName: string): void {
this.windowListeners[pluginEventName] = {
registered: false,
windowEventName,
pluginEventName,
handler: (event) => {
this.notifyListeners(pluginEventName, event);
},
};
}
protected unimplemented(msg = 'not implemented'): CapacitorException {
return new Capacitor.Exception(msg, ExceptionCode.Unimplemented);
}
protected unavailable(msg = 'not available'): CapacitorException {
return new Capacitor.Exception(msg, ExceptionCode.Unavailable);
}
private async removeListener(eventName: string, listenerFunc: ListenerCallback): Promise<void> {
const listeners = this.listeners[eventName];
if (!listeners) {
return;
}
const index = listeners.indexOf(listenerFunc);
if (index !== -1) {
this.listeners[eventName].splice(index, 1);
}
// If there are no more listeners for this type of event,
// remove the window listener
if (!this.listeners[eventName].length) {
this.removeWindowListener(this.windowListeners[eventName]);
}
}
private addWindowListener(handle: WindowListenerHandle): void {
window.addEventListener(handle.windowEventName, handle.handler);
handle.registered = true;
}
private removeWindowListener(handle: WindowListenerHandle): void {
if (!handle) {
return;
}
window.removeEventListener(handle.windowEventName, handle.handler);
handle.registered = false;
}
private sendRetainedArgumentsForEvent(eventName: string): void {
const args = this.retainedEventArguments[eventName];
if (!args) {
return;
}
delete this.retainedEventArguments[eventName];
args.forEach((arg) => {
this.notifyListeners(eventName, arg);
});
}
}
export type ListenerCallback = (err: any, ...args: any[]) => void;
export interface WindowListenerHandle {
registered: boolean;
windowEventName: string;
pluginEventName: string;
handler: (event: any) => void;
}