MCPcopy Index your code
hub / github.com/ionic-team/capacitor / initEvents

Function initEvents

core/native-bridge.ts:166–241  ·  view source on GitHub ↗
(win: WindowCapacitor, cap: CapacitorInstance)

Source from the content-addressed store, hash-verified

164 };
165
166 const initEvents = (win: WindowCapacitor, cap: CapacitorInstance) => {
167 cap.addListener = (pluginName, eventName, callback) => {
168 const callbackId = cap.nativeCallback(
169 pluginName,
170 'addListener',
171 {
172 eventName: eventName,
173 },
174 callback,
175 );
176 return {
177 remove: async () => {
178 win?.console?.debug('Removing listener', pluginName, eventName);
179 cap.removeListener(pluginName, callbackId, eventName, callback);
180 },
181 };
182 };
183
184 cap.removeListener = (pluginName, callbackId, eventName, callback) => {
185 cap.nativeCallback(
186 pluginName,
187 'removeListener',
188 {
189 callbackId: callbackId,
190 eventName: eventName,
191 },
192 callback,
193 );
194 };
195
196 cap.createEvent = (eventName, eventData) => {
197 const doc = win.document;
198 if (doc) {
199 const ev = doc.createEvent('Events');
200 ev.initEvent(eventName, false, false);
201 if (eventData && typeof eventData === 'object') {
202 for (const i in eventData) {
203 // eslint-disable-next-line no-prototype-builtins
204 if (eventData.hasOwnProperty(i)) {
205 ev[i] = eventData[i];
206 }
207 }
208 }
209 return ev;
210 }
211 return null;
212 };
213
214 cap.triggerEvent = (eventName, target, eventData) => {
215 const doc = win.document;
216 const cordova = win.cordova;
217 eventData = eventData || {};
218 const ev = cap.createEvent(eventName, eventData);
219
220 if (ev) {
221 if (target === 'document') {
222 if (cordova?.fireDocumentEvent) {
223 cordova.fireDocumentEvent(eventName, eventData);

Callers 1

initNativeBridgeFunction · 0.70

Calls 2

debugMethod · 0.80
removeListenerMethod · 0.45

Tested by

no test coverage detected