Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Loader): order of async operations #3562

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { TaskWithDelayAndMinimalDuration } from '../taskWithDelayAndMinimalDuration';
import { delay } from '../utils';

describe('stop() cases', () => {
it('after before task works', async () => {
const taskStartCallback = jest.fn();
const taskStopCallback = jest.fn();

const delayBeforeTaskStart = 300;
const durationOfTask = 1000;
const task = new TaskWithDelayAndMinimalDuration({
delayBeforeTaskStart,
durationOfTask,
taskStartCallback,
taskStopCallback,
});

expect(task).not.toBeNull();
expect(taskStartCallback).not.toBeCalled();
expect(taskStopCallback).not.toBeCalled();

task.start();
await delay(delayBeforeTaskStart);
expect(taskStartCallback).toBeCalled();
task.stop();
expect(taskStopCallback).not.toBeCalled();
await delay(durationOfTask);
expect(taskStopCallback).toBeCalled();
});

it('after duration task ends', async () => {
const taskStartCallback = jest.fn();
const taskStopCallback = jest.fn();

const delayBeforeTaskStart = 300;
const durationOfTask = 1000;
const task = new TaskWithDelayAndMinimalDuration({
delayBeforeTaskStart,
durationOfTask,
taskStartCallback,
taskStopCallback,
});

expect(task).not.toBeNull();
expect(taskStartCallback).not.toBeCalled();
expect(taskStopCallback).not.toBeCalled();

task.start();
await delay(delayBeforeTaskStart);
expect(taskStartCallback).toBeCalled();
await delay(durationOfTask);
task.stop();
expect(taskStopCallback).toBeCalled();
});

it('stop when before task start', async () => {
const taskStartCallback = jest.fn();
const taskStopCallback = jest.fn();

const delayBeforeTaskStart = 300;
const durationOfTask = 1000;
const task = new TaskWithDelayAndMinimalDuration({
delayBeforeTaskStart,
durationOfTask,
taskStartCallback,
taskStopCallback,
});

expect(task).not.toBeNull();
expect(taskStartCallback).not.toBeCalled();
expect(taskStopCallback).not.toBeCalled();

task.start();
await delay(100);
task.stop();
await delay(delayBeforeTaskStart);
expect(taskStartCallback).not.toBeCalled();
expect(taskStopCallback).not.toBeCalled();
});

it('stop callback fired when stop in last event', async () => {
const taskStartCallback = jest.fn();
const taskStopCallback = jest.fn();

const delayBeforeTaskStart = 300;
const durationOfTask = 1000;
const task = new TaskWithDelayAndMinimalDuration({
delayBeforeTaskStart,
durationOfTask,
taskStartCallback,
taskStopCallback,
});

task.start(); // запускаем таску "короткую"
await delay(delayBeforeTaskStart + 900); // ждем пока закончится "короткая" таска
task.stop(); // пытаемся остановить таски, но еще запущена "длинная" таска
task.start(); // пытаемся запустить новую короткую таску пока запущена "длинная" таска (она не должна запуститься)
await delay(200);
task.stop(); // пытаемся остановить таски, "длинная" уже закончилась, короткая еще бы не успела запуститься

await delay(delayBeforeTaskStart + durationOfTask); // можно ждать сколько угодно, taskStopCallback должен быть вызван, если короткая в итоге не запустилась
expect(taskStopCallback).toBeCalled();
});
});
4 changes: 4 additions & 0 deletions packages/react-ui/lib/taskWithDelayAndMinimalDuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class TaskWithDelayAndMinimalDuration {

this.isTaskActive = true;

if (this.timeoutBeforeTaskStop) {
return;
}

if (this.timeoutBeforeTaskStart) {
this.clearTimeoutBeforeTaskStart();
}
Expand Down