Add type annotations to ConcatList

This commit is contained in:
Danila Fedorin 2021-09-29 19:28:00 -07:00
parent 588da9b719
commit 3b131f2db6

View file

@ -14,16 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {BaseObservableList} from "./BaseObservableList"; import {BaseObservableList, IListObserver} from "./BaseObservableList";
export class ConcatList extends BaseObservableList { export class ConcatList<T> extends BaseObservableList<T> implements IListObserver<T> {
constructor(...sourceLists) { protected _sourceLists: BaseObservableList<T>[];
protected _sourceUnsubscribes: (() => void)[] | null = null;
constructor(...sourceLists: BaseObservableList<T>[]) {
super(); super();
this._sourceLists = sourceLists; this._sourceLists = sourceLists;
this._sourceUnsubscribes = null;
} }
_offsetForSource(sourceList) { _offsetForSource(sourceList: BaseObservableList<T>): number {
const listIdx = this._sourceLists.indexOf(sourceList); const listIdx = this._sourceLists.indexOf(sourceList);
let offset = 0; let offset = 0;
for (let i = 0; i < listIdx; ++i) { for (let i = 0; i < listIdx; ++i) {
@ -32,17 +34,17 @@ export class ConcatList extends BaseObservableList {
return offset; return offset;
} }
onSubscribeFirst() { onSubscribeFirst(): void {
this._sourceUnsubscribes = this._sourceLists.map(sourceList => sourceList.subscribe(this)); this._sourceUnsubscribes = this._sourceLists.map(sourceList => sourceList.subscribe(this));
} }
onUnsubscribeLast() { onUnsubscribeLast(): void {
for (const sourceUnsubscribe of this._sourceUnsubscribes) { for (const sourceUnsubscribe of this._sourceUnsubscribes!) {
sourceUnsubscribe(); sourceUnsubscribe();
} }
} }
onReset() { onReset(): void {
// TODO: not ideal if other source lists are large // TODO: not ideal if other source lists are large
// but working impl for now // but working impl for now
// reset, and // reset, and
@ -54,11 +56,11 @@ export class ConcatList extends BaseObservableList {
} }
} }
onAdd(index, value, sourceList) { onAdd(index: number, value: T, sourceList: BaseObservableList<T>): void {
this.emitAdd(this._offsetForSource(sourceList) + index, value); this.emitAdd(this._offsetForSource(sourceList) + index, value);
} }
onUpdate(index, value, params, sourceList) { onUpdate(index: number, value: T, params: any, sourceList: BaseObservableList<T>): void {
// if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it // if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it
// as we are not supposed to call `length` on any uninitialized list // as we are not supposed to call `length` on any uninitialized list
if (!this._sourceUnsubscribes) { if (!this._sourceUnsubscribes) {
@ -67,16 +69,16 @@ export class ConcatList extends BaseObservableList {
this.emitUpdate(this._offsetForSource(sourceList) + index, value, params); this.emitUpdate(this._offsetForSource(sourceList) + index, value, params);
} }
onRemove(index, value, sourceList) { onRemove(index: number, value: T, sourceList: BaseObservableList<T>): void {
this.emitRemove(this._offsetForSource(sourceList) + index, value); this.emitRemove(this._offsetForSource(sourceList) + index, value);
} }
onMove(fromIdx, toIdx, value, sourceList) { onMove(fromIdx: number, toIdx: number, value: T, sourceList: BaseObservableList<T>): void {
const offset = this._offsetForSource(sourceList); const offset = this._offsetForSource(sourceList);
this.emitMove(offset + fromIdx, offset + toIdx, value); this.emitMove(offset + fromIdx, offset + toIdx, value);
} }
get length() { get length(): number {
let len = 0; let len = 0;
for (let i = 0; i < this._sourceLists.length; ++i) { for (let i = 0; i < this._sourceLists.length; ++i) {
len += this._sourceLists[i].length; len += this._sourceLists[i].length;
@ -105,6 +107,7 @@ export class ConcatList extends BaseObservableList {
} }
import {ObservableArray} from "./ObservableArray"; import {ObservableArray} from "./ObservableArray";
import {defaultObserverWith} from "./BaseObservableList";
export async function tests() { export async function tests() {
return { return {
test_length(assert) { test_length(assert) {
@ -133,13 +136,13 @@ export async function tests() {
const list2 = new ObservableArray([11, 12, 13]); const list2 = new ObservableArray([11, 12, 13]);
const all = new ConcatList(list1, list2); const all = new ConcatList(list1, list2);
let fired = false; let fired = false;
all.subscribe({ all.subscribe(defaultObserverWith({
onAdd(index, value) { onAdd(index, value) {
fired = true; fired = true;
assert.equal(index, 4); assert.equal(index, 4);
assert.equal(value, 11.5); assert.equal(value, 11.5);
} }
}); }));
list2.insert(1, 11.5); list2.insert(1, 11.5);
assert(fired); assert(fired);
}, },
@ -148,13 +151,13 @@ export async function tests() {
const list2 = new ObservableArray([11, 12, 13]); const list2 = new ObservableArray([11, 12, 13]);
const all = new ConcatList(list1, list2); const all = new ConcatList(list1, list2);
let fired = false; let fired = false;
all.subscribe({ all.subscribe(defaultObserverWith({
onUpdate(index, value) { onUpdate(index, value) {
fired = true; fired = true;
assert.equal(index, 4); assert.equal(index, 4);
assert.equal(value, 10); assert.equal(value, 10);
} }
}); }));
list2.emitUpdate(1, 10); list2.emitUpdate(1, 10);
assert(fired); assert(fired);
}, },