Skip to content

Commit

Permalink
🎨 Improve target and fov control flow using new Lock object
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed May 2, 2024
1 parent c29cb79 commit 40c0823
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion js/imports.js
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 9 additions & 20 deletions js/models/event_handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MessageHandler from "./message_handler";
import { Lock } from "../utils";

export default class EventHandler {
/**
Expand Down Expand Up @@ -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]}`);
Expand All @@ -48,38 +48,27 @@ 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)));
this.model.save_changes();
});

this.model.on("change:shared_fov", () => {
if (fovJs) {
fovJs = false;
return;
}
fovPy = true;
let fov = this.model.get("shared_fov");
this.aladin.setFoV(fov);
});
Expand Down
22 changes: 21 additions & 1 deletion js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 40c0823

Please sign in to comment.