Skip to content

Commit

Permalink
Made setTimeout/setInterval lifecycle aware (#79)
Browse files Browse the repository at this point in the history
* logic

* tests

* earlier defaulting

* bump
  • Loading branch information
zoe-codez authored Oct 4, 2024
1 parent 9c99c66 commit d455905
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": {
"url": "git+https://github.com/Digital-Alchemy-TS/core"
},
"version": "24.10.1",
"version": "24.10.2",
"author": {
"url": "https://github.com/zoe-codez",
"name": "Zoe Codez"
Expand Down
12 changes: 12 additions & 0 deletions src/services/configuration.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ export function Configuration({
internal.utils.object.set(configuration, [library, key].join("."), definitions[key].default);
});
configDefinitions.set(library, definitions);
const bootConfig = internal.boot.options?.configuration ?? {};
if (library in bootConfig) {
const project = library as keyof typeof bootConfig;
const config = bootConfig[project];
Object.keys(config).forEach(key => {
internal.utils.object.set(
configuration,
[project, key].join("."),
config[key as keyof typeof config],
);
});
}
}

return {
Expand Down
38 changes: 32 additions & 6 deletions src/services/scheduler.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,43 @@ export function Scheduler({ logger, lifecycle, internal }: TServiceParams): Sche
cron,
interval,
setInterval: (callback: () => TBlackHole, ms: number) => {
const timer = setInterval(async () => await internal.safeExec(callback), ms);
const remove = () => clearTimeout(timer);
let timer: ReturnType<typeof setInterval>;
let stopped = false;
lifecycle.onReady(() => {
if (stopped) {
return;
}
timer = setInterval(async () => await internal.safeExec(callback), ms);
});
const remove = () => {
stopped = true;
stop.delete(remove);
if (timer) {
clearInterval(timer);
}
};
stop.add(remove);
return remove;
},
setTimeout: (callback: () => TBlackHole, ms: number) => {
const timer = setTimeout(async () => {
let timer: ReturnType<typeof setTimeout>;
let stopped = false;
lifecycle.onReady(() => {
if (stopped) {
return;
}
timer = setTimeout(async () => {
stop.delete(remove);
await internal.safeExec(callback);
}, ms);
});
const remove = () => {
stopped = true;
stop.delete(remove);
await internal.safeExec(callback);
}, ms);
const remove = () => clearTimeout(timer);
if (timer) {
clearTimeout(timer);
}
};
stop.add(remove);
return remove;
},
Expand Down
28 changes: 26 additions & 2 deletions testing/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,28 @@ describe("Scheduler", () => {
it("stops", async () => {
jest.useFakeTimers();
const spy = jest.fn();
const app = await TestRunner().run(({ scheduler }) => {
const app = await TestRunner().run(({ scheduler, lifecycle }) => {
const remove = scheduler.setInterval(spy, MINUTE);
setTimeout(() => remove(), 30 * MINUTE);
lifecycle.onReady(() => {
setTimeout(() => remove(), 30 * MINUTE);
});
});
jest.advanceTimersByTime(60 * MINUTE);
expect(spy).toHaveBeenCalledTimes(30);
jest.useRealTimers();
await app.teardown();
});

it("stops early", async () => {
const spy = jest.fn();
const intervalSpy = jest.spyOn(global, "setInterval");
const app = await TestRunner().run(({ scheduler }) => {
const remove = scheduler.setInterval(spy, MINUTE);
remove();
});
expect(intervalSpy).not.toHaveBeenCalled();
await app.teardown();
});
});

describe("setTimeout", () => {
Expand Down Expand Up @@ -96,6 +109,17 @@ describe("Scheduler", () => {
jest.useRealTimers();
await app.teardown();
});

it("stops early", async () => {
const spy = jest.fn();
const intervalSpy = jest.spyOn(global, "setTimeout");
const app = await TestRunner().run(({ scheduler }) => {
const remove = scheduler.setTimeout(spy, MINUTE);
remove();
});
expect(intervalSpy).not.toHaveBeenCalled();
await app.teardown();
});
});

describe("sliding", () => {
Expand Down

0 comments on commit d455905

Please sign in to comment.