Hope you all are doing well!! I'm facing some issues in calling the Diagram event. Here I'm posting a few use-cases which didn't work for me and what's worked.
CASE I) Did not work : Calling addDiagramListener and removeDiagramListener one after other for the same function reference "setLayout" didn't work. None of them or both addDiagramListener or removeDiagramListener got fired.
export class DiagramListenerTestClass {
execute(context: any): any {
let diagram = context.diagram;
diagram.layout = new go.ForceDirectedLayout;
diagram.addDiagramListener("LayoutCompleted", setLayout);
diagram.removeDiagramListener("LayoutCompleted", setLayout);
}
}
// Define outside the class
var setLayout = function (event) {
event.diagram.layout = new go.Layout();
console.log("Method called!!");
}
CASE II) Worked : Calling removeDiagramListener after some setTimeout, fired both addDiagramListener and removeDiagramListener.
export class DiagramListenerTestClass {
execute(context: any): any {
let diagram = context.diagram;
diagram.layout = new go.ForceDirectedLayout;
diagram.addDiagramListener("LayoutCompleted", setLayout);
setTimeout(() => { diagram.removeDiagramListener("LayoutCompleted", setLayout) }, 100);
}
}
// Define outside the class
var setLayout = function (event) {
event.diagram.layout = new go.Layout();
console.log("Method called!!");
}
Note: I'm not sure about the reason why case II worked and not CASE I. What I assume for the successful execution of CASE II is that: "the calls for addDiagramListener and removeDiagramListener was so quick that before addDiagramListener 'setLayout' function gets called, the listener gets removed by 'removeDiagramListener' ".So to get's the function successfully executed, I called 'removeDiagramListener' with some setTimeout of 100ms.
Please do let me correct if there's something not correct. Your suggestions would be very helpful and appreciated!! Thanks in advance!!