Skip to content

Commit aa92a22

Browse files
committed
add response value to getActionStatus
1 parent f36e73d commit aa92a22

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.8.0
4+
5+
### Feature
6+
* Allow to read action response/return value from `getActionStatus` function.
7+
38
## 2.7.0
49

510
### Feature
@@ -23,7 +28,7 @@
2328
## 2.5.0
2429

2530
### Feature
26-
* Allows deep inheritence.
31+
* Allows deep inheritance.
2732

2833
## 2.4.4
2934

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://jsr.io/schema/config-file.v1.json",
33
"name": "@exome/exome",
4-
"version": "2.7.0",
4+
"version": "2.8.0",
55
"publish": {
66
"include": ["LICENSE", "README.md", "deno.json", "package.json", "src", "logo"],
77
"exclude": ["src/**/*.test.ts"]

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "exome",
3-
"version": "2.7.0",
3+
"version": "2.8.0",
44
"description": "State manager for deeply nested states",
55
"main": "./dist/exome.js",
66
"module": "./dist/exome.mjs",

src/utils.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,41 @@ const { getActionStatus } = proxyquire
136136
assert.equal(String(test1.status.error), "Error: Poop");
137137
});
138138

139+
test("returns response", async () => {
140+
class TestStore extends Exome {
141+
public get status() {
142+
return getActionStatus(this, "run");
143+
}
144+
public async run() {
145+
return "Poop";
146+
}
147+
}
148+
const test1 = new TestStore();
149+
150+
// biome-ignore lint/suspicious/noSelfCompare: it's a getter, not pointless!
151+
assert.ok(test1.status === test1.status);
152+
153+
assert.snapshot(
154+
JSON.stringify(test1.status),
155+
`{"loading":false,"error":false}`,
156+
);
157+
158+
const promise = test1.run();
159+
160+
assert.snapshot(
161+
JSON.stringify(test1.status),
162+
`{"loading":true,"error":false}`,
163+
);
164+
165+
try {
166+
await promise;
167+
} catch (_) {}
168+
169+
assert.snapshot(
170+
JSON.stringify(test1.status),
171+
`{"loading":false,"error":false,"response":"Poop"}`,
172+
);
173+
});
174+
139175
test.run();
140176
}

src/utils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
*/
44
import { type Exome, addMiddleware, getExomeId, update } from "exome";
55

6-
interface ActionStatus<E = any> {
6+
interface ActionStatus<E = any, R = any> {
77
loading: boolean;
88
error: false | E;
9+
response: void | R;
910
unsubscribe: () => void;
1011
}
1112

@@ -14,10 +15,10 @@ const actionStatusCache: Record<string, ActionStatus> = {};
1415
/**
1516
* Subscribes to specific action in specific instance and returns satus about that action.
1617
*/
17-
export function getActionStatus<E = Error, T extends Exome = any>(
18+
export function getActionStatus<E = Error, T extends Exome = any, R = any>(
1819
store: T,
1920
action: keyof T,
20-
): ActionStatus<E> {
21+
): ActionStatus<E, R> {
2122
const key = getExomeId(store) + ":" + (action as string);
2223
let cached = actionStatusCache[key];
2324

@@ -28,6 +29,7 @@ export function getActionStatus<E = Error, T extends Exome = any>(
2829
cached = actionStatusCache[key] = {
2930
loading: false,
3031
error: false,
32+
response: undefined,
3133
unsubscribe() {
3234
unsubscribe();
3335
actionStatusCache[key] = undefined as any;
@@ -45,16 +47,18 @@ export function getActionStatus<E = Error, T extends Exome = any>(
4547
const currentActionIndex = actionIndex;
4648
cached.loading = true;
4749
cached.error = false;
50+
cached.response = undefined;
4851

4952
update(instance);
5053

51-
return (error) => {
54+
return (error, response) => {
5255
if (currentActionIndex !== actionIndex || !cached) {
5356
return;
5457
}
5558

5659
cached.loading = false;
5760
cached.error = error || false;
61+
cached.response = response || undefined;
5862

5963
update(instance);
6064
};

0 commit comments

Comments
 (0)