Skip to content

Commit aebb208

Browse files
committed
Promised data uses function name as key in object, instead of array
1 parent 613de78 commit aebb208

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ loader.value; // "idle"
5757
loader.next("FETCH");
5858
loader.value; // "loading"
5959

60-
loader.resolved.then(([response]) => {
60+
loader.resolved.then((result) => {
61+
console.log("Fetched", result.fetchData);
6162
// Use response of fetch()
6263
loader.value; // "success"
6364
});
6465

6566
/* Or with await: */
66-
// const [response] = await loader.resolved;
67+
// const { fetchData } = await loader.resolved;
6768
// loader.value; // "success"
6869
```
6970

src/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ describe("Machine with entry and exit actions", () => {
6363
expect(loader.changeCount).toEqual(1);
6464
expect(finishedLoading).toHaveBeenCalledTimes(0);
6565

66-
await expect(loader.resolved).resolves.toEqual([42]);
67-
await expect(Promise.resolve(transitionResult)).resolves.toEqual([42]);
66+
await expect(loader.resolved).resolves.toEqual({ fetchData: 42 });
67+
await expect(Promise.resolve(transitionResult)).resolves.toEqual({ fetchData: 42 });
6868
expect(finishedLoading).toHaveBeenCalledTimes(1);
6969
expect(loader.changeCount).toEqual(2);
7070
expect(loader.value).toEqual("success");
@@ -116,7 +116,7 @@ describe("Machine with entry and exit actions", () => {
116116
expect(fetch).toHaveBeenCalledTimes(2);
117117
expect(fetch).toHaveBeenLastCalledWith("https://example.org/");
118118

119-
await expect(loader.resolved).resolves.toEqual([42]);
119+
await expect(loader.resolved).resolves.toEqual({ fetchData: 42 });
120120
expect(loader.changeCount).toEqual(4);
121121
expect(loader.value).toEqual("success");
122122
});

src/index.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ class Handlers {
108108
if (value.type === "entry") {
109109
this.entryActions.push(value);
110110

111-
const result = new Promise((resolve) => {
111+
const resultPromise = new Promise((resolve) => {
112112
resolve(value.f());
113-
});
114-
this.promises.push(result);
113+
}).then(result => ({ [value.f.name]: result }));
114+
this.promises.push(resultPromise);
115115

116116
} else if (value.type === "exit") {
117117
this.exitActions.push(value);
@@ -122,12 +122,13 @@ class Handlers {
122122
}
123123
}
124124

125-
finish(): null | Promise<Array<any>> {
125+
finish(): null | Promise<Record<string, any>> {
126126
if (this.promises.length === 0) return null;
127127

128128
const promise = Promise.resolve(this.promise)
129129
.catch(() => {})
130-
.then(() => Promise.all(this.promises));
130+
.then(() => Promise.all(this.promises))
131+
.then(resultObjects => Object.assign({}, ...resultObjects));
131132

132133
this.promise = promise;
133134

@@ -153,7 +154,7 @@ class InternalInstance {
153154
private definition: (() => StateDefinition) | (() => Generator<Yielded, StateDefinition, never>)
154155
private parent: null | InternalInstance
155156
private globalHandlers = new Handlers()
156-
private resolved = null as Promise<Array<any>> | null
157+
private resolved = null as Promise<Record<string, any>> | null
157158
child: null | InternalInstance = null
158159

159160
constructor(
@@ -194,9 +195,9 @@ class InternalInstance {
194195
return Array.from(this.generateActions());
195196
}
196197

197-
private async *valuePromises(): AsyncGenerator<any, void, undefined> {
198+
private async *valuePromises(): AsyncGenerator<Record<string, any>, void, undefined> {
198199
if (this.resolved !== null) {
199-
yield* await this.resolved;
200+
yield await this.resolved;
200201
}
201202
if (this.child !== null) {
202203
yield* this.child.valuePromises();
@@ -205,14 +206,14 @@ class InternalInstance {
205206

206207
get value(): Promise<Array<any>> {
207208
const build = async () => {
208-
const values: Array<any> = [];
209-
for await (const value of this.valuePromises()) {
210-
values.push(value);
209+
const objects: Array<any> = [];
210+
for await (const object of this.valuePromises()) {
211+
objects.push(object);
211212
}
212-
return values;
213+
return objects;
213214
}
214215

215-
return build().then(values => Promise.all(values));
216+
return build().then(objects => Object.assign({}, ...objects));
216217
}
217218

218219
consume(stateGenerator: (() => StateDefinition) | (() => Generator<Yielded, StateDefinition, never>)) {

0 commit comments

Comments
 (0)