|
19 | 19 | #include "jswrapper.h" |
20 | 20 | #include "jswrap_stream.h" |
21 | 21 | #include "jswrap_functions.h" |
| 22 | +#include "jswrap_array.h" // for splice |
22 | 23 | #ifdef __MINGW32__ |
23 | 24 | #include "malloc.h" // needed for alloca |
24 | 25 | #endif//__MINGW32__ |
@@ -889,23 +890,22 @@ void jswrap_object_on_X(JsVar *parent, JsVar *event, JsVar *listener, bool addFi |
889 | 890 | JsVar *eventList = jsvFindChildFromVar(parent, eventName, true); |
890 | 891 | jsvUnLock(eventName); |
891 | 892 | JsVar *eventListeners = jsvSkipName(eventList); |
| 893 | + // if no array, add it |
892 | 894 | if (jsvIsUndefined(eventListeners)) { |
893 | | - // just add the one handler on its own |
894 | | - jsvSetValueOfName(eventList, listener); |
895 | | - } else { |
896 | | - // we already have an array and we just add to it |
897 | | - // OR it's not an array but we need to make it an array |
898 | | - JsVar *arr = jsvNewEmptyArray(); |
899 | | - if (addFirst) jsvArrayPush(arr, listener); |
900 | | - if (jsvIsArray(eventListeners)) |
901 | | - jsvArrayPushAll(arr, eventListeners, false); |
902 | | - else |
903 | | - jsvArrayPush(arr, eventListeners); |
904 | | - if (!addFirst) jsvArrayPush(arr, listener); |
905 | | - jsvSetValueOfName(eventList, arr); |
906 | | - jsvUnLock(arr); |
| 895 | + eventListeners = jsvNewEmptyArray(); |
| 896 | + jsvSetValueOfName(eventList, eventListeners); |
| 897 | + } |
| 898 | + jsvUnLock(eventList); |
| 899 | + if (!eventListeners) return; // out of memory |
| 900 | + // now have an array and we just add to it |
| 901 | + if (addFirst) { // add it first? |
| 902 | + JsVar *elements = jsvNewArray(&listener, 1); |
| 903 | + jswrap_array_unshift(eventListeners, elements); |
| 904 | + jsvUnLock(elements); |
| 905 | + } else { // or add it at the end |
| 906 | + jsvArrayPush(eventListeners, listener); |
907 | 907 | } |
908 | | - jsvUnLock2(eventListeners, eventList); |
| 908 | + jsvUnLock(eventListeners); |
909 | 909 | /* Special case if we're a data listener and data has already arrived then |
910 | 910 | * we queue an event immediately. */ |
911 | 911 | if (jsvIsStringEqual(event, "data")) { |
@@ -1033,16 +1033,12 @@ void jswrap_object_removeListener(JsVar *parent, JsVar *event, JsVar *callback) |
1033 | 1033 | jsvUnLock(eventName); |
1034 | 1034 | JsVar *eventList = jsvSkipName(eventListName); |
1035 | 1035 | if (eventList) { |
1036 | | - if (eventList == callback) { |
1037 | | - // there's no array, it was a single item |
1038 | | - jsvRemoveChild(parent, eventListName); |
1039 | | - } else if (jsvIsArray(eventList)) { |
| 1036 | + if (jsvIsArray(eventList)) { |
1040 | 1037 | // it's an array, search for the index |
1041 | 1038 | JsVar *idx = jsvGetIndexOf(eventList, callback, true); |
1042 | | - if (idx) { |
| 1039 | + if (idx) |
1043 | 1040 | jsvRemoveChildAndUnLock(eventList, idx); |
1044 | | - } |
1045 | | - } |
| 1041 | + } // otherwise something is wrong, but lets just ignore it |
1046 | 1042 | jsvUnLock(eventList); |
1047 | 1043 | } |
1048 | 1044 | jsvUnLock(eventListName); |
|
0 commit comments