support findAndUpdate with same predicate semantics in SortedArray too
This commit is contained in:
parent
fa37e8fedb
commit
63b371b6ef
4 changed files with 48 additions and 41 deletions
|
@ -90,27 +90,23 @@ export class Timeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
_applyAndEmitLocalRelationChange(pe, updater) {
|
_applyAndEmitLocalRelationChange(pe, updater) {
|
||||||
// first, look in local entries (separately, as it has its own update mechanism)
|
const updateOrFalse = e => {
|
||||||
const foundInLocalEntries = this._localEntries.findAndUpdate(
|
|
||||||
e => e.id === pe.relatedTxnId,
|
|
||||||
e => {
|
|
||||||
const params = updater(e);
|
const params = updater(e);
|
||||||
return params ? params : false;
|
return params ? params : false;
|
||||||
},
|
};
|
||||||
|
// first, look in local entries based on txn id
|
||||||
|
const foundInLocalEntries = this._localEntries.findAndUpdate(
|
||||||
|
e => e.id === pe.relatedTxnId,
|
||||||
|
updateOrFalse,
|
||||||
);
|
);
|
||||||
// now look in remote entries
|
// now look in remote entries based on event id
|
||||||
if (!foundInLocalEntries && pe.relatedEventId) {
|
if (!foundInLocalEntries && pe.relatedEventId) {
|
||||||
// TODO: ideally iterate in reverse as target is likely to be most recent,
|
// TODO: ideally iterate in reverse as target is likely to be most recent,
|
||||||
// but not easy through ObservableList contract
|
// but not easy through ObservableList contract
|
||||||
for (const entry of this._remoteEntries) {
|
this._remoteEntries.findAndUpdate(
|
||||||
if (pe.relatedEventId === entry.id) {
|
e => e.id === pe.relatedEventId,
|
||||||
const params = updater(entry);
|
updateOrFalse
|
||||||
if (params) {
|
);
|
||||||
this._remoteEntries.update(entry, params);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {BaseObservableList} from "./BaseObservableList.js";
|
import {BaseObservableList} from "./BaseObservableList.js";
|
||||||
|
import {findAndUpdateInArray} from "./common.js";
|
||||||
|
|
||||||
export class MappedList extends BaseObservableList {
|
export class MappedList extends BaseObservableList {
|
||||||
constructor(sourceList, mapper, updater, removeCallback) {
|
constructor(sourceList, mapper, updater, removeCallback) {
|
||||||
|
@ -80,18 +81,7 @@ export class MappedList extends BaseObservableList {
|
||||||
}
|
}
|
||||||
|
|
||||||
findAndUpdate(predicate, updater) {
|
findAndUpdate(predicate, updater) {
|
||||||
const index = this._mappedValues.findIndex(predicate);
|
return findAndUpdateInArray(predicate, this._mappedValues, this, updater);
|
||||||
if (index !== -1) {
|
|
||||||
const mappedValue = this._mappedValues[index];
|
|
||||||
// allow bailing out of sending an emit if updater determined its not needed
|
|
||||||
const params = updater(mappedValue);
|
|
||||||
if (params !== false) {
|
|
||||||
this.emitUpdate(index, mappedValue, params);
|
|
||||||
}
|
|
||||||
// found
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get length() {
|
get length() {
|
||||||
|
|
|
@ -16,6 +16,7 @@ limitations under the License.
|
||||||
|
|
||||||
import {BaseObservableList} from "./BaseObservableList.js";
|
import {BaseObservableList} from "./BaseObservableList.js";
|
||||||
import {sortedIndex} from "../../utils/sortedIndex.js";
|
import {sortedIndex} from "../../utils/sortedIndex.js";
|
||||||
|
import {findAndUpdateInArray} from "./common.js";
|
||||||
|
|
||||||
export class SortedArray extends BaseObservableList {
|
export class SortedArray extends BaseObservableList {
|
||||||
constructor(comparator) {
|
constructor(comparator) {
|
||||||
|
@ -41,20 +42,8 @@ export class SortedArray extends BaseObservableList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
findAndUpdate(newValue, updater) {
|
findAndUpdate(predicate, updater) {
|
||||||
const index = this.indexOf(newValue);
|
return findAndUpdateInArray(predicate, this._items, this, updater);
|
||||||
if (index !== -1) {
|
|
||||||
const oldValue = this._items[index];
|
|
||||||
// allow bailing out of sending an emit if updater determined its not needed
|
|
||||||
const params = updater(oldValue, newValue);
|
|
||||||
if (params !== false) {
|
|
||||||
this._items[index] = newValue;
|
|
||||||
this.emitUpdate(index, newValue, params);
|
|
||||||
}
|
|
||||||
// found
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update(item, updateParams = null, previousCallback = null) {
|
update(item, updateParams = null, previousCallback = null) {
|
||||||
|
|
32
src/observable/list/common.js
Normal file
32
src/observable/list/common.js
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
||||||
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* inline update of item in collection backed by array, without replacing the preexising item */
|
||||||
|
export function findAndUpdateInArray(predicate, array, observable, updater) {
|
||||||
|
const index = array.findIndex(predicate);
|
||||||
|
if (index !== -1) {
|
||||||
|
const value = array[index];
|
||||||
|
// allow bailing out of sending an emit if updater determined its not needed
|
||||||
|
const params = updater(value);
|
||||||
|
if (params !== false) {
|
||||||
|
observable.emitUpdate(index, value, params);
|
||||||
|
}
|
||||||
|
// found
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
Reference in a new issue