- commit
- 072dd5b
- parent
- c18cc7e
- author
- Eric Bower
- date
- 2024-02-22 21:15:41 +0000 UTC
fix: reset loader when task is cancelled (#40) This issue happens when: - User runs a task with a loader - Loading is in "loading" state - Task gets halted - Loader does not get cleaned up - Loader stays in "loading" state
1 files changed,
+50,
-32
+50,
-32
1@@ -93,6 +93,14 @@ export function loader<
2 message: err.message,
3 }),
4 ]);
5+ } finally {
6+ const loaders = yield* select((s: any) =>
7+ loaderSchema.selectByIds(s, { ids: [ctx.name, ctx.key] })
8+ );
9+ const ids = loaders
10+ .filter((loader) => loader.status === "loading")
11+ .map((loader) => loader.id);
12+ yield* updateStore(loaderSchema.resetByIds(ids));
13 }
14 };
15 }
16@@ -112,44 +120,54 @@ export function loaderApi<
17 },
18 ) {
19 return function* trackLoading(ctx: Ctx, next: Next) {
20- yield* updateStore([
21- loaderSchema.start({ id: ctx.name }),
22- loaderSchema.start({ id: ctx.key }),
23- ]);
24- if (!ctx.loader) ctx.loader = {} as any;
25+ try {
26+ yield* updateStore([
27+ loaderSchema.start({ id: ctx.name }),
28+ loaderSchema.start({ id: ctx.key }),
29+ ]);
30+ if (!ctx.loader) ctx.loader = {} as any;
31
32- yield* next();
33+ yield* next();
34
35- if (!ctx.response) {
36- yield* updateStore(
37- loaderSchema.resetByIds([ctx.name, ctx.key]),
38- );
39- return;
40- }
41+ if (!ctx.response) {
42+ yield* updateStore(
43+ loaderSchema.resetByIds([ctx.name, ctx.key]),
44+ );
45+ return;
46+ }
47
48- if (!ctx.loader) {
49- ctx.loader = {};
50- }
51+ if (!ctx.loader) {
52+ ctx.loader = {};
53+ }
54+
55+ if (!ctx.response.ok) {
56+ yield* updateStore([
57+ loaderSchema.error({
58+ id: ctx.name,
59+ message: errorFn(ctx),
60+ ...ctx.loader,
61+ }),
62+ loaderSchema.error({
63+ id: ctx.key,
64+ message: errorFn(ctx),
65+ ...ctx.loader,
66+ }),
67+ ]);
68+ return;
69+ }
70
71- if (!ctx.response.ok) {
72 yield* updateStore([
73- loaderSchema.error({
74- id: ctx.name,
75- message: errorFn(ctx),
76- ...ctx.loader,
77- }),
78- loaderSchema.error({
79- id: ctx.key,
80- message: errorFn(ctx),
81- ...ctx.loader,
82- }),
83+ loaderSchema.success({ id: ctx.name, ...ctx.loader }),
84+ loaderSchema.success({ id: ctx.key, ...ctx.loader }),
85 ]);
86- return;
87+ } finally {
88+ const loaders = yield* select((s: any) =>
89+ loaderSchema.selectByIds(s, { ids: [ctx.name, ctx.key] })
90+ );
91+ const ids = loaders
92+ .filter((loader) => loader.status === "loading")
93+ .map((loader) => loader.id);
94+ yield* updateStore(loaderSchema.resetByIds(ids));
95 }
96-
97- yield* updateStore([
98- loaderSchema.success({ id: ctx.name, ...ctx.loader }),
99- loaderSchema.success({ id: ctx.key, ...ctx.loader }),
100- ]);
101 };
102 }