Skip to content

Commit

Permalink
✨ Added task menu item to Janeway + added actions to it & the session…
Browse files Browse the repository at this point in the history
… menu
  • Loading branch information
skerit committed Oct 15, 2023
1 parent 8fbbb08 commit 882db08
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Add `Alchemy#afterStart()` method
* Add `Alchemy#isProcessRunning(pid)` method
* Complete Task system implementation
* Added task menu item to Janeway + added actions to it & the session menu

## 1.3.16 (2023-10-05)

Expand Down
9 changes: 7 additions & 2 deletions lib/class/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Session.setMethod(function hasAlreadyQueued() {
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.2.0
* @version 0.4.0
* @version 1.3.17
*
* @param {Conduit} conduit The conduit to postpone
*
Expand All @@ -187,7 +187,12 @@ Session.setMethod(function createMenuItem(conduit) {

browser += ': ' + conduit.ip;

this.menu_item = alchemy.Janeway.session_menu.addItem(browser);
this.menu_item = alchemy.Janeway.session_menu.addItem(browser, () => {
console.log('Clicked on session', this, this.id);
});

// Seems to fix render issues:
alchemy.Janeway.session_menu.reorderItems();
});

/**
Expand Down
78 changes: 78 additions & 0 deletions lib/class/task_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const Service = Function.inherits('Alchemy.Base', 'Alchemy.Task', function TaskS
this.initSchedules();

singleton = this;

if (alchemy.settings.janeway_task_menu) {
this.createJanewayTaskMenu();
}
});

/**
Expand Down Expand Up @@ -219,6 +223,34 @@ Service.setMethod(async function initSchedules() {
this.has_loaded = true;
});

/**
* Create the Janeway task menu
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.17
* @version 1.3.17
*/
Service.setMethod(function createJanewayTaskMenu() {

if (!alchemy.Janeway || alchemy.Janeway?.task_menu) {
return;
}

let task_menu = alchemy.Janeway.addIndicator('⌚︎');

if (!task_menu.addItem) {
return task_menu.remove();
}

task_menu.addItem('Currently scheduled tasks:', () => {
console.log('Currently scheduled tasks:', this.schedules);
});

alchemy.Janeway.task_menu = task_menu;
this.janeway_task_menu = task_menu;
this.janeway_task_menu_entries = [];
});

/**
* Update all the schedule `setTimeout` calls that need it
*
Expand Down Expand Up @@ -457,6 +489,7 @@ class TaskSchedule {
is_running = false;
task_instance = null;
history_document = null;
janeway_menu_item = null;

#change_counter = 0;

Expand Down Expand Up @@ -619,6 +652,14 @@ class TaskSchedule {
return;
}

if (alchemy.task_service.janeway_task_menu) {
let title = this.task_document?.title || this.task_schedules.task_constructor.type_name;
title += ' @ ' + scheduled_date.format('Y-m-d H:i:s');
this.janeway_menu_item = alchemy.task_service.janeway_task_menu.addItem(title, () => this.clickedJanewayMenuItem());
// Seems to fix render issues:
alchemy.task_service.janeway_task_menu.reorderItems();
}

// Calculate the timeout that should be set
let timeout = scheduled_date.getTime() - Date.now();

Expand All @@ -636,6 +677,17 @@ class TaskSchedule {
return this.#setDateAndTimeout(scheduled_date, timeout);
}

/**
* Do something when the Janeway menu item is clicked.
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.17
* @version 1.3.17
*/
clickedJanewayMenuItem() {
console.log('Clicked on scheduled task:', this);
}

/**
* Actually set the date & timeout (and history document)
*
Expand Down Expand Up @@ -716,6 +768,21 @@ class TaskSchedule {
}

this.is_running = true;
let janeway_item;
let janeway_title;

try {
if (alchemy.task_service.janeway_task_menu) {
janeway_title = this.task_document?.title || this.task_schedules.task_constructor.type_name;
let title = 'Currently running: ' + janeway_title;

janeway_item = alchemy.task_service.janeway_task_menu.addItem(title, () => this.clickedJanewayMenuItem());
// Seems to fix render issues:
alchemy.task_service.janeway_task_menu.reorderItems();
}
} catch (err) {
alchemy.registerError(err);
}

try {
if (await this.ownsScheduledTask()) {
Expand All @@ -733,6 +800,12 @@ class TaskSchedule {
this.is_running = false;

this.calculateNextScheduledDate();

if (janeway_item) {
let title = 'Finished: ' + janeway_title;
janeway_item.box.setContent(title);
setTimeout(() => janeway_item.remove(), 5000);
}
}

/**
Expand All @@ -747,5 +820,10 @@ class TaskSchedule {
this.timer_id = null;
this.next_scheduled_date = null;
this.#change_counter++;

if (this.janeway_menu_item) {
this.janeway_menu_item.remove();
this.janeway_menu_item = null;
}
}
}
4 changes: 4 additions & 0 deletions lib/init/alchemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ Alchemy.setMethod(function startJaneway(options) {
return session_menu.remove();
}

session_menu.addItem('Current active browser sessions:', () => {
console.log('All sessions:', alchemy.sessions);
});

this.Janeway.session_menu = session_menu;
}

Expand Down

0 comments on commit 882db08

Please sign in to comment.