KnockoutJS rebuilt

This commit is contained in:
the-djmaze 2024-03-03 23:10:16 +01:00
parent 9db2690710
commit b2009f6bc0
5 changed files with 134 additions and 169 deletions

View file

@ -39,29 +39,28 @@ ko['extenders']['trackArrayChanges'] = (target, options) => {
}
};
function trackChanges() {
function notifyChanges() {
if (pendingChanges) {
// Make a copy of the current contents and ensure it's an array
var currentContents = [].concat(target.peek() || []), changes;
function notifyChanges() {
if (pendingChanges) {
// Make a copy of the current contents and ensure it's an array
var currentContents = [].concat(target.peek() || []), changes;
// Compute the diff and issue notifications, but only if someone is listening
if (target.hasSubscriptionsForEvent(arrayChangeEventName)) {
changes = getChanges(previousContents, currentContents);
}
// Compute the diff and issue notifications, but only if someone is listening
if (target.hasSubscriptionsForEvent(arrayChangeEventName)) {
changes = getChanges(previousContents, currentContents);
}
// Eliminate references to the old, removed items, so they can be GCed
previousContents = currentContents;
cachedDiff = null;
pendingChanges = 0;
// Eliminate references to the old, removed items, so they can be GCed
previousContents = currentContents;
cachedDiff = null;
pendingChanges = 0;
if (changes?.length) {
target.notifySubscribers(changes, arrayChangeEventName);
}
if (changes?.length) {
target.notifySubscribers(changes, arrayChangeEventName);
}
}
}
function trackChanges() {
if (trackingChanges) {
// Whenever there's a new subscription and there are pending notifications, make sure all previous
// subscriptions are notified of the change so that all subscriptions are in sync.
@ -102,16 +101,15 @@ ko['extenders']['trackArrayChanges'] = (target, options) => {
var diff = [],
arrayLength = rawArray.length,
argsLength = args.length,
offset = 0;
offset = 0,
pushDiff = (status, value, index) =>
diff[diff.length] = { 'status': status, 'value': value, 'index': index };
function pushDiff(status, value, index) {
return diff[diff.length] = { 'status': status, 'value': value, 'index': index };
}
switch (operationName) {
case 'push':
offset = arrayLength;
case 'unshift':
for (let index = 0; index < argsLength; index++) {
for (let index = 0; index < argsLength; ++index) {
pushDiff('added', args[index], offset + index);
}
break;