More Knockout code to ES6

This commit is contained in:
djmaze 2021-07-29 11:43:47 +02:00
parent b7cb990b00
commit cbecdd93a0
22 changed files with 733 additions and 865 deletions

View file

@ -41,7 +41,8 @@ var defaultEvent = "change";
var ko_subscribable_fn = {
init: instance => {
instance._subscriptions = { "change": [] };
instance._subscriptions = new Map();
instance._subscriptions.set("change", new Set);
instance._versionNumber = 1;
},
@ -52,7 +53,7 @@ var ko_subscribable_fn = {
var boundCallback = callbackTarget ? callback.bind(callbackTarget) : callback;
var subscription = new koSubscription(self, boundCallback, () => {
ko.utils.arrayRemoveItem(self._subscriptions[event], subscription);
self._subscriptions.get(event).delete(subscription);
if (self.afterSubscriptionRemove)
self.afterSubscriptionRemove(event);
});
@ -60,9 +61,9 @@ var ko_subscribable_fn = {
if (self.beforeSubscriptionAdd)
self.beforeSubscriptionAdd(event);
if (!self._subscriptions[event])
self._subscriptions[event] = [];
self._subscriptions[event].push(subscription);
if (!self._subscriptions.has(event))
self._subscriptions.set(event, new Set);
self._subscriptions.get(event).add(subscription);
return subscription;
},
@ -73,16 +74,14 @@ var ko_subscribable_fn = {
this.updateVersion();
}
if (this.hasSubscriptionsForEvent(event)) {
var subs = event === defaultEvent && this._changeSubscriptions || this._subscriptions[event].slice(0);
var subs = event === defaultEvent && this._changeSubscriptions || new Set(this._subscriptions.get(event));
try {
ko.dependencyDetection.begin(); // Begin suppressing dependency detection (by setting the top frame to undefined)
var i = 0, subscription;
while ((subscription = subs[i++])) {
subs.forEach(subscription => {
// In case a subscription was disposed during the arrayForEach cycle, check
// for isDisposed on each subscription before invoking its callback
if (!subscription._isDisposed)
subscription._callback(valueToNotify);
}
subscription._isDisposed || subscription._callback(valueToNotify);
});
} finally {
ko.dependencyDetection.end(); // End suppressing dependency detection
}
@ -141,7 +140,7 @@ var ko_subscribable_fn = {
if (!isDirty || !self._notificationIsPending) {
didUpdate = !isDirty;
}
self._changeSubscriptions = self._subscriptions[defaultEvent].slice(0);
self._changeSubscriptions = new Set(self._subscriptions.get(defaultEvent));
self._notificationIsPending = ignoreBeforeChange = true;
pendingValue = value;
finish();
@ -163,7 +162,7 @@ var ko_subscribable_fn = {
},
hasSubscriptionsForEvent: function(event) {
return this._subscriptions[event] && this._subscriptions[event].length;
return (this._subscriptions.get(event) || []).size;
},
isDifferent: function(oldValue, newValue) {