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

Release 2.0.0 #117

Merged
merged 27 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4a9e4a3
feat: make dispose support disposing instance of useFactory (#116)
zhangpanweb Dec 3, 2023
dad48a8
fix: factory instances should save proxied
bytemain Dec 3, 2023
69443de
style: use eslint no-void
bytemain Dec 3, 2023
edaa25e
fix: factory support multiple value
bytemain Dec 3, 2023
362196f
fix: we should listen on instance disposed
bytemain Dec 3, 2023
5f09c6a
fix: instance should be disposed
bytemain Dec 3, 2023
cf82705
refactor: use instances to store objects
bytemain Dec 3, 2023
1e6f200
chore: remove unused code
bytemain Dec 3, 2023
c5e1a53
test: update testcase
bytemain Dec 3, 2023
be0c329
refactor: remove import all statements
bytemain Dec 4, 2023
82dac47
refactor: update folder structure
bytemain Dec 4, 2023
9374d71
refactor: remove reflect-metadata
bytemain Dec 4, 2023
cdfc135
chore: update comment
bytemain Dec 4, 2023
5f13619
feat: hooks support priority option
bytemain Dec 4, 2023
2a5df3d
build: build esm format dist
bytemain Dec 4, 2023
2201f13
chore: update code
bytemain Dec 5, 2023
d32e041
refactor: use @opensumi/events
bytemain Dec 7, 2023
b3f4361
build: add types dir
bytemain Dec 7, 2023
636ff25
refactor: throw if no instances
bytemain Dec 7, 2023
477877d
test: update testcase name
bytemain Dec 7, 2023
f32364d
Merge remote-tracking branch 'origin/main' into next
bytemain Dec 14, 2023
501d20a
test: ignore impossible path coverage
bytemain Dec 14, 2023
072f5c0
test: rename test name
bytemain Dec 14, 2023
68a8e83
ci(cov): skip unreachable code
bytemain Jan 16, 2024
6bb85ec
feat: add event emitter
bytemain Jan 16, 2024
48ea622
test: fix testcase
bytemain Jan 16, 2024
0e356bd
Merge branch 'main' into next
bytemain Jan 19, 2024
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
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ module.exports = {
allowedNames: ['self', 'injector'],
},
],
'no-void': [
'error',
{
allowAsStatement: true,
},
],
},
};
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 由 https://github.com/msfeldstein/gitignore 自动生成
lib
esm

