Skip to content

Commit

Permalink
feat: getAppComponent hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYuTay committed Jul 3, 2020
1 parent 3f8d478 commit 9a78eec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/runtime-core/src/lib/__tests__/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Application } from '../application';

function getApp({ render }: any = {}) {
const app = new Application({
context: {},
context: {
test: true
},
AppComponent: {},
routes: [
{
Expand Down Expand Up @@ -46,4 +48,24 @@ describe('application', () => {
expect(renderPrarmas['routes']).toBeDefined();
expect(renderPrarmas['appContext']).toBeDefined();
});

test('should wrap getAppComponent hook', async () => {
const app = getApp();

app.tap('getAppComponent', {
name: 'wrapAppComponent',
fn: (AppComponent: any, context: any) => {
expect(context.test).toBe(true);

const WrapApp = () => AppComponent;
WrapApp.test = 'test';
return WrapApp;
}
});

await app.run();

expect(typeof app.AppComponent).toBe('function');
expect(app.AppComponent.test).toBe('test');
});
});
12 changes: 12 additions & 0 deletions packages/runtime-core/src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ export class Application<Context extends {}> extends Hookable
async run() {
await this._init();
await this._createApplicationContext();
await this._getAppComponent();
await this._render();

return this._context;
}

async rerender() {
await this._getAppComponent();
await this._render();
}

Expand All @@ -62,6 +64,16 @@ export class Application<Context extends {}> extends Hookable
})) as IContext;
}

private async _getAppComponent() {
this.AppComponent = await this.callHook<RuntimeHooks.IHookGetAppComponent>(
{
name: 'getAppComponent',
initialValue: this.AppComponent
},
this._context
);
}

private async _render() {
const result = await this._renderFn({
appContext: this._context,
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/hooks/hooks-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export type IHookCreateAppContext = defineHook<
}
>;

export type IHookGetAppComponent = defineHook<
'getAppComponent',
{
initialValue: object;
args: [object];
}
>;

export type IHookRender = defineHook<'render'>;

export type IHookServerGetPageData = defineHook<
Expand Down

0 comments on commit 9a78eec

Please sign in to comment.