@hamur/adapter-angular 0.0.1 → 0.0.2
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.
- package/dist/src/adapter.js +3 -1
- package/dist/types/src/app.d.ts +3 -0
- package/dist/types/src/client-parts.d.ts +3 -0
- package/dist/types/src/compile.d.ts +4 -2
- package/dist/types/src/error-page.d.ts +5 -2
- package/dist/types/src/form.d.ts +12 -9
- package/dist/types/src/indicators.d.ts +15 -8
- package/dist/types/src/island.d.ts +9 -6
- package/dist/types/src/link.d.ts +25 -22
- package/dist/types/src/markdown.d.ts +6 -3
- package/dist/types/src/navigate.d.ts +6 -3
- package/dist/types/src/outlet.d.ts +10 -5
- package/dist/types/src/server-island.d.ts +6 -3
- package/dist/types/src/time.d.ts +28 -25
- package/package.json +30 -9
- package/src/app.ts +11 -5
- package/src/client-parts.ts +83 -103
- package/src/compile.ts +9 -5
- package/src/error-page.ts +23 -10
- package/src/form.ts +166 -237
- package/src/indicators.ts +198 -229
- package/src/island.ts +78 -84
- package/src/link.ts +204 -241
- package/src/markdown.ts +129 -159
- package/src/navigate.ts +20 -17
- package/src/outlet.ts +297 -325
- package/src/server-island.ts +62 -49
- package/src/time.ts +108 -101
package/src/form.ts
CHANGED
|
@@ -1,253 +1,182 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
assertInInjectionContext,
|
|
7
|
-
booleanAttribute,
|
|
8
|
-
computed,
|
|
9
|
-
effect,
|
|
10
|
-
inject,
|
|
11
|
-
input,
|
|
12
|
-
signal,
|
|
13
|
-
untracked,
|
|
14
|
-
type Signal,
|
|
15
|
-
} from "@angular/core"
|
|
16
|
-
import {
|
|
17
|
-
IDLE_FORM_STATUS,
|
|
18
|
-
assertFormMethod,
|
|
19
|
-
createActionRunner,
|
|
20
|
-
createFormStatus,
|
|
21
|
-
functionFormAction,
|
|
22
|
-
linkActionRunner,
|
|
23
|
-
optimisticSource,
|
|
24
|
-
restoredResult,
|
|
25
|
-
runFunctionForm,
|
|
26
|
-
} from "hamur/adapter"
|
|
27
|
-
import type { ActionOptions, ActionRunner, FormStatus, FormStatusStore, Handler } from "hamur/adapter"
|
|
28
|
-
import { createForm, enhance } from "hamur/app"
|
|
29
|
-
import type {
|
|
30
|
-
ActionFailure,
|
|
31
|
-
EnhanceOptions,
|
|
32
|
-
FieldsOf,
|
|
33
|
-
HamurError,
|
|
34
|
-
HandlerFormInstance,
|
|
35
|
-
PendingRun,
|
|
36
|
-
RouteLocation,
|
|
37
|
-
} from "hamur/app"
|
|
38
|
-
import {
|
|
39
|
-
HAMUR_APP,
|
|
40
|
-
HAMUR_HREF,
|
|
41
|
-
HAMUR_PAYLOAD,
|
|
42
|
-
HAMUR_ROUTE,
|
|
43
|
-
HAMUR_ROUTER,
|
|
44
|
-
detachedHref,
|
|
45
|
-
onServer,
|
|
46
|
-
required,
|
|
47
|
-
} from "./context.ts"
|
|
48
|
-
|
|
49
|
-
type FormHandler = Handler<any, any>
|
|
50
|
-
type FormAction = string | RouteLocation | FormHandler
|
|
51
|
-
|
|
1
|
+
import { DestroyRef, Directive, ElementRef, InjectionToken, assertInInjectionContext, booleanAttribute, computed, effect, inject, input, signal, untracked, } from "@angular/core";
|
|
2
|
+
import { IDLE_FORM_STATUS, assertFormMethod, createActionRunner, createFormStatus, functionFormAction, linkActionRunner, optimisticSource, restoredResult, runFunctionForm, } from "hamur/adapter";
|
|
3
|
+
import { createForm, enhance } from "hamur/app";
|
|
4
|
+
import { HAMUR_APP, HAMUR_HREF, HAMUR_PAYLOAD, HAMUR_ROUTE, HAMUR_ROUTER, detachedHref, onServer, required, } from "./context.ts";
|
|
5
|
+
import * as i0 from "@angular/core";
|
|
52
6
|
/** The status of the `Form` a component is created in: what `injectFormStatus` reads and `injectAction` links to. */
|
|
53
|
-
const FORM_STATUS = new InjectionToken
|
|
54
|
-
|
|
55
|
-
const handlerStateOf = (payload: Signal<unknown> | null) =>
|
|
56
|
-
(payload?.() as { handlerState?: unknown } | undefined)?.handlerState
|
|
57
|
-
|
|
7
|
+
const FORM_STATUS = new InjectionToken("hamur form status");
|
|
8
|
+
const handlerStateOf = (payload) => payload?.()?.handlerState;
|
|
58
9
|
/**
|
|
59
10
|
* `<form hamurForm>`. The host bindings write what the other adapters' `Form` writes on its
|
|
60
11
|
* `<form>`; with a handler it posts to the page with `_fn=<id>` without JavaScript and calls the handler's stub with it.
|
|
61
12
|
*/
|
|
62
|
-
@Directive({
|
|
63
|
-
selector: "form[hamurForm]",
|
|
64
|
-
providers: [{ provide: FORM_STATUS, useFactory: createFormStatus }],
|
|
65
|
-
host: {
|
|
66
|
-
"data-hamur-form": "",
|
|
67
|
-
"[attr.method]": "methodAttribute()",
|
|
68
|
-
"[attr.action]": "target()",
|
|
69
|
-
"[attr.enctype]": "encType()",
|
|
70
|
-
"[attr.data-hamur-replace]": "flag(replace())",
|
|
71
|
-
"[attr.data-hamur-reset]": "flag(reset())",
|
|
72
|
-
"[attr.data-hamur-reload]": "flag(reloadDocument())",
|
|
73
|
-
"[attr.data-hamur-refresh]": "refresh() === false ? 'false' : null",
|
|
74
|
-
"[attr.data-hamur-restore]": "restore() ? null : 'false'",
|
|
75
|
-
},
|
|
76
|
-
})
|
|
77
13
|
export class Form {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
14
|
+
action = input(undefined, { ...(ngDevMode ? { debugName: "action" } : /* istanbul ignore next */ {}), alias: "hamurForm",
|
|
15
|
+
transform: (value) => (value === "" ? undefined : value) });
|
|
16
|
+
method = input(/* @ts-ignore */
|
|
17
|
+
...(ngDevMode ? [undefined, { debugName: "method" }] : /* istanbul ignore next */ []));
|
|
18
|
+
encType = input(/* @ts-ignore */
|
|
19
|
+
...(ngDevMode ? [undefined, { debugName: "encType" }] : /* istanbul ignore next */ []));
|
|
20
|
+
replace = input(false, { ...(ngDevMode ? { debugName: "replace" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
21
|
+
reset = input(false, { ...(ngDevMode ? { debugName: "reset" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
22
|
+
reloadDocument = input(false, { ...(ngDevMode ? { debugName: "reloadDocument" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
23
|
+
refresh = input(undefined, { ...(ngDevMode ? { debugName: "refresh" } : /* istanbul ignore next */ {}), transform: (value) => (value === undefined ? undefined : booleanAttribute(value)) });
|
|
24
|
+
restore = input(true, { ...(ngDevMode ? { debugName: "restore" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
25
|
+
instance = input(/* @ts-ignore */
|
|
26
|
+
...(ngDevMode ? [undefined, { debugName: "instance" }] : /* istanbul ignore next */ []));
|
|
27
|
+
app = inject(HAMUR_APP, { optional: true });
|
|
28
|
+
route = inject(HAMUR_ROUTE, { optional: true });
|
|
29
|
+
router = inject(HAMUR_ROUTER, { optional: true });
|
|
30
|
+
resolve = inject(HAMUR_HREF, { optional: true }) ?? detachedHref;
|
|
31
|
+
status = inject(FORM_STATUS);
|
|
32
|
+
element = inject(ElementRef).nativeElement;
|
|
33
|
+
methodAttribute = computed(() => (typeof this.action() === "function" ? "post" : "get"), /* @ts-ignore */
|
|
34
|
+
...(ngDevMode ? [{ debugName: "methodAttribute" }] : /* istanbul ignore next */ []));
|
|
35
|
+
target = computed(() => {
|
|
36
|
+
const action = this.action();
|
|
37
|
+
assertFormMethod(action, this.method());
|
|
38
|
+
const page = this.route ? this.route().fullPath.replace(/#.*$/, "") : undefined;
|
|
39
|
+
if (typeof action === "function")
|
|
40
|
+
return functionFormAction(action, this.resolve(page ?? "/"), this.instance());
|
|
41
|
+
return action !== undefined ? this.resolve(action) : page !== undefined ? this.resolve(page) : undefined;
|
|
42
|
+
}, /* @ts-ignore */
|
|
43
|
+
...(ngDevMode ? [{ debugName: "target" }] : /* istanbul ignore next */ []));
|
|
44
|
+
posts = computed(() => typeof this.action() === "function", /* @ts-ignore */
|
|
45
|
+
...(ngDevMode ? [{ debugName: "posts" }] : /* istanbul ignore next */ []));
|
|
46
|
+
constructor() {
|
|
47
|
+
const browser = !onServer();
|
|
48
|
+
effect((onCleanup) => {
|
|
49
|
+
if (!browser)
|
|
50
|
+
return;
|
|
51
|
+
const posts = this.posts();
|
|
52
|
+
onCleanup(untracked(() => enhance(this.element, this.enhanceOptions(posts))));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
enhanceOptions(posts) {
|
|
56
|
+
const { status } = this;
|
|
57
|
+
return {
|
|
58
|
+
onSubmit: (args) => status.submitted(args.formData),
|
|
59
|
+
...(posts && this.app
|
|
60
|
+
? { call: (input, formData) => this.post(input, formData) }
|
|
61
|
+
: {}),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** The handler, `refresh` and `instance` of the moment of the submission, as `action` and `_fn` show them. */
|
|
65
|
+
post(input, formData) {
|
|
66
|
+
const action = this.action();
|
|
67
|
+
const { app, router, status } = this;
|
|
68
|
+
if (typeof action !== "function" || !app)
|
|
69
|
+
return Promise.resolve(false);
|
|
70
|
+
return runFunctionForm(app, action, input, formData, () => router?.refresh() ?? Promise.resolve(), status, {
|
|
71
|
+
refresh: this.refresh(),
|
|
72
|
+
instance: this.instance(),
|
|
73
|
+
});
|
|
127
74
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const { app, router, status } = this
|
|
134
|
-
if (typeof action !== "function" || !app) return Promise.resolve(false)
|
|
135
|
-
return runFunctionForm(app, action, input, formData, () => router?.refresh() ?? Promise.resolve(), status, {
|
|
136
|
-
refresh: this.refresh(),
|
|
137
|
-
instance: this.instance(),
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
protected flag(value: boolean): "" | null {
|
|
142
|
-
return value ? "" : null
|
|
143
|
-
}
|
|
75
|
+
flag(value) {
|
|
76
|
+
return value ? "" : null;
|
|
77
|
+
}
|
|
78
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Form, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
79
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.3.2", type: Form, isStandalone: true, selector: "form[hamurForm]", inputs: { action: { classPropertyName: "action", publicName: "hamurForm", isSignal: true, isRequired: false, transformFunction: null }, method: { classPropertyName: "method", publicName: "method", isSignal: true, isRequired: false, transformFunction: null }, encType: { classPropertyName: "encType", publicName: "encType", isSignal: true, isRequired: false, transformFunction: null }, replace: { classPropertyName: "replace", publicName: "replace", isSignal: true, isRequired: false, transformFunction: null }, reset: { classPropertyName: "reset", publicName: "reset", isSignal: true, isRequired: false, transformFunction: null }, reloadDocument: { classPropertyName: "reloadDocument", publicName: "reloadDocument", isSignal: true, isRequired: false, transformFunction: null }, refresh: { classPropertyName: "refresh", publicName: "refresh", isSignal: true, isRequired: false, transformFunction: null }, restore: { classPropertyName: "restore", publicName: "restore", isSignal: true, isRequired: false, transformFunction: null }, instance: { classPropertyName: "instance", publicName: "instance", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "data-hamur-form": "" }, properties: { "attr.method": "methodAttribute()", "attr.action": "target()", "attr.enctype": "encType()", "attr.data-hamur-replace": "flag(replace())", "attr.data-hamur-reset": "flag(reset())", "attr.data-hamur-reload": "flag(reloadDocument())", "attr.data-hamur-refresh": "refresh() === false ? 'false' : null", "attr.data-hamur-restore": "restore() ? null : 'false'" } }, providers: [{ provide: FORM_STATUS, useFactory: createFormStatus }], ngImport: i0 });
|
|
144
80
|
}
|
|
145
|
-
|
|
81
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Form, decorators: [{
|
|
82
|
+
type: Directive,
|
|
83
|
+
args: [{
|
|
84
|
+
selector: "form[hamurForm]",
|
|
85
|
+
providers: [{ provide: FORM_STATUS, useFactory: createFormStatus }],
|
|
86
|
+
host: {
|
|
87
|
+
"data-hamur-form": "",
|
|
88
|
+
"[attr.method]": "methodAttribute()",
|
|
89
|
+
"[attr.action]": "target()",
|
|
90
|
+
"[attr.enctype]": "encType()",
|
|
91
|
+
"[attr.data-hamur-replace]": "flag(replace())",
|
|
92
|
+
"[attr.data-hamur-reset]": "flag(reset())",
|
|
93
|
+
"[attr.data-hamur-reload]": "flag(reloadDocument())",
|
|
94
|
+
"[attr.data-hamur-refresh]": "refresh() === false ? 'false' : null",
|
|
95
|
+
"[attr.data-hamur-restore]": "restore() ? null : 'false'",
|
|
96
|
+
},
|
|
97
|
+
}]
|
|
98
|
+
}], ctorParameters: () => [], propDecorators: { action: [{ type: i0.Input, args: [{ isSignal: true, alias: "hamurForm", required: false }] }], method: [{ type: i0.Input, args: [{ isSignal: true, alias: "method", required: false }] }], encType: [{ type: i0.Input, args: [{ isSignal: true, alias: "encType", required: false }] }], replace: [{ type: i0.Input, args: [{ isSignal: true, alias: "replace", required: false }] }], reset: [{ type: i0.Input, args: [{ isSignal: true, alias: "reset", required: false }] }], reloadDocument: [{ type: i0.Input, args: [{ isSignal: true, alias: "reloadDocument", required: false }] }], refresh: [{ type: i0.Input, args: [{ isSignal: true, alias: "refresh", required: false }] }], restore: [{ type: i0.Input, args: [{ isSignal: true, alias: "restore", required: false }] }], instance: [{ type: i0.Input, args: [{ isSignal: true, alias: "instance", required: false }] }] } });
|
|
146
99
|
/** The run of the `Form` the component is created in; idle outside one. */
|
|
147
|
-
export function injectFormStatus()
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
/** What `injectForm(fn)` and its `for(key)` return: the field helpers, and the store's values as signals. */
|
|
157
|
-
export interface InjectFormInstance<I, O> {
|
|
158
|
-
readonly fields: FieldsOf<I>
|
|
159
|
-
/** True while a run of this handler and instance is in `pendingRuns`. */
|
|
160
|
-
readonly pending: Signal<boolean>
|
|
161
|
-
/** What the last run returned, a `fail()` included. */
|
|
162
|
-
readonly result: Signal<O | undefined>
|
|
100
|
+
export function injectFormStatus() {
|
|
101
|
+
assertInInjectionContext(injectFormStatus);
|
|
102
|
+
const store = inject(FORM_STATUS, { optional: true });
|
|
103
|
+
if (!store)
|
|
104
|
+
return signal(IDLE_FORM_STATUS).asReadonly();
|
|
105
|
+
const status = signal(store.get(), /* @ts-ignore */
|
|
106
|
+
...(ngDevMode ? [{ debugName: "status" }] : /* istanbul ignore next */ []));
|
|
107
|
+
inject(DestroyRef).onDestroy(store.subscribe(() => status.set(store.get())));
|
|
108
|
+
return status.asReadonly();
|
|
163
109
|
}
|
|
164
|
-
|
|
165
|
-
export interface InjectFormResult<I, O> extends InjectFormInstance<I, O> {
|
|
166
|
-
/** The store of `injectAction(fn, { instance: key })`. */
|
|
167
|
-
for(key: string | number): InjectFormInstance<I, O>
|
|
168
|
-
}
|
|
169
|
-
|
|
170
110
|
/** The field helpers of `fn`'s input and the store of its runs; a field's reads follow the store too. */
|
|
171
|
-
export function injectForm
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export interface InjectActionResult<I, O> {
|
|
198
|
-
readonly run: ActionRunner<I, O>["run"]
|
|
199
|
-
readonly state: Signal<O | undefined>
|
|
200
|
-
readonly pending: Signal<boolean>
|
|
201
|
-
readonly error: Signal<HamurError | undefined>
|
|
202
|
-
readonly failure: Signal<ActionFailure | undefined>
|
|
203
|
-
reset(): void
|
|
111
|
+
export function injectForm(fn) {
|
|
112
|
+
assertInInjectionContext(injectForm);
|
|
113
|
+
const payload = inject(HAMUR_PAYLOAD, { optional: true });
|
|
114
|
+
const version = signal(0, /* @ts-ignore */
|
|
115
|
+
...(ngDevMode ? [{ debugName: "version" }] : /* istanbul ignore next */ []));
|
|
116
|
+
const form = createForm(fn, {
|
|
117
|
+
app: inject(HAMUR_APP, { optional: true }),
|
|
118
|
+
submitted: () => ({ handlerState: handlerStateOf(payload) }),
|
|
119
|
+
track: () => void version(),
|
|
120
|
+
});
|
|
121
|
+
inject(DestroyRef).onDestroy(form.subscribe(() => version.set(form.version())));
|
|
122
|
+
const signals = (instance) => ({
|
|
123
|
+
fields: instance.fields,
|
|
124
|
+
pending: computed(() => instance.pending),
|
|
125
|
+
result: computed(() => instance.result),
|
|
126
|
+
});
|
|
127
|
+
const instances = new Map();
|
|
128
|
+
return {
|
|
129
|
+
...signals(form),
|
|
130
|
+
for(key) {
|
|
131
|
+
let instance = instances.get(key);
|
|
132
|
+
if (!instance)
|
|
133
|
+
instances.set(key, (instance = signals(form.for(key))));
|
|
134
|
+
return instance;
|
|
135
|
+
},
|
|
136
|
+
};
|
|
204
137
|
}
|
|
205
|
-
|
|
206
138
|
/** Runs `fn` one call after another; below a `Form` of `fn` it shows that form's posts. */
|
|
207
|
-
export function injectAction
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
139
|
+
export function injectAction(fn, options = {}) {
|
|
140
|
+
assertInInjectionContext(injectAction);
|
|
141
|
+
const router = required(HAMUR_ROUTER, "injectAction");
|
|
142
|
+
const app = inject(HAMUR_APP, { optional: true });
|
|
143
|
+
const { instance } = options;
|
|
144
|
+
const runner = createActionRunner(fn, {
|
|
145
|
+
app,
|
|
146
|
+
instance,
|
|
147
|
+
options: () => options,
|
|
148
|
+
refresh: () => router.refresh(),
|
|
149
|
+
restored: restoredResult(handlerStateOf(inject(HAMUR_PAYLOAD, { optional: true })), fn.id, instance),
|
|
150
|
+
});
|
|
151
|
+
const snapshot = signal(runner.get(), /* @ts-ignore */
|
|
152
|
+
...(ngDevMode ? [{ debugName: "snapshot" }] : /* istanbul ignore next */ []));
|
|
153
|
+
const destroy = inject(DestroyRef);
|
|
154
|
+
destroy.onDestroy(runner.subscribe(() => snapshot.set(runner.get())));
|
|
155
|
+
const below = inject(FORM_STATUS, { optional: true });
|
|
156
|
+
if (below)
|
|
157
|
+
destroy.onDestroy(below.owner.link(fn.id, runner, instance));
|
|
158
|
+
else if (app)
|
|
159
|
+
destroy.onDestroy(linkActionRunner(app, fn.id, runner, instance));
|
|
160
|
+
return {
|
|
161
|
+
run: runner.run,
|
|
162
|
+
reset: runner.reset,
|
|
163
|
+
state: computed(() => snapshot().state),
|
|
164
|
+
pending: computed(() => snapshot().pending),
|
|
165
|
+
error: computed(() => snapshot().error),
|
|
166
|
+
failure: computed(() => snapshot().failure),
|
|
167
|
+
};
|
|
236
168
|
}
|
|
237
|
-
|
|
238
169
|
/** `value` while no handler call runs, else `reduce(value, the running calls)`. */
|
|
239
|
-
export function injectOptimistic
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
return runs.length ? reduce(current, runs as unknown as A[]) : current
|
|
252
|
-
})
|
|
170
|
+
export function injectOptimistic(value, reduce) {
|
|
171
|
+
assertInInjectionContext(injectOptimistic);
|
|
172
|
+
const source = optimisticSource(inject(HAMUR_APP, { optional: true }));
|
|
173
|
+
const pending = signal(source.get(), /* @ts-ignore */
|
|
174
|
+
...(ngDevMode ? [{ debugName: "pending" }] : /* istanbul ignore next */ []));
|
|
175
|
+
inject(DestroyRef).onDestroy(source.subscribe(() => pending.set(source.get())));
|
|
176
|
+
const read = typeof value === "function" ? value : () => value;
|
|
177
|
+
return computed(() => {
|
|
178
|
+
const runs = pending();
|
|
179
|
+
const current = read();
|
|
180
|
+
return runs.length ? reduce(current, runs) : current;
|
|
181
|
+
});
|
|
253
182
|
}
|