wu-framework 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +773 -366
- package/package.json +34 -9
- package/src/adapters/angular.d.ts +154 -0
- package/src/adapters/angular.js +642 -0
- package/src/adapters/index.js +157 -0
- package/src/adapters/lit.d.ts +120 -0
- package/src/adapters/lit.js +726 -0
- package/src/adapters/preact.d.ts +108 -0
- package/src/adapters/preact.js +665 -0
- package/src/adapters/react.d.ts +212 -0
- package/src/adapters/react.js +513 -0
- package/src/adapters/solid.d.ts +101 -0
- package/src/adapters/solid.js +591 -0
- package/src/adapters/svelte.d.ts +166 -0
- package/src/adapters/svelte.js +803 -0
- package/src/adapters/vanilla.d.ts +179 -0
- package/src/adapters/vanilla.js +791 -0
- package/src/adapters/vue.d.ts +299 -0
- package/src/adapters/vue.js +570 -0
- package/src/core/wu-cache.js +87 -15
- package/src/core/wu-event-bus.js +147 -58
- package/src/core/wu-manifest.js +139 -8
- package/src/core/wu-plugin.js +201 -71
- package/src/index.js +1 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wu Framework React Adapter - TypeScript Definitions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { ComponentType, ReactElement, CSSProperties, RefObject } from 'react';
|
|
6
|
+
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// Core Types
|
|
9
|
+
// ============================================================================
|
|
10
|
+
|
|
11
|
+
export interface WuInstance {
|
|
12
|
+
init: (config: WuConfig) => Promise<void>;
|
|
13
|
+
mount: (appName: string, container: string) => Promise<void>;
|
|
14
|
+
unmount: (appName: string) => Promise<void>;
|
|
15
|
+
define: (appName: string, lifecycle: WuLifecycle) => void;
|
|
16
|
+
app: (name: string, config: WuAppConfig) => WuApp;
|
|
17
|
+
eventBus: WuEventBus;
|
|
18
|
+
store: WuStore;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface WuConfig {
|
|
22
|
+
apps: Array<{
|
|
23
|
+
name: string;
|
|
24
|
+
url: string;
|
|
25
|
+
strategy?: 'lazy' | 'eager' | 'preload' | 'idle';
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface WuLifecycle {
|
|
30
|
+
mount: (container: HTMLElement) => void | Promise<void>;
|
|
31
|
+
unmount: (container?: HTMLElement) => void | Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface WuAppConfig {
|
|
35
|
+
url: string;
|
|
36
|
+
container: string;
|
|
37
|
+
autoInit?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface WuApp {
|
|
41
|
+
mount: (container?: string) => Promise<void>;
|
|
42
|
+
unmount: () => Promise<void>;
|
|
43
|
+
remount: () => Promise<void>;
|
|
44
|
+
reload: () => Promise<void>;
|
|
45
|
+
destroy: () => Promise<void>;
|
|
46
|
+
isMounted: boolean;
|
|
47
|
+
info: WuAppInfo;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface WuAppInfo {
|
|
51
|
+
name: string;
|
|
52
|
+
url: string;
|
|
53
|
+
mounted: boolean;
|
|
54
|
+
status: 'stable' | 'refreshed' | 'unstable';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface WuEventBus {
|
|
58
|
+
emit: (event: string, data?: any, options?: EmitOptions) => void;
|
|
59
|
+
on: (event: string, callback: EventCallback) => () => void;
|
|
60
|
+
once: (event: string, callback: EventCallback) => () => void;
|
|
61
|
+
off: (event: string, callback?: EventCallback) => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface WuStore {
|
|
65
|
+
get: (path?: string) => any;
|
|
66
|
+
set: (path: string, value: any) => number;
|
|
67
|
+
on: (pattern: string, callback: StoreCallback) => () => void;
|
|
68
|
+
batch: (updates: Record<string, any>) => number[];
|
|
69
|
+
clear: () => void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type EventCallback = (event: WuEvent) => void;
|
|
73
|
+
export type StoreCallback = (change: { path: string; value: any }) => void;
|
|
74
|
+
|
|
75
|
+
export interface WuEvent {
|
|
76
|
+
name: string;
|
|
77
|
+
data: any;
|
|
78
|
+
timestamp: number;
|
|
79
|
+
source?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface EmitOptions {
|
|
83
|
+
appName?: string;
|
|
84
|
+
timestamp?: number;
|
|
85
|
+
meta?: Record<string, any>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ============================================================================
|
|
89
|
+
// React Adapter Types
|
|
90
|
+
// ============================================================================
|
|
91
|
+
|
|
92
|
+
export interface RegisterOptions {
|
|
93
|
+
/** Wrap component in StrictMode (default: true) */
|
|
94
|
+
strictMode?: boolean;
|
|
95
|
+
/** Initial props for the component */
|
|
96
|
+
props?: Record<string, any>;
|
|
97
|
+
/** Callback after mounting */
|
|
98
|
+
onMount?: (container: HTMLElement) => void;
|
|
99
|
+
/** Callback before unmounting */
|
|
100
|
+
onUnmount?: (container: HTMLElement) => void;
|
|
101
|
+
/** Allow standalone execution (default: true) */
|
|
102
|
+
standalone?: boolean;
|
|
103
|
+
/** Selector for standalone mode (default: '#root') */
|
|
104
|
+
standaloneContainer?: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Register a React component as a microfrontend
|
|
109
|
+
*/
|
|
110
|
+
export function register<P = {}>(
|
|
111
|
+
appName: string,
|
|
112
|
+
Component: ComponentType<P>,
|
|
113
|
+
options?: RegisterOptions
|
|
114
|
+
): Promise<boolean>;
|
|
115
|
+
|
|
116
|
+
// ============================================================================
|
|
117
|
+
// WuSlot Component Types
|
|
118
|
+
// ============================================================================
|
|
119
|
+
|
|
120
|
+
export interface WuSlotProps {
|
|
121
|
+
/** Display name for the microfrontend */
|
|
122
|
+
name: string;
|
|
123
|
+
/** URL where the microfrontend is hosted */
|
|
124
|
+
url: string;
|
|
125
|
+
/** App name from wu.json (defaults to name if not provided) */
|
|
126
|
+
appName?: string;
|
|
127
|
+
/** Custom fallback while loading */
|
|
128
|
+
fallback?: ReactElement | null;
|
|
129
|
+
/** Callback when microfrontend loads successfully */
|
|
130
|
+
onLoad?: (info: { name: string; url: string }) => void;
|
|
131
|
+
/** Callback when loading fails */
|
|
132
|
+
onError?: (error: Error) => void;
|
|
133
|
+
/** Callback when microfrontend mounts */
|
|
134
|
+
onMount?: (info: { name: string; container: HTMLElement }) => void;
|
|
135
|
+
/** Callback when microfrontend unmounts */
|
|
136
|
+
onUnmount?: (info: { name: string }) => void;
|
|
137
|
+
/** Additional CSS class */
|
|
138
|
+
className?: string;
|
|
139
|
+
/** Inline styles */
|
|
140
|
+
style?: CSSProperties;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Create a WuSlot component for loading microfrontends
|
|
145
|
+
* @param React - React instance
|
|
146
|
+
*/
|
|
147
|
+
export function createWuSlot(React: any): ComponentType<WuSlotProps>;
|
|
148
|
+
|
|
149
|
+
// ============================================================================
|
|
150
|
+
// Hooks Types
|
|
151
|
+
// ============================================================================
|
|
152
|
+
|
|
153
|
+
export interface UseWuEventsReturn {
|
|
154
|
+
/** Emit an event to the event bus */
|
|
155
|
+
emit: (event: string, data?: any, options?: EmitOptions) => void;
|
|
156
|
+
/** Subscribe to an event */
|
|
157
|
+
on: (event: string, callback: EventCallback) => () => void;
|
|
158
|
+
/** Subscribe to an event once */
|
|
159
|
+
once: (event: string, callback: EventCallback) => () => void;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Create the useWuEvents hook
|
|
164
|
+
* @param React - React instance
|
|
165
|
+
*/
|
|
166
|
+
export function createUseWuEvents(React: any): () => UseWuEventsReturn;
|
|
167
|
+
|
|
168
|
+
export interface UseWuStoreReturn<T = any> {
|
|
169
|
+
/** Current state value (reactive) */
|
|
170
|
+
state: T | null;
|
|
171
|
+
/** Set a value in the store */
|
|
172
|
+
setState: (path: string, value: any) => void;
|
|
173
|
+
/** Get a value from the store */
|
|
174
|
+
getState: (path?: string) => any;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Create the useWuStore hook
|
|
179
|
+
* @param React - React instance
|
|
180
|
+
*/
|
|
181
|
+
export function createUseWuStore(React: any): <T = any>(namespace?: string) => UseWuStoreReturn<T>;
|
|
182
|
+
|
|
183
|
+
// ============================================================================
|
|
184
|
+
// Utility Functions
|
|
185
|
+
// ============================================================================
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Get the Wu Framework instance
|
|
189
|
+
*/
|
|
190
|
+
export function getWuInstance(): WuInstance | null;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Wait for Wu Framework to be available
|
|
194
|
+
* @param timeout - Maximum time to wait in ms (default: 5000)
|
|
195
|
+
*/
|
|
196
|
+
export function waitForWu(timeout?: number): Promise<WuInstance>;
|
|
197
|
+
|
|
198
|
+
// ============================================================================
|
|
199
|
+
// Main Export
|
|
200
|
+
// ============================================================================
|
|
201
|
+
|
|
202
|
+
export interface WuReactAdapter {
|
|
203
|
+
register: typeof register;
|
|
204
|
+
createWuSlot: typeof createWuSlot;
|
|
205
|
+
createUseWuEvents: typeof createUseWuEvents;
|
|
206
|
+
createUseWuStore: typeof createUseWuStore;
|
|
207
|
+
getWuInstance: typeof getWuInstance;
|
|
208
|
+
waitForWu: typeof waitForWu;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export const wuReact: WuReactAdapter;
|
|
212
|
+
export default wuReact;
|
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🚀 WU-FRAMEWORK REACT ADAPTER
|
|
3
|
+
*
|
|
4
|
+
* Simplifica la integración de React con Wu Framework.
|
|
5
|
+
* Convierte componentes React en microfrontends con UNA línea de código.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Microfrontend (main.tsx)
|
|
9
|
+
* import { wuReact } from 'wu-framework/adapters/react';
|
|
10
|
+
* import App from './App';
|
|
11
|
+
*
|
|
12
|
+
* wuReact.register('my-app', App);
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Shell (cargar microfrontend)
|
|
16
|
+
* import { WuSlot } from 'wu-framework/adapters/react';
|
|
17
|
+
*
|
|
18
|
+
* <WuSlot name="my-app" url="http://localhost:3001" />
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// Estado global del adapter
|
|
22
|
+
const adapterState = {
|
|
23
|
+
roots: new Map(),
|
|
24
|
+
React: null,
|
|
25
|
+
ReactDOM: null,
|
|
26
|
+
createRoot: null,
|
|
27
|
+
initialized: false
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Detecta y obtiene React del contexto global o lo importa
|
|
32
|
+
*/
|
|
33
|
+
async function ensureReact() {
|
|
34
|
+
if (adapterState.initialized) return true;
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
// Intentar obtener de window (común en microfrontends)
|
|
38
|
+
if (typeof window !== 'undefined') {
|
|
39
|
+
if (window.React && window.ReactDOM) {
|
|
40
|
+
adapterState.React = window.React;
|
|
41
|
+
adapterState.ReactDOM = window.ReactDOM;
|
|
42
|
+
adapterState.createRoot = window.ReactDOM.createRoot;
|
|
43
|
+
adapterState.initialized = true;
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Intentar import dinámico
|
|
49
|
+
const [React, ReactDOM] = await Promise.all([
|
|
50
|
+
import('react'),
|
|
51
|
+
import('react-dom/client')
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
adapterState.React = React.default || React;
|
|
55
|
+
adapterState.ReactDOM = ReactDOM;
|
|
56
|
+
adapterState.createRoot = ReactDOM.createRoot;
|
|
57
|
+
adapterState.initialized = true;
|
|
58
|
+
return true;
|
|
59
|
+
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error('[WuReact] Failed to load React:', error);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Obtiene la instancia de Wu Framework
|
|
68
|
+
*/
|
|
69
|
+
function getWuInstance() {
|
|
70
|
+
if (typeof window === 'undefined') return null;
|
|
71
|
+
|
|
72
|
+
return window.wu
|
|
73
|
+
|| window.parent?.wu
|
|
74
|
+
|| window.top?.wu
|
|
75
|
+
|| null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Espera a que Wu Framework esté disponible (sin polling agresivo)
|
|
80
|
+
*/
|
|
81
|
+
function waitForWu(timeout = 5000) {
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
// Check inmediato
|
|
84
|
+
const wu = getWuInstance();
|
|
85
|
+
if (wu) {
|
|
86
|
+
resolve(wu);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Usar MutationObserver + evento en lugar de polling
|
|
91
|
+
const startTime = Date.now();
|
|
92
|
+
|
|
93
|
+
// Escuchar evento de Wu Framework
|
|
94
|
+
const handleWuReady = (event) => {
|
|
95
|
+
cleanup();
|
|
96
|
+
resolve(getWuInstance());
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
window.addEventListener('wu:ready', handleWuReady);
|
|
100
|
+
window.addEventListener('wu:app:ready', handleWuReady);
|
|
101
|
+
|
|
102
|
+
// Fallback con polling conservador (cada 200ms, no 100ms)
|
|
103
|
+
const checkInterval = setInterval(() => {
|
|
104
|
+
const wu = getWuInstance();
|
|
105
|
+
if (wu) {
|
|
106
|
+
cleanup();
|
|
107
|
+
resolve(wu);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (Date.now() - startTime > timeout) {
|
|
112
|
+
cleanup();
|
|
113
|
+
reject(new Error(`Wu Framework not found after ${timeout}ms`));
|
|
114
|
+
}
|
|
115
|
+
}, 200);
|
|
116
|
+
|
|
117
|
+
function cleanup() {
|
|
118
|
+
clearInterval(checkInterval);
|
|
119
|
+
window.removeEventListener('wu:ready', handleWuReady);
|
|
120
|
+
window.removeEventListener('wu:app:ready', handleWuReady);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Registra un componente React como microfrontend
|
|
127
|
+
*
|
|
128
|
+
* @param {string} appName - Nombre único del microfrontend (debe coincidir con wu.json)
|
|
129
|
+
* @param {React.ComponentType} Component - Componente React principal
|
|
130
|
+
* @param {Object} options - Opciones adicionales
|
|
131
|
+
* @param {boolean} options.strictMode - Envolver en StrictMode (default: true)
|
|
132
|
+
* @param {Object} options.props - Props iniciales para el componente
|
|
133
|
+
* @param {Function} options.onMount - Callback después de montar
|
|
134
|
+
* @param {Function} options.onUnmount - Callback antes de desmontar
|
|
135
|
+
* @param {boolean} options.standalone - Permitir ejecución standalone (default: true)
|
|
136
|
+
* @param {string} options.standaloneContainer - Selector para modo standalone (default: '#root')
|
|
137
|
+
*/
|
|
138
|
+
async function register(appName, Component, options = {}) {
|
|
139
|
+
const {
|
|
140
|
+
strictMode = true,
|
|
141
|
+
props = {},
|
|
142
|
+
onMount = null,
|
|
143
|
+
onUnmount = null,
|
|
144
|
+
standalone = true,
|
|
145
|
+
standaloneContainer = '#root'
|
|
146
|
+
} = options;
|
|
147
|
+
|
|
148
|
+
// Asegurar que React está disponible
|
|
149
|
+
const hasReact = await ensureReact();
|
|
150
|
+
if (!hasReact) {
|
|
151
|
+
console.error(`[WuReact] Cannot register ${appName}: React not available`);
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const { React, createRoot } = adapterState;
|
|
156
|
+
|
|
157
|
+
// Función de mount interna
|
|
158
|
+
const mountApp = (container) => {
|
|
159
|
+
if (!container) {
|
|
160
|
+
console.error(`[WuReact] Mount failed for ${appName}: container is null`);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Evitar doble mount
|
|
165
|
+
if (adapterState.roots.has(appName)) {
|
|
166
|
+
console.warn(`[WuReact] ${appName} already mounted, unmounting first`);
|
|
167
|
+
unmountApp();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
const root = createRoot(container);
|
|
172
|
+
|
|
173
|
+
// Crear elemento con o sin StrictMode
|
|
174
|
+
let element = React.createElement(Component, props);
|
|
175
|
+
if (strictMode && React.StrictMode) {
|
|
176
|
+
element = React.createElement(React.StrictMode, null, element);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
root.render(element);
|
|
180
|
+
adapterState.roots.set(appName, { root, container });
|
|
181
|
+
|
|
182
|
+
console.log(`[WuReact] ✅ ${appName} mounted successfully`);
|
|
183
|
+
|
|
184
|
+
if (onMount) {
|
|
185
|
+
onMount(container);
|
|
186
|
+
}
|
|
187
|
+
} catch (error) {
|
|
188
|
+
console.error(`[WuReact] Mount error for ${appName}:`, error);
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// Función de unmount interna
|
|
194
|
+
const unmountApp = (container) => {
|
|
195
|
+
const instance = adapterState.roots.get(appName);
|
|
196
|
+
|
|
197
|
+
if (instance) {
|
|
198
|
+
try {
|
|
199
|
+
if (onUnmount) {
|
|
200
|
+
onUnmount(instance.container);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
instance.root.unmount();
|
|
204
|
+
adapterState.roots.delete(appName);
|
|
205
|
+
|
|
206
|
+
console.log(`[WuReact] ✅ ${appName} unmounted successfully`);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
console.error(`[WuReact] Unmount error for ${appName}:`, error);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Limpiar container si se proporciona
|
|
213
|
+
if (container) {
|
|
214
|
+
container.innerHTML = '';
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Intentar registrar con Wu Framework
|
|
219
|
+
try {
|
|
220
|
+
const wu = await waitForWu(3000);
|
|
221
|
+
|
|
222
|
+
wu.define(appName, {
|
|
223
|
+
mount: mountApp,
|
|
224
|
+
unmount: unmountApp
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
console.log(`[WuReact] ✅ ${appName} registered with Wu Framework`);
|
|
228
|
+
return true;
|
|
229
|
+
|
|
230
|
+
} catch (error) {
|
|
231
|
+
// Wu no disponible
|
|
232
|
+
console.warn(`[WuReact] Wu Framework not available for ${appName}`);
|
|
233
|
+
|
|
234
|
+
// Modo standalone si está habilitado
|
|
235
|
+
if (standalone) {
|
|
236
|
+
const containerElement = document.querySelector(standaloneContainer);
|
|
237
|
+
|
|
238
|
+
if (containerElement) {
|
|
239
|
+
console.log(`[WuReact] Running ${appName} in standalone mode`);
|
|
240
|
+
mountApp(containerElement);
|
|
241
|
+
return true;
|
|
242
|
+
} else {
|
|
243
|
+
console.warn(`[WuReact] Standalone container ${standaloneContainer} not found`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Crea un componente React para cargar microfrontends (para el Shell)
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* import { createWuSlot } from 'wu-framework/adapters/react';
|
|
256
|
+
* const WuSlot = createWuSlot(React);
|
|
257
|
+
*
|
|
258
|
+
* <WuSlot name="my-app" url="http://localhost:3001" />
|
|
259
|
+
*/
|
|
260
|
+
function createWuSlot(React) {
|
|
261
|
+
const { useState, useEffect, useRef, useCallback } = React;
|
|
262
|
+
|
|
263
|
+
return function WuSlot({
|
|
264
|
+
name,
|
|
265
|
+
url,
|
|
266
|
+
appName = null,
|
|
267
|
+
fallback = null,
|
|
268
|
+
onLoad = null,
|
|
269
|
+
onError = null,
|
|
270
|
+
onMount = null,
|
|
271
|
+
onUnmount = null,
|
|
272
|
+
className = '',
|
|
273
|
+
style = {},
|
|
274
|
+
...props
|
|
275
|
+
}) {
|
|
276
|
+
const containerRef = useRef(null);
|
|
277
|
+
const appInstanceRef = useRef(null);
|
|
278
|
+
const [loading, setLoading] = useState(true);
|
|
279
|
+
const [error, setError] = useState(null);
|
|
280
|
+
|
|
281
|
+
const actualAppName = appName || name;
|
|
282
|
+
|
|
283
|
+
const mountMicrofrontend = useCallback(async () => {
|
|
284
|
+
if (!containerRef.current) return;
|
|
285
|
+
|
|
286
|
+
try {
|
|
287
|
+
setLoading(true);
|
|
288
|
+
setError(null);
|
|
289
|
+
|
|
290
|
+
const wu = getWuInstance();
|
|
291
|
+
if (!wu) {
|
|
292
|
+
throw new Error('Wu Framework not initialized');
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Crear container interno
|
|
296
|
+
const containerId = `wu-slot-${actualAppName}-${Date.now()}`;
|
|
297
|
+
const innerContainer = document.createElement('div');
|
|
298
|
+
innerContainer.id = containerId;
|
|
299
|
+
innerContainer.style.width = '100%';
|
|
300
|
+
innerContainer.style.height = '100%';
|
|
301
|
+
containerRef.current.innerHTML = '';
|
|
302
|
+
containerRef.current.appendChild(innerContainer);
|
|
303
|
+
|
|
304
|
+
// Crear y montar la app
|
|
305
|
+
const app = wu.app(actualAppName, {
|
|
306
|
+
url,
|
|
307
|
+
container: `#${containerId}`,
|
|
308
|
+
autoInit: true
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
appInstanceRef.current = app;
|
|
312
|
+
await app.mount();
|
|
313
|
+
|
|
314
|
+
setLoading(false);
|
|
315
|
+
|
|
316
|
+
if (onLoad) onLoad({ name: actualAppName, url });
|
|
317
|
+
if (onMount) onMount({ name: actualAppName, container: innerContainer });
|
|
318
|
+
|
|
319
|
+
} catch (err) {
|
|
320
|
+
console.error(`[WuSlot] Error loading ${actualAppName}:`, err);
|
|
321
|
+
setError(err.message || 'Failed to load microfrontend');
|
|
322
|
+
setLoading(false);
|
|
323
|
+
|
|
324
|
+
if (onError) onError(err);
|
|
325
|
+
}
|
|
326
|
+
}, [actualAppName, url, onLoad, onError, onMount]);
|
|
327
|
+
|
|
328
|
+
useEffect(() => {
|
|
329
|
+
mountMicrofrontend();
|
|
330
|
+
|
|
331
|
+
return () => {
|
|
332
|
+
if (appInstanceRef.current) {
|
|
333
|
+
if (onUnmount) onUnmount({ name: actualAppName });
|
|
334
|
+
|
|
335
|
+
appInstanceRef.current.unmount().catch(err => {
|
|
336
|
+
console.warn(`[WuSlot] Error unmounting ${actualAppName}:`, err);
|
|
337
|
+
});
|
|
338
|
+
appInstanceRef.current = null;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}, [mountMicrofrontend, actualAppName, onUnmount]);
|
|
342
|
+
|
|
343
|
+
// Render de error
|
|
344
|
+
if (error) {
|
|
345
|
+
return React.createElement('div', {
|
|
346
|
+
className: `wu-slot wu-slot-error ${className}`,
|
|
347
|
+
style: {
|
|
348
|
+
padding: '1rem',
|
|
349
|
+
border: '1px solid #f5c6cb',
|
|
350
|
+
borderRadius: '4px',
|
|
351
|
+
backgroundColor: '#f8d7da',
|
|
352
|
+
color: '#721c24',
|
|
353
|
+
...style
|
|
354
|
+
},
|
|
355
|
+
...props
|
|
356
|
+
}, [
|
|
357
|
+
React.createElement('strong', { key: 'title' }, `Error loading ${name}`),
|
|
358
|
+
React.createElement('p', { key: 'message', style: { margin: '0.5rem 0 0 0' } }, error)
|
|
359
|
+
]);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Render principal
|
|
363
|
+
return React.createElement('div', {
|
|
364
|
+
ref: containerRef,
|
|
365
|
+
className: `wu-slot ${loading ? 'wu-slot-loading' : 'wu-slot-loaded'} ${className}`,
|
|
366
|
+
style: {
|
|
367
|
+
minHeight: '100px',
|
|
368
|
+
position: 'relative',
|
|
369
|
+
...style
|
|
370
|
+
},
|
|
371
|
+
'data-wu-app': actualAppName,
|
|
372
|
+
'data-wu-url': url,
|
|
373
|
+
...props
|
|
374
|
+
}, loading && (fallback || React.createElement('div', {
|
|
375
|
+
style: {
|
|
376
|
+
display: 'flex',
|
|
377
|
+
alignItems: 'center',
|
|
378
|
+
justifyContent: 'center',
|
|
379
|
+
padding: '2rem',
|
|
380
|
+
color: '#666'
|
|
381
|
+
}
|
|
382
|
+
}, `Loading ${name}...`)));
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Hook para usar el EventBus de Wu Framework en React
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* const { emit, on } = useWuEvents();
|
|
391
|
+
*
|
|
392
|
+
* useEffect(() => {
|
|
393
|
+
* const unsub = on('user:login', (data) => console.log(data));
|
|
394
|
+
* return unsub;
|
|
395
|
+
* }, [on]);
|
|
396
|
+
*/
|
|
397
|
+
function createUseWuEvents(React) {
|
|
398
|
+
const { useCallback, useEffect, useRef } = React;
|
|
399
|
+
|
|
400
|
+
return function useWuEvents() {
|
|
401
|
+
const subscriptionsRef = useRef([]);
|
|
402
|
+
|
|
403
|
+
const emit = useCallback((event, data, options) => {
|
|
404
|
+
const wu = getWuInstance();
|
|
405
|
+
if (wu?.eventBus) {
|
|
406
|
+
wu.eventBus.emit(event, data, options);
|
|
407
|
+
} else {
|
|
408
|
+
console.warn('[useWuEvents] Wu Framework not available');
|
|
409
|
+
}
|
|
410
|
+
}, []);
|
|
411
|
+
|
|
412
|
+
const on = useCallback((event, callback) => {
|
|
413
|
+
const wu = getWuInstance();
|
|
414
|
+
if (wu?.eventBus) {
|
|
415
|
+
const unsubscribe = wu.eventBus.on(event, callback);
|
|
416
|
+
subscriptionsRef.current.push(unsubscribe);
|
|
417
|
+
return unsubscribe;
|
|
418
|
+
}
|
|
419
|
+
console.warn('[useWuEvents] Wu Framework not available');
|
|
420
|
+
return () => {};
|
|
421
|
+
}, []);
|
|
422
|
+
|
|
423
|
+
const once = useCallback((event, callback) => {
|
|
424
|
+
const wu = getWuInstance();
|
|
425
|
+
if (wu?.eventBus) {
|
|
426
|
+
return wu.eventBus.once(event, callback);
|
|
427
|
+
}
|
|
428
|
+
console.warn('[useWuEvents] Wu Framework not available');
|
|
429
|
+
return () => {};
|
|
430
|
+
}, []);
|
|
431
|
+
|
|
432
|
+
// Cleanup on unmount
|
|
433
|
+
useEffect(() => {
|
|
434
|
+
return () => {
|
|
435
|
+
subscriptionsRef.current.forEach(unsub => unsub());
|
|
436
|
+
subscriptionsRef.current = [];
|
|
437
|
+
};
|
|
438
|
+
}, []);
|
|
439
|
+
|
|
440
|
+
return { emit, on, once };
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Hook para usar el Store de Wu Framework en React
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* const { state, setState, subscribe } = useWuStore('user');
|
|
449
|
+
*/
|
|
450
|
+
function createUseWuStore(React) {
|
|
451
|
+
const { useState, useCallback, useEffect } = React;
|
|
452
|
+
|
|
453
|
+
return function useWuStore(namespace = '') {
|
|
454
|
+
const [state, setLocalState] = useState(() => {
|
|
455
|
+
const wu = getWuInstance();
|
|
456
|
+
return wu?.store?.get(namespace) || null;
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
const setState = useCallback((path, value) => {
|
|
460
|
+
const wu = getWuInstance();
|
|
461
|
+
if (wu?.store) {
|
|
462
|
+
const fullPath = namespace ? `${namespace}.${path}` : path;
|
|
463
|
+
wu.store.set(fullPath, value);
|
|
464
|
+
}
|
|
465
|
+
}, [namespace]);
|
|
466
|
+
|
|
467
|
+
const getState = useCallback((path = '') => {
|
|
468
|
+
const wu = getWuInstance();
|
|
469
|
+
if (wu?.store) {
|
|
470
|
+
const fullPath = namespace ? (path ? `${namespace}.${path}` : namespace) : path;
|
|
471
|
+
return wu.store.get(fullPath);
|
|
472
|
+
}
|
|
473
|
+
return null;
|
|
474
|
+
}, [namespace]);
|
|
475
|
+
|
|
476
|
+
// Subscribe to changes
|
|
477
|
+
useEffect(() => {
|
|
478
|
+
const wu = getWuInstance();
|
|
479
|
+
if (!wu?.store) return;
|
|
480
|
+
|
|
481
|
+
const pattern = namespace ? `${namespace}.*` : '*';
|
|
482
|
+
const unsubscribe = wu.store.on(pattern, () => {
|
|
483
|
+
setLocalState(wu.store.get(namespace));
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
return unsubscribe;
|
|
487
|
+
}, [namespace]);
|
|
488
|
+
|
|
489
|
+
return { state, setState, getState };
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// API pública del adapter
|
|
494
|
+
export const wuReact = {
|
|
495
|
+
register,
|
|
496
|
+
createWuSlot,
|
|
497
|
+
createUseWuEvents,
|
|
498
|
+
createUseWuStore,
|
|
499
|
+
getWuInstance,
|
|
500
|
+
waitForWu
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// Named exports para conveniencia
|
|
504
|
+
export {
|
|
505
|
+
register,
|
|
506
|
+
createWuSlot,
|
|
507
|
+
createUseWuEvents,
|
|
508
|
+
createUseWuStore,
|
|
509
|
+
getWuInstance,
|
|
510
|
+
waitForWu
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
export default wuReact;
|