Skip to content

Commit

Permalink
🚑 Fix widget crash when moving from Aladin Lite view
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed May 6, 2024
1 parent b5723a6 commit 5ea3dd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
46 changes: 28 additions & 18 deletions js/models/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,55 @@ export default class EventHandler {
// is also necessary for the field of view.

/* Target control */
let targetLock = new Lock();
const jsTargetLock = new Lock();
const pyTargetLock = new Lock();

// Event triggered when the user moves the map in Aladin Lite
this.aladin.on("positionChanged", () => {
if (targetLock.locked) {
targetLock.unlock();
this.aladin.on("positionChanged", (position) => {
if (pyTargetLock.locked) {
pyTargetLock.unlock();
return;
}
targetLock.lock();
const raDec = this.aladin.getRaDec();
jsTargetLock.lock();
const raDec = [position.ra, position.dec];
// const raDec = this.aladin.getRaDec();
this.model.set("_target", `${raDec[0]} ${raDec[1]}`);
this.model.set("shared_target", `${raDec[0]} ${raDec[1]}`);
this.model.save_changes();
});

// Event triggered when the target is changed from the Python side using jslink
this.model.on("change:shared_target", () => {
const target = this.model.get("shared_target");
this.model.on("change:_target", () => {
if (jsTargetLock.locked) {
jsTargetLock.unlock();
return;
}
pyTargetLock.lock();
let target = this.model.get("_target");
const [ra, dec] = target.split(" ");
this.aladin.gotoRaDec(ra, dec);
});

/* Field of View control */
let fovLock = new Lock();
const jsFovLock = new Lock();
const pyFovLock = new Lock();

this.aladin.on("zoomChanged", (fov) => {
if (fovLock.locked) {
fovLock.unlock();
if (pyFovLock.locked) {
pyFovLock.unlock();
return;
}
fovLock.lock();
jsFovLock.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", () => {
let fov = this.model.get("shared_fov");
this.model.on("change:_fov", () => {
if (jsFovLock.locked) {
jsFovLock.unlock();
return;
}
pyFovLock.lock();
let fov = this.model.get("_fov");
this.aladin.setFoV(fov);
});

Expand Down Expand Up @@ -186,7 +196,7 @@ export default class EventHandler {
* There is no need to unsubscribe from the Aladin Lite events.
*/
unsubscribeAll() {
this.model.off("change:shared_target");
this.model.off("change:_target");
this.model.off("change:fov");
this.model.off("change:height");
this.model.off("change:coo_frame");
Expand Down
6 changes: 2 additions & 4 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ class Lock {

/**
* Locks the object
* @returns {boolean} True if the object was locked, false otherwise
*/
unlock() {
return false;
this.locked = false;
}

/**
* Unlocks the object
* @returns {boolean} True if the object was unlocked, false otherwise
*/
lock() {
return true;
this.locked = true;
}
}

Expand Down

0 comments on commit 5ea3dd4

Please sign in to comment.