diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0e1b51e --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +fmt: + deno fmt +.PHONY: + +lint: + deno lint +.PHONY: lint + +test: + deno test --allow-env --allow-read +.PHONY: test diff --git a/mdw/store.ts b/mdw/store.ts index 84bf677..b0a0bf7 100644 --- a/mdw/store.ts +++ b/mdw/store.ts @@ -1,4 +1,4 @@ -import type { ApiCtx, ThunkCtx } from "../query/mod.ts"; +import type { ApiCtx, ThunkCtxWLoader } from "../query/mod.ts"; import { compose } from "../compose.ts"; import type { AnyState, Next } from "../types.ts"; import { @@ -75,28 +75,41 @@ export function loader(schema: { loaders: LoaderOutput; }) { return function* < - Ctx extends ThunkCtx = ThunkCtx, + Ctx extends ThunkCtxWLoader = ThunkCtxWLoader, >(ctx: Ctx, next: Next) { yield* updateStore([ schema.loaders.start({ id: ctx.name }), schema.loaders.start({ id: ctx.key }), ]); + if (!ctx.loader) ctx.loader = {} as any; + try { yield* next(); + + if (!ctx.loader) { + ctx.loader = {}; + } + yield* updateStore([ - schema.loaders.success({ id: ctx.name }), - schema.loaders.success({ id: ctx.key }), + schema.loaders.success({ id: ctx.name, ...ctx.loader }), + schema.loaders.success({ id: ctx.key, ...ctx.loader }), ]); } catch (err) { + if (!ctx.loader) { + ctx.loader = {}; + } + yield* updateStore([ schema.loaders.error({ id: ctx.name, message: err.message, + ...ctx.loader, }), schema.loaders.error({ id: ctx.key, message: err.message, + ...ctx.loader, }), ]); } finally { @@ -106,7 +119,10 @@ export function loader(schema: { const ids = loaders .filter((loader) => loader.status === "loading") .map((loader) => loader.id); - yield* updateStore(schema.loaders.resetByIds(ids)); + + if (ids.length > 0) { + yield* updateStore(schema.loaders.resetByIds(ids)); + } } }; } diff --git a/query/types.ts b/query/types.ts index a0b0016..489f61b 100644 --- a/query/types.ts +++ b/query/types.ts @@ -22,6 +22,10 @@ export interface ThunkCtx

extends Payload

{ result: Result; } +export interface ThunkCtxWLoader extends ThunkCtx { + loader: Omit, "id"> | null; +} + export interface LoaderCtx

extends ThunkCtx

{ loader: Partial | null; }