hydrogen-web/src/observable/list/MappedList.js

201 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
2021-05-26 16:38:33 +05:30
Copyright 2021 The Matrix.org Foundation C.I.C.
2020-08-05 22:08:55 +05:30
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.
*/
import {BaseObservableList} from "./BaseObservableList.js";
import {findAndUpdateInArray} from "./common.js";
2019-07-29 22:47:55 +05:30
export class MappedList extends BaseObservableList {
2021-05-26 16:38:33 +05:30
constructor(sourceList, mapper, updater, removeCallback) {
2019-07-29 22:47:55 +05:30
super();
this._sourceList = sourceList;
this._mapper = mapper;
this._updater = updater;
2021-05-26 16:38:33 +05:30
this._removeCallback = removeCallback;
2019-07-29 22:47:55 +05:30
this._sourceUnsubscribe = null;
this._mappedValues = null;
}
onSubscribeFirst() {
this._sourceUnsubscribe = this._sourceList.subscribe(this);
this._mappedValues = [];
for (const item of this._sourceList) {
this._mappedValues.push(this._mapper(item));
}
}
onReset() {
this._mappedValues = [];
this.emitReset();
}
onAdd(index, value) {
const mappedValue = this._mapper(value);
this._mappedValues.splice(index, 0, mappedValue);
this.emitAdd(index, mappedValue);
}
onUpdate(index, value, params) {
guard against updates emitted while populating during first subscription This came up now because Timeline uses a MappedList to map PendingEvents to PendingEventEntries. In the map function, we setup links between entries to support local echo for relations. When opening a timeline that has unsent relations, the initial populating of the MappedList will try to emit an update for the target entry in remoteEntries. This all happens while the ListView of the timeline is calling subscribe and all collections in the chain are populating themselves based on their sources. This usually entails calling subscribe on the source, and now you are subscribed, iterate over the source (as you're not allowed to query an unsubscribed observable collection, as it might not be populated yet, and even if it did, it wouldn't be guaranteed to be up to date as events aren't flowing yet). So in this concrete example, TilesCollection hadn't populated its tiles yet and when the update to the target of the unsent relation reached TilesCollection, the tiles array was still null and it crashed. I thought what would be the best way to fix this and have a solid model for observable collections to ensure they are always compatible with each other. I considered splitting up the subscription process in two steps where you'd first populate the source and then explicitly start events flowing. I didn't go with this way because it's really only updates that make sense to be emitted during setup. A missed update wouldn't usually bring the collections out of sync like a missed add or remove would. It would just mean the UI isn't updated (or any subsequent filtered collections are not updated), but this should be fine to ignore during setup, as you can rely on the subscribing collections down the chain picking up the update while populating. If we ever want to support add or remove events during setup, we would have to explicitly support them, but for now they are correct to throw. So for now, just ignore update events that happen during setup where needed.
2021-05-27 13:32:05 +05:30
// if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it
if (!this._mappedValues) {
return;
}
2019-07-29 22:47:55 +05:30
const mappedValue = this._mappedValues[index];
if (this._updater) {
2019-07-29 23:22:28 +05:30
this._updater(mappedValue, params, value);
2019-07-29 22:47:55 +05:30
}
this.emitUpdate(index, mappedValue, params);
}
onRemove(index) {
const mappedValue = this._mappedValues[index];
this._mappedValues.splice(index, 1);
2021-05-26 16:38:33 +05:30
if (this._removeCallback) {
this._removeCallback(mappedValue);
}
2019-07-29 22:47:55 +05:30
this.emitRemove(index, mappedValue);
}
onMove(fromIdx, toIdx) {
const mappedValue = this._mappedValues[fromIdx];
this._mappedValues.splice(fromIdx, 1);
this._mappedValues.splice(toIdx, 0, mappedValue);
this.emitMove(fromIdx, toIdx, mappedValue);
}
onUnsubscribeLast() {
this._sourceUnsubscribe();
}
2021-05-26 16:38:33 +05:30
findAndUpdate(predicate, updater) {
return findAndUpdateInArray(predicate, this._mappedValues, this, updater);
2021-05-26 16:38:33 +05:30
}
2019-07-29 22:47:55 +05:30
get length() {
return this._mappedValues.length;
}
[Symbol.iterator]() {
return this._mappedValues.values();
}
}
2021-05-26 16:38:33 +05:30
import {ObservableArray} from "./ObservableArray.js";
2019-07-29 22:47:55 +05:30
export async function tests() {
class MockList extends BaseObservableList {
get length() {
return 0;
}
[Symbol.iterator]() {
return [].values();
}
}
return {
test_add(assert) {
const source = new MockList();
const mapped = new MappedList(source, n => {return {n: n*n};});
let fired = false;
const unsubscribe = mapped.subscribe({
onAdd(idx, value) {
fired = true;
assert.equal(idx, 0);
assert.equal(value.n, 36);
}
});
source.emitAdd(0, 6);
assert(fired);
unsubscribe();
},
test_update(assert) {
const source = new MockList();
const mapped = new MappedList(
source,
n => {return {n: n*n};},
(o, p, n) => o.m = n*n
2019-07-29 22:47:55 +05:30
);
let fired = false;
const unsubscribe = mapped.subscribe({
onAdd() {},
onUpdate(idx, value) {
fired = true;
assert.equal(idx, 0);
assert.equal(value.n, 36);
assert.equal(value.m, 49);
}
});
source.emitAdd(0, 6);
source.emitUpdate(0, 7);
assert(fired);
unsubscribe();
2021-05-26 16:38:33 +05:30
},
"test findAndUpdate not found": assert => {
const source = new ObservableArray([1, 3, 4]);
const mapped = new MappedList(
source,
n => {return n*n;}
);
mapped.subscribe({
onUpdate() { assert.fail(); }
});
assert.equal(mapped.findAndUpdate(
n => n === 100,
() => assert.fail()
), false);
},
"test findAndUpdate found but updater bails out of update": assert => {
const source = new ObservableArray([1, 3, 4]);
const mapped = new MappedList(
source,
n => {return n*n;}
);
mapped.subscribe({
onUpdate() { assert.fail(); }
});
let fired = false;
assert.equal(mapped.findAndUpdate(
n => n === 9,
n => {
assert.equal(n, 9);
fired = true;
return false;
}
), true);
assert.equal(fired, true);
},
"test findAndUpdate emits update": assert => {
const source = new ObservableArray([1, 3, 4]);
const mapped = new MappedList(
source,
n => {return n*n;}
);
let fired = false;
mapped.subscribe({
onUpdate(idx, n, params) {
assert.equal(idx, 1);
assert.equal(n, 9);
assert.equal(params, "param");
fired = true;
}
});
assert.equal(mapped.findAndUpdate(n => n === 9, () => "param"), true);
assert.equal(fired, true);
},
2019-07-29 22:47:55 +05:30
};
}