zova-core 5.1.89 → 5.1.91

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.
Files changed (40) hide show
  1. package/dist/bean/beanContainer.d.ts.map +1 -1
  2. package/dist/bean/resource/error/type.d.ts +8 -0
  3. package/dist/bean/resource/error/type.d.ts.map +1 -1
  4. package/dist/bean/type.d.ts +15 -0
  5. package/dist/bean/type.d.ts.map +1 -1
  6. package/dist/components/component.d.ts +6 -1
  7. package/dist/components/component.d.ts.map +1 -1
  8. package/dist/composables/useController.d.ts.map +1 -1
  9. package/dist/core/component/error.d.ts +7 -0
  10. package/dist/core/component/error.d.ts.map +1 -1
  11. package/dist/core/context/component.d.ts +2 -0
  12. package/dist/core/context/component.d.ts.map +1 -1
  13. package/dist/core/context/hooks.d.ts +3 -2
  14. package/dist/core/context/hooks.d.ts.map +1 -1
  15. package/dist/core/context/meta.d.ts.map +1 -1
  16. package/dist/core/context/state.d.ts +42 -0
  17. package/dist/core/context/state.d.ts.map +1 -1
  18. package/dist/core/sys/config.d.ts +19 -7
  19. package/dist/core/sys/config.d.ts.map +1 -1
  20. package/dist/index.js +484 -126
  21. package/dist/index.js.map +1 -1
  22. package/dist/types/interface/monkey.d.ts +13 -0
  23. package/dist/types/interface/monkey.d.ts.map +1 -1
  24. package/dist/types/utils/powerPartial.d.ts +1 -1
  25. package/dist/types/utils/powerPartial.d.ts.map +1 -1
  26. package/package.json +2 -2
  27. package/src/bean/beanBase.ts +1 -1
  28. package/src/bean/beanContainer.ts +99 -39
  29. package/src/bean/resource/error/type.ts +9 -0
  30. package/src/bean/type.ts +18 -0
  31. package/src/components/component.ts +2 -1
  32. package/src/composables/useController.ts +158 -25
  33. package/src/core/component/error.ts +62 -5
  34. package/src/core/context/component.ts +37 -2
  35. package/src/core/context/hooks.ts +13 -2
  36. package/src/core/context/meta.ts +1 -0
  37. package/src/core/context/state.ts +161 -0
  38. package/src/core/sys/config.ts +32 -7
  39. package/src/types/interface/monkey.ts +15 -0
  40. package/src/types/utils/powerPartial.ts +1 -1
@@ -1,7 +1,12 @@
1
1
  import type { ComponentPublicInstance } from 'vue';
2
2
 
3
3
  import type { IErrorObject } from '../../bean/resource/error/errorObject.ts';
4
- import type { IErrorInstanceInfo, IModuleError } from '../../bean/resource/error/type.ts';
4
+ import type {
5
+ IErrorHandlerLoad,
6
+ IErrorInstanceInfo,
7
+ IModuleError,
8
+ } from '../../bean/resource/error/type.ts';
9
+ import type { ZovaContext } from '../context/context.ts';
5
10
 
6
11
  import { ErrorClass } from '../../bean/resource/error/errorClass.ts';
7
12
  import { SymbolErrorInstanceInfo } from '../../bean/resource/error/type.ts';
@@ -42,6 +47,22 @@ export class AppError extends ErrorClass {
42
47
  };
43
48
  }
44
49
 
