Skip to content

Commit

Permalink
feat: add interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoushaw committed Apr 30, 2021
1 parent 9aacf41 commit 53f88f6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions dev/main/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import VueRouter from 'vue-router';
import Garfish from './dist/core.esm-browser.js';
import Gar from './dist/core.esm-browser.js';
// import store from './store';
// import { observable, autorun } from 'mobx';
Expand Down
7 changes: 4 additions & 3 deletions packages/runtime/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Hooks } from './plugin/hooks';
import Hook from 'packages/runtime/hooks/src/Hook';
import { Loader } from './module/loader';

container.bind<Garfish>(TYPES.Garfish).toConstructor(Garfish);
container.bind<Hooks>(TYPES.Hooks).to(Hooks);
container.bind<Loader>(TYPES.Loader).to(Loader);
// container.bind<Garfish>(TYPES.Garfish).toConstructor(Garfish);
// container.bind<Hooks>(TYPES.Hooks).to(Hooks).inRequestScope();
// container.bind<Loader>(TYPES.Loader).to(Loader).inRequestScope();

window.__GARFISH__ = true;

Expand All @@ -35,6 +35,7 @@ async function use() {
await app.unmount();
});
}
window.Garfish = GarfishInstance;

use();

Expand Down
22 changes: 17 additions & 5 deletions packages/runtime/core/src/instance/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ import {
} from '@garfish/utils';
import { Loader } from '../module/loader';
import { getRenderNode } from '../utils';
import { injectable } from 'inversify';
// import { injectable } from 'inversify';
import { lazyInject, TYPES } from '../ioc/container';

function injectable() {
return function (target: any) {
if (Reflect.hasOwnMetadata('inversify:paramtypes', target)) {
throw new Error('Cannot apply @injectable decorator multiple times.');
}

const types = Reflect.getMetadata('design:returntype', target) || [];
Reflect.defineMetadata('inversify:paramtypes', types, target);
return target;
};
}

@injectable()
export class Garfish {
public version = __VERSION__;
Expand All @@ -27,11 +39,11 @@ export class Garfish {
private loading: Record<string, Promise<any> | null> = {};
public plugins: Array<(context: Garfish) => Plugin> = [];

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

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

constructor(options?: Options) {
// register plugins
Expand Down
28 changes: 28 additions & 0 deletions packages/runtime/core/src/interface/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { VText, VNode } from '@garfish/utils';

namespace interfaces {
export interface Loader {
takeJsResources: () => void;
}

export interface HtmlResourceOpts {
url: string;
code: string;
size: number;
}

export interface JsResourceOpts {
url: string;
code: string;
size: number;
attributes: VNode['attributes'];
}

export interface VNode {
key?: string;
type: 'element';
tagName: string;
children: Array<VNode | VText>;
attributes: Array<Record<string, string | null>>;
}
}
2 changes: 1 addition & 1 deletion packages/runtime/core/src/ioc/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import getDecorators from 'inversify-inject-decorators';
import { Container, tagged, named } from 'inversify';
// import { Garfish } from "../instance/context";

export const container = new Container();
export const container = new Container({ defaultScope: 'Request' });
const decorators = getDecorators(container, true);
export const lazyInject = decorators.lazyInject;

Expand Down
5 changes: 5 additions & 0 deletions packages/runtime/core/src/module/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { AppInfo } from '../type';
import { CssResource, JsResource, HtmlResource } from './source';
import { Garfish } from '../instance/context';
import { injectable } from 'inversify';
import { lazyInject, TYPES } from '../ioc/container';
import { Hooks } from '../plugin/hooks';

let currentSize = 0;

Expand All @@ -38,6 +40,9 @@ export class Loader {
// 允许自定义配置
public requestConfig: RequestInit | ((url: string) => RequestInit) = {};

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

constructor() {
this.forceCaches = new Set();
this.caches = Object.create(null);
Expand Down

0 comments on commit 53f88f6

Please sign in to comment.