# Logs
logs
*.log
Expand Down
15 changes: 15 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
coveragePathIgnorePatterns: ['/node_modules/', '/test/'],
coverageThreshold: {
global: {
branches: 10,
functions: 10,
lines: 10,
statements: 10,
},
},
setupFilesAfterEnv: ['<rootDir>/scripts/jest-setup.ts'],
};
27 changes: 6 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
"version": "1.10.0",
"description": "A dependency injection tool for Javascript.",
"license": "MIT",
"main": "dist/index.js",
"module": "esm/index.js",
"main": "lib/index.js",
"scripts": {
"prepare": "husky install",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"build": "rm -rf dist && tsc -p tsconfig.prod.json",
"build": "npm run build:lib && npm run build:esm",
"build:lib": "rm -rf lib && tsc -p tsconfig.lib.json",
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json",
"test": "jest --coverage test/**",
"test:watch": "yarn test --watch",
"ci": "npm run lint && npm run test",
Expand All @@ -16,9 +19,6 @@
"release": "commit-and-tag-version --npmPublishHint 'echo Just Push code to remote repo, npm publish will be done by CI.'",
"release:beta": "npm run release -- --prerelease beta"
},
"dependencies": {
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@commitlint/cli": "17.2.0",
"@commitlint/config-conventional": "17.2.0",
Expand All @@ -34,6 +34,7 @@
"jest": "29.2.2",
"lint-staged": "13.0.3",
"prettier": "2.7.1",
"reflect-metadata": "^0.1.13",
"ts-jest": "29.0.3",
"typescript": "4.8.4"
},
Expand All @@ -55,22 +56,6 @@
"@commitlint/config-conventional"
]
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"coveragePathIgnorePatterns": [
"/node_modules/",
"/test/"
],
"coverageThreshold": {
"global": {
"branches": 10,
"functions": 10,
"lines": 10,
"statements": 10
}
}
},
"keywords": [
"di",
"injector"
Expand Down
1 change: 1 addition & 0 deletions scripts/jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'reflect-metadata';
2 changes: 1 addition & 1 deletion src/helper/compose.ts → src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function dispatch<C>(

stack.depth = idx;

let maybePromise: Promise<void> | void;
let maybePromise: Promise<void> | void | undefined;

if (idx < middlewareList.length) {
const middleware = middlewareList[idx];
Expand Down
52 changes: 30 additions & 22 deletions src/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import 'reflect-metadata';
import * as Helper from './helper';
import * as Error from './error';
import {
Token,
InstanceOpts,
Expand All @@ -13,31 +10,42 @@ import {
IAfterReturningAspectHookFunction,
IAfterThrowingAspectHookFunction,
IAroundHookOptions,
} from './declare';
import { markAsAspect, markAsHook } from './helper';
} from './types';
import {
addDeps,
getInjectorOfInstance,
getParameterDeps,
isToken,
markAsAspect,
markAsHook,
markInjectable,
setParameterIn,
setParameters,
} from './helper';
import { noInjectorError, notInjectError, tokenInvalidError } from './error';

/**
* 装饰一个 Class 是否是可以被依赖注入
* Decorate a Class to mark it as injectable
* @param opts
*/
export function Injectable(opts?: InstanceOpts): ClassDecorator {
return <T extends Function>(target: T) => {
Helper.markInjectable(target, opts);
markInjectable(target, opts);

const params = Reflect.getMetadata('design:paramtypes', target);
if (Array.isArray(params)) {
Helper.setParameters(target, params);
setParameters(target, params);

// 如果支持多例创建,就不检查构造函数依赖的可注入性
// If it supports multiple instances, do not check the injectability of the constructor dependencies
if (opts && opts.multiple) {
return;
}

// 检查依赖的可注入性
const depTokens = Helper.getParameterDeps(target);
// Check the injectability of the constructor dependencies
const depTokens = getParameterDeps(target);
depTokens.forEach((item, index) => {
if (!Helper.isToken(item)) {
throw Error.notInjectError(target, index);
if (!isToken(item)) {
throw notInjectError(target, index);
}
});
}
Expand All @@ -46,7 +54,7 @@ export function Injectable(opts?: InstanceOpts): ClassDecorator {

interface InjectOpts {
/**
* 默认值
* Default value when the token is not found
*/
default?: any;
}
Expand All @@ -57,7 +65,7 @@ interface InjectOpts {
*/
export function Inject(token: Token, opts: InjectOpts = {}): ParameterDecorator {
return (target, _: string | symbol | undefined, index: number) => {
Helper.setParameterIn(target, { ...opts, token }, index);
setParameterIn(target, { ...opts, token }, index);
};
}

Expand All @@ -67,7 +75,7 @@ export function Inject(token: Token, opts: InjectOpts = {}): ParameterDecorator
*/
export function Optional(token: Token = Symbol()): ParameterDecorator {
return (target, _: string | symbol | undefined, index: number) => {
Helper.setParameterIn(target, { default: undefined, token }, index);
setParameterIn(target, { default: undefined, token }, index);
};
}

Expand All @@ -84,22 +92,22 @@ export function Autowired(token?: Token, opts?: InstanceOpts): PropertyDecorator
realToken = Reflect.getMetadata('design:type', target, propertyKey);
}

if (!Helper.isToken(realToken)) {
throw Error.tokenInvalidError(target, propertyKey, realToken);
if (!isToken(realToken)) {
throw tokenInvalidError(target, propertyKey, realToken);
}

// 添加构造函数的依赖
Helper.addDeps(target, realToken);
// Add the dependency of the constructor
addDeps(target, realToken);

const descriptor: PropertyDescriptor = {
configurable: true,
enumerable: true,
get(this: any) {
if (!this[INSTANCE_KEY]) {
const injector = Helper.getInjectorOfInstance(this);
const injector = getInjectorOfInstance(this);

if (!injector) {
throw Error.noInjectorError(this);
throw noInjectorError(this);
}

this[INSTANCE_KEY] = injector.get(realToken, opts);
Expand Down
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Token } from './declare';
import { Context, Token } from './types';

function stringify(target: object | Token) {
if (typeof target === 'object') {
Expand Down
2 changes: 1 addition & 1 deletion src/factoryHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FactoryFunction } from './declare';
import { FactoryFunction } from './types';

import type { Injector } from './injector';

Expand Down
4 changes: 2 additions & 2 deletions src/helper/dep-helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { flatten, uniq } from './util';
import { Token } from '../declare';
import { flatten, uniq } from './utils';
import { Token } from '../types';
import { getParameterDeps } from './parameter-helper';
import { createConstructorMetadataManager } from './reflect-helper';

Expand Down Expand Up @@ -39,7 +39,7 @@

function getDepsWithCache(target: Token, cache: Map<any, Token[]>): Token[] {
if (cache.has(target)) {
return cache.get(target)!;

Check warning on line 42 in src/helper/dep-helper.ts

View workflow job for this annotation

GitHub Actions / ci

Forbidden non-null assertion
}

const scanned: Token[] = [];
Expand Down
Loading
Loading