Skip to content

Commit

Permalink
Fix update propagation logic (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc authored Jun 16, 2024
1 parent e7a6d29 commit c0f24df
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
24 changes: 23 additions & 1 deletion generate_code/templates/class.ts.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,29 @@ export class {{class_name}}Model extends WidgetModel {
this.listenTo(this, 'change', this.valueChanged);
}

valueChanged(): void {
valueChanged(m: {{class_name}}Model): void {
if (m.changed) {
Object.values(m.changed).forEach(it => {
if (Array.isArray(it)) {
it.forEach(subModel => {
if (subModel?.model_id) {
{{class_name}}Model.updateManager?.registerChildModel({
child: subModel,
parent: m
});
}
});
} else {
if (it?.model_id) {
{{class_name}}Model.updateManager?.registerChildModel({
child: it,
parent: m
});
}
}
});
}

if ({{class_name}}Model.updateManager) {
{{class_name}}Model.updateManager.sendUpdateSignal(this);
this.comm?.on_close(() => {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface IUpdateManager {
findRoot(modeId: DOMWidgetModel): DOMWidgetModel[];
sendUpdateSignal(modeId: DOMWidgetModel): void;
registerModel(model: DOMWidgetModel): void;
registerChildModel(options: {
child: DOMWidgetModel;
parent: DOMWidgetModel;
}): void;
unregisterModel(model: DOMWidgetModel): void;
}

Expand Down
23 changes: 17 additions & 6 deletions src/updateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,29 @@ class UpdateManager implements IUpdateManager {
registerModel(model: DOMWidgetModel): void {
const setParentId = (m: any) => {
if (m?.['model_id']) {
if (this._modelMap.has(m)) {
const currentSet = this._modelMap.get(m);
currentSet?.add(model);
} else {
this._modelMap.set(m, new Set([model]));
}
this.registerChildModel({ child: m, parent: model });
} else if (Array.isArray(m)) {
m.forEach(c => setParentId(c));
}
};
Object.values(model.attributes).forEach(m => setParentId(m));
}

registerChildModel(options: {
child: DOMWidgetModel;
parent: DOMWidgetModel;
}): void {
const { child, parent } = options;
if (child?.model_id && parent?.model_id) {
if (this._modelMap.has(child)) {
const currentSet = this._modelMap.get(child);
currentSet?.add(parent);
} else {
this._modelMap.set(child, new Set([parent]));
}
}
}

unregisterModel(model: DOMWidgetModel): void {
this._modelMap.delete(model);
}
Expand Down

0 comments on commit c0f24df

Please sign in to comment.