Skip to content

Commit

Permalink
fix: use egg-layer load framework circular reference (#730)
Browse files Browse the repository at this point in the history
* fix: loader ignore appInfo

* test: change all decorator to @midwayjs/decorator package

* fix: add WorkerLoader Property

* fix: circular reference

* fix: circular reference
  • Loading branch information
czy88840616 authored Nov 24, 2020
1 parent 685e516 commit f012d78
Show file tree
Hide file tree
Showing 45 changed files with 259 additions and 246 deletions.
12 changes: 8 additions & 4 deletions packages/web/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function getFramework() {
}

export const createAppWorkerLoader = () => {
const AppWorkerLoader = require(getFramework()).AppWorkerLoader;
const AppWorkerLoader =
require(getFramework())?.AppWorkerLoader || require('egg').AppWorkerLoader;
class EggAppWorkerLoader extends (AppWorkerLoader as any) {
app: any;
framework;
Expand Down Expand Up @@ -95,7 +96,9 @@ export const createAppWorkerLoader = () => {
};

export const createAgentWorkerLoader = () => {
const AppWorkerLoader = require(getFramework()).AgentWorkerLoader;
const AppWorkerLoader =
require(getFramework())?.AgentWorkerLoader ||
require('egg').AgentWorkerLoader;
class EggAppWorkerLoader extends (AppWorkerLoader as any) {
getEggPaths() {
if (!this.appDir) {
Expand Down Expand Up @@ -155,7 +158,8 @@ export const createAgentWorkerLoader = () => {
};

export const createEggApplication = () => {
const Application = require(getFramework()).Application;
const Application =
require(getFramework())?.Application || require('egg').Application;
class EggApplication extends (Application as any) {
constructor(options) {
// eslint-disable-next-line constructor-super
Expand All @@ -175,7 +179,7 @@ export const createEggApplication = () => {
};

export const createEggAgent = () => {
const Agent = require(getFramework()).Agent;
const Agent = require(getFramework())?.Agent || require('egg').Agent;
class EggAgent extends (Agent as any) {
constructor(options) {
// eslint-disable-next-line constructor-super
Expand Down
1 change: 0 additions & 1 deletion packages/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ export {
createAppWorkerLoader,
createAgentWorkerLoader,
} from './base';
// must export mock app here
export { Application, Agent } from './application';
export { startCluster } from 'egg';
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { async, config, init, plugin, provide } from '../../../../../../src';
import { Async, Config, Init, Plugin, Provide } from '@midwayjs/decorator';

@async()
@provide()
@Async()
@Provide()
export class BaseService {

@config('hello')
@Config('hello')
config;

@plugin('plugin2')
@Plugin('plugin2')
plugin2;

@init()
@Init()
async init() {
await new Promise(resolve => {
setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { async, config, init, inject, plugin, provide } from '../../../../../../src';
import { Async, Config, Init, Inject, Plugin, Provide } from '@midwayjs/decorator';

@provide()
@Provide()
export class A {
config = {
c: 20
};
}

@provide()
@Provide()
export class B {
config = {
c: 40
};
}

@async()
@provide()
@Async()
@Provide()
export class BaseService {

config;
plugin2;

constructor(
@inject() a,
@config('hello') config,
@inject() b,
@plugin('plugin2') plugin2
@Inject() a,
@Config('hello') config,
@Inject() b,
@Plugin('plugin2') plugin2
) {
this.config = Object.assign(config, {
c: a.config.c + b.config.c + config.c
});
this.plugin2 = plugin2;
}

@init()
@Init()
async init() {
await new Promise(resolve => {
setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

import { ScopeEnum } from '@midwayjs/decorator';
import {
controller,
get,
inject,
provide,
scope,
} from '../../../../../../../src/';
Controller,
Get,
Inject,
Provide,
Scope,
} from '@midwayjs/decorator';

const assert = require('assert');

@provide()
@scope(ScopeEnum.Request)
@Provide()
@Scope(ScopeEnum.Request)
export class BaseApi {
async index(ctx) {
ctx.body = 'index';
}
}

@provide()
@controller('/components/')
@Provide()
@Controller('/components/')
export class My {
@inject()
@Inject()
logger;

@get('/')
@Get('/')
async index(ctx) {
assert(this.logger.constructor.name === 'ContextLogger');
ctx.body = 'hello';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { provide, controller, get } from '../../../../../../../src/';
import { Provide, Controller, Get } from '@midwayjs/decorator';

@provide()
@controller('/')
@Provide()
@Controller('/')
export class My {
@get('/')
@Get('/')
async index(ctx) {
ctx.body = 'root_test';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
'use strict';

import { inject, provide, scope, ScopeEnum, controller, get } from '../../../../../../../src/';
import { Inject, Provide, Scope, ScopeEnum, Controller, Get } from '@midwayjs/decorator';

const assert = require('assert');

@provide()
@scope(ScopeEnum.Request)
@Provide()
@Scope(ScopeEnum.Request)
export class BaseApi {
async index(ctx) {
ctx.body = 'index';
}
}

@provide()
@controller('/components/')
@Provide()
@Controller('/components/')
export class Api {

@inject()
@Inject()
logger;

@get('/')
@Get('/')
async index(ctx) {
assert(this.logger.constructor.name === 'ContextLogger');
ctx.body = 'hello';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { provide, controller, get } from '../../../../../../../src/';
import { Provide, Controller, Get } from '@midwayjs/decorator';

@provide()
@controller('/')
@Provide()
@Controller('/')
class My {
@get('/')
@Get('/')
async index(ctx) {
ctx.body = 'root_test';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { provide, controller, get } from '../../../../../../../src/';
import { Provide, Controller, Get } from '@midwayjs/decorator';
import * as React from 'react';
import * as ReactDOM from 'react-dom/server';
import App from '../../shared/App';

@provide()
@controller('/')
@Provide()
@Controller('/')
export class My {
@get('/')
@Get('/')
async index(ctx) {
ctx.body = `
<!DOCTYPE html>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
'use strict';

import { inject, provide, scope, ScopeEnum, controller, get } from '../../../../../../../src/';
import { Inject, Provide, Scope, ScopeEnum, Controller, Get } from '@midwayjs/decorator';

const assert = require('assert');

@provide()
@scope(ScopeEnum.Request)
@Provide()
@Scope(ScopeEnum.Request)
export class BaseApi {
async index(ctx) {
ctx.body = 'index';
}
}

@provide()
@controller('/components/')
@Provide()
@Controller('/components/')
export class Api {

@inject()
@Inject()
logger;

@get('/')
@Get('/')
async index(ctx) {
assert(this.logger.constructor.name === 'ContextLogger');
ctx.body = 'hello';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { controller, get, provide } from '../../../../../../../src/';
import { Controller, Get, Provide } from '@midwayjs/decorator';

@provide()
@controller('/')
@Provide()
@Controller('/')
export class My {
@get('/')
@Get('/')
async index(ctx) {
ctx.body = 'root_test';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { provide, controller, get, inject } from '../../../../../../../src';
import { Provide, Controller, Get, Inject } from '@midwayjs/decorator';

@provide()
@controller('/circular')
@Provide()
@Controller('/circular')
export class CircularController {
@inject()
@Inject()
planA: any;

@get('/test')
@Get('/test')
async test(ctx) {
ctx.body = 'success';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { provide, inject, controller, get, config } from '../../../../../../../src';
import { Provide, Inject, Controller, Get, Config } from '@midwayjs/decorator';

@provide()
@controller('/config')
@Provide()
@Controller('/config')
export class ConfigController {

@inject('ctx')
@Inject('ctx')
ctx: any;

// should be 1
@config('hello.a')
@Config('hello.a')
a: number;

// should be 2
@config('hello.e.f')
@Config('hello.e.f')
c: number;

// should be undefined
@config('hello.f')
@Config('hello.f')
d: number;

@config('plugins')
@Config('plugins')
plugins: any;

b: boolean;

constructor(
// should be true
@config('plugins.plugin2') pluginFlag: boolean
@Config('plugins.plugin2') pluginFlag: boolean
) {
this.b = pluginFlag;
}

@get('/test')
@Get('/test')
async test() {
const data = {
a: this.a,
Expand All @@ -42,7 +42,7 @@ export class ConfigController {
this.ctx.body = data;
}

@get('/test2')
@Get('/test2')
async test2() {
this.ctx.body = this.plugins;
}
Expand Down
Loading

0 comments on commit f012d78

Please sign in to comment.