Skip to content

Commit cfc89e6

Browse files
authored
Document resource management using .manage() (#83)
* Document resource management using .manage() Added documentation on managing resources with .manage() in Effection. * remove store registration lines * Add 'Managing Resources' section to sidebar
1 parent 160c345 commit cfc89e6

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

docs/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func main() {
110110
Href: "/fx",
111111
Page: pager("fx.md"),
112112
},
113+
{
114+
Text: "Managing Resources",
115+
Href: "/managing-resources",
116+
Page: pager("managing-resources.md"),
117+
},
113118
{
114119
Text: "Error Handling",
115120
Href: "/error-handling",

docs/posts/managing-resources.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Managing resources
3+
description: How to use .manage() to register effection resources with thunks/api
4+
---
5+
6+
# Managed resources ✅
7+
8+
`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.
9+
10+
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.
11+
12+
## Example
13+
14+
This is a contrived resource, but demonstrates the implementation and use of a resource.
15+
16+
```ts
17+
import { resource } from "effection";
18+
19+
function guessAge(): Operation<{ guess: number; cumulative: number | null }> {
20+
return resource(function* (provide) {
21+
let cumulative: number | null = 0;
22+
try {
23+
// this wouldn't be valuable per se, but demonstrates how the functionality is exposed
24+
yield* provide({
25+
get guess() {
26+
const n = Math.floor(Math.random() * 100);
27+
if (cumulative !== null) cumulative += n;
28+
return n;
29+
},
30+
get cumulative() {
31+
return cumulative;
32+
},
33+
});
34+
} finally {
35+
// cleanup when the resource is closed
36+
cumulative = null;
37+
}
38+
});
39+
}
40+
```
41+
42+
Manage the resource:
43+
44+
```ts
45+
// With `createThunks` (the pattern is the same for `createApi`):
46+
const thunks = createThunks();
47+
const GuesserCtx = thunks.manage("guesser", guessAge());
48+
49+
// inside an operation (thunk, middleware, etc.)
50+
const action = thunks.create("do-thing", function* (ctx, next) {
51+
// use the managed resource inside an action
52+
const g = yield* GuesserCtx.get(); // may return undefined
53+
const g2 = yield* GuesserCtx.expect(); // will throw if resource is not available
54+
55+
console.log(g2.guess, g2.cumulative);
56+
yield* next();
57+
});
58+
```

0 commit comments

Comments
 (0)