@dooboostore/simple-web-component 1.0.37 → 1.0.39

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 (157) hide show
  1. package/README.md +280 -791
  2. package/dist/cjs/SwcAppEngine.js +3 -0
  3. package/dist/cjs/SwcAppEngine.js.map +2 -2
  4. package/dist/cjs/decorators/addEventListener.js +20 -4
  5. package/dist/cjs/decorators/addEventListener.js.map +2 -2
  6. package/dist/cjs/decorators/applyAttribute.js.map +1 -1
  7. package/dist/cjs/decorators/applyClass.js.map +1 -1
  8. package/dist/cjs/decorators/applyNode.js +104 -35
  9. package/dist/cjs/decorators/applyNode.js.map +2 -2
  10. package/dist/cjs/decorators/applySlot.js +69 -57
  11. package/dist/cjs/decorators/applySlot.js.map +3 -3
  12. package/dist/cjs/decorators/applyStyle.js.map +2 -2
  13. package/dist/cjs/decorators/attribute.js.map +1 -1
  14. package/dist/cjs/decorators/elementDefine.js +65 -42
  15. package/dist/cjs/decorators/elementDefine.js.map +2 -2
  16. package/dist/cjs/decorators/emitCustomEvent.js.map +1 -1
  17. package/dist/cjs/decorators/index.js +1 -0
  18. package/dist/cjs/decorators/index.js.map +2 -2
  19. package/dist/cjs/decorators/lifecycles.js +47 -31
  20. package/dist/cjs/decorators/lifecycles.js.map +3 -3
  21. package/dist/cjs/decorators/publishSwcAppMessage.js.map +2 -2
  22. package/dist/cjs/decorators/query.js +1 -1
  23. package/dist/cjs/decorators/query.js.map +2 -2
  24. package/dist/cjs/decorators/queryAll.js.map +1 -1
  25. package/dist/cjs/decorators/setProperty.js.map +1 -1
  26. package/dist/cjs/decorators/state.js +83 -0
  27. package/dist/cjs/decorators/state.js.map +7 -0
  28. package/dist/cjs/decorators/subscribeSwcAppMessageWhileConnected.js.map +1 -1
  29. package/dist/cjs/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +2 -2
  30. package/dist/cjs/elements/SwcApp.js +193 -305
  31. package/dist/cjs/elements/SwcApp.js.map +3 -3
  32. package/dist/cjs/elements/SwcAppMixin.js +291 -0
  33. package/dist/cjs/elements/SwcAppMixin.js.map +7 -0
  34. package/dist/cjs/elements/index.js +3 -21
  35. package/dist/cjs/elements/index.js.map +3 -3
  36. package/dist/cjs/index.js +1 -3
  37. package/dist/cjs/index.js.map +2 -2
  38. package/dist/cjs/{types.js → types/index.js} +1 -1
  39. package/dist/cjs/types/index.js.map +7 -0
  40. package/dist/cjs/types/window.d.js.map +1 -1
  41. package/dist/cjs/utils/Utils.js +159 -20
  42. package/dist/cjs/utils/Utils.js.map +3 -3
  43. package/dist/esm/SwcAppEngine.js +3 -0
  44. package/dist/esm/SwcAppEngine.js.map +2 -2
  45. package/dist/esm/decorators/addEventListener.js +20 -4
  46. package/dist/esm/decorators/addEventListener.js.map +2 -2
  47. package/dist/esm/decorators/applyAttribute.js.map +1 -1
  48. package/dist/esm/decorators/applyClass.js.map +1 -1
  49. package/dist/esm/decorators/applyNode.js +104 -35
  50. package/dist/esm/decorators/applyNode.js.map +2 -2
  51. package/dist/esm/decorators/applySlot.js +69 -57
  52. package/dist/esm/decorators/applySlot.js.map +3 -3
  53. package/dist/esm/decorators/applyStyle.js.map +2 -2
  54. package/dist/esm/decorators/attribute.js.map +1 -1
  55. package/dist/esm/decorators/elementDefine.js +69 -46
  56. package/dist/esm/decorators/elementDefine.js.map +2 -2
  57. package/dist/esm/decorators/emitCustomEvent.js.map +1 -1
  58. package/dist/esm/decorators/index.js +1 -0
  59. package/dist/esm/decorators/index.js.map +2 -2
  60. package/dist/esm/decorators/lifecycles.js +47 -31
  61. package/dist/esm/decorators/lifecycles.js.map +3 -3
  62. package/dist/esm/decorators/publishSwcAppMessage.js.map +2 -2
  63. package/dist/esm/decorators/query.js +1 -1
  64. package/dist/esm/decorators/query.js.map +2 -2
  65. package/dist/esm/decorators/queryAll.js.map +1 -1
  66. package/dist/esm/decorators/setProperty.js.map +1 -1
  67. package/dist/esm/decorators/state.js +64 -0
  68. package/dist/esm/decorators/state.js.map +7 -0
  69. package/dist/esm/decorators/subscribeSwcAppMessageWhileConnected.js.map +1 -1
  70. package/dist/esm/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +2 -2
  71. package/dist/esm/elements/SwcApp.js +195 -307
  72. package/dist/esm/elements/SwcApp.js.map +3 -3
  73. package/dist/esm/elements/SwcAppMixin.js +273 -0
  74. package/dist/esm/elements/SwcAppMixin.js.map +7 -0
  75. package/dist/esm/elements/index.js +2 -7
  76. package/dist/esm/elements/index.js.map +2 -2
  77. package/dist/esm/index.js +2 -4
  78. package/dist/esm/index.js.map +2 -2
  79. package/dist/esm/{types.js → types/index.js} +1 -1
  80. package/dist/esm/types/index.js.map +7 -0
  81. package/dist/esm/utils/Utils.js +159 -20
  82. package/dist/esm/utils/Utils.js.map +3 -3
  83. package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +3448 -2584
  84. package/dist/esm-bundle/dooboostore-simple-web-component.esm.js.map +4 -4
  85. package/dist/types/SwcAppEngine.d.ts +1 -1
  86. package/dist/types/SwcAppEngine.d.ts.map +1 -1
  87. package/dist/types/decorators/addEventListener.d.ts +5 -2
  88. package/dist/types/decorators/addEventListener.d.ts.map +1 -1
  89. package/dist/types/decorators/applyAttribute.d.ts.map +1 -1
  90. package/dist/types/decorators/applyClass.d.ts.map +1 -1
  91. package/dist/types/decorators/applyNode.d.ts +23 -7
  92. package/dist/types/decorators/applyNode.d.ts.map +1 -1
  93. package/dist/types/decorators/applySlot.d.ts +14 -12
  94. package/dist/types/decorators/applySlot.d.ts.map +1 -1
  95. package/dist/types/decorators/applyStyle.d.ts.map +1 -1
  96. package/dist/types/decorators/attribute.d.ts.map +1 -1
  97. package/dist/types/decorators/elementDefine.d.ts.map +1 -1
  98. package/dist/types/decorators/emitCustomEvent.d.ts.map +1 -1
  99. package/dist/types/decorators/index.d.ts +1 -0
  100. package/dist/types/decorators/index.d.ts.map +1 -1
  101. package/dist/types/decorators/lifecycles.d.ts +10 -14
  102. package/dist/types/decorators/lifecycles.d.ts.map +1 -1
  103. package/dist/types/decorators/publishSwcAppMessage.d.ts.map +1 -1
  104. package/dist/types/decorators/query.d.ts +1 -1
  105. package/dist/types/decorators/query.d.ts.map +1 -1
  106. package/dist/types/decorators/queryAll.d.ts.map +1 -1
  107. package/dist/types/decorators/setProperty.d.ts +1 -1
  108. package/dist/types/decorators/setProperty.d.ts.map +1 -1
  109. package/dist/types/decorators/state.d.ts +9 -0
  110. package/dist/types/decorators/state.d.ts.map +1 -0
  111. package/dist/types/decorators/subscribeSwcAppMessageWhileConnected.d.ts.map +1 -1
  112. package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts +5 -1
  113. package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts.map +1 -1
  114. package/dist/types/elements/SwcApp.d.ts +35 -6
  115. package/dist/types/elements/SwcApp.d.ts.map +1 -1
  116. package/dist/types/elements/SwcAppMixin.d.ts +405 -0
  117. package/dist/types/elements/SwcAppMixin.d.ts.map +1 -0
  118. package/dist/types/elements/index.d.ts +2 -2
  119. package/dist/types/elements/index.d.ts.map +1 -1
  120. package/dist/types/index.d.ts +1 -1
  121. package/dist/types/index.d.ts.map +1 -1
  122. package/dist/types/{types.d.ts → types/index.d.ts} +11 -11
  123. package/dist/types/types/index.d.ts.map +1 -0
  124. package/dist/types/utils/Utils.d.ts +14 -2
  125. package/dist/types/utils/Utils.d.ts.map +1 -1
  126. package/dist/umd-bundle/dooboostore-simple-web-component.umd.js +3431 -2567
  127. package/dist/umd-bundle/dooboostore-simple-web-component.umd.js.map +4 -4
  128. package/package.json +4 -4
  129. package/src/SwcAppEngine.ts +6 -2
  130. package/src/decorators/addEventListener.ts +21 -5
  131. package/src/decorators/applyAttribute.ts +1 -1
  132. package/src/decorators/applyClass.ts +1 -1
  133. package/src/decorators/applyNode.ts +183 -73
  134. package/src/decorators/applySlot.ts +99 -128
  135. package/src/decorators/applyStyle.ts +4 -4
  136. package/src/decorators/attribute.ts +1 -1
  137. package/src/decorators/elementDefine.ts +112 -60
  138. package/src/decorators/emitCustomEvent.ts +1 -1
  139. package/src/decorators/index.ts +2 -0
  140. package/src/decorators/lifecycles.ts +50 -26
  141. package/src/decorators/publishSwcAppMessage.ts +2 -3
  142. package/src/decorators/query.ts +4 -4
  143. package/src/decorators/queryAll.ts +1 -1
  144. package/src/decorators/setProperty.ts +1 -1
  145. package/src/decorators/state.ts +83 -0
  146. package/src/decorators/subscribeSwcAppMessageWhileConnected.ts +1 -1
  147. package/src/decorators/subscribeSwcAppRouteChangeWhileConnected.ts +4 -3
  148. package/src/elements/SwcApp.ts +239 -441
  149. package/src/elements/SwcAppMixin.ts +328 -0
  150. package/src/elements/index.ts +2 -9
  151. package/src/index.ts +2 -4
  152. package/src/{types.ts → types/index.ts} +13 -10
  153. package/src/types/window.d.ts +3 -0
  154. package/src/utils/Utils.ts +205 -20
  155. package/dist/cjs/types.js.map +0 -7
  156. package/dist/esm/types.js.map +0 -7
  157. package/dist/types/types.d.ts.map +0 -1
