From 20914ef4fa6c776ba8987a36430ed75d91dbbf4b Mon Sep 17 00:00:00 2001 From: "mathias.chouet" <mathias.chouet@irstea.fr> Date: Thu, 29 Aug 2019 17:39:11 +0200 Subject: [PATCH] Notify observers when completely replacng props --- src/nub.ts | 47 +++++++++++++++++++++++++++++------------------ src/props.ts | 7 +++++++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/nub.ts b/src/nub.ts index 72085815..c4cc3895 100644 --- a/src/nub.ts +++ b/src/nub.ts @@ -17,6 +17,22 @@ import { ResultElement } from "./util/resultelement"; */ export abstract class Nub extends ComputeNode implements IObservable { + protected static concatPrms(p1: ParamsEquation[], p2: ParamsEquation[]): ParamsEquation[] { + const p3: ParamsEquation[] = p1; + for (const p of p2) { + p3.push(p); + } + return p3; + } + + private static progressPercentageAccordedToChainCalculation = 50; + + /** paramétrage de la dichotomie */ + public dichoStartIntervalMaxSteps: number = 100; + + /** pointer to parent Nub */ + public parent: Nub; + /** type of children elements, used by GUI for translation */ protected _childrenType: string = ""; @@ -51,10 +67,16 @@ export abstract class Nub extends ComputeNode implements IObservable { public set properties(props: Props) { // copy observers const observers = this._props.getObservers(); - this._props = props.clone(); + // empty props + this._props.reset(); + // restore observers for (const obs of observers) { this._props.addObserver(obs); } + // set new props values + for (const p of Object.keys(props.props)) { + this._props.setPropValue(p, props.getPropValue(p)); + } } /** @@ -159,22 +181,6 @@ export abstract class Nub extends ComputeNode implements IObservable { } } - protected static concatPrms(p1: ParamsEquation[], p2: ParamsEquation[]): ParamsEquation[] { - const p3: ParamsEquation[] = p1; - for (const p of p2) { - p3.push(p); - } - return p3; - } - - private static progressPercentageAccordedToChainCalculation = 50; - - /** paramétrage de la dichotomie */ - public dichoStartIntervalMaxSteps: number = 100; - - /** pointer to parent Nub */ - public parent: Nub; - /** parameter that is to be computed by default - to be overloaded by child classes */ protected _defaultCalculatedParam: ParamDefinition; @@ -207,7 +213,7 @@ export abstract class Nub extends ComputeNode implements IObservable { public constructor(prms: ParamsEquation, dbg: boolean = false) { super(prms, dbg); - this._children = []; + this.deleteAllChildren(); this._observable = new Observable(); this._defaultCalculatedParam = this.getFirstAnalyticalParameter(); this.resetDefaultCalculatedParam(); @@ -1166,6 +1172,11 @@ export abstract class Nub extends ComputeNode implements IObservable { } } + /** Removes all children */ + public deleteAllChildren() { + this._children = []; + } + /** * Returns the position or the current structure (starting at 0) * in the parent Nub diff --git a/src/props.ts b/src/props.ts index 580a3e40..99f40d37 100644 --- a/src/props.ts +++ b/src/props.ts @@ -59,6 +59,13 @@ export class Props implements IObservable { return this._observable.getObservers(); } + /** + * Remove all properties, does not notify of any change + */ + public reset() { + this._props = {}; + } + /** * Clones properties into a new Props object */ -- GitLab