@evobug_com/safe-ts-vue 0.6.0 → 0.7.0
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/README.md +1 -1
- package/dist/async-component.d.ts +36 -0
- package/dist/async-component.js +42 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/listen.js +3 -1
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @evobug_com/safe-ts-vue
|
|
2
2
|
|
|
3
|
-
SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href and typed safe HTML for v-html.
|
|
3
|
+
SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, components loaded on demand that never reject, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href and typed safe HTML for v-html.
|
|
4
4
|
|
|
5
5
|
**Documentation: [safe-ts.evobug-pages.com/vue](https://safe-ts.evobug-pages.com/vue/)**
|
|
6
6
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ChunkGone } from "@evobug_com/safe-ts-std/async";
|
|
2
|
+
import { type Component } from "vue";
|
|
3
|
+
/** What `safeAsyncComponent` shows besides the component. */
|
|
4
|
+
export type SafeAsyncComponentOptions = {
|
|
5
|
+
/** Shown in place of the component when its module can't be loaded. */
|
|
6
|
+
readonly gone: Component;
|
|
7
|
+
/** Shown while the module loads (after `delay`). */
|
|
8
|
+
readonly loading?: Component;
|
|
9
|
+
/** How long to wait before showing `loading`, in milliseconds (Vue's default: 200). */
|
|
10
|
+
readonly delay?: number;
|
|
11
|
+
/** Called once when the module can't be loaded, with why: to log it, not to show it. */
|
|
12
|
+
readonly onGone?: (gone: ChunkGone) => void;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* `defineAsyncComponent` whose loader never rejects: when the module can't be loaded (a deploy
|
|
16
|
+
* replaced the file, the server is restarting, the network dropped), the component renders
|
|
17
|
+
* `gone`, and nothing reaches `app.config.errorHandler`.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* const ReportChart = safeAsyncComponent(() => import("./ReportChart.vue.js"), { gone: Unavailable });
|
|
21
|
+
* const icons = import.meta.glob<{ default: Component }>("./icons/*.vue");
|
|
22
|
+
* const icon = (name: string) => safeAsyncComponent(icons[`./icons/${name}.vue`] ?? noIcon, { gone: Blank });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Vue's own `errorComponent` doesn't do this: Vue shows it and still reports the rejection to
|
|
26
|
+
* `app.config.errorHandler`, which an app's error boundary turns into a crash page. `load` is
|
|
27
|
+
* the `import()` written out, or a loader of `import.meta.glob(...)`; it may resolve to the
|
|
28
|
+
* module (its `default` is the component) or to the component itself.
|
|
29
|
+
*
|
|
30
|
+
* Vue keeps what the loader resolved to: once `gone` is shown, this component stays `gone`
|
|
31
|
+
* until the page is loaded again. A timeout isn't offered, because Vue reports a timeout to
|
|
32
|
+
* `app.config.errorHandler` too.
|
|
33
|
+
*/
|
|
34
|
+
export declare function safeAsyncComponent<C extends Component>(load: () => Promise<C | {
|
|
35
|
+
default: C;
|
|
36
|
+
}>, options: SafeAsyncComponentOptions): C;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// @safe-ts std: the component shown when the module is gone stands in for the one loaded, with one cast.
|
|
2
|
+
// A component loaded on demand that never rejects to Vue: when its module can't load, it shows
|
|
3
|
+
// `gone` instead.
|
|
4
|
+
import { importModule } from "@evobug_com/safe-ts-std/async";
|
|
5
|
+
import { isErr } from "@evobug_com/safe-ts-std/result";
|
|
6
|
+
import { defineAsyncComponent } from "vue";
|
|
7
|
+
/**
|
|
8
|
+
* `defineAsyncComponent` whose loader never rejects: when the module can't be loaded (a deploy
|
|
9
|
+
* replaced the file, the server is restarting, the network dropped), the component renders
|
|
10
|
+
* `gone`, and nothing reaches `app.config.errorHandler`.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const ReportChart = safeAsyncComponent(() => import("./ReportChart.vue.js"), { gone: Unavailable });
|
|
14
|
+
* const icons = import.meta.glob<{ default: Component }>("./icons/*.vue");
|
|
15
|
+
* const icon = (name: string) => safeAsyncComponent(icons[`./icons/${name}.vue`] ?? noIcon, { gone: Blank });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Vue's own `errorComponent` doesn't do this: Vue shows it and still reports the rejection to
|
|
19
|
+
* `app.config.errorHandler`, which an app's error boundary turns into a crash page. `load` is
|
|
20
|
+
* the `import()` written out, or a loader of `import.meta.glob(...)`; it may resolve to the
|
|
21
|
+
* module (its `default` is the component) or to the component itself.
|
|
22
|
+
*
|
|
23
|
+
* Vue keeps what the loader resolved to: once `gone` is shown, this component stays `gone`
|
|
24
|
+
* until the page is loaded again. A timeout isn't offered, because Vue reports a timeout to
|
|
25
|
+
* `app.config.errorHandler` too.
|
|
26
|
+
*/
|
|
27
|
+
export function safeAsyncComponent(load, options) {
|
|
28
|
+
const { gone, loading, delay, onGone } = options;
|
|
29
|
+
return defineAsyncComponent({
|
|
30
|
+
loader: async () => {
|
|
31
|
+
const loaded = await importModule(load);
|
|
32
|
+
// A module's `default` is the component; Vue unwraps it only from a real module namespace.
|
|
33
|
+
if (!isErr(loaded))
|
|
34
|
+
return typeof loaded === "object" && "default" in loaded ? loaded.default : loaded;
|
|
35
|
+
onGone?.(loaded.error);
|
|
36
|
+
// `gone` takes the place of the component: Vue renders whichever the loader gives.
|
|
37
|
+
return gone;
|
|
38
|
+
},
|
|
39
|
+
...(loading === undefined ? {} : { loadingComponent: loading }),
|
|
40
|
+
...(delay === undefined ? {} : { delay }),
|
|
41
|
+
});
|
|
42
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export { Match, type MatchSlots } from "./match.js";
|
|
|
5
5
|
export { type DefaultedProp, oneOf, type OptionalProp, prop, type PropConstructorOf, type PropDefault, type PropTypeOf, type PropValidator, type RequiredProp, } from "./prop.js";
|
|
6
6
|
export { AsyncState } from "./state.js";
|
|
7
7
|
export { type Task, useTask } from "./task.js";
|
|
8
|
+
export { safeAsyncComponent, type SafeAsyncComponentOptions } from "./async-component.js";
|
|
8
9
|
export { defineSanitizer, escapeHtml, type Html } from "./html.js";
|
|
9
10
|
export { safeHref, type SafeUrl, safeUrl, type UnsafeUrl } from "./url.js";
|
package/dist/index.js
CHANGED
|
@@ -7,5 +7,6 @@ export { Match } from "./match.js";
|
|
|
7
7
|
export { oneOf, prop, } from "./prop.js";
|
|
8
8
|
export { AsyncState } from "./state.js";
|
|
9
9
|
export { useTask } from "./task.js";
|
|
10
|
+
export { safeAsyncComponent } from "./async-component.js";
|
|
10
11
|
export { defineSanitizer, escapeHtml } from "./html.js";
|
|
11
12
|
export { safeHref, safeUrl } from "./url.js";
|
package/dist/listen.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// ends, and in `<script setup>` the block ends as soon as setup returns, before the component
|
|
5
5
|
// is even shown. These remove theirs when the component unmounts instead.
|
|
6
6
|
import { listen } from "@evobug_com/safe-ts-std/events";
|
|
7
|
+
import { ms as milliseconds } from "@evobug_com/safe-ts-std/time";
|
|
7
8
|
import { getCurrentScope, onScopeDispose } from "vue";
|
|
8
9
|
/**
|
|
9
10
|
* `target.addEventListener(type, handler, options)` until the component unmounts:
|
|
@@ -43,7 +44,8 @@ export function useListener(target, type, handler, options) {
|
|
|
43
44
|
* `fn` runs from a timer, outside any `try` of yours: handle its failures inside it.
|
|
44
45
|
*/
|
|
45
46
|
export function useInterval(fn, ms) {
|
|
46
|
-
|
|
47
|
+
// `useInterval` is a timer itself: `brand-sink` holds its callers to milliseconds.
|
|
48
|
+
const id = setInterval(fn, milliseconds(ms));
|
|
47
49
|
const stop = () => clearInterval(id);
|
|
48
50
|
stopWithScope(stop);
|
|
49
51
|
return stop;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evobug_com/safe-ts-vue",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href and typed safe HTML for v-html.",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, components loaded on demand that never reject, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href and typed safe HTML for v-html.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"safe-ts",
|
|
7
7
|
"typescript",
|
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
"types": "./dist/task.d.ts",
|
|
41
41
|
"default": "./dist/task.js"
|
|
42
42
|
},
|
|
43
|
+
"./async-component": {
|
|
44
|
+
"types": "./dist/async-component.d.ts",
|
|
45
|
+
"default": "./dist/async-component.js"
|
|
46
|
+
},
|
|
43
47
|
"./element": {
|
|
44
48
|
"types": "./dist/element.d.ts",
|
|
45
49
|
"default": "./dist/element.js"
|
|
@@ -79,7 +83,7 @@
|
|
|
79
83
|
"LICENSE-APACHE"
|
|
80
84
|
],
|
|
81
85
|
"peerDependencies": {
|
|
82
|
-
"@evobug_com/safe-ts-std": "^0.
|
|
86
|
+
"@evobug_com/safe-ts-std": "^0.7.0",
|
|
83
87
|
"vue": "^3.5.0"
|
|
84
88
|
},
|
|
85
89
|
"publishConfig": {
|
|
@@ -87,6 +91,7 @@
|
|
|
87
91
|
},
|
|
88
92
|
"safe-ts": {
|
|
89
93
|
"trustedFiles": [
|
|
94
|
+
"src/async-component.ts",
|
|
90
95
|
"src/element.ts",
|
|
91
96
|
"src/jsx-runtime.ts",
|
|
92
97
|
"src/match.ts",
|