@@ -1,443 +1,241 @@
1
- import {SwcAppInterface, SwcAppMessage} from "../types";
2
- import {elementDefine, findAllLifecycleMetadata, getSubscribeSwcAppMessageWhileConnectedMetadata, getSubscribeSwcAppRouteChangeWhileConnectedMetadata, ON_CONNECTED_SWC_APP_METADATA_KEY} from "../decorators";
3
- import {SwcAppEngine, SwcAttributeConfigType, SwcConfigType} from "../SwcAppEngine";
4
- import {debounceTimeIntervalLock, FunctionUtils, Subscription} from "@dooboostore/core";
5
- import {RouterEventType} from "@dooboostore/core-web";
6
- import {SwcUtils} from "../utils/Utils";
7
-
8
- export const isSSR = (i: HTMLElement) => {
9
- return i.hasAttribute('swc-use-ssr');
10
- }
11
- export const setSSRAttribute = (i: HTMLElement) => {
12
- i.setAttribute('swc-use-ssr', (i as any)._swcId ?? '');
13
- }
14
- export const removeSSRAttribute = (i: HTMLElement) => {
15
- i.removeAttribute('swc-use-ssr');
16
- }
17
-
18
-
19
- export const tagName = 'swc-app';
20
- export default async (w: Window) => {
21
- const existing = w.customElements.get(tagName);
1
+ import {elementDefine} from "../decorators";
2
+ import {SwcAppInterface} from "../types";
3
+ import {SwcAppMixin} from "./SwcAppMixin";
4
+ import {SwcUtils, createElement, CreateElementConfig} from "../utils/Utils";
5
+
6
+ // ============================================
7
+ // SwcApp (HTMLElement)
8
+ // ============================================
9
+ export const swcAppTagName = 'swc-app';
10
+
11
+ export const SwcApp = (w: Window, data?: CreateElementConfig) => {
12
+ return createElement<SwcAppInterface>(w, swcAppTagName, data);
13
+ };
14
+
15
+ export const defineSwcApp = async (w: Window) => {
16
+ const existing = w.customElements.get(swcAppTagName);
22
17
  if (existing) return existing;
23
18
 
24
- @elementDefine(tagName, {window: w})
25
- class SwcApp extends w.HTMLElement implements SwcAppInterface {
26
- __swc_engine = new SwcAppEngine(this as any);
27
- _swc_connected_instance = new Set<any>();
28
- // _swc_notified_routes = new WeakMap<any, RouterEventType>();
29
- _routerSubscription?: Subscription;
30
- _lastRouterEvent?: RouterEventType;
31
- _routeChangeInvocations = 0;
32
- _connectedInvocations = 0;
33
-
34
- _childrenRouteChangedInterval: any;
35
- _childrenConnectedDoneInterval: any;
36
-
37
- /*
38
- 사파리 is 일경우
39
- <script src="https://unpkg.com/@ungap/custom-elements"></script>
40
- 이걸사용하는데 is 대응하는거다 그런데 이때 처리되는 순서가 크롬이나 표준에서는 부모부터 만들어지고 자식들이 만들어지는데
41
- 저 polyfill에서는 자식부터 만들어지고 부모가 만들어진다 is일때 아 .. 이문제때문에 자식이 _connected_safari 에 강제로 자기자식넣어준다
42
- */
43
- _connected_safari_and_standby = [];
44
-
45
- get simpleApplication() {
46
- return this.__swc_engine.simpleApplication;
47
- }
48
-
49
- get config() {
50
- return this.__swc_engine?.config;
51
- }
52
-
53
- get router() {
54
- return this.__swc_engine?.router;
55
- }
56
-
57
- async _invokeRouteChangeSubscribers(instance: HTMLElement, re: RouterEventType) {
58
- if (instance.isConnected === false) return;
59
-
60
- this._routeChangeInvocations++;
61
-
62
- if (this._childrenRouteChangedInterval) {
63
- this.config.window.clearInterval(this._childrenRouteChangedInterval);
64
- }
65
- const intervalConfig = {setInterval: this.config.window.setInterval.bind(this.config.window), clearInterval: this.config.window.clearInterval.bind(this.config.window)};
66
- this._childrenRouteChangedInterval = debounceTimeIntervalLock(
67
- () => this._routeChangeInvocations === 0,
68
- () => {
69
- this._childrenRouteChangedInterval = null;
70
- this.config.onChildrenRouteChanged?.(re, this)
71
- },
72
- this.config?.childrenConnectedDoneCheckIntervalTime ?? 30,
73
- intervalConfig
74
- // {setInterval: window.setInterval, clearInterval: window.clearInterval}
75
- );
76
-
77
-
78
- // debounceTimeIntervalLock({
79
- // unlockChecker: () => this._routeChangeInvocations === 0,
80
- // unlocked: () => this.config.onChildrenRouteChanged(re, this)
81
- // }, {
82
- // intervalId: this._childrenRouteChangedInterval,
83
- // setIntervalId: (id) => this._childrenRouteChangedInterval = id,
84
- // intervalTime: this.config?.childrenRouteChangedCheckIntervalTime ?? 30
85
- // }, {
86
- // setInterval: window.setInterval,
87
- // clearInterval: window.clearInterval
88
- // });
89
-
90
- try {
91
- // if (this._swc_notified_routes.get(instance) === re) {
92
- // return;
93
- // }
94
- // this._swc_notified_routes.set(instance, re);
95
-
96
- const routeChangeSubscribers = getSubscribeSwcAppRouteChangeWhileConnectedMetadata(instance);
97
- if (routeChangeSubscribers && Array.isArray(routeChangeSubscribers)) {
98
- const ps = routeChangeSubscribers.map(async metadata => {
99
- const methodName = metadata.propertyKey;
100
- let pathPattern = metadata.options?.path as any;
101
- const filter = metadata.options?.filter;
102
-
103
- if (typeof pathPattern === 'function') {
104
- pathPattern = (pathPattern as Function)(instance);
105
- }
106
-
107
- let pathMatched = false;
108
- let pathData: any = null;
109
-
110
- if (!pathPattern) {
111
- pathMatched = true;
112
- pathData = {};
113
- } else if (Array.isArray(pathPattern)) {
114
- for (const pattern of pathPattern) {
115
- const data = SwcUtils.parsePathPattern(pattern, re.path || '/');
116
- if (data !== null) {
117
- pathMatched = true;
118
- pathData = data;
119
- break;
120
- }
121
- }
122
- } else {
123
- pathData = SwcUtils.parsePathPattern(pathPattern, re.path || '/');
124
- pathMatched = pathData !== null;
125
- }
126
-
127
- const filterPassed = !filter || filter(this.router!, instance);
128
-
129
- if (pathMatched && filterPassed && instance[methodName]) {
130
- await instance[methodName]({...re, pathData: pathData});
131
- }
132
- });
133
- await Promise.allSettled(ps);
134
- }
135
- } finally {
136
- if (this.config?.ssr) {
137
- setSSRAttribute(instance);
138
- } else {
139
- removeSSRAttribute(instance);
140
- }
141
- this._routeChangeInvocations = Math.max(0, this._routeChangeInvocations - 1);
142
- }
143
- }
144
-
145
- async _connected(instance: HTMLElement, option?: { noIncrements: boolean }) {
146
- if (instance) {
147
- // console.log('_connected-->', instance.tagName, this._connectedInvocations)
148
-
149
- if (!this.simpleApplication && !this.config) {
150
- this._connected_safari_and_standby.push(instance);
151
- return;
152
- }
153
-
154
- this.config.onConnectedChildBefore?.(instance);
155
- if (!option?.noIncrements) {
156
- this._connectedInvocations++;
157
- }
158
- this._swc_connected_instance.add(instance);
159
-
160
- const connectedSwcAppCallBacks = findAllLifecycleMetadata(instance, ON_CONNECTED_SWC_APP_METADATA_KEY) ?? [];
161
- for (let connectedSwcAppCallBack of connectedSwcAppCallBacks) {
162
- await (instance as any)?._invokeLifecycleMethod?.(connectedSwcAppCallBack);
163
- }
164
-
165
- if (this._childrenConnectedDoneInterval) {
166
- this.config.window.clearInterval(this._childrenConnectedDoneInterval);
167
- }
168
- const intervalConfig = {setInterval: this.config.window.setInterval.bind(this.config.window), clearInterval: this.config.window.clearInterval.bind(this.config.window)};
169
- this._childrenConnectedDoneInterval = debounceTimeIntervalLock(
170
- () => this._connectedInvocations === 0,
171
- () => {
172
- this._childrenConnectedDoneInterval = null;
173
- this.config.onChildrenConnectedDone?.(this)
174
- },
175
- this.config?.childrenConnectedDoneCheckIntervalTime ?? 30,
176
- intervalConfig
177
- );
178
-
179
- // console.log('onChildConnectedDone-->')
180
-
181
- if (this.simpleApplication) {
182
- try {
183
- // await this._invokeOnConnectedSwcAppStarted(instance);
184
-
185
- if (this.router && this._lastRouterEvent) {
186
- await this._invokeRouteChangeSubscribers(instance, this._lastRouterEvent!);
187
- }
188
- } catch (e) {
189
- throw e;
190
- }
191
- }
192
-
193
- if (this.config?.ssr) {
194
- setSSRAttribute(instance);
195
- } else {
196
- removeSSRAttribute(instance);
197
- }
198
- }
199
-
200
-
201
- this.config.onConnectedChildAfter?.(instance);
202
- return this;
203
- }
204
-
205
- async _connectedDone(instance: any) {
206
- try {
207
- // 내부 연결 처리 로직이 있다면 여기에 추가
208
- // console.log('_connectedDone-->', instance, this._connectedInvocations)
209
- } finally {
210
- this._connectedInvocations = Math.max(0, this._connectedInvocations - 1);
211
- }
212
- }
213
-
214
- _disconnected(instance: any) {
215
- // console.log('_disconnected', instance);
216
- if (instance) {
217
- this._swc_connected_instance.delete(instance);
218
- this.config?.onDisconnectedChildAfter?.(instance);
219
- // this._swc_notified_routes.delete(instance);
220
- }
221
- }
222
-
223
- async _handleRouteChange(route: RouterEventType) {
224
- // Save last router event
225
- this._lastRouterEvent = route;
226
- // if (route.triggerPoint === 'end') {
227
- // Iterate through all connected component instances
228
- const swcConnectedInstance = Array.from(this._swc_connected_instance);
229
- // console.log('routing handle-->', swcConnectedInstance)
230
- const ps = swcConnectedInstance.map((instance: any) => this._invokeRouteChangeSubscribers(instance, route));
231
- await Promise.allSettled(ps);
232
- // }
233
- }
234
-
235
- async connect(config?: SwcAttributeConfigType) {
236
- console.log('[SWC] connect')
237
- const swcConfig = {
238
- routeType: 'element',
239
- window: config?.window,
240
- ...config
241
- } as SwcConfigType;
242
-
243
- const mode = config?.connectMode || 'direct';
244
-
245
- if (mode === 'direct' || mode === 'hydrate') {
246
- this.setAttribute('swc-connect-mode', mode);
247
- await this.__swc_engine.connect(swcConfig);
248
- } else if (mode === 'swap' || mode === 'clear-swap') {
249
- if (!this.parentNode) {
250
- // 부모가 없으면 swap을 할 수 없으므로 direct로 폴백합니다.
251
- await this.__swc_engine.connect(swcConfig);
252
- this.setAttribute('swc-connect-mode', 'direct');
253
- } else {
254
- // 1. 현재 SSR 상태(화면)를 그대로 유지한 채로, 백그라운드용 클론을 만듭니다.
255
- const clonedApp = this.cloneNode(mode === 'swap') as any; // clear-swap 모드면 자식 노드를 클론하지 않음(false)
256
- clonedApp.style.display = 'none';
257
-
258
- // 2. DOM에 클론을 몰래 붙입니다.
259
- this.parentNode.insertBefore(clonedApp, this.nextSibling);
260
-
261
- try {
262
- // 3. 클론을 direct 모드로 연결(초기화/렌더링) 시킵니다.
263
- await clonedApp.connect({...(config || {}), connectMode: 'direct'});
264
-
265
- // 4. 클론의 렌더링이 완전히 끝나면, 기존 SSR 화면(this)을 지우고 클론을 보여줍니다.
266
- if (this.parentNode) {
267
- this.parentNode.removeChild(this);
268
- }
269
- clonedApp.style.display = '';
270
- clonedApp.setAttribute('swc-connect-mode', mode);
271
-
272
- // ★ 매우 중요: 교체가 완료되었으므로 기존 앱(this)은 여기서 즉시 종료하여 불필요한 라우터/이벤트 구독을 막아야 합니다!
273
- return;
274
- } catch (e) {
275
- // 실패 시 클론을 지우고 원본을 direct로 강제 연결합니다.
276
- if (clonedApp.parentNode) {
277
- clonedApp.parentNode.removeChild(clonedApp);
278
- }
279
- await this.__swc_engine.connect(swcConfig);
280
- this.setAttribute('swc-connect-mode', 'direct');
281
- }
282
- }
283
- }
284
-
285
- // 사파리일떄 예외이다 is 일때 polyfill되는데 이떄 부모부터 만들어지는게 아니라 자식부터 만들어진다 그래서 부모가 만들어질때 자식들이 이미 만들어져있어서 부모가 자식들을 등록해주는 형태로 강제로 등록해준다
286
- this._connected_safari_and_standby.forEach((instance: any) => {
287
- // console.log('safari!!!!!!!!!!', instance)
288
- this._connected(instance, {noIncrements: true});
289
- // this._swc_connected_instance.add(instance);
290
- });
291
- // Invoke @onConnectedSwcApp lifecycle methods on all connected component instances
292
- // const hostSet = SwcUtils.getHostSet(this as any);
293
-
294
- // const scis = Array.from(this._swc_connected_instance);
295
- // await Promise.allSettled(scis.map(instance => this._invokeOnConnectedSwcAppStarted(instance)));
296
-
297
- // this._swc_connected_instance.forEach((instance: any) => {
298
- // this._invokeOnConnectedSwcAppStarted(instance);
299
- // });
300
-
301
- // Subscribe to router changes after connect
302
- if (!this._routerSubscription && this.router) {
303
- const config = this.__swc_engine.config;
304
-
305
- this._routerSubscription = this.router.observable.subscribe(async (route: RouterEventType) => {
306
- console.log('[SWC] Router event received:', route.triggerPoint);
307
- if (route.triggerPoint === 'end') {
308
- // 1. 모든 기존 연결된 자식들에게 라우트 이벤트 알림
309
- await this._handleRouteChange(route);
310
- }
311
- });
312
-
313
- // Navigate to initial path if specified
314
- if (config?.path) {
315
- console.log('[DEBUG] Navigating to initial path:', config.path);
316
- // this.router.go(config.path);
317
- }
318
- }
319
- }
320
-
321
- // async _invokeOnConnectedSwcAppStarted(instance: any) {
322
- // this._swcAppStartedInvocations++;
323
- // try {
324
- // // 이미 시작된 인스턴스라면 스킵 (비동기 병렬 중복 호출 방지)
325
- // if (this._swc_started_instances.has(instance)) return;
326
- // this._swc_started_instances.add(instance);
327
- //
328
- // const startedMethods = findAllLifecycleMetadata(instance, ON_CONNECTED_SWC_APP_METADATA_KEY);
329
- // if (startedMethods) {
330
- // const ps = startedMethods.map(m => instance._invokeLifecycleMethod(m));
331
- // await Promise.allSettled(ps);
332
- // }
333
- // } finally {
334
- // this._swcAppStartedInvocations--;
335
- // }
336
- // }
337
-
338
- connectedCallback() {
339
- // this.innerHTML='';
340
- // @ts-ignore
341
- if (super.connectedCallback) super.connectedCallback();
342
-
343
- const configStr = this.getAttribute('swc-get-application-config');
344
- const win = this.ownerDocument?.defaultView || window;
345
- if (configStr && win) {
346
- try {
347
- // console.log('vvvvvvvvvvvvvvvv->>>>', SwcUtils.findNearestSwcHost(this.host), SwcUtils.getHostSet(this.host))
348
- const userConfig = FunctionUtils.executeReturn({
349
- script: configStr,
350
- context: this,
351
- args: SwcUtils.getHelperAndHostSet(win, this)
352
- });
353
- this.connect(userConfig);
354
- } catch (e) {
355
- console.error('[SWC-APP-ENGINE] Script execution failed:', e);
356
- }
357
- }
358
-
359
- // 자기자신은 apphost로 포함이 도지않으니(부모) 내자신은 self로 넣는다
360
- this._connected(this);
361
- this.__swc_engine?.config?.onConnected?.(this);
362
- this._connectedDone(this);
363
- }
364
-
365
- disconnectedCallback() {
366
- // @ts-ignore
367
- if (super.disconnectedCallback) super.disconnectedCallback();
368
- this._routerSubscription?.unsubscribe();
369
- this._routerSubscription = undefined;
370
- this._lastRouterEvent = undefined;
371
- this.__swc_engine.disconnect();
372
- this.__swc_engine?.config?.onDisconnected?.(this);
373
- this._disconnected(this);
374
- }
375
-
376
- async routing(path: string) {
377
- await this.router?.go(path);
378
- }
379
-
380
- back(): void {
381
- this.router?.back();
382
- }
383
-
384
- forward(): void {
385
- this.router?.forward();
386
- }
387
-
388
- reload(): void {
389
- this.router?.reload();
390
- }
391
-
392
- // async completedSwcApp() {
393
- // const ss = Array.from(this._swc_connected_instance).flatMap((it) => {
394
- // const ms = findAllLifecycleMetadata(it, ON_CONNECTED_COMPLETED_SWC_APP_METADATA_KEY) ?? [];
395
- // return ms.map((sit) => it[sit](this));
396
- // });
397
- // await Promise.allSettled(ss);
398
- // }
399
-
400
- publishMessage(message: SwcAppMessage): void {
401
- // 메시지를 모든 connected component instances로 분배
402
- this._swc_connected_instance.forEach((instance: any) => {
403
- this._invokeMessageSubscribers(instance, message);
404
- });
405
- }
406
-
407
- _invokeMessageSubscribers(instance: any, message: SwcAppMessage) {
408
- const messageSubscribers = getSubscribeSwcAppMessageWhileConnectedMetadata(instance);
409
- if (messageSubscribers && Array.isArray(messageSubscribers)) {
410
- messageSubscribers.forEach(metadata => {
411
- const methodName = metadata.propertyKey;
412
- const messageType = metadata.messageType;
413
- const filter = metadata.filter;
414
-
415
- // 메시지 타입 확인
416
- let typeMatched = false;
417
- if (!messageType) {
418
- // messageType이 없으면 모든 메시지 매칭 (와일드카드)
419
- typeMatched = true;
420
- } else if (message.type === messageType) {
421
- typeMatched = true;
422
- }
423
-
424
- // 필터 적용
425
- let filterMatched = true;
426
- if (filter && typeMatched) {
427
- filterMatched = filter(message, instance);
428
- }
429
-
430
- // 조건을 만족하면 메서드 호출
431
- if (typeMatched && filterMatched && typeof instance[methodName] === 'function') {
432
- instance[methodName](message);
433
- }
434
- });
435
- }
436
- }
437
- };
438
-
439
-
440
- return w.customElements.whenDefined(tagName);
441
-
442
-
443
- }
19
+ @elementDefine(swcAppTagName, {window: w})
20
+ class SwcAppImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
21
+ // SwcAppMixin에서 모든 기능을 상속받음
22
+ }
23
+
24
+ return w.customElements.whenDefined(swcAppTagName);
25
+ };
26
+
27
+ // ============================================
28
+ // SwcAppBody (HTMLBodyElement)
29
+ // ============================================
30
+ export const swcAppBodyTagName = 'swc-app-body';
31
+
32
+ export const SwcAppBody = (w: Window, data?: CreateElementConfig) => {
33
+ return createElement<SwcAppInterface & HTMLBodyElement>(w, swcAppBodyTagName, data);
34
+ };
35
+
36
+ export const defineSwcAppBody = async (w: Window) => {
37
+ const existing = w.customElements.get(swcAppBodyTagName);
38
+ if (existing) return existing;
39
+
40
+ @elementDefine(swcAppBodyTagName, {window: w, extends: 'body'})
41
+ class SwcAppBodyImpl extends SwcAppMixin(w.HTMLBodyElement) implements SwcAppInterface {
42
+ // SwcAppMixin에서 모든 기능을 상속받음
43
+ }
44
+
45
+ return w.customElements.whenDefined(swcAppBodyTagName);
46
+ };
47
+
48
+ // ============================================
49
+ // SwcAppDiv (HTMLDivElement)
50
+ // ============================================
51
+ export const swcAppDivTagName = 'swc-app-div';
52
+
53
+ export const SwcAppDiv = (w: Window, data?: CreateElementConfig) => {
54
+ return createElement<SwcAppInterface & HTMLDivElement>(w, swcAppDivTagName, data);
55
+ };
56
+
57
+ export const defineSwcAppDiv = async (w: Window) => {
58
+ const existing = w.customElements.get(swcAppDivTagName);
59
+ if (existing) return existing;
60
+
61
+ @elementDefine(swcAppDivTagName, {window: w, extends: 'div'})
62
+ class SwcAppDivImpl extends SwcAppMixin(w.HTMLDivElement) implements SwcAppInterface {
63
+ // SwcAppMixin에서 모든 기능을 상속받음
64
+ }
65
+
66
+ return w.customElements.whenDefined(swcAppDivTagName);
67
+ };
68
+
69
+ // ============================================
70
+ // SwcAppSection (HTMLElement)
71
+ // ============================================
72
+ export const swcAppSectionTagName = 'swc-app-section';
73
+
74
+ export const SwcAppSection = (w: Window, data?: CreateElementConfig) => {
75
+ return createElement<SwcAppInterface & HTMLElement>(w, swcAppSectionTagName, data);
76
+ };
77
+
78
+ export const defineSwcAppSection = async (w: Window) => {
79
+ const existing = w.customElements.get(swcAppSectionTagName);
80
+ if (existing) return existing;
81
+
82
+ @elementDefine(swcAppSectionTagName, {window: w, extends: 'section'})
83
+ class SwcAppSectionImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
84
+ // SwcAppMixin에서 모든 기능을 상속받음
85
+ }
86
+
87
+ return w.customElements.whenDefined(swcAppSectionTagName);
88
+ };
89
+
90
+ // ============================================
91
+ // SwcAppMain (HTMLElement)
92
+ // ============================================
93
+ export const swcAppMainTagName = 'swc-app-main';
94
+
95
+ export const SwcAppMain = (w: Window, data?: CreateElementConfig) => {
96
+ return createElement<SwcAppInterface & HTMLElement>(w, swcAppMainTagName, data);
97
+ };
98
+
99
+ export const defineSwcAppMain = async (w: Window) => {
100
+ const existing = w.customElements.get(swcAppMainTagName);
101
+ if (existing) return existing;
102
+
103
+ @elementDefine(swcAppMainTagName, {window: w, extends: 'main'})
104
+ class SwcAppMainImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
105
+ // SwcAppMixin에서 모든 기능을 상속받음
106
+ }
107
+
108
+ return w.customElements.whenDefined(swcAppMainTagName);
109
+ };
110
+
111
+ // ============================================
112
+ // SwcAppArticle (HTMLElement)
113
+ // ============================================
114
+ export const swcAppArticleTagName = 'swc-app-article';
115
+
116
+ export const SwcAppArticle = (w: Window, data?: CreateElementConfig) => {
117
+ return createElement<SwcAppInterface & HTMLElement>(w, swcAppArticleTagName, data);
118
+ };
119
+
120
+ export const defineSwcAppArticle = async (w: Window) => {
121
+ const existing = w.customElements.get(swcAppArticleTagName);
122
+ if (existing) return existing;
123
+
124
+ @elementDefine(swcAppArticleTagName, {window: w, extends: 'article'})
125
+ class SwcAppArticleImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
126
+ // SwcAppMixin에서 모든 기능을 상속받음
127
+ }
128
+
129
+ return w.customElements.whenDefined(swcAppArticleTagName);
130
+ };
131
+
132
+ // ============================================
133
+ // SwcAppHeader (HTMLElement)
134
+ // ============================================
135
+ export const swcAppHeaderTagName = 'swc-app-header';
136
+
137
+ export const SwcAppHeader = (w: Window, data?: CreateElementConfig) => {
138
+ return createElement<SwcAppInterface & HTMLElement>(w, swcAppHeaderTagName, data);
139
+ };
140
+
141
+ export const defineSwcAppHeader = async (w: Window) => {
142
+ const existing = w.customElements.get(swcAppHeaderTagName);
143
+ if (existing) return existing;
144
+
145
+ @elementDefine(swcAppHeaderTagName, {window: w, extends: 'header'})
146
+ class SwcAppHeaderImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
147
+ // SwcAppMixin에서 모든 기능을 상속받음
148
+ }
149
+
150
+ return w.customElements.whenDefined(swcAppHeaderTagName);
151
+ };
152
+
153
+ // ============================================
154
+ // SwcAppFooter (HTMLElement)
155
+ // ============================================
156
+ export const swcAppFooterTagName = 'swc-app-footer';
157
+
158
+ export const SwcAppFooter = (w: Window, data?: CreateElementConfig) => {
159
+ return createElement<SwcAppInterface & HTMLElement>(w, swcAppFooterTagName, data);
160
+ };
161
+
162
+ export const defineSwcAppFooter = async (w: Window) => {
163
+ const existing = w.customElements.get(swcAppFooterTagName);
164
+ if (existing) return existing;
165
+
166
+ @elementDefine(swcAppFooterTagName, {window: w, extends: 'footer'})
167
+ class SwcAppFooterImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
168
+ // SwcAppMixin에서 모든 기능을 상속받음
169
+ }
170
+
171
+ return w.customElements.whenDefined(swcAppFooterTagName);
172
+ };
173
+
174
+ // ============================================
175
+ // SwcAppNav (HTMLElement)
176
+ // ============================================
177
+ export const swcAppNavTagName = 'swc-app-nav';
178
+
179
+ export const SwcAppNav = (w: Window, data?: CreateElementConfig) => {
180
+ return createElement<SwcAppInterface & HTMLElement>(w, swcAppNavTagName, data);
181
+ };
182
+
183
+ export const defineSwcAppNav = async (w: Window) => {
184
+ const existing = w.customElements.get(swcAppNavTagName);
185
+ if (existing) return existing;
186
+
187
+ @elementDefine(swcAppNavTagName, {window: w, extends: 'nav'})
188
+ class SwcAppNavImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
189
+ // SwcAppMixin에서 모든 기능을 상속받음
190
+ }
191
+
192
+ return w.customElements.whenDefined(swcAppNavTagName);
193
+ };
194
+
195
+ // ============================================
196
+ // SwcAppAside (HTMLElement)
197
+ // ============================================
198
+ export const swcAppAsideTagName = 'swc-app-aside';
199
+
200
+ export const SwcAppAside = (w: Window, data?: CreateElementConfig) => {
201
+ return createElement<SwcAppInterface & HTMLElement>(w, swcAppAsideTagName, data);
202
+ };
203
+
204
+ export const defineSwcAppAside = async (w: Window) => {
205
+ const existing = w.customElements.get(swcAppAsideTagName);
206
+ if (existing) return existing;
207
+
208
+ @elementDefine(swcAppAsideTagName, {window: w, extends: 'aside'})
209
+ class SwcAppAsideImpl extends SwcAppMixin(w.HTMLElement) implements SwcAppInterface {
210
+ // SwcAppMixin에서 모든 기능을 상속받음
211
+ }
212
+
213
+ return w.customElements.whenDefined(swcAppAsideTagName);
214
+ };
215
+
216
+ // ============================================
217
+ // Factory Array
218
+ // ============================================
219
+ export const swcAppFactories = [
220
+ defineSwcApp,
221
+ defineSwcAppBody,
222
+ defineSwcAppDiv,
223
+ defineSwcAppSection,
224
+ defineSwcAppMain,
225
+ defineSwcAppArticle,
226
+ defineSwcAppHeader,
227
+ defineSwcAppFooter,
228
+ defineSwcAppNav,
229
+ defineSwcAppAside
230
+ ];
231
+
232
+ // ============================================
233
+ // Define All SwcApp Elements
234
+ // ============================================
235
+ export const defineSwcAppAll = async (w: Window) => {
236
+ await Promise.all(swcAppFactories.map(factory => factory(w)));
237
+ return w.customElements.whenDefined(swcAppTagName);
238
+ };
239
+
240
+ // Default export for backward compatibility
241
+ export default defineSwcAppAll;