From 0e3a91bb3f384bdd3c90b2ac5cd6d4ead882785d 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 | 29 +++++++++-------------------- js/utils.js | 22 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 22 deletions(-) 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..40bac042 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 "../utils"; export default class EventHandler { /** @@ -30,16 +31,15 @@ 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; + if (targetLock.locked) { + targetLock.unlock(); return; } - targetJs = true; + 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 +48,20 @@ 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; + if (fovLock.locked) { + fovLock.unlock(); return; } - fovJs = true; + 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 +69,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/utils.js b/js/utils.js index fa0f283f..b78bfdd6 100644 --- a/js/utils.js +++ b/js/utils.js @@ -23,4 +23,24 @@ function convertOptionNamesToCamelCase(options) { return newOptions; } -export { snakeCaseToCamelCase, convertOptionNamesToCamelCase }; +class Lock { + locked = false; + + /** + * Locks the object + * @returns {boolean} True if the object was locked, false otherwise + */ + unlock() { + return false; + } + + /** + * Unlocks the object + * @returns {boolean} True if the object was unlocked, false otherwise + */ + lock() { + return true; + } +} + +export { snakeCaseToCamelCase, convertOptionNamesToCamelCase, Lock };