mirror of
https://github.com/DaanVandenBosch/phantasmal-world.git
synced 2025-04-05 07:18:29 +08:00
Length is now updated when a SimpleWritableListProperty is modified.
This commit is contained in:
parent
62db02e278
commit
46ba5bb018
@ -12,7 +12,7 @@ const logger = Logger.get("core/observable/property/list/SimpleWritableListPrope
|
|||||||
|
|
||||||
export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
||||||
implements WritableListProperty<T> {
|
implements WritableListProperty<T> {
|
||||||
readonly length: Property<number>; // TODO: update length
|
readonly length: Property<number>;
|
||||||
|
|
||||||
get val(): T[] {
|
get val(): T[] {
|
||||||
return this.get_val();
|
return this.get_val();
|
||||||
@ -28,7 +28,7 @@ export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
|||||||
|
|
||||||
set_val(elements: T[]): T[] {
|
set_val(elements: T[]): T[] {
|
||||||
const removed = this.elements.splice(0, this.elements.length, ...elements);
|
const removed = this.elements.splice(0, this.elements.length, ...elements);
|
||||||
this.emit_list({
|
this.finalize_update({
|
||||||
type: ListChangeType.Replacement,
|
type: ListChangeType.Replacement,
|
||||||
removed,
|
removed,
|
||||||
inserted: elements,
|
inserted: elements,
|
||||||
@ -126,7 +126,7 @@ export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
|||||||
set(index: number, element: T): void {
|
set(index: number, element: T): void {
|
||||||
const removed = [this.elements[index]];
|
const removed = [this.elements[index]];
|
||||||
this.elements[index] = element;
|
this.elements[index] = element;
|
||||||
this.emit_list({
|
this.finalize_update({
|
||||||
type: ListChangeType.Replacement,
|
type: ListChangeType.Replacement,
|
||||||
removed,
|
removed,
|
||||||
inserted: [element],
|
inserted: [element],
|
||||||
@ -138,7 +138,7 @@ export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
|||||||
|
|
||||||
clear(): void {
|
clear(): void {
|
||||||
const removed = this.elements.splice(0, this.elements.length);
|
const removed = this.elements.splice(0, this.elements.length);
|
||||||
this.emit_list({
|
this.finalize_update({
|
||||||
type: ListChangeType.Replacement,
|
type: ListChangeType.Replacement,
|
||||||
removed,
|
removed,
|
||||||
inserted: [],
|
inserted: [],
|
||||||
@ -157,7 +157,7 @@ export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
|||||||
removed = this.elements.splice(index, delete_count, ...items);
|
removed = this.elements.splice(index, delete_count, ...items);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit_list({
|
this.finalize_update({
|
||||||
type: ListChangeType.Replacement,
|
type: ListChangeType.Replacement,
|
||||||
removed,
|
removed,
|
||||||
inserted: items,
|
inserted: items,
|
||||||
@ -169,7 +169,14 @@ export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
|||||||
return removed;
|
return removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected emit_list(change: ListPropertyChangeEvent<T>): void {
|
/**
|
||||||
|
* Does the following in the given order:
|
||||||
|
* - Updates element observers
|
||||||
|
* - Emits ListPropertyChangeEvent
|
||||||
|
* - Emits PropertyChangeEvent
|
||||||
|
* - Sets length
|
||||||
|
*/
|
||||||
|
protected finalize_update(change: ListPropertyChangeEvent<T>): void {
|
||||||
if (this.list_observers.length && this.extract_observables) {
|
if (this.list_observers.length && this.extract_observables) {
|
||||||
switch (change.type) {
|
switch (change.type) {
|
||||||
case ListChangeType.Insertion:
|
case ListChangeType.Insertion:
|
||||||
@ -195,6 +202,8 @@ export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.emit(this.elements);
|
this.emit(this.elements);
|
||||||
|
|
||||||
|
this._length.val = this.elements.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
private call_list_observer(
|
private call_list_observer(
|
||||||
@ -219,7 +228,7 @@ export class SimpleWritableListProperty<T> extends AbstractProperty<T[]>
|
|||||||
index,
|
index,
|
||||||
disposables: this.extract_observables!(element).map(observable =>
|
disposables: this.extract_observables!(element).map(observable =>
|
||||||
observable.observe(() => {
|
observable.observe(() => {
|
||||||
this.emit_list({
|
this.finalize_update({
|
||||||
type: ListChangeType.Update,
|
type: ListChangeType.Update,
|
||||||
updated: [element],
|
updated: [element],
|
||||||
index: obj.index,
|
index: obj.index,
|
||||||
|
@ -74,10 +74,6 @@ export class WantedItemsView extends Widget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ListChangeType.Update:
|
|
||||||
// TODO: update row
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user