50
+ public handleLoadError(
51
+ originalError: unknown,
52
+ ctx: ZovaContext,
53
+ instance?: ComponentPublicInstance | null,
54
+ ) {
55
+ const err = normalizeError(originalError);
56
+ const load: IErrorHandlerLoad = {
57
+ kind: 'controller-load',
58
+ ctx,
59
+ originalError,
60
+ disposition: 'fallback',
61
+ };
62
+ this._handleError(err, instance, 'useController:load', load);
63
+ return { err, disposition: load.disposition };
64
+ }
65
+
45
66
  private _handleUnhandledError(error: Error, infoDefault: string) {
46
67
  if (error instanceof Error) {
47
68
  const errorInfo: IErrorInstanceInfo | undefined = error[SymbolErrorInstanceInfo];
@@ -61,6 +82,7 @@ export class AppError extends ErrorClass {
61
82
  err: Error,
62
83
  instance: ComponentPublicInstance | null | undefined,
63
84
  info: string | undefined,
85
+ load?: IErrorHandlerLoad,
64
86
  ) {
65
87
  if (!this.app) {
66
88
  // means destroyed
@@ -69,7 +91,7 @@ export class AppError extends ErrorClass {
69
91
  }
70
92
  const err2 = this.app.meta.event.emitSync(
71
93
  'app:errorHandler',
72
- { err: err as Error, instance, info },
94
+ { err: err as Error, instance, info, load },
73
95
  data => {
74
96
  return data.err;
75
97
  },
@@ -77,10 +99,45 @@ export class AppError extends ErrorClass {
77
99
  // only log error in client
78
100
  if (process.env.CLIENT) {
79
101
  if (!err2 || !(err2 instanceof Error)) return err2;
80
- if (!info || !['useMutationData'].includes(info)) {
81
- console.error(err2);
82
- }
102
+ console.error(err2);
83
103
  }
84
104
  return err2;
85
105
  }
86
106
  }
107
+
108
+ export function normalizeError(error: unknown) {
109
+ if (error instanceof Error) return error;
110
+ const normalized = new Error(getErrorMessage(error));
111
+ if (error && (typeof error === 'object' || typeof error === 'function')) {
112
+ for (const key of ['code', 'status', 'pagePath', 'url']) {
113
+ const descriptor = Object.getOwnPropertyDescriptor(error, key);
114
+ if (!descriptor || !('value' in descriptor)) continue;
115
+ const value = descriptor.value;
116
+ if (
117
+ (key === 'code' || key === 'status') &&
118
+ (typeof value === 'number' || typeof value === 'string')
119
+ ) {
120
+ (normalized as any)[key] = value;
121
+ }
122
+ if ((key === 'pagePath' || key === 'url') && typeof value === 'string') {
123
+ (normalized as any)[key] = value;
124
+ }
125
+ }
126
+ }
127
+ return normalized;
128
+ }
129
+
130
+ function getErrorMessage(error: unknown) {
131
+ if (
132
+ error === null ||
133
+ error === undefined ||
134
+ typeof error === 'string' ||
135
+ typeof error === 'number' ||
136
+ typeof error === 'boolean' ||
137
+ typeof error === 'bigint' ||
138
+ typeof error === 'symbol'
139
+ ) {
140
+ return String(error);
141
+ }
142
+ return 'Unknown error';
143
+ }
@@ -21,8 +21,12 @@ export class CtxComponent extends BeanSimple {
21
21
  const self = this;
22
22
  instance[renderMethod] = function (this, ...args) {
23
23
  if (instance.isUnmounted) return;
24
- if (!self.ctx.meta.state.inited.state) {
25
- return self._bean_render_original.call(this, ...args);
24
+ const loadStatus = self.ctx.meta.state.loadStatus;
25
+ if (loadStatus === 'loading') {
26
+ return self.ctx.meta.state.loadLoadingVisible ? self._renderLoading() : null;
27
+ }
28
+ if (loadStatus === 'error') {
29
+ return self._renderError(self.ctx.meta.state.loadError);
26
30
  }
27
31
  const render = self._getRender();
28
32
  if (!render) {
@@ -77,6 +81,30 @@ export class CtxComponent extends BeanSimple {
77
81
  this._ssrRenderReset = false;
78
82
  }
79
83
 
84
+ private _renderLoading() {
85
+ const renderMode = this.ctx.meta.state.loadRenderMode;
86
+ const controller = this.ctx.meta.state.loadController as any;
87
+ if (controller?.renderLoading) {
88
+ return controller.renderLoading(renderMode);
89
+ }
90
+ const renderLoading = this.sys.config.boundary.renderLoading;
91
+ return renderLoading?.(this.ctx, renderMode) ?? null;
92
+ }
93
+
94
+ private _renderError(error: unknown) {
95
+ const renderMode = this.ctx.meta.state.loadRenderMode;
96
+ const retry = this.ctx.meta.state.loadRetry;
97
+ const controller = this.ctx.meta.state.loadController as any;
98
+ if (controller?.renderError) {
99
+ return controller.renderError(error, renderMode, retry);
100
+ }
101
+ const renderError = this.sys.config.boundary.renderError;
102
+ if (renderError) {
103
+ return renderError(this.ctx, error, renderMode, retry);
104
+ }
105
+ return _renderErrorDefault(error);
106
+ }
107
+
80
108
  private _getRender(): any {
81
109
  const render = this.bean._getBeanSyncOnly<BeanControllerBase>(BeanControllerIdentifier);
82
110
  if (!render) return;
@@ -85,3 +113,10 @@ export class CtxComponent extends BeanSimple {
85
113
  return this.bean._getBeanSyncOnly(BeanRenderIdentifier);
86
114
  }
87
115
  }
116
+
117
+ function _renderErrorDefault(error: unknown) {
118
+ if (error instanceof Error) {
119
+ return error.message;
120
+ }
121
+ return String(error);
122
+ }
@@ -1,6 +1,6 @@
1
1
  import { BeanSimple } from '../../bean/beanSimple.ts';
2
2
 
3
- export type ZovaHookType = 'created' | 'mounted';
3
+ export type ZovaHookType = 'created' | 'mounted' | 'hydrated';
4
4
 
5
5
  export const SymbolHooksFns = Symbol('SymbolHooksFns');
6
6
  export const SymbolHooksState = Symbol('SymbolHooksState');
@@ -18,10 +18,21 @@ export class CtxHooks extends BeanSimple {
18
18
  this._onHook('created', fn);
19
19
  }
20
20
 
21
- onMounted(fn: any) {
21
+ onMounted(fn: any, owner?: object) {
22
+ if (owner && !this.ctx.meta.state.isLoadBeanActive(owner)) return;
22
23
  this._onHook('mounted', fn);
23
24
  }
24
25
 
26
+ /** @internal */
27
+ public onHydrated(fn: any) {
28
+ this._onHook('hydrated', fn);
29
+ }
30
+
31
+ clearMounted() {
32
+ if (this[SymbolHooksState].mounted) return;
33
+ this[SymbolHooksFns].mounted = undefined;
34
+ }
35
+
25
36
  private _onHook(type: ZovaHookType, fn: any) {
26
37
  if (this[SymbolHooksState][type]) {
27
38
  this.ctx.util.instanceScope(fn);
@@ -23,6 +23,7 @@ export class CtxMeta extends BeanSimple {
23
23
 
24
24
  /** @internal */
25
25
  public dispose() {
26
+ this.state.dispose();
26
27
  this.component.dispose();
27
28
  this.hooks.dispose();
28
29
  }
@@ -1,17 +1,42 @@
1
+ import type {
2
+ TypeComponentBoundaryRenderMode,
3
+ TypeComponentBoundaryRetry,
4
+ } from '../../bean/type.ts';
5
+
1
6
  import { BeanSimple } from '../../bean/beanSimple.ts';
2
7
  import { StateLock } from '../../utils/stateLock.ts';
3
8
 
9
+ export type CtxLoadStatus = 'loading' | 'ready' | 'error';
10
+
4
11
  let __id: number = 0;
5
12
 
6
13
  export class CtxState extends BeanSimple {
7
14
  private _id: number;
8
15
  private _inited: StateLock;
16
+ private _loadStatus: CtxLoadStatus = 'loading';
17
+ private _loadRenderMode: TypeComponentBoundaryRenderMode = 'block';
18
+ private _loadRetryEnabled = false;
19
+ private _loadRetryReady = false;
20
+ private _loadRetry?: TypeComponentBoundaryRetry;
21
+ private _loadAttemptId = 0;
22
+ private _loadBeanAttempts = new WeakMap<object, number>();
23
+ private _loadError: unknown;
24
+ private _loadController: unknown;
25
+ /** @internal SSR hydration protocol key. */
26
+ public controllerLoadKey?: string;
27
+ private _loadLoadingVisible = false;
28
+ private _loadLoadingTimer: ReturnType<typeof setTimeout> | undefined;
9
29
 
10
30
  protected __init__() {
11
31
  this._id = ++__id;
12
32
  this._inited = StateLock.create();
13
33
  }
14
34
 
35
+ /** @internal */
36
+ public dispose() {
37
+ this._clearLoadLoadingTimer();
38
+ }
39
+
15
40
  get id() {
16
41
  return this._id;
17
42
  }
@@ -19,4 +44,140 @@ export class CtxState extends BeanSimple {
19
44
  get inited() {
20
45
  return this._inited;
21
46
  }
47
+
48
+ get loadStatus() {
49
+ return this._loadStatus;
50
+ }
51
+
52
+ get loadRenderMode() {
53
+ return this._loadRenderMode;
54
+ }
55
+
56
+ get loadRetry() {
57
+ if (!this.isLoadRetryAvailable) return undefined;
58
+ return this._loadRetry;
59
+ }
60
+
61
+ get isLoadRetryAvailable() {
62
+ return (
63
+ this._loadRetryEnabled &&
64
+ this._loadRetryReady &&
65
+ this._loadStatus === 'error' &&
66
+ !this.ctx.disposed &&
67
+ !!this._loadRetry
68
+ );
69
+ }
70
+
71
+ get loadAttemptId() {
72
+ return this._loadAttemptId;
73
+ }
74
+
75
+ get loadError() {
76
+ return this._loadError;
77
+ }
78
+
79
+ get loadController() {
80
+ return this._loadController;
81
+ }
82
+
83
+ get loadLoadingVisible() {
84
+ return this._loadLoadingVisible;
85
+ }
86
+
87
+ setLoadController(controller: unknown) {
88
+ this._loadController = controller;
89
+ }
90
+
91
+ setLoadRenderMode(renderMode: unknown) {
92
+ this._loadRenderMode = renderMode === 'inline' ? 'inline' : 'block';
93
+ }
94
+
95
+ setLoadRetryEnabled(retryEnabled: unknown) {
96
+ this._loadRetryEnabled = retryEnabled === true;
97
+ }
98
+
99
+ setLoadRetry(retry: TypeComponentBoundaryRetry) {
100
+ this._loadRetry = retry;
101
+ }
102
+
103
+ setLoadRetryReady() {
104
+ this._loadRetryReady = true;
105
+ }
106
+
107
+ beginLoadAttempt(delay?: number) {
108
+ this._clearLoadLoadingTimer();
109
+ this._loadAttemptId++;
110
+ this._loadStatus = 'loading';
111
+ this._loadError = undefined;
112
+ this._loadController = undefined;
113
+ this._loadLoadingVisible = false;
114
+ if (delay !== undefined) {
115
+ this.startLoadLoading(delay);
116
+ }
117
+ return this._loadAttemptId;
118
+ }
119
+
120
+ isLoadAttemptActive(attemptId: number) {
121
+ return !this.ctx.disposed && this._loadAttemptId === attemptId;
122
+ }
123
+
124
+ setLoadBeanAttempt(bean: object, attemptId: number) {
125
+ this._loadBeanAttempts.set(bean, attemptId);
126
+ }
127
+
128
+ getLoadBeanAttempt(bean: object) {
129
+ return this._loadBeanAttempts.get(bean);
130
+ }
131
+
132
+ isLoadBeanActive(bean: object) {
133
+ const attemptId = this._loadBeanAttempts.get(bean);
134
+ return attemptId === undefined || this.isLoadAttemptActive(attemptId);
135
+ }
136
+
137
+ startLoadLoading(delay: number) {
138
+ this._clearLoadLoadingTimer();
139
+ if (delay === 0) {
140
+ this._loadLoadingVisible = true;
141
+ return;
142
+ }
143
+ this._loadLoadingTimer = setTimeout(() => {
144
+ this._loadLoadingTimer = undefined;
145
+ if (this.ctx.disposed || this._loadStatus !== 'loading') return;
146
+ this._loadLoadingVisible = true;
147
+ }, delay);
148
+ }
149
+
150
+ /**
151
+ * Resolves the Controller-load terminal lock consumed by the custom SSR hydration runtime.
152
+ * A settled load is not necessarily a successful load: error and handled control-flow paths
153
+ * must also allow the rendered boundary subtree to hydrate.
154
+ */
155
+ settleLoad() {
156
+ this._inited.touch();
157
+ }
158
+
159
+ setLoadReady() {
160
+ this._clearLoadLoadingTimer();
161
+ this._loadError = undefined;
162
+ this._loadStatus = 'ready';
163
+ this.settleLoad();
164
+ }
165
+
166
+ setLoadError(error: unknown) {
167
+ this._clearLoadLoadingTimer();
168
+ this._loadError = error;
169
+ this._loadStatus = 'error';
170
+ this.settleLoad();
171
+ }
172
+
173
+ setLoadHandled() {
174
+ this._clearLoadLoadingTimer();
175
+ this.settleLoad();
176
+ }
177
+
178
+ private _clearLoadLoadingTimer() {
179
+ if (this._loadLoadingTimer === undefined) return;
180
+ clearTimeout(this._loadLoadingTimer);
181
+ this._loadLoadingTimer = undefined;
182
+ }
22
183
  }
@@ -1,5 +1,6 @@
1
1
  import type { ZovaConfigMeta } from '@cabloy/module-info';
2
2
  import type ms from 'ms';
3
+ import type { VNodeChild } from 'vue';
3
4
 
4
5
  import type {
5
6
  ILayoutRecord,
@@ -8,9 +9,14 @@ import type {
8
9
  } from '../../bean/resource/component/type.ts';
9
10
  import type { ILocaleRecord } from '../../bean/resource/locale/type.ts';
10
11
  import type { ZovaConfigRoutes } from '../../bean/resource/page/type.ts';
11
- import type { IBeanScopeConfig } from '../../bean/type.ts';
12
+ import type {
13
+ IBeanScopeConfig,
14
+ TypeComponentBoundaryRenderMode,
15
+ TypeComponentBoundaryRetry,
16
+ } from '../../bean/type.ts';
12
17
  import type { ZovaConfigEnv } from '../../types/utils/env.ts';
13
18
  import type { PowerPartial } from '../../types/utils/powerPartial.ts';
19
+ import type { ZovaContext } from '../context/context.ts';
14
20
  import type { ConfigLogger } from '../logger/types.ts';
15
21
 
16
22
  export function configDefault(env: ZovaConfigEnv): PowerPartial<ZovaConfig> {
@@ -20,10 +26,33 @@ export function configDefault(env: ZovaConfigEnv): PowerPartial<ZovaConfig> {
20
26
  mode: env.META_MODE,
21
27
  appMode: env.META_APP_MODE,
22
28
  },
29
+ boundary: {},
23
30
  };
24
31
  return config;
25
32
  }
26
33
 
34
+ export interface ZovaConfigApp {
35
+ name: string;
36
+ title: string;
37
+ description: string;
38
+ version: string;
39
+ }
40
+
41
+ export interface ZovaConfigBoundaryLoading {
42
+ delay?: number;
43
+ }
44
+
45
+ export interface ZovaConfigBoundary {
46
+ loading?: ZovaConfigBoundaryLoading;
47
+ renderLoading?: (ctx: ZovaContext, renderMode: TypeComponentBoundaryRenderMode) => VNodeChild;
48
+ renderError?: (
49
+ ctx: ZovaContext,
50
+ error: unknown,
51
+ renderMode: TypeComponentBoundaryRenderMode,
52
+ retry?: TypeComponentBoundaryRetry,
53
+ ) => VNodeChild;
54
+ }
55
+
27
56
  export interface ZovaConfigSsrResponseCachePolicy {
28
57
  expires?: number | ms.StringValue;
29
58
  }
@@ -35,12 +64,8 @@ export interface ZovaConfigSsrProfile {
35
64
 
36
65
  export interface ZovaConfig {
37
66
  meta: ZovaConfigMeta;
38
- app: {
39
- name: string;
40
- title: string;
41
- description: string;
42
- version: string;
43
- };
67
+ app: ZovaConfigApp;
68
+ boundary: ZovaConfigBoundary;
44
69
  api: {
45
70
  baseURL: string;
46
71
  prefix: string;
@@ -5,6 +5,7 @@ import type { BeanContainer } from '../../bean/beanContainer.ts';
5
5
  import type { IControllerData } from '../../bean/type.ts';
6
6
  import type { ZovaApplication } from '../../core/app/application.ts';
7
7
  import type { ZovaContext } from '../../core/context/context.ts';
8
+ import type { Constructable } from '../../decorator/type/constructable.ts';
8
9
 
9
10
  export type TypeMonkeyName =
10
11
  | keyof IMonkeyModuleSys
@@ -59,10 +60,24 @@ export interface IMonkeyApp
59
60
  IMonkeyBeanDispose,
60
61
  IMonkeyBeanDisposed {}
61
62
 
63
+ export type ControllerLoadPhase = 'prepare' | 'fallback' | 'replay';
64
+
65
+ export interface IControllerLoadEvent {
66
+ phase: ControllerLoadPhase;
67
+ ctx: ZovaContext;
68
+ controllerBeanFullName: Constructable | string;
69
+ controllerBeanName?: string;
70
+ error?: Error;
71
+ controllerRecorded?: boolean;
72
+ replayError?: Error;
73
+ replayControllerRecorded?: boolean;
74
+ }
75
+
62
76
  export interface IMonkeyController {
63
77
  controllerDataPrepare(controllerData: IControllerData, ctx: ZovaContext): void;
64
78
  controllerDataInit(controllerData: IControllerData, controller: BeanBase): void;
65
79
  controllerDataUpdate(controller: BeanBase): void;
80
+ controllerLoad?(event: IControllerLoadEvent): void;
66
81
  }
67
82
 
68
83
  export interface IMonkeySysApplicationInitialize {
@@ -1,3 +1,3 @@
1
1
  export type PowerPartial<T> = {
2
- [U in keyof T]?: T[U] extends object ? PowerPartial<T[U]> : T[U];
2
+ [U in keyof T]?: T[U] extends Function ? T[U] : T[U] extends object ? PowerPartial<T[U]> : T[U];
3
3
  };