Skip to content

Commit 6b10d8e

Browse files
committed
fix: adjust the parameter handling of loadApp
1 parent 141f84c commit 6b10d8e

File tree

1 file changed

+56
-43
lines changed

1 file changed

+56
-43
lines changed

packages/runtime/core/src/garfish.ts

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,58 @@ export class Garfish implements interfaces.Garfish {
5858
});
5959
}
6060

61-
public usePlugin(
61+
private mergeAppOptions(
62+
appName: string,
63+
options?: Partial<interfaces.LoadAppOptions> | string,
64+
) {
65+
// `Garfish.loadApp('appName', 'https://xx.html');`
66+
if (typeof options === 'string') {
67+
options = {
68+
basename: '/',
69+
entry: options,
70+
};
71+
}
72+
let appInfo = this.appInfos[appName];
73+
const tempInfo = Object.assign({}, appInfo);
74+
const tempOpts = Object.assign({}, this.options);
75+
const tempCurOptions = Object.assign({}, options);
76+
77+
delete tempInfo.props;
78+
delete tempOpts.props;
79+
delete tempCurOptions.props;
80+
81+
appInfo = deepMerge(tempOpts, deepMerge(tempInfo, tempCurOptions));
82+
appInfo.props = options?.props || appInfo?.props || this.options.props;
83+
appInfo.name = appName;
84+
85+
if (!appInfo.domGetter) {
86+
appInfo.domGetter = document.createElement('div');
87+
}
88+
89+
// Does not support does not have remote resources and no registered application
90+
assert(
91+
appInfo.entry,
92+
`Can't load unexpected module "${appName}".` +
93+
'Please provide the entry parameters or registered in advance of the app',
94+
);
95+
return appInfo;
96+
}
97+
98+
private setOptions(options: Partial<interfaces.Options>) {
99+
assert(!this.running, 'Garfish is running, can`t set options');
100+
if (isPlainObject(options)) {
101+
const tempCurOpts = Object.assign({}, options);
102+
const tempOpts = Object.assign({}, this.options);
103+
// Index object can't deep copy otherwise unable to communicate
104+
delete tempOpts.props;
105+
delete tempCurOpts.props;
106+
this.options = deepMerge(tempOpts, tempCurOpts);
107+
this.options.props = options.props || this.options.props;
108+
}
109+
return this;
110+
}
111+
112+
usePlugin(
62113
hooks,
63114
plugin: (context: Garfish) => interfaces.Plugin,
64115
...args: Array<any>
@@ -74,18 +125,6 @@ export class Garfish implements interfaces.Garfish {
74125
return hooks.usePlugins(res);
75126
}
76127

77-
setOptions(options: Partial<interfaces.Options>) {
78-
assert(!this.running, 'Garfish is running, can`t set options');
79-
if (isPlainObject(options)) {
80-
this.options = deepMerge(this.options, options);
81-
// Index object can't deep copy otherwise unable to communicate
82-
if (hasOwn(options, 'props')) {
83-
this.options.props = options.props;
84-
}
85-
}
86-
return this;
87-
}
88-
89128
run(options?: interfaces.Options) {
90129
if (this.running) {
91130
// Nested scene can be repeated registration application, and basic information for the basename、domGetter、lifeCycle
@@ -178,39 +217,13 @@ export class Garfish implements interfaces.Garfish {
178217

179218
async loadApp(
180219
appName: string,
181-
options: Partial<interfaces.LoadAppOptions> | string,
220+
options?: Partial<interfaces.LoadAppOptions> | string,
182221
): Promise<interfaces.App | null> {
183-
let appInfo = this.appInfos[appName];
184-
185-
if (isPlainObject(options)) {
186-
// Does not support does not have remote resources and no registered application
187-
assert(
188-
!(!appInfo && !appInfo.entry),
189-
`Can't load unexpected module "${appName}".` +
190-
'Please provide the entry parameters or registered in advance of the app',
191-
);
192-
// Deep clone app options
193-
const tempInfo = appInfo;
194-
appInfo = deepMerge(tempInfo, options);
195-
appInfo.props = hasOwn(tempInfo, 'props')
196-
? tempInfo.props
197-
: this.options.props;
198-
appInfo.hooks = hasOwn(tempInfo, 'hooks') ? tempInfo.hooks : null;
199-
} else if (typeof options === 'string') {
200-
// `Garfish.loadApp('appName', 'https://xx.html');`
201-
appInfo = {
202-
name: appName,
203-
entry: options,
204-
basename: this.options.basename || '/',
205-
props: this.options.props,
206-
domGetter:
207-
this.options.domGetter || (() => document.createElement('div')),
208-
};
209-
}
210-
222+
const appInfo = this.mergeAppOptions(appName, options);
211223
// Initialize the mount point, support domGetter as promise, is advantageous for the compatibility
212-
if (appInfo.domGetter)
224+
if (appInfo.domGetter) {
213225
appInfo.domGetter = await getRenderNode(appInfo.domGetter);
226+
}
214227

215228
const asyncLoadProcess = async () => {
216229
// Return not undefined type data directly to end loading

0 commit comments

Comments
 (0)