dispose tiles

also add more defence against emitting event when disposed
This commit is contained in:
Bruno Windels 2020-09-14 17:44:22 +02:00
parent 9ea961ae53
commit b2e6e8687e
2 changed files with 18 additions and 4 deletions

View file

@ -145,7 +145,7 @@ export class TilesCollection extends BaseObservableList {
if (tile) {
const action = tile.updateEntry(entry, params);
if (action.shouldReplace) {
this._replaceTile(tileIdx, this._tileCreator(entry));
this._replaceTile(tileIdx, tile, this._tileCreator(entry));
}
if (action.shouldRemove) {
this._removeTile(tileIdx, tile);
@ -167,7 +167,8 @@ export class TilesCollection extends BaseObservableList {
// merge with neighbours? ... hard to imagine use case for this ...
}
_replaceTile(tileIdx, newTile) {
_replaceTile(tileIdx, existingTile, newTile) {
existingTile.dispose();
const prevTile = this._getTileAtIdx(tileIdx - 1);
const nextTile = this._getTileAtIdx(tileIdx + 1);
this._tiles[tileIdx] = newTile;
@ -184,7 +185,7 @@ export class TilesCollection extends BaseObservableList {
this._tiles.splice(tileIdx, 1);
prevTile && prevTile.updateNextSibling(nextTile);
nextTile && nextTile.updatePreviousSibling(prevTile);
tile.setUpdateEmit(null);
tile.dispose();
this.emitRemove(tileIdx, tile);
}
@ -254,6 +255,8 @@ export function tests() {
updateEntry() {
return UpdateAction.Nothing;
}
dispose() {}
}
return {

View file

@ -48,7 +48,13 @@ export class SimpleTile extends ViewModel {
}
// TilesCollection contract below
setUpdateEmit(emitUpdate) {
this.updateOptions({emitChange: paramName => emitUpdate(this, paramName)});
this.updateOptions({emitChange: paramName => {
if (emitUpdate) {
emitUpdate(this, paramName);
} else {
console.trace("Tile is emitting event after being disposed");
}
}});
}
get upperEntry() {
@ -88,5 +94,10 @@ export class SimpleTile extends ViewModel {
updateNextSibling(next) {
}
dispose() {
this.setUpdateEmit(null);
super.dispose();
}
// TilesCollection contract above
}