From 53f88f6c4264cd14fb5ac7106ef5f8c4a347ce9d Mon Sep 17 00:00:00 2001 From: zhoushaw Date: Fri, 30 Apr 2021 18:21:49 +0800 Subject: [PATCH] feat: add interfaces --- dev/main/index.js | 1 + packages/runtime/core/src/index.ts | 7 +++-- packages/runtime/core/src/instance/context.ts | 22 +++++++++++---- .../runtime/core/src/interface/interfaces.ts | 28 +++++++++++++++++++ packages/runtime/core/src/ioc/container.ts | 2 +- packages/runtime/core/src/module/loader.ts | 5 ++++ 6 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 packages/runtime/core/src/interface/interfaces.ts diff --git a/dev/main/index.js b/dev/main/index.js index 9ed4dc429..46d6d185b 100644 --- a/dev/main/index.js +++ b/dev/main/index.js @@ -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'; diff --git a/packages/runtime/core/src/index.ts b/packages/runtime/core/src/index.ts index b3a07389b..d8428dc87 100644 --- a/packages/runtime/core/src/index.ts +++ b/packages/runtime/core/src/index.ts @@ -8,9 +8,9 @@ import { Hooks } from './plugin/hooks'; import Hook from 'packages/runtime/hooks/src/Hook'; import { Loader } from './module/loader'; -container.bind(TYPES.Garfish).toConstructor(Garfish); -container.bind(TYPES.Hooks).to(Hooks); -container.bind(TYPES.Loader).to(Loader); +// container.bind(TYPES.Garfish).toConstructor(Garfish); +// container.bind(TYPES.Hooks).to(Hooks).inRequestScope(); +// container.bind(TYPES.Loader).to(Loader).inRequestScope(); window.__GARFISH__ = true; @@ -35,6 +35,7 @@ async function use() { await app.unmount(); }); } +window.Garfish = GarfishInstance; use(); diff --git a/packages/runtime/core/src/instance/context.ts b/packages/runtime/core/src/instance/context.ts index ec470feb4..ed4008c08 100644 --- a/packages/runtime/core/src/instance/context.ts +++ b/packages/runtime/core/src/instance/context.ts @@ -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__; @@ -27,11 +39,11 @@ export class Garfish { private loading: Record | 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 diff --git a/packages/runtime/core/src/interface/interfaces.ts b/packages/runtime/core/src/interface/interfaces.ts new file mode 100644 index 000000000..bc8bbe2d3 --- /dev/null +++ b/packages/runtime/core/src/interface/interfaces.ts @@ -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; + attributes: Array>; + } +} diff --git a/packages/runtime/core/src/ioc/container.ts b/packages/runtime/core/src/ioc/container.ts index 28fd78e86..54adf50e0 100644 --- a/packages/runtime/core/src/ioc/container.ts +++ b/packages/runtime/core/src/ioc/container.ts @@ -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; diff --git a/packages/runtime/core/src/module/loader.ts b/packages/runtime/core/src/module/loader.ts index d2fb3aebd..9ea5f269a 100644 --- a/packages/runtime/core/src/module/loader.ts +++ b/packages/runtime/core/src/module/loader.ts @@ -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; @@ -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);