Skip to content

Commit

Permalink
fix: Impede concurrent session execution
Browse files Browse the repository at this point in the history
- Move teardown to top-level code
  • Loading branch information
sameersismail committed Mar 13, 2023
1 parent 71d1af1 commit 5637972
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StateField } from "@codemirror/state";
import { Editor, MarkdownView, Plugin } from "obsidian";
import { Editor, MarkdownView, Notice, Plugin } from "obsidian";
import { startSession, abortSession } from "./perilous";
import { canStartScheduler, Scheduler } from "./perilous/scheduler";
import {
DEFAULT_SETTINGS,
PerilousWritingSettings,
Expand All @@ -10,7 +10,7 @@ import { log } from "./utilities";

export default class PerilousWritingPlugin extends Plugin {
settings: PerilousWritingSettings;
cmExtensions: Array<StateField<unknown>> = [];
scheduler: Scheduler | undefined;

async onload() {
await this.loadSettings();
Expand All @@ -20,27 +20,33 @@ export default class PerilousWritingPlugin extends Plugin {
id: "short-session",
name: `Begin short session`,
editorCallback: (editor: Editor, view: MarkdownView) => {
startSession(editor, view, this, this.cmExtensions, "short");
if (canStartScheduler(this.scheduler)) {
this.scheduler = startSession(editor, view, this, "short");
} else {
new Notice("A session is already in progress.");
}
},
});

this.addCommand({
id: "long-session",
name: `Begin long session`,
editorCallback: (editor: Editor, view: MarkdownView) => {
startSession(editor, view, this, this.cmExtensions, "long");
if (canStartScheduler(this.scheduler)) {
this.scheduler = startSession(editor, view, this, "long");
} else {
new Notice("A session is already in progress.");
}
},
});

log("Loaded plugin");
}

onunload() {
abortSession();
// Remove the CodeMirror transaction handler, if it still exists.
if (this.cmExtensions.length > 0) {
this.cmExtensions.pop();
this.app.workspace.updateOptions();
if (this.scheduler !== undefined) {
this.scheduler.teardown();
abortSession();
}
log("Unloaded plugin");
}
Expand Down
6 changes: 4 additions & 2 deletions src/perilous/perilous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function startSession(
editor: Editor,
view: MarkdownView,
plugin: PerilousWritingPlugin,
cmExtensions: Array<StateField<unknown>>,
sessionType: SessionType
) {
const sessionLength =
Expand All @@ -27,9 +26,10 @@ export function startSession(
: plugin.settings.longSessionLength) * MILLISECONDS_PER_MINUTE;
log(`Session length: ${sessionLength / MILLISECONDS_PER_MINUTE}m`);

const cmExtensions: Array<StateField<unknown>> = [];
const [initialContent, initialCursor] = getSnapshot(editor);
removeProgressBar();
addProgressBar(editor, sessionLength);
addProgressBar(view, sessionLength);

const hooks: SchedulerHooks = {
onInitialisation: () => {
Expand Down Expand Up @@ -88,6 +88,8 @@ export function startSession(
plugin.registerEditorExtension(cmExtensions);
log("Registered CodeMirror hook");
});

return scheduler;
}

export function abortSession() {
Expand Down
20 changes: 20 additions & 0 deletions src/perilous/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type SchedulerState = "initialised" | "running" | "success" | "failure";

export type SchedulerHooks = {
/* The first entry point in to the application; a session has been requested. */
onInitialisation: () => void;
Expand Down Expand Up @@ -26,6 +28,9 @@ export class Scheduler {
// Event hooks
hooks: SchedulerHooks;

// Scheduler state
schedulerState: SchedulerState = "initialised";

// Internals
elapsedTime: number = 0;
sinceLastEvent: number = 0;
Expand Down Expand Up @@ -83,6 +88,7 @@ export class Scheduler {
this.tickLength
);
this.startedScheduler = true;
this.schedulerState = "running";
this.hooks.onStart(this.intervalId);
} else {
this.sinceLastEvent = 0;
Expand All @@ -91,6 +97,7 @@ export class Scheduler {
}

succeed() {
this.schedulerState = "success";
this.hooks.onSuccess();
this.teardown();
}
Expand All @@ -105,6 +112,7 @@ export class Scheduler {
}

fail() {
this.schedulerState = "failure";
this.hooks.onFailure();
this.teardown();
}
Expand All @@ -114,3 +122,15 @@ export class Scheduler {
clearInterval(this.intervalId);
}
}

/**
* A scheduler can be requested if (1) there is no current scheduler, or (2)
* the previous scheduler is not in progress.
*/
export function canStartScheduler(scheduler: Scheduler | undefined): boolean {
return (
scheduler === undefined ||
(scheduler.schedulerState !== "initialised" &&
scheduler.schedulerState !== "running")
);
}

0 comments on commit 5637972

Please sign in to comment.