@zemnmez/future 1.0.2 → 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.
Files changed (2) hide show
  1. package/README.md +22 -26
  2. 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 TodoList() {
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,19 +75,22 @@ const message = future(
73
75
  ```
74
76
 
75
77
  `future_and_then` maps a resolved value while preserving the loading and error
76
- types. `future_flatten_then` flattens nested Futures, and `future_collect`
77
- combines several Futures into one.
78
+ types. Combine `future_and_then` with `future_flatten_then` to chain an
79
+ operation that returns another Future, and
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
82
- into a pipeline. Each operation receives the preceding resolved value, while a
83
- loading or error state stops the pipeline:
85
+ into a pipeline.
86
+ Each operation receives the preceding resolved value, while a loading or error
87
+ state stops the pipeline. The resulting Future includes the loading and error
88
+ types from every operation:
84
89
 
85
- ```typescript
90
+ ```typescript file=examples/pipeline.ts
86
91
  import {
87
92
  error,
88
- Future,
93
+ type Future,
89
94
  future_and_then,
90
95
  future_flatten_then,
91
96
  resolve,
@@ -96,29 +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 = future_flatten_then(
112
- future_and_then(resolve(42), findUser)
113
- );
114
- const name = future_flatten_then(
115
- future_and_then(user, displayName)
116
- );
117
- // Future<
118
- // string,
119
- // 'fetching user' | 'formatting name',
120
- // 'user not found' | 'missing name'
121
- // >
116
+ const user = future_flatten_then(future_and_then(resolve(42), findUser));
117
+ export const name = future_flatten_then(future_and_then(user, displayName));
122
118
  ```
123
119
 
124
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.2",
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.2%3A+something+went+wrong%21"
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
  }