From d90e7305d342378cb2526238d9d974a6b9272359 Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Tue, 30 Apr 2024 11:20:40 +0200 Subject: [PATCH] :art: Improve target and fov control flow using new Lock object --- js/imports.js | 2 +- js/models/event_handler.js | 31 +++++++------------------------ js/models/lock.js | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 js/models/lock.js diff --git a/js/imports.js b/js/imports.js index d5274429..27c2fa99 100644 --- a/js/imports.js +++ b/js/imports.js @@ -1,3 +1,3 @@ -import A from "https://esm.sh/aladin-lite@3.3.3-beta"; +import A from "https://esm.sh/aladin-lite@3.4.0-beta"; export default A; diff --git a/js/models/event_handler.js b/js/models/event_handler.js index df62997e..cee4f9e7 100644 --- a/js/models/event_handler.js +++ b/js/models/event_handler.js @@ -1,4 +1,5 @@ import MessageHandler from "./message_handler"; +import Lock from "./lock"; export default class EventHandler { /** @@ -30,16 +31,12 @@ export default class EventHandler { // is also necessary for the field of view. /* Target control */ - let targetJs = false; - let targetPy = false; + let targetLock = new Lock(); // Event triggered when the user moves the map in Aladin Lite this.aladin.on("positionChanged", () => { - if (targetPy) { - targetPy = false; - return; - } - targetJs = true; + if (targetLock.unlock()) return; + targetLock.lock(); const raDec = this.aladin.getRaDec(); this.model.set("_target", `${raDec[0]} ${raDec[1]}`); this.model.set("shared_target", `${raDec[0]} ${raDec[1]}`); @@ -48,26 +45,17 @@ export default class EventHandler { // Event triggered when the target is changed from the Python side using jslink this.model.on("change:shared_target", () => { - if (targetJs) { - targetJs = false; - return; - } - targetPy = true; const target = this.model.get("shared_target"); const [ra, dec] = target.split(" "); this.aladin.gotoRaDec(ra, dec); }); /* Field of View control */ - let fovJs = false; - let fovPy = false; + let fovLock = new Lock(); this.aladin.on("zoomChanged", (fov) => { - if (fovPy) { - fovPy = false; - return; - } - fovJs = true; + if (fovLock.unlock()) return; + fovLock.lock(); // fov MUST be cast into float in order to be sent to the model this.model.set("_fov", parseFloat(fov.toFixed(5))); this.model.set("shared_fov", parseFloat(fov.toFixed(5))); @@ -75,11 +63,6 @@ export default class EventHandler { }); this.model.on("change:shared_fov", () => { - if (fovJs) { - fovJs = false; - return; - } - fovPy = true; let fov = this.model.get("shared_fov"); this.aladin.setFoV(fov); }); diff --git a/js/models/lock.js b/js/models/lock.js new file mode 100644 index 00000000..c70b51b6 --- /dev/null +++ b/js/models/lock.js @@ -0,0 +1,27 @@ +export default class Lock { + state = false; + + /** + * Locks the object + * @returns {boolean} True if the object was locked, false otherwise + */ + unlock() { + if (this.state) { + this.state = false; + return true; + } + return false; + } + + /** + * Unlocks the object + * @returns {boolean} True if the object was unlocked, false otherwise + */ + lock() { + if (!this.state) { + this.state = true; + return true; + } + return false; + } +}