From 46ba5bb0189eb5a4cce9cd547565297a71e1d9ee Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Tue, 3 Sep 2019 16:49:04 +0200 Subject: [PATCH] Length is now updated when a SimpleWritableListProperty is modified. --- .../list/SimpleWritableListProperty.ts | 23 +++++++++++++------ src/hunt_optimizer/gui/WantedItemsView.ts | 4 ---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/core/observable/property/list/SimpleWritableListProperty.ts b/src/core/observable/property/list/SimpleWritableListProperty.ts index 2da2330a..88d1bb32 100644 --- a/src/core/observable/property/list/SimpleWritableListProperty.ts +++ b/src/core/observable/property/list/SimpleWritableListProperty.ts @@ -12,7 +12,7 @@ const logger = Logger.get("core/observable/property/list/SimpleWritableListPrope export class SimpleWritableListProperty extends AbstractProperty implements WritableListProperty { - readonly length: Property; // TODO: update length + readonly length: Property; get val(): T[] { return this.get_val(); @@ -28,7 +28,7 @@ export class SimpleWritableListProperty extends AbstractProperty set_val(elements: T[]): T[] { const removed = this.elements.splice(0, this.elements.length, ...elements); - this.emit_list({ + this.finalize_update({ type: ListChangeType.Replacement, removed, inserted: elements, @@ -126,7 +126,7 @@ export class SimpleWritableListProperty extends AbstractProperty set(index: number, element: T): void { const removed = [this.elements[index]]; this.elements[index] = element; - this.emit_list({ + this.finalize_update({ type: ListChangeType.Replacement, removed, inserted: [element], @@ -138,7 +138,7 @@ export class SimpleWritableListProperty extends AbstractProperty clear(): void { const removed = this.elements.splice(0, this.elements.length); - this.emit_list({ + this.finalize_update({ type: ListChangeType.Replacement, removed, inserted: [], @@ -157,7 +157,7 @@ export class SimpleWritableListProperty extends AbstractProperty removed = this.elements.splice(index, delete_count, ...items); } - this.emit_list({ + this.finalize_update({ type: ListChangeType.Replacement, removed, inserted: items, @@ -169,7 +169,14 @@ export class SimpleWritableListProperty extends AbstractProperty return removed; } - protected emit_list(change: ListPropertyChangeEvent): void { + /** + * Does the following in the given order: + * - Updates element observers + * - Emits ListPropertyChangeEvent + * - Emits PropertyChangeEvent + * - Sets length + */ + protected finalize_update(change: ListPropertyChangeEvent): void { if (this.list_observers.length && this.extract_observables) { switch (change.type) { case ListChangeType.Insertion: @@ -195,6 +202,8 @@ export class SimpleWritableListProperty extends AbstractProperty } this.emit(this.elements); + + this._length.val = this.elements.length; } private call_list_observer( @@ -219,7 +228,7 @@ export class SimpleWritableListProperty extends AbstractProperty index, disposables: this.extract_observables!(element).map(observable => observable.observe(() => { - this.emit_list({ + this.finalize_update({ type: ListChangeType.Update, updated: [element], index: obj.index, diff --git a/src/hunt_optimizer/gui/WantedItemsView.ts b/src/hunt_optimizer/gui/WantedItemsView.ts index a555bf39..d2f9e500 100644 --- a/src/hunt_optimizer/gui/WantedItemsView.ts +++ b/src/hunt_optimizer/gui/WantedItemsView.ts @@ -74,10 +74,6 @@ export class WantedItemsView extends Widget { } } break; - - case ListChangeType.Update: - // TODO: update row - break; } };