Skip to content

Commit

Permalink
fix(core): support onerror for js
Browse files Browse the repository at this point in the history
  • Loading branch information
etkmao committed Aug 30, 2023
1 parent f47ca71 commit 1d72a4b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
95 changes: 88 additions & 7 deletions driver/js/lib/global/Others.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ global.__GLOBAL__ = {
globalErrorHandle: [],
};

class ErrorEvent {
constructor(message, filename, lineno, colno, error) {
this.message = message;
this.filename = filename;
this.lineno = lineno;
this.colno = colno;
this.error = error;
}
}

/**
* Register the Hippy app entry function, the native will trigger an event to execute the function
* and start the app.
Expand Down Expand Up @@ -140,19 +150,88 @@ Object.defineProperty(Hippy, 'onerror', {
* @param {any} args - Event callback arguments: event, source, lineno, colno, error.
*/
function emitError(...args) {
if (args.length !== 5) {
throw new TypeError('Hippy.emitError only accept 5 params');
}

let errObj = new Error();
errObj.message = JSON.stringify(args[4]);

const listeners = __GLOBAL__.globalErrorHandle;
if (!listeners) {
if (listeners) {
try {
listeners.forEach(listener => listener(args[0], args[1], args[2], args[3], errObj));
} catch (err) {
/* eslint-disable-next-line no-console */
console.error(err);
}
} else {
if (args[0]) {
console.error(args[0].toString());
}
return;
}
try {
listeners.forEach(listener => listener(...args));
} catch (err) {
/* eslint-disable-next-line no-console */
console.error(err);

const eventName = 'error';
const eventListeners = __GLOBAL__.globalEventHandle[eventName];
if (eventListeners) {
try {
let event = new ErrorEvent(args[0], args[1], args[2], args[3], errObj);
eventListeners.forEach(listener => listener(event));
} catch (err) {
/* eslint-disable-next-line no-console */
console.error(err);
}
} else {
if (args[0]) {
console.error(args[0].toString());
}
}
}

/**
* Register a listener for a specific event, and the listener will be called
* when the event is triggered.
*
* @param {string} eventName - The event name will be registered.
* @param {Function} listener - Event callback.
* @returns {Set} - Set of event listeners
*/
function addEventListener(eventName, listener) {
if (typeof eventName !== 'string' || typeof listener !== 'function') {
throw new TypeError('Hippy.addEventListener() only accept a string as event name and a function as listener');
}

let eventListeners = __GLOBAL__.globalEventHandle[eventName];
if (!(eventListeners instanceof Set)) {
__GLOBAL__.globalEventHandle[eventName] = new Set();
eventListeners = __GLOBAL__.globalEventHandle[eventName];
}
eventListeners.add(listener);
return eventListeners;
}

/**
* Remove specific event listener,
*
* @param {string} eventName - The event name will be removed.
* @param {Function} listener - Specific event callback will be removed,
* the listeners will clean all if not specific.
* @returns {Set | null} - Set of event listeners, or null of empty.
*/
function removeEventListener(eventName, listener) {
if (typeof eventName !== 'string') {
throw new TypeError('Hippy.removeEventListener() only accept a string as event name');
}
const eventListeners = __GLOBAL__.globalEventHandle[eventName];
if (!(eventListeners instanceof Set)) {
return null;
}
if (listener) {
eventListeners.delete(listener);
return eventListeners;
}
eventListeners.clear();
return null;
}

Hippy.device = {};
Expand All @@ -164,3 +243,5 @@ Hippy.on = on;
Hippy.off = off;
Hippy.emit = emit;
Hippy.emitError = emitError;
Hippy.addEventListener = addEventListener;
Hippy.removeEventListener = removeEventListener;
2 changes: 1 addition & 1 deletion driver/js/src/vm/jsc/native_source_code_ios.cc

Large diffs are not rendered by default.

Loading

0 comments on commit 1d72a4b

Please sign in to comment.