#12 fix addObserver函数异常错误

This commit is contained in:
yhh
2020-07-27 17:27:32 +08:00
parent 506f8ddc0f
commit 60921f703b
6 changed files with 71 additions and 52 deletions
+7 -3
View File
@@ -214,7 +214,9 @@ Array.prototype.groupBy = function (keySelector) {
var keys_1 = []; var keys_1 = [];
return array.reduce(function (groups, element, index) { return array.reduce(function (groups, element, index) {
var key = JSON.stringify(keySelector.call(arguments[1], element, index, array)); var key = JSON.stringify(keySelector.call(arguments[1], element, index, array));
var index2 = keys_1.findIndex(function (x) { return x === key; }); var index2 = keys_1.findIndex(function (x) {
return x === key;
});
if (index2 < 0) { if (index2 < 0) {
index2 = keys_1.push(key) - 1; index2 = keys_1.push(key) - 1;
} }
@@ -230,7 +232,9 @@ Array.prototype.groupBy = function (keySelector) {
var keys = []; var keys = [];
var _loop_1 = function (i, len) { var _loop_1 = function (i, len) {
var key = JSON.stringify(keySelector.call(arguments_1[1], array[i], i, array)); var key = JSON.stringify(keySelector.call(arguments_1[1], array[i], i, array));
var index = keys.findIndex(function (x) { return x === key; }); var index = keys.findIndex(function (x) {
return x === key;
});
if (index < 0) { if (index < 0) {
index = keys.push(key) - 1; index = keys.push(key) - 1;
} }
@@ -7056,7 +7060,7 @@ var es;
list = []; list = [];
this._messageTable.set(eventType, list); this._messageTable.set(eventType, list);
} }
if (list.contains(handler)) if (list.findIndex(function (funcPack) { return funcPack.func == handler; }) != -1)
console.warn("您试图添加相同的观察者两次"); console.warn("您试图添加相同的观察者两次");
list.push(new FuncPack(handler, context)); list.push(new FuncPack(handler, context));
}; };
File diff suppressed because one or more lines are too long
+7 -3
View File
@@ -214,7 +214,9 @@ Array.prototype.groupBy = function (keySelector) {
var keys_1 = []; var keys_1 = [];
return array.reduce(function (groups, element, index) { return array.reduce(function (groups, element, index) {
var key = JSON.stringify(keySelector.call(arguments[1], element, index, array)); var key = JSON.stringify(keySelector.call(arguments[1], element, index, array));
var index2 = keys_1.findIndex(function (x) { return x === key; }); var index2 = keys_1.findIndex(function (x) {
return x === key;
});
if (index2 < 0) { if (index2 < 0) {
index2 = keys_1.push(key) - 1; index2 = keys_1.push(key) - 1;
} }
@@ -230,7 +232,9 @@ Array.prototype.groupBy = function (keySelector) {
var keys = []; var keys = [];
var _loop_1 = function (i, len) { var _loop_1 = function (i, len) {
var key = JSON.stringify(keySelector.call(arguments_1[1], array[i], i, array)); var key = JSON.stringify(keySelector.call(arguments_1[1], array[i], i, array));
var index = keys.findIndex(function (x) { return x === key; }); var index = keys.findIndex(function (x) {
return x === key;
});
if (index < 0) { if (index < 0) {
index = keys.push(key) - 1; index = keys.push(key) - 1;
} }
@@ -7056,7 +7060,7 @@ var es;
list = []; list = [];
this._messageTable.set(eventType, list); this._messageTable.set(eventType, list);
} }
if (list.contains(handler)) if (list.findIndex(function (funcPack) { return funcPack.func == handler; }) != -1)
console.warn("您试图添加相同的观察者两次"); console.warn("您试图添加相同的观察者两次");
list.push(new FuncPack(handler, context)); list.push(new FuncPack(handler, context));
}; };
+1 -1
View File
File diff suppressed because one or more lines are too long
+54 -43
View File
@@ -4,84 +4,100 @@ declare interface Array<T> {
* @param predicate * @param predicate
*/ */
findIndex(predicate: Function): number; findIndex(predicate: Function): number;
/** /**
* *
* @param predicate * @param predicate
*/ */
any(predicate: Function): boolean; any(predicate: Function): boolean;
/** /**
* *
* @param predicate * @param predicate
*/ */
firstOrDefault(predicate: Function): T; firstOrDefault(predicate: Function): T;
/** /**
* *
* @param predicate * @param predicate
*/ */
find(predicate: Function): T; find(predicate: Function): T;
/** /**
* *
* @param predicate * @param predicate
*/ */
where(predicate: Function): Array<T>; where(predicate: Function): Array<T>;
/** /**
* *
* @param predicate * @param predicate
*/ */
count(predicate: Function): number; count(predicate: Function): number;
/** /**
* *
* @param predicate * @param predicate
*/ */
findAll(predicate: Function): Array<T>; findAll(predicate: Function): Array<T>;
/** /**
* *
* @param value * @param value
*/ */
contains(value): boolean; contains(value): boolean;
/** /**
* *
* @param predicate * @param predicate
*/ */
removeAll(predicate: Function): void; removeAll(predicate: Function): void;
/** /**
* *
* @param element * @param element
*/ */
remove(element: T): boolean; remove(element: T): boolean;
/** /**
* *
* @param index * @param index
*/ */
removeAt(index): void; removeAt(index): void;
/** /**
* *
* @param index * @param index
* @param count * @param count
*/ */
removeRange(index, count): void; removeRange(index, count): void;
/** /**
* *
* @param selector * @param selector
*/ */
select(selector: Function): Array<T>; select(selector: Function): Array<T>;
/** /**
* *
* @param keySelector key选择器 * @param keySelector key选择器
* @param comparer * @param comparer
*/ */
orderBy(keySelector: Function, comparer: Function): Array<T>; orderBy(keySelector: Function, comparer: Function): Array<T>;
/** /**
* *
* @param keySelector key选择器 * @param keySelector key选择器
* @param comparer * @param comparer
*/ */
orderByDescending(keySelector: Function, comparer: Function): Array<T>; orderByDescending(keySelector: Function, comparer: Function): Array<T>;
/** /**
* *
* @param keySelector key选择器 * @param keySelector key选择器
*/ */
groupBy(keySelector: Function): Array<T>; groupBy(keySelector: Function): Array<T>;
/** /**
* *
* @param selector * @param selector
@@ -101,7 +117,7 @@ Array.prototype.findIndex = function (predicate) {
} }
return findIndex(this, predicate); return findIndex(this, predicate);
} };
Array.prototype.any = function (predicate) { Array.prototype.any = function (predicate) {
function any(array, predicate) { function any(array, predicate) {
@@ -109,7 +125,7 @@ Array.prototype.any = function (predicate) {
} }
return any(this, predicate); return any(this, predicate);
} };
Array.prototype.firstOrDefault = function (predicate) { Array.prototype.firstOrDefault = function (predicate) {
function firstOrDefault(array, predicate) { function firstOrDefault(array, predicate) {
@@ -118,7 +134,7 @@ Array.prototype.firstOrDefault = function (predicate) {
} }
return firstOrDefault(this, predicate); return firstOrDefault(this, predicate);
} };
Array.prototype.find = function (predicate) { Array.prototype.find = function (predicate) {
function find(array, predicate) { function find(array, predicate) {
@@ -126,7 +142,7 @@ Array.prototype.find = function (predicate) {
} }
return find(this, predicate); return find(this, predicate);
} };
Array.prototype.where = function (predicate) { Array.prototype.where = function (predicate) {
function where(array, predicate) { function where(array, predicate) {
@@ -138,8 +154,7 @@ Array.prototype.where = function (predicate) {
return ret; return ret;
}, []); }, []);
} } else {
else {
let ret = []; let ret = [];
for (let i = 0, len = array.length; i < len; i++) { for (let i = 0, len = array.length; i < len; i++) {
let element = array[i]; let element = array[i];
@@ -153,7 +168,7 @@ Array.prototype.where = function (predicate) {
} }
return where(this, predicate); return where(this, predicate);
} };
Array.prototype.count = function (predicate) { Array.prototype.count = function (predicate) {
function count(array, predicate) { function count(array, predicate) {
@@ -161,7 +176,7 @@ Array.prototype.count = function (predicate) {
} }
return count(this, predicate); return count(this, predicate);
} };
Array.prototype.findAll = function (predicate) { Array.prototype.findAll = function (predicate) {
function findAll(array, predicate) { function findAll(array, predicate) {
@@ -169,7 +184,7 @@ Array.prototype.findAll = function (predicate) {
} }
return findAll(this, predicate); return findAll(this, predicate);
} };
Array.prototype.contains = function (value) { Array.prototype.contains = function (value) {
function contains(array, value) { function contains(array, value) {
@@ -183,7 +198,7 @@ Array.prototype.contains = function (value) {
} }
return contains(this, value); return contains(this, value);
} };
Array.prototype.removeAll = function (predicate) { Array.prototype.removeAll = function (predicate) {
function removeAll(array, predicate) { function removeAll(array, predicate) {
@@ -198,7 +213,7 @@ Array.prototype.removeAll = function (predicate) {
} }
removeAll(this, predicate); removeAll(this, predicate);
} };
Array.prototype.remove = function (element) { Array.prototype.remove = function (element) {
function remove(array, element) { function remove(array, element) {
@@ -209,14 +224,13 @@ Array.prototype.remove = function (element) {
if (index >= 0) { if (index >= 0) {
array.splice(index, 1); array.splice(index, 1);
return true; return true;
} } else {
else {
return false; return false;
} }
} }
return remove(this, element); return remove(this, element);
} };
Array.prototype.removeAt = function (index) { Array.prototype.removeAt = function (index) {
function removeAt(array, index) { function removeAt(array, index) {
@@ -224,7 +238,7 @@ Array.prototype.removeAt = function (index) {
} }
return removeAt(this, index); return removeAt(this, index);
} };
Array.prototype.removeRange = function (index, count) { Array.prototype.removeRange = function (index, count) {
function removeRange(array, index, count) { function removeRange(array, index, count) {
@@ -232,7 +246,7 @@ Array.prototype.removeRange = function (index, count) {
} }
return removeRange(this, index, count); return removeRange(this, index, count);
} };
Array.prototype.select = function (selector) { Array.prototype.select = function (selector) {
function select(array, selector) { function select(array, selector) {
@@ -241,8 +255,7 @@ Array.prototype.select = function (selector) {
ret.push(selector.call(arguments[2], element, index, array)); ret.push(selector.call(arguments[2], element, index, array));
return ret; return ret;
}, []); }, []);
} } else {
else {
let ret = []; let ret = [];
for (let i = 0, len = array.length; i < len; i++) { for (let i = 0, len = array.length; i < len; i++) {
ret.push(selector.call(arguments[2], array[i], i, array)) ret.push(selector.call(arguments[2], array[i], i, array))
@@ -253,17 +266,16 @@ Array.prototype.select = function (selector) {
} }
return select(this, selector); return select(this, selector);
} };
Array.prototype.orderBy = function (keySelector, comparer) { Array.prototype.orderBy = function (keySelector, comparer) {
function orderBy(array, keySelector, comparer) { function orderBy(array, keySelector, comparer) {
array.sort(function (x, y) { array.sort(function (x, y) {
let v1 = keySelector(x) let v1 = keySelector(x);
let v2 = keySelector(y) let v2 = keySelector(y);
if (comparer) { if (comparer) {
return comparer(v1, v2); return comparer(v1, v2);
} } else {
else {
return (v1 > v2) ? 1 : -1; return (v1 > v2) ? 1 : -1;
} }
}); });
@@ -272,17 +284,16 @@ Array.prototype.orderBy = function (keySelector, comparer) {
} }
return orderBy(this, keySelector, comparer); return orderBy(this, keySelector, comparer);
} };
Array.prototype.orderByDescending = function (keySelector, comparer) { Array.prototype.orderByDescending = function (keySelector, comparer) {
function orderByDescending(array, keySelector, comparer) { function orderByDescending(array, keySelector, comparer) {
array.sort(function (x, y) { array.sort(function (x, y) {
let v1 = keySelector(x) let v1 = keySelector(x);
let v2 = keySelector(y) let v2 = keySelector(y);
if (comparer) { if (comparer) {
return -comparer(v1, v2); return -comparer(v1, v2);
} } else {
else {
return (v1 < v2) ? 1 : -1; return (v1 < v2) ? 1 : -1;
} }
}); });
@@ -291,7 +302,7 @@ Array.prototype.orderByDescending = function (keySelector, comparer) {
} }
return orderByDescending(this, keySelector, comparer); return orderByDescending(this, keySelector, comparer);
} };
Array.prototype.groupBy = function (keySelector) { Array.prototype.groupBy = function (keySelector) {
function groupBy(array, keySelector) { function groupBy(array, keySelector) {
@@ -299,7 +310,9 @@ Array.prototype.groupBy = function (keySelector) {
let keys = []; let keys = [];
return array.reduce(function (groups, element, index) { return array.reduce(function (groups, element, index) {
let key = JSON.stringify(keySelector.call(arguments[1], element, index, array)) let key = JSON.stringify(keySelector.call(arguments[1], element, index, array))
let index2 = keys.findIndex(function (x) { return x === key }); let index2 = keys.findIndex(function (x) {
return x === key;
});
if (index2 < 0) { if (index2 < 0) {
index2 = keys.push(key) - 1; index2 = keys.push(key) - 1;
@@ -312,13 +325,14 @@ Array.prototype.groupBy = function (keySelector) {
groups[index2].push(element); groups[index2].push(element);
return groups; return groups;
}, []); }, []);
} } else {
else {
let groups = []; let groups = [];
let keys = []; let keys = [];
for (let i = 0, len = array.length; i < len; i++) { for (let i = 0, len = array.length; i < len; i++) {
let key = JSON.stringify(keySelector.call(arguments[1], array[i], i, array)); let key = JSON.stringify(keySelector.call(arguments[1], array[i], i, array));
let index = keys.findIndex(function (x) { return x === key }); let index = keys.findIndex(function (x) {
return x === key;
});
if (index < 0) { if (index < 0) {
index = keys.push(key) - 1; index = keys.push(key) - 1;
@@ -336,7 +350,7 @@ Array.prototype.groupBy = function (keySelector) {
} }
return groupBy(this, keySelector); return groupBy(this, keySelector);
} };
Array.prototype.sum = function (selector) { Array.prototype.sum = function (selector) {
function sum(array, selector) { function sum(array, selector) {
@@ -345,17 +359,14 @@ Array.prototype.sum = function (selector) {
if (i == 0) { if (i == 0) {
if (selector) { if (selector) {
ret = selector.call(arguments[2], array[i], i, array); ret = selector.call(arguments[2], array[i], i, array);
} else {
ret = array[i];
} }
else { } else {
ret = array[i]
}
}
else {
if (selector) { if (selector) {
ret += selector.call(arguments[2], array[i], i, array); ret += selector.call(arguments[2], array[i], i, array);
} } else {
else { ret += array[i];
ret += array[i]
} }
} }
} }
@@ -364,4 +375,4 @@ Array.prototype.sum = function (selector) {
} }
return sum(this, selector); return sum(this, selector);
} };
+1 -1
View File
@@ -36,7 +36,7 @@ module es {
this._messageTable.set(eventType, list); this._messageTable.set(eventType, list);
} }
if (list.contains(handler)) if (list.findIndex(funcPack => funcPack.func == handler) != -1)
console.warn("您试图添加相同的观察者两次"); console.warn("您试图添加相同的观察者两次");
list.push(new FuncPack(handler, context)); list.push(new FuncPack(handler, context));
} }