@dooboostore/simple-web-component 1.0.50 → 1.0.51
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 +735 -354
- package/dist/cjs/SwcAppEngine.js.map +2 -2
- package/dist/cjs/decorators/addEventListener.js +64 -146
- package/dist/cjs/decorators/addEventListener.js.map +2 -2
- package/dist/cjs/decorators/applyAttribute.js +36 -48
- package/dist/cjs/decorators/applyAttribute.js.map +2 -2
- package/dist/cjs/decorators/applyClass.js +9 -12
- package/dist/cjs/decorators/applyClass.js.map +2 -2
- package/dist/cjs/decorators/applyNode.js +14 -22
- package/dist/cjs/decorators/applyNode.js.map +2 -2
- package/dist/cjs/decorators/applyProperty.js +12 -42
- package/dist/cjs/decorators/applyProperty.js.map +2 -2
- package/dist/cjs/decorators/applySlot.js +3 -3
- package/dist/cjs/decorators/applySlot.js.map +2 -2
- package/dist/cjs/decorators/applyStyle.js +9 -12
- package/dist/cjs/decorators/applyStyle.js.map +2 -2
- package/dist/cjs/decorators/around.js +124 -0
- package/dist/cjs/decorators/around.js.map +7 -0
- package/dist/cjs/decorators/changedAttribute.js +40 -12
- package/dist/cjs/decorators/changedAttribute.js.map +2 -2
- package/dist/cjs/decorators/elementDefine.js +224 -195
- package/dist/cjs/decorators/elementDefine.js.map +3 -3
- package/dist/cjs/decorators/emitCustomEvent.js +14 -12
- package/dist/cjs/decorators/emitCustomEvent.js.map +2 -2
- package/dist/cjs/decorators/eventMedia.js +36 -8
- package/dist/cjs/decorators/eventMedia.js.map +2 -2
- package/dist/cjs/decorators/fetch.js +278 -0
- package/dist/cjs/decorators/fetch.js.map +7 -0
- package/dist/cjs/decorators/index.js +5 -2
- package/dist/cjs/decorators/index.js.map +2 -2
- package/dist/cjs/decorators/intersectionObserver.js +33 -13
- package/dist/cjs/decorators/intersectionObserver.js.map +2 -2
- package/dist/cjs/decorators/lifecycles.js +11 -11
- package/dist/cjs/decorators/lifecycles.js.map +2 -2
- package/dist/cjs/decorators/mutationObserver.js +55 -39
- package/dist/cjs/decorators/mutationObserver.js.map +3 -3
- package/dist/cjs/decorators/parameter.js +92 -9
- package/dist/cjs/decorators/parameter.js.map +2 -2
- package/dist/cjs/decorators/publishSwcAppMessage.js +2 -2
- package/dist/cjs/decorators/publishSwcAppMessage.js.map +2 -2
- package/dist/cjs/decorators/query.js +11 -5
- package/dist/cjs/decorators/query.js.map +2 -2
- package/dist/cjs/decorators/requestAnimationFrame.js +7 -16
- package/dist/cjs/decorators/requestAnimationFrame.js.map +2 -2
- package/dist/cjs/decorators/resizeObserver.js +41 -17
- package/dist/cjs/decorators/resizeObserver.js.map +3 -3
- package/dist/cjs/decorators/setInterval.js +36 -25
- package/dist/cjs/decorators/setInterval.js.map +2 -2
- package/dist/cjs/decorators/setTimeout.js +36 -25
- package/dist/cjs/decorators/setTimeout.js.map +2 -2
- package/dist/cjs/decorators/state.js +13 -10
- package/dist/cjs/decorators/state.js.map +2 -2
- package/dist/cjs/decorators/{subscribeSwcAppMessageWhileConnected.js → subscribeSwcAppMessage.js} +20 -21
- package/dist/cjs/decorators/subscribeSwcAppMessage.js.map +7 -0
- package/dist/cjs/decorators/{subscribeSwcAppRouteChangeWhileConnected.js → subscribeSwcAppRouteChange.js} +23 -19
- package/dist/cjs/decorators/subscribeSwcAppRouteChange.js.map +7 -0
- package/dist/cjs/decorators/swcAppRoute.js +149 -0
- package/dist/cjs/decorators/swcAppRoute.js.map +7 -0
- package/dist/cjs/elements/SwcApp.js +70 -0
- package/dist/cjs/elements/SwcApp.js.map +2 -2
- package/dist/cjs/elements/SwcAppMixin.js +256 -83
- package/dist/cjs/elements/SwcAppMixin.js.map +3 -3
- package/dist/cjs/types/index.js +5 -1
- package/dist/cjs/types/index.js.map +2 -2
- package/dist/cjs/utils/Utils.js +36 -15
- package/dist/cjs/utils/Utils.js.map +2 -2
- package/dist/esm/SwcAppEngine.js.map +2 -2
- package/dist/esm/decorators/addEventListener.js +64 -146
- package/dist/esm/decorators/addEventListener.js.map +2 -2
- package/dist/esm/decorators/applyAttribute.js +36 -48
- package/dist/esm/decorators/applyAttribute.js.map +2 -2
- package/dist/esm/decorators/applyClass.js +9 -12
- package/dist/esm/decorators/applyClass.js.map +2 -2
- package/dist/esm/decorators/applyNode.js +15 -23
- package/dist/esm/decorators/applyNode.js.map +2 -2
- package/dist/esm/decorators/applyProperty.js +12 -42
- package/dist/esm/decorators/applyProperty.js.map +2 -2
- package/dist/esm/decorators/applySlot.js +3 -3
- package/dist/esm/decorators/applySlot.js.map +2 -2
- package/dist/esm/decorators/applyStyle.js +9 -12
- package/dist/esm/decorators/applyStyle.js.map +2 -2
- package/dist/esm/decorators/around.js +105 -0
- package/dist/esm/decorators/around.js.map +7 -0
- package/dist/esm/decorators/changedAttribute.js +40 -12
- package/dist/esm/decorators/changedAttribute.js.map +2 -2
- package/dist/esm/decorators/elementDefine.js +222 -193
- package/dist/esm/decorators/elementDefine.js.map +3 -3
- package/dist/esm/decorators/emitCustomEvent.js +15 -13
- package/dist/esm/decorators/emitCustomEvent.js.map +2 -2
- package/dist/esm/decorators/eventMedia.js +36 -8
- package/dist/esm/decorators/eventMedia.js.map +2 -2
- package/dist/esm/decorators/fetch.js +259 -0
- package/dist/esm/decorators/fetch.js.map +7 -0
- package/dist/esm/decorators/index.js +5 -2
- package/dist/esm/decorators/index.js.map +2 -2
- package/dist/esm/decorators/intersectionObserver.js +33 -13
- package/dist/esm/decorators/intersectionObserver.js.map +2 -2
- package/dist/esm/decorators/lifecycles.js +11 -11
- package/dist/esm/decorators/lifecycles.js.map +2 -2
- package/dist/esm/decorators/mutationObserver.js +55 -39
- package/dist/esm/decorators/mutationObserver.js.map +3 -3
- package/dist/esm/decorators/parameter.js +92 -9
- package/dist/esm/decorators/parameter.js.map +2 -2
- package/dist/esm/decorators/publishSwcAppMessage.js +2 -2
- package/dist/esm/decorators/publishSwcAppMessage.js.map +2 -2
- package/dist/esm/decorators/query.js +11 -5
- package/dist/esm/decorators/query.js.map +2 -2
- package/dist/esm/decorators/requestAnimationFrame.js +8 -17
- package/dist/esm/decorators/requestAnimationFrame.js.map +2 -2
- package/dist/esm/decorators/resizeObserver.js +41 -17
- package/dist/esm/decorators/resizeObserver.js.map +2 -2
- package/dist/esm/decorators/setInterval.js +37 -26
- package/dist/esm/decorators/setInterval.js.map +2 -2
- package/dist/esm/decorators/setTimeout.js +37 -26
- package/dist/esm/decorators/setTimeout.js.map +2 -2
- package/dist/esm/decorators/state.js +13 -10
- package/dist/esm/decorators/state.js.map +2 -2
- package/dist/esm/decorators/subscribeSwcAppMessage.js +53 -0
- package/dist/esm/decorators/subscribeSwcAppMessage.js.map +7 -0
- package/dist/esm/decorators/{subscribeSwcAppRouteChangeWhileConnected.js → subscribeSwcAppRouteChange.js} +20 -16
- package/dist/esm/decorators/subscribeSwcAppRouteChange.js.map +7 -0
- package/dist/esm/decorators/swcAppRoute.js +130 -0
- package/dist/esm/decorators/swcAppRoute.js.map +7 -0
- package/dist/esm/elements/SwcApp.js +70 -0
- package/dist/esm/elements/SwcApp.js.map +2 -2
- package/dist/esm/elements/SwcAppMixin.js +258 -85
- package/dist/esm/elements/SwcAppMixin.js.map +3 -3
- package/dist/esm/types/index.js +5 -1
- package/dist/esm/types/index.js.map +2 -2
- package/dist/esm/utils/Utils.js +36 -15
- package/dist/esm/utils/Utils.js.map +2 -2
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +1886 -999
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js.map +4 -4
- package/dist/types/SwcAppEngine.d.ts +1 -0
- package/dist/types/SwcAppEngine.d.ts.map +1 -1
- package/dist/types/decorators/addEventListener.d.ts +923 -960
- package/dist/types/decorators/addEventListener.d.ts.map +1 -1
- package/dist/types/decorators/applyAttribute.d.ts +26 -38
- package/dist/types/decorators/applyAttribute.d.ts.map +1 -1
- package/dist/types/decorators/applyClass.d.ts +1 -3
- package/dist/types/decorators/applyClass.d.ts.map +1 -1
- package/dist/types/decorators/applyNode.d.ts +15 -17
- package/dist/types/decorators/applyNode.d.ts.map +1 -1
- package/dist/types/decorators/applyProperty.d.ts +25 -45
- package/dist/types/decorators/applyProperty.d.ts.map +1 -1
- package/dist/types/decorators/applyStyle.d.ts +1 -3
- package/dist/types/decorators/applyStyle.d.ts.map +1 -1
- package/dist/types/decorators/around.d.ts +87 -0
- package/dist/types/decorators/around.d.ts.map +1 -0
- package/dist/types/decorators/changedAttribute.d.ts +21 -1
- package/dist/types/decorators/changedAttribute.d.ts.map +1 -1
- package/dist/types/decorators/elementDefine.d.ts.map +1 -1
- package/dist/types/decorators/emitCustomEvent.d.ts +0 -1
- package/dist/types/decorators/emitCustomEvent.d.ts.map +1 -1
- package/dist/types/decorators/eventMedia.d.ts +24 -1
- package/dist/types/decorators/eventMedia.d.ts.map +1 -1
- package/dist/types/decorators/fetch.d.ts +139 -0
- package/dist/types/decorators/fetch.d.ts.map +1 -0
- package/dist/types/decorators/index.d.ts +5 -2
- package/dist/types/decorators/index.d.ts.map +1 -1
- package/dist/types/decorators/intersectionObserver.d.ts +24 -11
- package/dist/types/decorators/intersectionObserver.d.ts.map +1 -1
- package/dist/types/decorators/lifecycles.d.ts +8 -7
- package/dist/types/decorators/lifecycles.d.ts.map +1 -1
- package/dist/types/decorators/mutationObserver.d.ts +26 -13
- package/dist/types/decorators/mutationObserver.d.ts.map +1 -1
- package/dist/types/decorators/parameter.d.ts +84 -6
- package/dist/types/decorators/parameter.d.ts.map +1 -1
- package/dist/types/decorators/publishSwcAppMessage.d.ts +0 -1
- package/dist/types/decorators/publishSwcAppMessage.d.ts.map +1 -1
- package/dist/types/decorators/query.d.ts +7 -7
- package/dist/types/decorators/query.d.ts.map +1 -1
- package/dist/types/decorators/requestAnimationFrame.d.ts +1 -1
- package/dist/types/decorators/requestAnimationFrame.d.ts.map +1 -1
- package/dist/types/decorators/resizeObserver.d.ts +26 -13
- package/dist/types/decorators/resizeObserver.d.ts.map +1 -1
- package/dist/types/decorators/setInterval.d.ts +10 -0
- package/dist/types/decorators/setInterval.d.ts.map +1 -1
- package/dist/types/decorators/setTimeout.d.ts +10 -0
- package/dist/types/decorators/setTimeout.d.ts.map +1 -1
- package/dist/types/decorators/state.d.ts.map +1 -1
- package/dist/types/decorators/subscribeSwcAppMessage.d.ts +44 -0
- package/dist/types/decorators/subscribeSwcAppMessage.d.ts.map +1 -0
- package/dist/types/decorators/subscribeSwcAppRouteChange.d.ts +60 -0
- package/dist/types/decorators/subscribeSwcAppRouteChange.d.ts.map +1 -0
- package/dist/types/decorators/swcAppRoute.d.ts +94 -0
- package/dist/types/decorators/swcAppRoute.d.ts.map +1 -0
- package/dist/types/elements/SwcApp.d.ts.map +1 -1
- package/dist/types/elements/SwcAppMixin.d.ts +423 -388
- package/dist/types/elements/SwcAppMixin.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +43 -4
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/utils/Utils.d.ts +15 -5
- package/dist/types/utils/Utils.d.ts.map +1 -1
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js +1886 -999
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js.map +4 -4
- package/package.json +8 -6
- package/src/SwcAppEngine.ts +1 -0
- package/src/decorators/addEventListener.ts +102 -96
- package/src/decorators/applyAttribute.ts +76 -138
- package/src/decorators/applyClass.ts +8 -14
- package/src/decorators/applyNode.ts +33 -41
- package/src/decorators/applyProperty.ts +51 -99
- package/src/decorators/applySlot.ts +3 -3
- package/src/decorators/applyStyle.ts +8 -14
- package/src/decorators/around.ts +196 -0
- package/src/decorators/changedAttribute.ts +39 -15
- package/src/decorators/elementDefine.ts +270 -219
- package/src/decorators/emitCustomEvent.ts +17 -13
- package/src/decorators/eventMedia.ts +50 -9
- package/src/decorators/fetch.ts +454 -0
- package/src/decorators/index.ts +5 -2
- package/src/decorators/intersectionObserver.ts +38 -24
- package/src/decorators/lifecycles.ts +24 -22
- package/src/decorators/mutationObserver.ts +71 -52
- package/src/decorators/parameter.ts +145 -14
- package/src/decorators/publishSwcAppMessage.ts +3 -4
- package/src/decorators/query.ts +22 -14
- package/src/decorators/requestAnimationFrame.ts +10 -19
- package/src/decorators/resizeObserver.ts +50 -29
- package/src/decorators/setInterval.ts +37 -26
- package/src/decorators/setTimeout.ts +37 -26
- package/src/decorators/state.ts +19 -13
- package/src/decorators/subscribeSwcAppMessage.ts +106 -0
- package/src/decorators/subscribeSwcAppRouteChange.ts +131 -0
- package/src/decorators/swcAppRoute.ts +231 -0
- package/src/elements/SwcApp.ts +40 -10
- package/src/elements/SwcAppMixin.ts +330 -101
- package/src/types/index.ts +38 -5
- package/src/utils/Utils.ts +50 -18
- package/dist/cjs/decorators/subscribeSwcAppMessageWhileConnected.js.map +0 -7
- package/dist/cjs/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +0 -7
- package/dist/esm/decorators/subscribeSwcAppMessageWhileConnected.js +0 -54
- package/dist/esm/decorators/subscribeSwcAppMessageWhileConnected.js.map +0 -7
- package/dist/esm/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +0 -7
- package/dist/types/decorators/subscribeSwcAppMessageWhileConnected.d.ts +0 -23
- package/dist/types/decorators/subscribeSwcAppMessageWhileConnected.d.ts.map +0 -1
- package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts +0 -31
- package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts.map +0 -1
- package/src/decorators/subscribeSwcAppMessageWhileConnected.ts +0 -91
- package/src/decorators/subscribeSwcAppRouteChangeWhileConnected.ts +0 -107
|
@@ -1,10 +1,54 @@
|
|
|
1
|
-
import { SwcAppInterface, SwcAppMessage } from '../types';
|
|
2
|
-
import { APPLY_NODE_METADATA_KEY, findAllLifecycleMetadata,
|
|
1
|
+
import { SwcAppInterface, SwcAppMessage, SwcAppMessageObserveOptions, SwcElement } from '../types';
|
|
2
|
+
import { APPLY_NODE_METADATA_KEY, findAllLifecycleMetadata, getSubscribeSwcAppMessageMetadata, getSubscribeSwcAppRouteChangeMetadata, ON_CONNECTED_SWC_APP_METADATA_KEY, SUBSCRIBE_SWC_APP_ROUTE_CHANGE_METADATA_KEY, buildSwcParameterArgs } from '../decorators';
|
|
3
3
|
import { SwcAppEngine, SwcAttributeConfigType, SwcConfigType } from '../SwcAppEngine';
|
|
4
|
-
import { debounceTimeIntervalLock, FunctionUtils, Subscription } from '@dooboostore/core';
|
|
4
|
+
import { debounceTimeIntervalLock, FunctionUtils, Subscription, Subject, Observable } from '@dooboostore/core';
|
|
5
5
|
import {RouterEventType, ValidUtils} from '@dooboostore/core-web';
|
|
6
6
|
import { SwcUtils } from '../utils/Utils';
|
|
7
7
|
|
|
8
|
+
type RouteSubscriberState = { matched: boolean; signature: string; pathData?: { [k: string]: string }; pathDataAll?: { [k: string]: string[] } };
|
|
9
|
+
const routeStateOf = (instance: any): Map<string | symbol, RouteSubscriberState> => (instance.__swc_routeState ??= new Map());
|
|
10
|
+
|
|
11
|
+
/** 라우트 파라미터 데코레이터 값 (kind 이름 = 데코레이터 이름). 키를 받는 것은 (key) => 값 */
|
|
12
|
+
const routeParamValues = (sp: URLSearchParams, pathDataAll: { [k: string]: string[] }) => {
|
|
13
|
+
const queryAll: { [k: string]: string[] } = Object.fromEntries([...new Set(sp.keys())].map(k => [k, sp.getAll(k)]));
|
|
14
|
+
const pick = (all: { [k: string]: string[] }, at: 'first' | 'last') =>
|
|
15
|
+
Object.fromEntries(Object.entries(all).map(([k, v]) => [k, at === 'first' ? v[0] : v[v.length - 1]]));
|
|
16
|
+
const lastOf = (v: string[] | undefined) => (v && v.length ? v[v.length - 1] : undefined);
|
|
17
|
+
const firstQuery = (k: string) => sp.get(k);
|
|
18
|
+
const firstPath = (k: string) => pathDataAll[k]?.[0];
|
|
19
|
+
return {
|
|
20
|
+
swcAppRouteQueryParam: firstQuery,
|
|
21
|
+
swcAppRouteFirstQueryParam: firstQuery,
|
|
22
|
+
swcAppRouteLastQueryParam: (k: string) => lastOf(sp.getAll(k)) ?? null,
|
|
23
|
+
swcAppRouteQueryParams: (k: string) => sp.getAll(k),
|
|
24
|
+
swcAppRouteQueryParamObject: pick(queryAll, 'first'),
|
|
25
|
+
swcAppRouteFirstQueryParamObject: pick(queryAll, 'first'),
|
|
26
|
+
swcAppRouteLastQueryParamObject: pick(queryAll, 'last'),
|
|
27
|
+
swcAppRouteQueryParamsObject: queryAll,
|
|
28
|
+
swcAppRouteURLSearchParams: sp,
|
|
29
|
+
swcAppRoutePathVariable: firstPath,
|
|
30
|
+
swcAppRouteFirstPathVariable: firstPath,
|
|
31
|
+
swcAppRouteLastPathVariable: (k: string) => lastOf(pathDataAll[k]),
|
|
32
|
+
swcAppRoutePathVariables: (k: string) => pathDataAll[k] ?? [],
|
|
33
|
+
swcAppRoutePathVariableObject: pick(pathDataAll, 'first'),
|
|
34
|
+
swcAppRouteFirstPathVariableObject: pick(pathDataAll, 'first'),
|
|
35
|
+
swcAppRouteLastPathVariableObject: pick(pathDataAll, 'last'),
|
|
36
|
+
swcAppRoutePathVariablesObject: pathDataAll
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** 구독자의 path(문자열/배열/함수/없음)를 route 에 매칭. 안 맞으면 null */
|
|
41
|
+
const matchRoute = (pathOption: any, instance: any, re: { path?: string }): { pathData: { [k: string]: string }; pathDataAll: { [k: string]: string[] } } | null => {
|
|
42
|
+
let pattern = typeof pathOption === 'function' ? pathOption(instance) : pathOption;
|
|
43
|
+
if (!pattern) return { pathData: {}, pathDataAll: {} };
|
|
44
|
+
const path = re.path || '/';
|
|
45
|
+
for (const p of Array.isArray(pattern) ? pattern : [pattern]) {
|
|
46
|
+
const pathData = SwcUtils.parsePathPattern(p, path);
|
|
47
|
+
if (pathData !== null) return { pathData, pathDataAll: SwcUtils.parsePathPatternAll(p, path) ?? {} };
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
};
|
|
51
|
+
|
|
8
52
|
export const isSSR = (i: HTMLElement) => {
|
|
9
53
|
return i.hasAttribute('swc-use-ssr');
|
|
10
54
|
};
|
|
@@ -25,11 +69,23 @@ export const removeSSRAttribute = (i: HTMLElement) => {
|
|
|
25
69
|
* }
|
|
26
70
|
*/
|
|
27
71
|
export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Base: T) {
|
|
28
|
-
|
|
72
|
+
abstract class SwcAppMixinClass extends Base implements SwcAppInterface {
|
|
29
73
|
__swc_engine = new SwcAppEngine(this as any);
|
|
74
|
+
_swcId!: string;
|
|
30
75
|
_swc_connected_instance = new Set<any>();
|
|
31
76
|
_routerSubscription?: Subscription;
|
|
32
77
|
_lastRouterEvent?: RouterEventType;
|
|
78
|
+
// replay 옵션으로 발행된 타입별 마지막 메시지 1개 보관
|
|
79
|
+
_replayedMessages = new Map<string, SwcAppMessage[]>();
|
|
80
|
+
// 코드 구독(observeMessage)용 live 스트림. 데코레이터 구독과 같은 publishMessage에서 흘러나옴.
|
|
81
|
+
_messageSubject = new Subject<SwcAppMessage>();
|
|
82
|
+
// ── 호스트 라이프사이클 훅 (abstract): 상속한 클래스가 반드시 구현. @inject 파라미터가 있으면 DI로 자동 해결됨 ──
|
|
83
|
+
// connectedCallback 시점 (DOM에 붙을 때마다). connect() 전이라 DI 컨테이너가 아직 없을 수 있음.
|
|
84
|
+
abstract onConnected(...args: any[]): void | Promise<void>;
|
|
85
|
+
// disconnectedCallback 시점 (DOM에서 떨어질 때마다). onConnected에서 만든 구독 등을 여기서 해제.
|
|
86
|
+
abstract onDisconnected(...args: any[]): void | Promise<void>;
|
|
87
|
+
// connect() 완료 후 (DI 컨테이너·라우터 준비됨). 서비스 호출은 여기서.
|
|
88
|
+
abstract onSwcAppConnected(...args: any[]): void | Promise<void>;
|
|
33
89
|
_routeChangeInvocations = 0;
|
|
34
90
|
_connectedInvocations = 0;
|
|
35
91
|
|
|
@@ -42,11 +98,8 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
42
98
|
* 부모가 자식들을 강제로 등록해주는 형태로 처리
|
|
43
99
|
*/
|
|
44
100
|
_connected_safari_and_standby = [];
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
super(...args);
|
|
48
|
-
console.log('[SWC-APP-MIXIN] constructor');
|
|
49
|
-
}
|
|
101
|
+
// connect() 전에 붙은 자식들이 기다리는 곳 — connect() 가 엔진을 띄운 뒤 깨운다
|
|
102
|
+
_appReadyWaiters: Array<() => void> = [];
|
|
50
103
|
|
|
51
104
|
get simpleApplication() {
|
|
52
105
|
return this.__swc_engine.simpleApplication;
|
|
@@ -60,7 +113,8 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
60
113
|
return this.__swc_engine?.router;
|
|
61
114
|
}
|
|
62
115
|
|
|
63
|
-
|
|
116
|
+
// phase: 'live'(기본, 전체 방송) | 'connected' | 'connectedDone' (replay, 해당 trigger만)
|
|
117
|
+
async _invokeRouteChangeSubscribers(instance: HTMLElement, re: RouterEventType, phase: 'live' | 'connected' | 'connectedDone' = 'live') {
|
|
64
118
|
if (instance.isConnected === false) return;
|
|
65
119
|
|
|
66
120
|
this._routeChangeInvocations++;
|
|
@@ -80,66 +134,70 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
80
134
|
);
|
|
81
135
|
|
|
82
136
|
try {
|
|
83
|
-
const routeChangeSubscribers =
|
|
137
|
+
const routeChangeSubscribers = getSubscribeSwcAppRouteChangeMetadata(instance);
|
|
84
138
|
|
|
85
139
|
if (routeChangeSubscribers.length > 0) {
|
|
86
|
-
// Subscribers are already sorted by order from
|
|
87
|
-
// Execute subscribers in order, stop if one returns a value
|
|
140
|
+
// Subscribers are already sorted by order from getSubscribeSwcAppRouteChangeMetadata
|
|
141
|
+
// Execute subscribers in order, stop if one returns a value.
|
|
142
|
+
// live는 전부 실행, replay는 해당 trigger 구독자만 실행.
|
|
143
|
+
// on(enter/update/leave) 판정을 위해 구독자별 직전 매칭 상태를 기억한다 (멈춘 뒤에도 상태는 계속 갱신).
|
|
144
|
+
const state = routeStateOf(instance);
|
|
145
|
+
let stopped = false;
|
|
88
146
|
for (const metadata of routeChangeSubscribers) {
|
|
147
|
+
if (phase !== 'live' && (metadata.options?.trigger ?? 'connected') !== phase) continue;
|
|
148
|
+
const on = metadata.options?.on ?? 'match';
|
|
149
|
+
if (on === 'beforeLeave') continue; // 라우터 가드로 처리 (_registerLeaveGuards)
|
|
89
150
|
const methodName = metadata.propertyKey;
|
|
90
|
-
let pathPattern = metadata.options?.path as any;
|
|
91
|
-
const filter = metadata.options?.filter;
|
|
92
151
|
const extractValue = (v: any) => {
|
|
93
|
-
const keyToUse = metadata.options?.valueKey ??
|
|
152
|
+
const keyToUse = metadata.options?.valueKey ?? SUBSCRIBE_SWC_APP_ROUTE_CHANGE_METADATA_KEY;
|
|
94
153
|
if (v && typeof v === 'object' && keyToUse in v) {
|
|
95
154
|
return v[keyToUse];
|
|
96
155
|
}
|
|
97
156
|
return v;
|
|
98
157
|
};
|
|
99
158
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
159
|
+
const match = matchRoute(metadata.options?.path, instance, re);
|
|
160
|
+
const prev = state.get(methodName);
|
|
161
|
+
// update 판정 기준: 경로 + query (path 변수는 경로에서 나오므로 포함됨. 패턴 없는 bare 구독도 경로 변경을 감지)
|
|
162
|
+
const signature = match ? JSON.stringify([re.path ?? '', re.search ?? '']) : '';
|
|
163
|
+
state.set(methodName, match ? { matched: true, signature, pathData: match.pathData, pathDataAll: match.pathDataAll } : { matched: false, signature: '' });
|
|
103
164
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
for (const pattern of pathPattern) {
|
|
112
|
-
const data = SwcUtils.parsePathPattern(pattern, re.path || '/');
|
|
113
|
-
if (data !== null) {
|
|
114
|
-
pathMatched = true;
|
|
115
|
-
pathData = data;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
pathData = SwcUtils.parsePathPattern(pathPattern, re.path || '/');
|
|
121
|
-
pathMatched = pathData !== null;
|
|
122
|
-
}
|
|
165
|
+
const shouldRun =
|
|
166
|
+
on === 'match' ? !!match
|
|
167
|
+
: on === 'enter' ? !!match && !prev?.matched
|
|
168
|
+
: on === 'update' ? !!match && !!prev?.matched && prev.signature !== signature
|
|
169
|
+
: on === 'leave' ? !match && !!prev?.matched
|
|
170
|
+
: false;
|
|
171
|
+
if (stopped || !shouldRun || !instance[methodName]) continue;
|
|
123
172
|
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
173
|
+
// leave 는 지금 경로가 안 맞으므로 직전 매칭의 pathData 로 준다
|
|
174
|
+
const pathData = match ? match.pathData : prev?.pathData ?? {};
|
|
175
|
+
const pathDataAll = match ? match.pathDataAll : prev?.pathDataAll ?? {};
|
|
176
|
+
|
|
177
|
+
const filter = metadata.options?.filter;
|
|
178
|
+
const instanceHelperHostSet = SwcUtils.getHelperAndHostSet(instance, this.config.window);
|
|
179
|
+
const filterPassed = !filter || (await filter(this.router!, { helper: instanceHelperHostSet, currentThis: instance }));
|
|
180
|
+
if (!filterPassed) continue;
|
|
181
|
+
|
|
182
|
+
const before = metadata.options?.before;
|
|
183
|
+
const finaly = metadata.options?.finally;
|
|
184
|
+
// before를 먼저 돌려 리턴값을 @routeChangeBeforeReturn 으로 주입할 수 있게 캡처.
|
|
185
|
+
const beforeReturn = before ? await before(this.router!, { helper: instanceHelperHostSet, currentThis: instance }) : undefined;
|
|
186
|
+
const routeEventValue = { ...re, pathData };
|
|
187
|
+
const args = this._buildRouteArgs(instance, methodName, routeEventValue, pathDataAll, beforeReturn);
|
|
188
|
+
let rawResult: any, error: any;
|
|
189
|
+
try {
|
|
190
|
+
rawResult = await instance[methodName](...args);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
error = e;
|
|
193
|
+
} finally {
|
|
194
|
+
if (finaly) await finaly(this.router!, { helper: instanceHelperHostSet, currentThis: instance }, { args, result: rawResult, error });
|
|
195
|
+
}
|
|
196
|
+
if (error) throw error;
|
|
197
|
+
const result = extractValue(rawResult);
|
|
198
|
+
// If handler returns a value, stop propagation to next handlers
|
|
199
|
+
if (result !== undefined && result !== null) {
|
|
200
|
+
stopped = true;
|
|
143
201
|
}
|
|
144
202
|
}
|
|
145
203
|
}
|
|
@@ -155,11 +213,70 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
155
213
|
}
|
|
156
214
|
}
|
|
157
215
|
|
|
216
|
+
/** 라우트 핸들러 인자: @swcAppRouterEvent / host 계열 / @routeChangeBeforeReturn / 라우트 파라미터(query·path 변수) */
|
|
217
|
+
_buildRouteArgs(instance: any, methodName: string | symbol, routeEventValue: any, pathDataAll: { [k: string]: string[] }, beforeReturn: any) {
|
|
218
|
+
const instanceHelperHostSet = SwcUtils.getHelperAndHostSet(instance, this.config.window);
|
|
219
|
+
const sp: URLSearchParams = routeEventValue.searchParams ?? new URLSearchParams(routeEventValue.search ?? '');
|
|
220
|
+
return buildSwcParameterArgs(
|
|
221
|
+
instance,
|
|
222
|
+
methodName,
|
|
223
|
+
{
|
|
224
|
+
swcAppRouterEvent: routeEventValue,
|
|
225
|
+
hostSet: instanceHelperHostSet,
|
|
226
|
+
helperHostSet: instanceHelperHostSet,
|
|
227
|
+
helperSet: SwcUtils.getHelperSet(this.config.window),
|
|
228
|
+
routeChangeBeforeReturn: beforeReturn,
|
|
229
|
+
...routeParamValues(sp, pathDataAll)
|
|
230
|
+
},
|
|
231
|
+
[routeEventValue]
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* on: 'beforeLeave' 구독을 라우터 가드로 등록. 지금 경로는 맞고 다음 경로는 안 맞을 때 핸들러를 부르고,
|
|
237
|
+
* false 를 리턴하면 이동을 취소한다. 요소가 떨어지면 _disconnected 에서 해제.
|
|
238
|
+
*/
|
|
239
|
+
_registerLeaveGuards(instance: any) {
|
|
240
|
+
if (!this.router?.addLeaveGuard) return;
|
|
241
|
+
instance.__swc_leaveGuardOffs?.forEach((off: () => void) => off());
|
|
242
|
+
instance.__swc_leaveGuardOffs = getSubscribeSwcAppRouteChangeMetadata(instance)
|
|
243
|
+
.filter(m => m.options?.on === 'beforeLeave')
|
|
244
|
+
.map(metadata =>
|
|
245
|
+
this.router!.addLeaveGuard(async (to, from) => {
|
|
246
|
+
if (instance.isConnected === false || !instance[metadata.propertyKey]) return true;
|
|
247
|
+
const fromMatch = matchRoute(metadata.options?.path, instance, from);
|
|
248
|
+
if (!fromMatch || matchRoute(metadata.options?.path, instance, to)) return true; // 이 라우트를 떠나는 경우만
|
|
249
|
+
const filter = metadata.options?.filter;
|
|
250
|
+
const helper = SwcUtils.getHelperAndHostSet(instance, this.config.window);
|
|
251
|
+
if (filter && !(await filter(this.router!, { helper, currentThis: instance }))) return true;
|
|
252
|
+
const beforeReturn = metadata.options?.before ? await metadata.options.before(this.router!, { helper, currentThis: instance }) : undefined;
|
|
253
|
+
const routeEventValue = { ...from, pathData: fromMatch.pathData, to };
|
|
254
|
+
const args = this._buildRouteArgs(instance, metadata.propertyKey, routeEventValue, fromMatch.pathDataAll, beforeReturn);
|
|
255
|
+
let result: any, error: any;
|
|
256
|
+
try {
|
|
257
|
+
result = await instance[metadata.propertyKey](...args);
|
|
258
|
+
} catch (e) {
|
|
259
|
+
error = e;
|
|
260
|
+
} finally {
|
|
261
|
+
if (metadata.options?.finally) await metadata.options.finally(this.router!, { helper, currentThis: instance }, { args, result, error });
|
|
262
|
+
}
|
|
263
|
+
if (error) throw error;
|
|
264
|
+
return result !== false;
|
|
265
|
+
})
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
158
269
|
async _connected(instance: HTMLElement, option?: { noIncrements: boolean }) {
|
|
159
270
|
if (instance) {
|
|
160
271
|
if (!this.simpleApplication && !this.config) {
|
|
161
|
-
|
|
162
|
-
|
|
272
|
+
// 자기 자신은 connect() 가 나중에 재생한다
|
|
273
|
+
if (instance === (this as any)) {
|
|
274
|
+
this._connected_safari_and_standby.push(instance);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
// 앱이 아직 connect 전: 준비될 때까지 기다린다. 안 기다리면 이미 정의된 자식이 DI·라우터 없이
|
|
278
|
+
// 라이프사이클(@inject 등)을 먼저 돌려 undefined 를 받는다 (elementDefine 이 이 await 뒤에 렌더한다).
|
|
279
|
+
await new Promise<void>(resolve => this._appReadyWaiters.push(resolve));
|
|
163
280
|
}
|
|
164
281
|
|
|
165
282
|
this.config.onConnectedChildBefore?.(instance);
|
|
@@ -167,10 +284,14 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
167
284
|
this._connectedInvocations++;
|
|
168
285
|
}
|
|
169
286
|
this._swc_connected_instance.add(instance);
|
|
287
|
+
// await 사이에 요소가 떨어지거나 다시 붙으면(elementDefine 의 차례 번호가 바뀌면) 나머지(가드 등록·재생)는 건너뛴다
|
|
288
|
+
const gen = (instance as any).__swc_connectGen;
|
|
289
|
+
const stale = () => (instance as any).__swc_connectGen !== gen;
|
|
170
290
|
|
|
171
291
|
const connectedSwcAppCallBacks = findAllLifecycleMetadata(instance, ON_CONNECTED_SWC_APP_METADATA_KEY);
|
|
172
292
|
for (let connectedSwcAppCallBack of connectedSwcAppCallBacks) {
|
|
173
293
|
await (instance as any)?._invokeLifecycleMethod?.(connectedSwcAppCallBack.propertyKey);
|
|
294
|
+
if (stale()) return this;
|
|
174
295
|
}
|
|
175
296
|
|
|
176
297
|
if (this._childrenConnectedDoneInterval) {
|
|
@@ -190,15 +311,20 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
190
311
|
if (this.simpleApplication) {
|
|
191
312
|
try {
|
|
192
313
|
if (this.router && this._lastRouterEvent) {
|
|
193
|
-
await this._invokeRouteChangeSubscribers(instance, this._lastRouterEvent
|
|
314
|
+
await this._invokeRouteChangeSubscribers(instance, this._lastRouterEvent!, 'connected');
|
|
194
315
|
}
|
|
195
316
|
} catch (e) {
|
|
196
317
|
throw e;
|
|
197
318
|
}
|
|
198
319
|
}
|
|
320
|
+
if (stale()) return this;
|
|
321
|
+
|
|
322
|
+
if (this.simpleApplication) this._registerLeaveGuards(instance);
|
|
323
|
+
|
|
324
|
+
// replay 버퍼가 있으면 신규 인스턴스에만 재생 (fire-and-forget, 연결 블로킹 안 함)
|
|
325
|
+
this._replayMessagesTo(instance);
|
|
199
326
|
|
|
200
327
|
const isBrowser = ValidUtils.isBrowser();
|
|
201
|
-
console.log('------>', isBrowser, this.config?.ssr)
|
|
202
328
|
if (!isBrowser && this.config?.ssr) {
|
|
203
329
|
setSSRAttribute(instance);
|
|
204
330
|
} else {
|
|
@@ -210,9 +336,20 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
210
336
|
return this;
|
|
211
337
|
}
|
|
212
338
|
|
|
213
|
-
async _connectedDone(instance: any) {
|
|
339
|
+
async _connectedDone(instance: any, option?: { aborted?: boolean }) {
|
|
214
340
|
try {
|
|
215
|
-
//
|
|
341
|
+
if (option?.aborted) return; // 중간에 멈춘 연결: 재생 없이 연결 수만 맞춘다
|
|
342
|
+
// trigger 'connectedDone' 구독에만 버퍼 재생 (render 완료 후라 DOM 타겟 안전)
|
|
343
|
+
this._replayMessagesTo(instance, 'connectedDone');
|
|
344
|
+
if (this.simpleApplication) {
|
|
345
|
+
try {
|
|
346
|
+
if (this.router && this._lastRouterEvent) {
|
|
347
|
+
await this._invokeRouteChangeSubscribers(instance, this._lastRouterEvent!, 'connectedDone');
|
|
348
|
+
}
|
|
349
|
+
} catch (e) {
|
|
350
|
+
throw e;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
216
353
|
} finally {
|
|
217
354
|
this._connectedInvocations = Math.max(0, this._connectedInvocations - 1);
|
|
218
355
|
}
|
|
@@ -221,6 +358,10 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
221
358
|
_disconnected(instance: any) {
|
|
222
359
|
if (instance) {
|
|
223
360
|
this._swc_connected_instance.delete(instance);
|
|
361
|
+
// beforeLeave 가드 해제 + on 판정 상태 초기화 (다시 붙으면 enter 부터)
|
|
362
|
+
instance.__swc_leaveGuardOffs?.forEach((off: () => void) => off());
|
|
363
|
+
instance.__swc_leaveGuardOffs = undefined;
|
|
364
|
+
instance.__swc_routeState = undefined;
|
|
224
365
|
this.config?.onDisconnectedChildAfter?.(instance);
|
|
225
366
|
}
|
|
226
367
|
}
|
|
@@ -233,7 +374,6 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
233
374
|
}
|
|
234
375
|
|
|
235
376
|
async connect(config?: SwcAttributeConfigType) {
|
|
236
|
-
console.log('[SWC-MIXIN] connect');
|
|
237
377
|
const swcConfig = {
|
|
238
378
|
routeType: 'element',
|
|
239
379
|
window: config?.window,
|
|
@@ -241,6 +381,7 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
241
381
|
} as SwcConfigType;
|
|
242
382
|
|
|
243
383
|
await this.__swc_engine.connect(swcConfig);
|
|
384
|
+
this._appReadyWaiters.splice(0).forEach(resolve => resolve());
|
|
244
385
|
|
|
245
386
|
// Safari is 속성 처리
|
|
246
387
|
this._connected_safari_and_standby.forEach((instance: any) => {
|
|
@@ -249,19 +390,15 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
249
390
|
|
|
250
391
|
// 라우터 변경 구독
|
|
251
392
|
if (!this._routerSubscription && this.router) {
|
|
252
|
-
const config = this.__swc_engine.config;
|
|
253
|
-
|
|
254
393
|
this._routerSubscription = this.router.observable.subscribe(async (route: RouterEventType) => {
|
|
255
|
-
console.log('[SWC-MIXIN] Router event received:', route.triggerPoint);
|
|
256
394
|
if (route.triggerPoint === 'end') {
|
|
257
395
|
await this._handleRouteChange(route);
|
|
258
396
|
}
|
|
259
397
|
});
|
|
260
|
-
|
|
261
|
-
if (config?.path) {
|
|
262
|
-
console.log('[DEBUG] Navigating to initial path:', config.path);
|
|
263
|
-
}
|
|
264
398
|
}
|
|
399
|
+
|
|
400
|
+
// onSwcAppConnected 훅 (@inject 파라미터 있으면 DI 해결 — _invokeLifecycleMethod 경로)
|
|
401
|
+
await this._invokeHook('onSwcAppConnected');
|
|
265
402
|
}
|
|
266
403
|
|
|
267
404
|
connectedCallback() {
|
|
@@ -276,7 +413,7 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
276
413
|
const userConfig = FunctionUtils.executeReturn({
|
|
277
414
|
script: configStr,
|
|
278
415
|
context: this,
|
|
279
|
-
args: SwcUtils.getHelperAndHostSet(
|
|
416
|
+
args: SwcUtils.getHelperAndHostSet(this, win)
|
|
280
417
|
});
|
|
281
418
|
if (userConfig instanceof Promise) {
|
|
282
419
|
userConfig
|
|
@@ -297,6 +434,7 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
297
434
|
this._connected(this);
|
|
298
435
|
this.__swc_engine?.config?.onConnected?.(this);
|
|
299
436
|
this._connectedDone(this);
|
|
437
|
+
void this._invokeHook('onConnected').catch(e => console.error('[SWC-MIXIN] onConnected error:', e));
|
|
300
438
|
}
|
|
301
439
|
|
|
302
440
|
disconnectedCallback() {
|
|
@@ -310,6 +448,7 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
310
448
|
this.__swc_engine.disconnect();
|
|
311
449
|
this.__swc_engine?.config?.onDisconnected?.(this);
|
|
312
450
|
this._disconnected(this);
|
|
451
|
+
void this._invokeHook('onDisconnected').catch(e => console.error('[SWC-MIXIN] onDisconnected error:', e));
|
|
313
452
|
}
|
|
314
453
|
|
|
315
454
|
async routing(path: string) {
|
|
@@ -328,45 +467,135 @@ export function SwcAppMixin<T extends { new (...args: any[]): HTMLElement }>(Bas
|
|
|
328
467
|
this.router?.reload();
|
|
329
468
|
}
|
|
330
469
|
|
|
331
|
-
publishMessage(message: SwcAppMessage): void {
|
|
470
|
+
publishMessage(message: SwcAppMessage): void { // 버퍼 먼저 (핸들러가 동기 재발행해도 순서 보장)
|
|
471
|
+
// 전 타입 과거 메시지 보관 (늦게 연결될 인스턴스에 재생). 개수는 앱 설정 1곳.
|
|
472
|
+
if (message.type) {
|
|
473
|
+
const buf = [...(this._replayedMessages.get(message.type) ?? []), message];
|
|
474
|
+
this._replayedMessages.set(message.type, buf.slice(-Math.max(1, this.config?.messageReplayBufferSize ?? 10)));
|
|
475
|
+
}
|
|
332
476
|
this._swc_connected_instance.forEach((instance: any) => {
|
|
333
477
|
this._invokeMessageSubscribers(instance, message);
|
|
334
478
|
});
|
|
479
|
+
this._messageSubject.next(message);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
connectedElements(): Array<HTMLElement & SwcElement> {
|
|
483
|
+
return Array.from(this._swc_connected_instance);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* 데코레이터 없이 코드로 메시지를 구독한다 (RxJS 스타일). 서비스·동적 구독 등 데코레이터를 못 붙이는 곳용.
|
|
488
|
+
* host.observeMessage<Me>(AUTH_CHANGED).subscribe(msg => ...) → Subscription (unsubscribe 필수)
|
|
489
|
+
* type 생략 시 전 타입 live. subject 옵션은 @subscribeSwcAppMessage 와 같은 규칙(type 지정 시에만 재생):
|
|
490
|
+
* 'behavior' = 버퍼 마지막 1개 먼저 / 'replay' = 버퍼 전체 시간순 먼저 / 미지정·'subject' = live만.
|
|
491
|
+
* 호출 형태:
|
|
492
|
+
* observeMessage() 전 타입 live
|
|
493
|
+
* observeMessage('auth') auth live
|
|
494
|
+
* observeMessage('auth', { subject: 'behavior' }) auth 마지막 1개 + live
|
|
495
|
+
* observeMessage({ type: 'auth', subject: 'replay' }) 옵션 객체 하나로
|
|
496
|
+
*/
|
|
497
|
+
observeMessage<T = any>(): Observable<SwcAppMessage<T>>;
|
|
498
|
+
observeMessage<T = any>(type: string): Observable<SwcAppMessage<T>>;
|
|
499
|
+
observeMessage<T = any>(type: string, options: Omit<SwcAppMessageObserveOptions, 'type'>): Observable<SwcAppMessage<T>>;
|
|
500
|
+
observeMessage<T = any>(options: SwcAppMessageObserveOptions): Observable<SwcAppMessage<T>>;
|
|
501
|
+
observeMessage<T = any>(typeOrOptions?: string | SwcAppMessageObserveOptions, options?: Omit<SwcAppMessageObserveOptions, 'type'>): Observable<SwcAppMessage<T>> {
|
|
502
|
+
const { type, subject } = typeof typeOrOptions === 'string' ? { ...options, type: typeOrOptions } : { ...typeOrOptions };
|
|
503
|
+
return new Observable<SwcAppMessage<T>>(subscriber => {
|
|
504
|
+
if (type && (subject === 'behavior' || subject === 'replay')) {
|
|
505
|
+
const buf = this._replayedMessages.get(type) ?? [];
|
|
506
|
+
(subject === 'behavior' ? buf.slice(-1) : buf).forEach(m => subscriber.next(m as SwcAppMessage<T>));
|
|
507
|
+
}
|
|
508
|
+
const sub = this._messageSubject.subscribe(m => {
|
|
509
|
+
if (!type || m.type === type) subscriber.next(m as SwcAppMessage<T>);
|
|
510
|
+
});
|
|
511
|
+
return () => sub.unsubscribe();
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// hook 실행 + @inject 파라미터 DI 해결 (host 본체 포함.
|
|
516
|
+
// _invokeLifecycleMethod는 host/hostSet 상황 분기가 달라 호스트 본체에서 주입이 안 됨)
|
|
517
|
+
async _invokeHook(methodName: string | symbol) {
|
|
518
|
+
const fn = (this as any)[methodName];
|
|
519
|
+
if (typeof fn !== 'function') return;
|
|
520
|
+
const manager = (this as any).simpleApplication?.simstanceManager;
|
|
521
|
+
if (!manager) {
|
|
522
|
+
return fn.call(this);
|
|
523
|
+
}
|
|
524
|
+
return manager.executeBindParameterSimPromise({ target: this, targetKey: methodName });
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// 신규 연결 인스턴스 1곳에만 버퍼 재생. 구독자별 subject 선언 기준:
|
|
528
|
+
// - 'behavior': 마지막 1개 / 'replay': 버퍼 전체 시간순 / 미지정·'subject': live만 (재생 안 함)
|
|
529
|
+
// - trigger 'connectedDone'인 구독은 _connectedDone(render 완료 후)에서 재생. phase로 구분.
|
|
530
|
+
// 이미 연결된 인스턴스는 publishMessage 때 live로 받으므로 여기 오지 않음.
|
|
531
|
+
_replayMessagesTo(instance: any, phase: 'connected' | 'connectedDone' = 'connected') {
|
|
532
|
+
const subs = getSubscribeSwcAppMessageMetadata(instance);
|
|
533
|
+
if (!subs || !Array.isArray(subs)) return;
|
|
534
|
+
const seen = new Set<string>();
|
|
535
|
+
subs.forEach(meta => {
|
|
536
|
+
const type = meta.messageType as string | undefined;
|
|
537
|
+
if (!type || seen.has(type)) return;
|
|
538
|
+
seen.add(type);
|
|
539
|
+
if ((meta.trigger ?? 'connected') !== phase) return;
|
|
540
|
+
const buf = this._replayedMessages.get(type) ?? [];
|
|
541
|
+
if (meta.subject === 'behavior') {
|
|
542
|
+
const last = buf[buf.length - 1];
|
|
543
|
+
if (last) this._invokeMessageSubscribers(instance, last);
|
|
544
|
+
} else if (meta.subject === 'replay') {
|
|
545
|
+
buf.forEach(msg => this._invokeMessageSubscribers(instance, msg));
|
|
546
|
+
}
|
|
547
|
+
});
|
|
335
548
|
}
|
|
336
549
|
|
|
337
550
|
_invokeMessageSubscribers(instance: any, message: SwcAppMessage) {
|
|
338
|
-
const messageSubscribers =
|
|
551
|
+
const messageSubscribers = getSubscribeSwcAppMessageMetadata(instance);
|
|
339
552
|
if (messageSubscribers && Array.isArray(messageSubscribers)) {
|
|
553
|
+
// 메시지 구독은 fire-and-forget — async filter/before/finally를 위해 구독자마다 비동기 러너로 돌리되
|
|
554
|
+
// 호출측(publishMessage)은 논블로킹 유지. 미처리 rejection은 로깅만.
|
|
340
555
|
messageSubscribers.forEach(metadata => {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
let typeMatched = false;
|
|
346
|
-
if (!messageType) {
|
|
347
|
-
typeMatched = true;
|
|
348
|
-
} else if (message.type === messageType) {
|
|
349
|
-
typeMatched = true;
|
|
350
|
-
}
|
|
556
|
+
void this._runMessageSubscriber(instance, message, metadata).catch(e => console.error('[SWC] subscribeSwcAppMessage error:', e));
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
}
|
|
351
560
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
561
|
+
async _runMessageSubscriber(instance: any, message: SwcAppMessage, metadata: any) {
|
|
562
|
+
const methodName = metadata.propertyKey;
|
|
563
|
+
const messageType = metadata.messageType;
|
|
564
|
+
const { filter, before, finally: finaly } = metadata;
|
|
565
|
+
|
|
566
|
+
const typeMatched = !messageType || message.type === messageType;
|
|
567
|
+
if (!typeMatched) return;
|
|
568
|
+
if (filter && !(await filter(message, instance))) return;
|
|
569
|
+
if (typeof instance[methodName] !== 'function') return;
|
|
570
|
+
|
|
571
|
+
const instanceHelperHostSet = SwcUtils.getHelperAndHostSet(instance, this.config.window);
|
|
572
|
+
const instanceHelperSet = SwcUtils.getHelperSet(this.config.window);
|
|
573
|
+
// before를 먼저 돌려 리턴값을 @appMessageBeforeReturn 으로 주입할 수 있게 캡처.
|
|
574
|
+
const beforeReturn = before ? await before(message, instance) : undefined;
|
|
575
|
+
const args = buildSwcParameterArgs(
|
|
576
|
+
instance,
|
|
577
|
+
methodName,
|
|
578
|
+
{
|
|
579
|
+
appMessage: message,
|
|
580
|
+
hostSet: instanceHelperHostSet,
|
|
581
|
+
helperHostSet: instanceHelperHostSet,
|
|
582
|
+
helperSet: instanceHelperSet,
|
|
583
|
+
appMessageBeforeReturn: beforeReturn
|
|
584
|
+
},
|
|
585
|
+
[message]
|
|
586
|
+
);
|
|
356
587
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
helperSet: instanceHelperSet
|
|
365
|
-
}, [message]);
|
|
366
|
-
instance[methodName](...args);
|
|
367
|
-
}
|
|
368
|
-
});
|
|
588
|
+
let result: any, error: any;
|
|
589
|
+
try {
|
|
590
|
+
result = await instance[methodName](...args);
|
|
591
|
+
} catch (e) {
|
|
592
|
+
error = e;
|
|
593
|
+
} finally {
|
|
594
|
+
if (finaly) await finaly(message, instance, { args, result, error });
|
|
369
595
|
}
|
|
596
|
+
if (error) throw error;
|
|
370
597
|
}
|
|
371
|
-
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return SwcAppMixinClass;
|
|
372
601
|
}
|