From c7ec4a12b4c81c0cf3339cf9a036b3c990a0d1d4 Mon Sep 17 00:00:00 2001 From: Jacob Bolda Date: Mon, 23 Mar 2026 14:10:58 -0500 Subject: [PATCH 1/3] Document resource management using .manage() Added documentation on managing resources with .manage() in Effection. --- docs/posts/managing-resources.md | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 docs/posts/managing-resources.md diff --git a/docs/posts/managing-resources.md b/docs/posts/managing-resources.md new file mode 100644 index 0000000..6d75d20 --- /dev/null +++ b/docs/posts/managing-resources.md @@ -0,0 +1,61 @@ +--- +title: Managing resources +description: How to use .manage() to register effection resources with thunks/api +--- + +# Managed resources ✅ + +`starfx` supports managing Effection `resource`s and exposing them via a `Context` using the `.manage()` helper on `thunks` (from `createThunks`) and `api` (from `createApi`). The `manage` call will start the resource inside the scope and return a `Context` you can `get()` or `expect()` inside your operations to access the provided value. + +A `resource` is useful in encapsulating logic or functionality. It is particularly useful for managing connections such as a WebSocket connections, Web Workers, auth or telemetry. These processes can be wired up, including failure, restart and shutdown logic, and then used in any of your actions. See the [`effectionx` repo](https://github.com/thefrontside/effectionx) for published packages which implement an effection `resource` and also serve as good examples. Resources are only available in the thunk or api managing it. + +## Example + +This is a contrived resource, but demonstrates the implementation and use of a resource. + +```ts +import { resource } from "effection"; + +function guessAge(): Operation<{ guess: number; cumulative: number | null }> { + return resource(function* (provide) { + let cumulative: number | null = 0; + try { + // this wouldn't be valuable per se, but demonstrates how the functionality is exposed + yield* provide({ + get guess() { + const n = Math.floor(Math.random() * 100); + if (cumulative !== null) cumulative += n; + return n; + }, + get cumulative() { + return cumulative; + }, + }); + } finally { + // cleanup when the resource is closed + cumulative = null; + } + }); +} +``` + +Manage the resource: + +```ts +// With `createThunks` (the pattern is the same for `createApi`): +const thunks = createThunks(); +const GuesserCtx = thunks.manage("guesser", guessAge()); + +// inside an operation (thunk, middleware, etc.) +const action = thunks.create("do-thing", function* (ctx, next) { + // use the managed resource inside an action + const g = yield* GuesserCtx.get(); // may return undefined + const g2 = yield* GuesserCtx.expect(); // will throw if resource is not available + + console.log(g2.guess, g2.cumulative); + yield* next(); +}); + +store.initilize(thunks.register); +store.dispatch(action()); +``` From fa84e7da57ac28e6b9c860df8b321af652b2eb94 Mon Sep 17 00:00:00 2001 From: Jacob Bolda Date: Mon, 23 Mar 2026 14:13:52 -0500 Subject: [PATCH 2/3] remove store registration lines --- docs/posts/managing-resources.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/posts/managing-resources.md b/docs/posts/managing-resources.md index 6d75d20..2ce001f 100644 --- a/docs/posts/managing-resources.md +++ b/docs/posts/managing-resources.md @@ -55,7 +55,4 @@ const action = thunks.create("do-thing", function* (ctx, next) { console.log(g2.guess, g2.cumulative); yield* next(); }); - -store.initilize(thunks.register); -store.dispatch(action()); ``` From a0516cdb7410c5de775e693c16a1d28055bbf6e9 Mon Sep 17 00:00:00 2001 From: Jacob Bolda Date: Mon, 23 Mar 2026 14:14:36 -0500 Subject: [PATCH 3/3] Add 'Managing Resources' section to sidebar --- docs/main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/main.go b/docs/main.go index d6c8dce..fa446b5 100644 --- a/docs/main.go +++ b/docs/main.go @@ -110,6 +110,11 @@ func main() { Href: "/fx", Page: pager("fx.md"), }, + { + Text: "Managing Resources", + Href: "/managing-resources", + Page: pager("managing-resources.md"), + }, { Text: "Error Handling", Href: "/error-handling",