@zemnmez/future 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -21
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ need to remain in a minified bundle.
|
|
|
18
18
|
when a component should call all of its hooks unconditionally and select its
|
|
19
19
|
loading, error, or resolved UI only after those hooks have run:
|
|
20
20
|
|
|
21
|
-
```tsx
|
|
21
|
+
```tsx file=examples/react-query.tsx
|
|
22
22
|
import { useQuery } from '@tanstack/react-query';
|
|
23
23
|
import { future, useQueryFuture } from '@zemnmez/future';
|
|
24
24
|
|
|
@@ -27,7 +27,9 @@ interface Todo {
|
|
|
27
27
|
title: string;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
function
|
|
30
|
+
declare function useTheme(): { error: string; todoList: string };
|
|
31
|
+
|
|
32
|
+
export function TodoList() {
|
|
31
33
|
const theme = useTheme();
|
|
32
34
|
const todos = useQueryFuture(
|
|
33
35
|
useQuery<Todo[], Error>({
|
|
@@ -35,7 +37,7 @@ function TodoList() {
|
|
|
35
37
|
queryFn: async () => {
|
|
36
38
|
const response = await fetch('/api/todos');
|
|
37
39
|
if (!response.ok) throw new Error('Could not load todos.');
|
|
38
|
-
return response.json()
|
|
40
|
+
return response.json() as Promise<Todo[]>;
|
|
39
41
|
},
|
|
40
42
|
})
|
|
41
43
|
);
|
|
@@ -60,11 +62,11 @@ function TodoList() {
|
|
|
60
62
|
Construct a Future with `resolve`, `loading`, or `error`, then handle its three
|
|
61
63
|
possible states with `future`:
|
|
62
64
|
|
|
63
|
-
```typescript
|
|
64
|
-
import { Future, future, resolve } from '@zemnmez/future';
|
|
65
|
+
```typescript file=examples/basic.ts
|
|
66
|
+
import { type Future, future, resolve } from '@zemnmez/future';
|
|
65
67
|
|
|
66
|
-
const answer: Future<number, number, Error> = resolve(42);
|
|
67
|
-
const message = future(
|
|
68
|
+
export const answer: Future<number, number, Error> = resolve(42);
|
|
69
|
+
export const message = future(
|
|
68
70
|
answer,
|
|
69
71
|
value => `The answer is ${value}.`,
|
|
70
72
|
progress => `Loading: ${progress}%`,
|
|
@@ -73,21 +75,24 @@ const message = future(
|
|
|
73
75
|
```
|
|
74
76
|
|
|
75
77
|
`future_and_then` maps a resolved value while preserving the loading and error
|
|
76
|
-
types. `
|
|
78
|
+
types. Combine `future_and_then` with `future_flatten_then` to chain an
|
|
79
|
+
operation that returns another Future, and
|
|
77
80
|
`future_collect` combines several Futures into one.
|
|
78
81
|
|
|
79
82
|
## Pipelines
|
|
80
83
|
|
|
81
|
-
`
|
|
84
|
+
`future_and_then` and `future_flatten_then` compose Future-returning operations
|
|
85
|
+
into a pipeline.
|
|
82
86
|
Each operation receives the preceding resolved value, while a loading or error
|
|
83
87
|
state stops the pipeline. The resulting Future includes the loading and error
|
|
84
88
|
types from every operation:
|
|
85
89
|
|
|
86
|
-
```typescript
|
|
90
|
+
```typescript file=examples/pipeline.ts
|
|
87
91
|
import {
|
|
88
92
|
error,
|
|
89
|
-
Future,
|
|
90
|
-
|
|
93
|
+
type Future,
|
|
94
|
+
future_and_then,
|
|
95
|
+
future_flatten_then,
|
|
91
96
|
resolve,
|
|
92
97
|
} from '@zemnmez/future';
|
|
93
98
|
|
|
@@ -96,25 +101,20 @@ interface User {
|
|
|
96
101
|
name: string;
|
|
97
102
|
}
|
|
98
103
|
|
|
99
|
-
const findUser = (
|
|
104
|
+
export const findUser = (
|
|
100
105
|
id: number
|
|
101
106
|
): Future<User, 'fetching user', 'user not found'> =>
|
|
102
107
|
id === 42
|
|
103
108
|
? resolve({ id, name: 'Deep Thought' })
|
|
104
109
|
: error('user not found');
|
|
105
110
|
|
|
106
|
-
const displayName = (
|
|
111
|
+
export const displayName = (
|
|
107
112
|
user: User
|
|
108
113
|
): Future<string, 'formatting name', 'missing name'> =>
|
|
109
114
|
user.name ? resolve(user.name) : error('missing name');
|
|
110
115
|
|
|
111
|
-
const user =
|
|
112
|
-
const name =
|
|
113
|
-
// Future<
|
|
114
|
-
// string,
|
|
115
|
-
// 'fetching user' | 'formatting name',
|
|
116
|
-
// 'user not found' | 'missing name'
|
|
117
|
-
// >
|
|
116
|
+
const user = future_flatten_then(future_and_then(resolve(42), findUser));
|
|
117
|
+
export const name = future_flatten_then(future_and_then(user, displayName));
|
|
118
118
|
```
|
|
119
119
|
|
|
120
120
|
[result]: https://www.npmjs.com/package/@zemnmez/result
|
package/package.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"publishConfig": {
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
|
-
"version": "1.0.
|
|
21
|
+
"version": "1.0.5",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@tanstack/react-query": "5.101.4",
|
|
24
24
|
"react": "19.2.8"
|
|
@@ -33,6 +33,6 @@
|
|
|
33
33
|
"directory": "ts/future"
|
|
34
34
|
},
|
|
35
35
|
"bugs": {
|
|
36
|
-
"url": "https://github.com/zemn-me/monorepo/issues/new?title=%2F%2Fts%2Ffuture%401.0.
|
|
36
|
+
"url": "https://github.com/zemn-me/monorepo/issues/new?title=%2F%2Fts%2Ffuture%401.0.5%3A+something+went+wrong%21"
|
|
37
37
|
}
|
|
38
38
|
}
|