Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: format code #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ package-lock.json
/lib/**/*.js
/test/**/*.js
/config/**/*.js
/scripts/**/*.js
*.d.ts
*.js.map
.vscode
!typings/**/*.d.ts
.nyc_output
14 changes: 0 additions & 14 deletions .gitlab-ci.yml

This file was deleted.

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sudo: false
language: node_js
node_js:
- '8'
- '10'
install:
- npm i npminstall && npminstall
script:
Expand Down
2 changes: 1 addition & 1 deletion app/extend/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export default {
return function <T = any>(clsType: any) {
return getInstance<T>(clsType, this, undefined);
};
}
},
};
2 changes: 1 addition & 1 deletion app/extend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export default {
return function <T = any>(clsType: any) {
return getInstance<T>(clsType, this.app, this);
};
}
},
};
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
2 changes: 1 addition & 1 deletion config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export default {
aop: {
useCtxProxyForAppComponent: false,
autoRegisterToCtx: false,
}
},
};
4 changes: 2 additions & 2 deletions lib/appctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function setCtx(target: any, ctx: any) {
Object.defineProperty(target, ctxSymbol, {
enumerable: false,
writable: false,
value: ctx
value: ctx,
});
}
export function getCtx(target: any) {
Expand All @@ -15,7 +15,7 @@ export function setApp(target: any, app: any) {
Object.defineProperty(target, appSymbol, {
enumerable: false,
writable: false,
value: app
value: app,
});
}
export function getApp(target: any) {
Expand Down
14 changes: 7 additions & 7 deletions lib/aspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ export interface AspectPoint<T = any> {
}

function run(func: any, context: FunctionContext) {
func && func(context);
if (func) func(context);
}

function createContext(inst: any, fn: Function, args: any[]) {
function createContext(inst: any, fn: (a: any) => void, args: any[]) {
return {
functionName: (fn as any).__name || fn.name,
inst,
args,
} as FunctionContext;
}

export function funcWrapper(point: AspectPoint, fn: Function) {
export function funcWrapper(point: AspectPoint, fn: (a: any) => void) {
let newFn: any;

if (isGeneratorFunction(fn)) {
Expand All @@ -60,7 +60,7 @@ export function funcWrapper(point: AspectPoint, fn: Function) {
run(point.before, context);
context.ret = fn.apply(this, context.args);
if (context.ret instanceof Promise) {
context.ret = context.ret.then((ret) => {
context.ret = context.ret.then(ret => {
context.ret = ret;
run(point.after, context);
return context.ret;
Expand Down Expand Up @@ -108,9 +108,9 @@ export function aspect<T = any>(point: AspectPoint<T> = {}) {
get() {
return fn;
},
set(value: Function) {
fn = funcWrapper(point, value);
}
set(v: (a: any) => void) {
fn = funcWrapper(point, v);
},
};
Object.defineProperty(target, key, value);
return value;
Expand Down
4 changes: 2 additions & 2 deletions lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function inject(type?: any) {
const app = getApp(this) || (ctx && ctx.app);
value = getInstance(clsType, app, ctx);
return target[key];
}
},
});
};
}
Expand All @@ -79,7 +79,7 @@ export function lazyInject(type?: any) {
const defaultValue = descriptor && descriptor.value;
Object.defineProperty(target, key, {
configurable: true,
get: function () {
get () {
const ctx = getCtx(this);
const app = getApp(this) || (ctx && ctx.app);
const value = getInstance(clsType, app, ctx);
Expand Down
7 changes: 5 additions & 2 deletions lib/getInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function injectInstance(ioc: IocContext, inst: any, app: any, ctx: any) {
}

export function getInstance<T = undefined, KeyType = any>(clsType: KeyType, app: any, ctx?: any): GetReturnType<T, KeyType> {
let ioc: IocContext = undefined;
let ioc: IocContext;

const { useCtxProxyForAppComponent, autoRegisterToCtx } = app.config.aop;

Expand All @@ -56,6 +56,7 @@ export function getInstance<T = undefined, KeyType = any>(clsType: KeyType, app:
}
ioc = ctx.iocContext;
break;
default:
}

let value: any = ioc.get(clsType);
Expand All @@ -82,7 +83,7 @@ export function getInstance<T = undefined, KeyType = any>(clsType: KeyType, app:
return target[property];
}
return typeof target[property] === 'function' ? target[property].bind(value) : target[property];
}
},
});

value = ciHooks.reduce((pre, cur) => cur(pre, app), value);
Expand All @@ -97,6 +98,8 @@ export function getInstance<T = undefined, KeyType = any>(clsType: KeyType, app:
value = ciHooks.reduce((pre, cur) => cur(pre, app, ctx), new targetClsType(ctx));
setCtx(value, ctx);
break;

default:
}

ioc.register(value, clsType as any);
Expand Down
11 changes: 0 additions & 11 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,3 @@ export { getApp, getCtx } from './appctx';
export * from './getInstance';
export { typeLoader } from './typeLoader';
export * from './aspect';

import 'egg';
import { GetReturnType } from 'power-di';
declare module 'egg' {
export interface Context {
getComponent<T = undefined, KeyType= any>(clsType: KeyType): GetReturnType<T, KeyType>;
}
export interface Application {
getComponent<T = undefined, KeyType= any>(clsType: KeyType): GetReturnType<T, KeyType>;
}
}
4 changes: 2 additions & 2 deletions lib/typeLoader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IocContext } from 'power-di';
import { contextTypeSymbol, InstanceSource } from './getInstance';

export const typeLoader = new IocContext;
export const typeLoader = new IocContext();

export function register(clsType: any, keyType: any, from: InstanceSource) {
Object.defineProperty(clsType, contextTypeSymbol, {
value: from
value: from,
});
typeLoader.register(clsType, keyType, { autoNew: false });
}
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,50 @@
"eggPlugin": {
"name": "aop"
},
"egg": {
"typescript": true
},
"dependencies": {
"power-di": "^1.4.16",
"tslib": "^1.9.0"
"power-di": "^1.4.16"
},
"devDependencies": {
"@types/mocha": "^5.1.0",
"@types/node": "^9.6.5",
"@types/supertest": "^2.0.4",
"autod": "^3.0.1",
"co": "^4.6.0",
"egg": "^2.7.1",
"egg-bin": "^4.7.0",
"egg-ci": "^1.8.0",
"egg-mock": "^3.17.0",
"egg-ts-helper": "^1.6.0",
"egg": "^2.7.1",
"rimraf": "^2.6.2",
"supertest": "^3.0.0",
"tslib": "^1.9.0",
"tslint": "^5.9.1",
"typescript": "~2.8.1"
"tslint": "^5.12.0",
"tslint-config-egg": "^1.0.0",
"typescript": "^2.8.1"
},
"egg": {
"typescript": true
},
"engines": {
"node": ">=8.9.0"
},
"scripts": {
"lint": "tslint -p tsconfig.json",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test -r egg-ts-helper/register",
"cov": "egg-bin cov -r egg-ts-helper/register",
"lint": "tslint .",
"ci": "npm run cov",
"autod": "autod",
"ts": "rimraf app/**/*.js lib/**/*.js && tsc",
"clean": "git clean -xf",
"ts": "npm run clean && ets && tsc",
"prepublish": "npm run ts",
"postpublish": "node scripts/published.js"
},
"ci": {
"version": "8"
"version": "8, 10"
},
"repository": {
"type": "git",
"url": "https://github.com/zhang740/egg-aop.git"
"url": "https://github.com/eggjs/egg-aop.git"
},
"eslintIgnore": [
"coverage"
Expand All @@ -73,7 +73,7 @@
"author": "zhang740",
"license": "MIT",
"bugs": {
"url": "https://github.com/zhang740/egg-aop/issues"
"url": "https://github.com/eggjs/egg-aop/issues"
},
"homepage": "https://github.com/zhang740/egg-aop#readme"
"homepage": "https://github.com/eggjs/egg-aop#readme"
}
2 changes: 1 addition & 1 deletion scripts/published.ts → scripts/published.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ const shell = require('child_process').execSync;
const package = require('../package.json');

shell(`git tag ${package.version}`);
shell(`git push`);
shell('git push');
shell(`git push origin ${package.version}`);
Loading