Skip to content

Commit

Permalink
feat: add create app hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoushaw committed May 3, 2021
1 parent 165bd9b commit 4866f3f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 45 deletions.
20 changes: 14 additions & 6 deletions packages/runtime/cjs-external/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@

import Garfish, { Plugin } from '@garfish/core'
import Garfish, { Plugin } from '@garfish/core';
import { assert, warn } from '@garfish/utils';

declare module '@garfish/core' {
export default interface Garfish {
setExternal: (nameOrExtObj: string | Record<string, any>, value?: any)=> void;
externals: Record<string, any>
setExternal: (
nameOrExtObj: string | Record<string, any>,
value?: any,
) => void;
externals: Record<string, any>;
}
}

export default function addCjsExternalPlugin( Garfish: Garfish ): Plugin {
export default function addCjsExternalPlugin(Garfish: Garfish): Plugin {
Garfish.setExternal = setExternal;

function setExternal(nameOrExtObj: string | Record<string, any>, value?: any) {
function setExternal(
nameOrExtObj: string | Record<string, any>,
value?: any,
) {
assert(nameOrExtObj, 'Invalid parameter.');
if (typeof nameOrExtObj === 'object') {
for (const key in nameOrExtObj) {
Expand All @@ -28,6 +33,9 @@ export default function addCjsExternalPlugin( Garfish: Garfish ): Plugin {

return {
name: 'bb',
beforeEval(appInfo, code, env) {
env.require = (name) => Garfish.externals[name];
},
// initialize() {
// // Garfish.fe = setExternal;
// },
Expand Down
22 changes: 13 additions & 9 deletions packages/runtime/core/src/instance/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
validURL,
hasOwn,
} from '@garfish/utils';
import { Loader } from '../module/loader';
import { getRenderNode } from '../utils';
import { lazyInject, TYPES, injectable } from '../ioc/container';
import { interfaces } from '../interface';
import { App } from '../module/app';
import { Loader } from '../module/loader';

@injectable()
export class Garfish {
Expand All @@ -23,15 +23,15 @@ export class Garfish {
public externals: Record<string, any> = {};
public appInfos: Record<string, AppInfo> = {};
public activeApps: Array<any> = [];
private cacheApps: Record<string, any> = {};
private cacheApps: Record<string, App> = {};
private loading: Record<string, Promise<any> | null> = {};
public plugins: Array<(context: Garfish) => Plugin> = [];

@lazyInject(TYPES.Loader)
public loader: interfaces.Loader;
public loader: Loader;

@lazyInject(TYPES.Hooks)
public hooks: interfaces.Hooks;
public hooks: Hooks;

constructor(options?: Options) {
// register plugins
Expand Down Expand Up @@ -114,7 +114,7 @@ export class Garfish {
}

// // TODO: 1. loader增加preload权重 2.
public async loadApp(opts: LoadAppOptions) {
public async loadApp(opts: LoadAppOptions): Promise<App> {
let appInfo = this.appInfos[opts.name];
const appName = opts.name;

Expand All @@ -128,7 +128,6 @@ export class Garfish {
if (!appInfo) {
appInfo = { cache: true, ...opts };
}
appInfo.domGetter = getRenderNode(appInfo.domGetter);

const asyncLoadProcess = async () => {
// Return not undefined type data directly to end loading
Expand All @@ -145,7 +144,12 @@ export class Garfish {
result = cacheApp;
} else {
try {
result = await this.loader.loadApp(appInfo);
const {
manager,
isHtmlMode,
resources,
} = await this.loader.loadAppSources(appInfo);
result = new App(appInfo, manager, resources, isHtmlMode);
this.cacheApps[appName] = result;
} catch (e) {
__DEV__ && error(e);
Expand All @@ -154,7 +158,7 @@ export class Garfish {
this.loading[appName] = null;
}
}
await this.hooks.lifecycle.afterLoad.promise(appInfo);
this.hooks.lifecycle.afterLoad.call(appInfo, result as App);
return result;
};

Expand Down
20 changes: 18 additions & 2 deletions packages/runtime/core/src/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export namespace interfaces {
domGetter: DomGetter;
};

type AsyncResource = {
async: boolean;
content: () => Promise<any>;
};

export interface ResourceModules {
link: Array<any>;
js: Array<any | AsyncResource>;
}

export interface AppInfo {
name: string;
entry: string;
Expand All @@ -99,8 +109,14 @@ export namespace interfaces {
deactive?: (appInfo: AppInfo, rootPath: string) => void;
}

export interface AppSources {
manager: HtmlResource;
resources: ResourceModules;
isHtmlMode: boolean;
}

export interface Loader {
loadApp(appInfo: AppInfo): Promise<App>;
loadAppSources(appInfo: AppInfo): Promise<AppSources>;
// takeJsResources: (manager: HtmlResource) => void;
// takeLinkResources: (manager: HtmlResource) => void;
// createApp(appInfo: AppInfo, manager: HtmlResource, isHtmlMode: boolean): Promise<any>
Expand All @@ -116,7 +132,7 @@ export namespace interfaces {
beforeRegisterApp: SyncHook<[AppInfo | Array<AppInfo>], void>;
registerApp: SyncHook<[AppInfo | Array<AppInfo>], void>;
beforeLoad: AsyncSeriesBailHook<AppInfo, void | boolean>; // 根据返回值决定是否继续执行后续代码
afterLoad: AsyncSeriesBailHook<AppInfo, void | boolean>;
afterLoad: SyncHook<[AppInfo, App], void | boolean>;
errorLoadApp: SyncHook<[AppInfo, Error], void>;
beforeEval: SyncHook<
[
Expand Down
20 changes: 7 additions & 13 deletions packages/runtime/core/src/module/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,12 @@ import {
warn,
} from '@garfish/utils';
import { hooks } from '../plugin/hooks';
import { createAppContainer } from '../utils';
import { createAppContainer, getRenderNode } from '../utils';
import { HtmlResource } from './source';
import { interfaces } from '../interface';

const __GARFISH_EXPORTS__ = '__GARFISH_EXPORTS__';

type AsyncResource = {
async: boolean;
content: () => Promise<any>;
};

export interface ResourceModules {
link: Array<any>;
js: Array<any | AsyncResource>;
}

/**
* Have the ability to App instance
* 1. Provide static resource, the structure of the HTML, CSS, js
Expand All @@ -60,15 +51,18 @@ export class App {
public provider: Provider;
private entryResManager: HtmlResource;
public htmlNode: HTMLElement | ShadowRoot;
private resources: ResourceModules;
private resources: interfaces.ResourceModules;
public isHtmlMode: boolean;

constructor(
appInfo: AppInfo,
entryResManager: HtmlResource,
resources: ResourceModules,
resources: interfaces.ResourceModules,
isHtmlMode: boolean,
) {
// get container dom
appInfo.domGetter = getRenderNode(appInfo.domGetter);

this.appInfo = appInfo;
this.name = appInfo.name;

Expand Down
24 changes: 11 additions & 13 deletions packages/runtime/core/src/module/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
transformUrl,
parseContentType,
} from '@garfish/utils';
import { App, ResourceModules } from './app';
import { AppInfo } from '../type';
import { CssResource, JsResource, HtmlResource } from './source';
import { injectable } from 'inversify';
Expand All @@ -27,7 +26,7 @@ export function isOverCapacity(size: number) {
}

@injectable()
export class Loader implements interfaces.Loader {
export class Loader {
private forceCaches: Set<string>;
private caches: Record<string, HtmlResource | CssResource | JsResource>;
private loadings: Record<
Expand Down Expand Up @@ -117,14 +116,14 @@ export class Loader implements interfaces.Loader {
return requestList;
}

private createApp(
appInfo: AppInfo,
manager: HtmlResource,
isHtmlMode: boolean,
) {
const run = (resources: ResourceModules) => {
const app = new App(appInfo, manager, resources, isHtmlMode);
return app;
private loadAllSources(manager: HtmlResource, isHtmlMode: boolean) {
const run = (resources: interfaces.ResourceModules) => {
// const app = new App(appInfo, manager, resources, isHtmlMode);
return {
manager,
resources,
isHtmlMode,
};
};

// 如果是 html, 就需要加载用到的资源
Expand Down Expand Up @@ -186,7 +185,7 @@ export class Loader implements interfaces.Loader {
}

// load app
loadApp(appInfo: AppInfo): Promise<App> {
loadAppSources(appInfo: AppInfo): Promise<interfaces.AppSources> {
assert(appInfo?.entry, 'Miss appInfo or appInfo.entry');
const resolveEntry = transformUrl(location.href, appInfo.entry);
return this.load(resolveEntry).then(
Expand All @@ -200,8 +199,7 @@ export class Loader implements interfaces.Loader {
this.forceCaches.add(url);
resManager = new HtmlResource({ url, code, size: 0 });
}
return this.createApp(
appInfo,
return this.loadAllSources(
// @ts-ignore
resManager,
isHtmlMode,
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/core/src/plugin/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type Plugin = { name: string } & PickParam<
>;

@injectable()
export class Hooks implements interfaces.Hooks {
export class Hooks {
public lifecycle: interfaces.Lifecycle;

constructor() {
Expand All @@ -46,7 +46,7 @@ export class Hooks implements interfaces.Hooks {
beforeRegisterApp: new SyncHook(['appInfos']),
registerApp: new SyncHook(['appInfos']),
beforeLoad: new AsyncSeriesBailHook(['appInfo']),
afterLoad: new AsyncSeriesBailHook(['appInfo']),
afterLoad: new SyncHook(['appInfo', 'appInstance']),
errorLoadApp: new SyncHook(['appInfo', 'error']),
beforeEval: new SyncHook([
'appInfo',
Expand Down

0 comments on commit 4866f3f

Please sign in to comment.