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

Added a way to stop the operation #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./App.css";
import { sleep, Operation } from "effection";
import { sleep, Operation, run } from "effection";
import { Player } from "./components/Player";
import { useState } from "react";

Expand Down Expand Up @@ -174,6 +174,21 @@ export const App = () => {
{...options}
/>
</li>
<li key="7" className="p-5">
<Player
title="Can be stopped when encountering a specific problem"
description="Show a loading spinner after 1 minute then show a message that connect because of VPN connection"
load={async function ({ stop }) {
await run(function* () {
yield* sleep(4000);
yield* stop("Not connected to the VPN.");
});

return "Will never happen"
}}
{...options}
/>
</li>
</ul>
</>
);
Expand Down
6 changes: 6 additions & 0 deletions src/components/LoadingSpinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export function LoadingSpinner({ loader }: { loader: LoaderState<unknown> }): JS
Failed after 3 attempts. Please contact support.
</p>
)
case "stopped":
return (
<p className="text-sky-400">
{loader.reason}
</p>
)
}

return <></>
Expand Down
32 changes: 26 additions & 6 deletions src/hooks/useLoader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo, useEffect, useState, useCallback } from "react";
import { run, type Callable } from "effection";
import { createChannel, run, spawn, type Callable, each, race, Operation } from "effection";
import { CreateLoaderOptions, createLoader } from "../operations/createLoader";
import { setUpdateContext, update } from "../operations/UpdateContext";
import { setUpdateContext } from "../operations/UpdateContext";

export type LoaderState<T> =
| {
Expand Down Expand Up @@ -33,11 +33,16 @@ export type LoaderState<T> =
| {
type: "failed";
error: Error;
};
}
| {
type: "stopped";
reason: string;
};

export type LoaderFn<T> = (params: {
attempt: number;
signal: AbortSignal;
stop: (reason: string) => Operation<void>;
}) => Callable<T>;

export type UseLoaderOptions<T> = Partial<Omit<CreateLoaderOptions<T>, "load">>;
Expand Down Expand Up @@ -86,9 +91,24 @@ export function useLoader<T>(

useEffect(() => {
const task = run(function* () {
yield* setUpdateContext(setState);
yield* update({ type: "initial" })
yield* loader();
const channel = createChannel<LoaderState<unknown>>();

yield* setUpdateContext(channel.send);

const updates = yield* spawn(function*() {
for (const value of yield* each(channel)) {
setState(value);
if (value.type === "stopped") {
throw new Error("Forced interrupt")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just stop your iteration and the updates task will win your race. I'd do that since it is an expected condition.

Suggested change
throw new Error("Forced interrupt")
break;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm throwing the error to short-circuit the other child operations. Will break do the same here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read it correctly, then yes. breaking here exits the loop, which then causes the updates task to win the race, which then causes the main task invoked with run() to return, which then shuts down everything created in that scope.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice, I didn't catch that.

}
yield* each.next();
}
});

const driver = yield* spawn(loader);

yield* race([updates, driver]);

});

return () => {
Expand Down
15 changes: 9 additions & 6 deletions src/operations/UpdateContext.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Operation, createContext, lift } from "effection";
import { Operation, createContext, suspend } from "effection";
import { LoaderState } from "../hooks/useLoader";

export const UpdateContext = createContext<typeof update>("update");
export const UpdateContext = createContext<<T>(value: LoaderState<T>) => Operation<void>>("update");

export const update = function* update<T>(value: LoaderState<T>): Operation<void> {
const setState = yield* UpdateContext;
yield* setState(value);
const send = yield* UpdateContext;
yield* send(value);
if (value.type === "stopped") {
yield* suspend();
}
}

export function* setUpdateContext(setState: (value: LoaderState<unknown>) => void): Operation<void> {
yield* UpdateContext.set(lift(setState));
export function* setUpdateContext(send: (value: LoaderState<unknown>) => Operation<void>): Operation<void> {
yield* UpdateContext.set(send);
}
20 changes: 18 additions & 2 deletions src/operations/createLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { Operation, sleep, spawn, call, useAbortSignal } from "effection";
import {
Operation,
sleep,
spawn,
call,
useAbortSignal,
useScope,
} from "effection";
import { CreateSpinnerOptions, createSpinner } from "./createSpinner";
import { update } from "./UpdateContext";
import { LoaderFn } from "../hooks/useLoader";
Expand All @@ -26,6 +33,8 @@ export function createLoader<T>({
type: "started",
});

const scope = yield* useScope();

for (let attempt = 0; attempt <= retryAttempts; attempt++) {
const spinner = yield* spawn(function* () {
if (attempt === 0) {
Expand All @@ -40,7 +49,14 @@ export function createLoader<T>({
const signal = yield* useAbortSignal();

try {
const result = yield* call(() => load({ attempt, signal }));
const result = yield* call(() =>
load({
attempt,
signal,
stop: (reason: string) =>
scope.run(() => update({ type: "stopped", reason })),
})
);

yield* update({
type: "success",
Expand Down