Skip to content

Commit

Permalink
breaking: rename isLoading to isRunning
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti committed May 31, 2024
1 parent d096fef commit ffc9075
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions src/lib/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export function _task<TArgs = unknown, TReturn = undefined>(
const results: TReturn[] = [];

const { subscribe, ...result } = writable({
isLoading: false,
isRunning: false,
lastSuccessful: undefined as undefined | TReturn,
error: undefined as undefined | unknown,
results,
performCount: 0,
});

const instances = new Map<string, { isLoading: boolean }>();
const instances = new Map<string, { is_running: boolean }>();

const actual_task = createTask<TArgs, TReturn>(
{
Expand All @@ -32,36 +32,36 @@ export function _task<TArgs = unknown, TReturn = undefined>(
},
onError(instance_id, error) {
const instance = instances.get(instance_id);
if (instance) instance.isLoading = false;
if (instance) instance.is_running = false;
result.update((old) => {
old.error = error;
old.isLoading = [...instances.values()].some((val) => val.isLoading);
old.isRunning = [...instances.values()].some((val) => val.is_running);
return old;
});
},
onInstanceCancel(instance_id) {
const instance = instances.get(instance_id);
if (instance) instance.isLoading = false;
if (instance) instance.is_running = false;
result.update((old) => {
old.isLoading = [...instances.values()].some((val) => val.isLoading);
old.isRunning = [...instances.values()].some((val) => val.is_running);
return old;
});
},
onInstanceStart(instance_id) {
instances.set(instance_id, { isLoading: true });
instances.set(instance_id, { is_running: true });
result.update((old) => {
old.isLoading = [...instances.values()].some((val) => val.isLoading);
old.isRunning = [...instances.values()].some((val) => val.is_running);
old.performCount++;
return old;
});
},
onInstanceComplete(instance_id, last_result) {
results.push(last_result);
const instance = instances.get(instance_id);
if (instance) instance.isLoading = false;
if (instance) instance.is_running = false;
result.update((old) => {
old.error = undefined;
old.isLoading = [...instances.values()].some((val) => val.isLoading);
old.isRunning = [...instances.values()].some((val) => val.is_running);
old.lastSuccessful = last_result;
return old;
});
Expand Down
22 changes: 11 additions & 11 deletions src/lib/tests/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe.each([
perform_count: {
expected: 5,
},
is_loading: {
is_running: {
wait_for: 3,
wait_after: 5,
},
Expand All @@ -39,7 +39,7 @@ describe.each([
perform_count: {
expected: 5,
},
is_loading: {
is_running: {
wait_for: 3,
wait_after: 5,
},
Expand All @@ -50,7 +50,7 @@ describe.each([
perform_count: {
expected: 1,
},
is_loading: {
is_running: {
wait_for: 0,
wait_after: 1,
},
Expand All @@ -61,7 +61,7 @@ describe.each([
perform_count: {
expected: 2,
},
is_loading: {
is_running: {
wait_for: 1,
wait_after: 2,
},
Expand All @@ -72,12 +72,12 @@ describe.each([
perform_count: {
expected: 5,
},
is_loading: {
is_running: {
wait_for: 0,
wait_after: 1,
},
},
])('task - basic functionality $name', ({ component, perform_count, is_loading }) => {
])('task - basic functionality $name', ({ component, perform_count, is_running }) => {
all_options((selector) => {
it('calls the function you pass in', async () => {
const fn = vi.fn();
Expand Down Expand Up @@ -187,7 +187,7 @@ describe.each([
expect(get(store).performCount).toBe(perform_count.expected);
});

it('has the correct derived state for isLoading', async () => {
it('has the correct derived state for isRunning', async () => {
let finished = 0;
const fn = vi.fn(async function* () {
await wait(50);
Expand All @@ -210,17 +210,17 @@ describe.each([
perform.click();
await vi.waitFor(
() => {
expect(finished).toBe(is_loading.wait_for);
expect(finished).toBe(is_running.wait_for);
},
{
interval: 10,
},
);
expect(get(store).isLoading).toBe(true);
expect(get(store).isRunning).toBe(true);
await vi.waitFor(() => {
expect(finished).toBe(is_loading.wait_after);
expect(finished).toBe(is_running.wait_after);
});
expect(get(store).isLoading).toBe(false);
expect(get(store).isRunning).toBe(false);
});
});

Expand Down

0 comments on commit ffc9075

Please sign in to comment.