diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f0b6e62..bf4b676 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,7 +4,6 @@ on: [push] jobs: build: - runs-on: ubuntu-latest strategy: @@ -12,18 +11,18 @@ jobs: node-version: [8.x, 10.x, 12.x] steps: - - uses: actions/checkout@v1 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - name: Install yarn - run: | - npm -g i yarn - - name: npm install, build, and test - run: | - yarn - yarn build --if-present - yarn test - env: - CI: true + - uses: actions/checkout@v1 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: Install yarn + run: | + npm -g i yarn + - name: npm install, build, and test + run: | + yarn + yarn build + yarn test + env: + CI: true diff --git a/.npmignore b/.npmignore index a95bd39..9e05af4 100644 --- a/.npmignore +++ b/.npmignore @@ -4,17 +4,23 @@ coverage/ # Source code src/ +# Logs +*.log + # Tests test/ *.test.js # Configs +.github/ +.vscode/ .travis.yml tsconfig.json typescript/ .eslintrc.yml .prettierrc.yml .huskyrc.json +.nvmrc # Lock files yarn.lock diff --git a/.vscode/settings.json b/.vscode/settings.json index d0283c7..3165469 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -33,4 +33,5 @@ "[typescriptreact]": { "editor.formatOnSave": false, }, + "markdown.extension.toc.levels": "1..3", } \ No newline at end of file diff --git a/README.md b/README.md index ad40dbc..2073729 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,25 @@ A lightweight dependency injection container for TypeScript/JavaScript for constructor injection. - + - [TSyringe](#tsyringe) - [Installation](#installation) - [API](#api) - [Decorators](#decorators) + - [injectable()](#injectable) + - [singleton()](#singleton) + - [autoInjectable()](#autoinjectable) + - [inject()](#inject) + - [injectAll()](#injectall) + - [scoped()](#scoped) - [Container](#container) - - [Manual resolution](#manual-resolution) + - [Injection Token](#injection-token) + - [Providers](#providers) + - [Register](#register) + - [Registry](#registry) + - [Resolution](#resolution) + - [Child Containers](#child-containers) - [Full examples](#full-examples) - [Example without interfaces](#example-without-interfaces) - [Example with interfaces](#example-with-interfaces) @@ -183,6 +194,26 @@ class Bar { } ``` +### scoped() + +Class decorator factory that registers the class as a scoped dependency within the global container. + +#### Available scopes +- ResolutionScoped + - The same instance will be resolved for each resolution of this dependency during a single + resolution chain +- ContainerScoped + - The dependency container will return the same instance each time a resolution for this dependency + is requested. This is similar to being a singleton, however if a child container is made, that child + container will resolve an instance unique to it. + +#### Usage + +```typescript +@scoped(Lifecycle.ContainerScoped) +class Foo {} +``` + ## Container The general principle behind [Inversion of Control](https://en.wikipedia.org/wiki/Inversion_of_control) (IoC) containers diff --git a/package.json b/package.json index 9815c0d..fa85494 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,20 @@ { "name": "tsyringe", - "version": "3.4.0", + "version": "4.0.0", "description": "Lightweight dependency injection container for JavaScript/TypeScript", "main": "dist/cjs/index.js", "module": "./dist/esm5/index.js", "es2015": "./dist/esm2015/index.js", "typings": "./dist/typings/index.d.ts", "scripts": { - "build": "npm run clean && npm run build:cjs && npm run build:es5 && npm run build:es2015 && npm run build:types", + "build": "yarn clean && yarn build:cjs && yarn build:es5 && yarn build:es2015 && yarn build:types", "build:cjs": "tsc", "build:es5": "tsc -p ./typescript/tsconfig.esm5.json", "build:es2015": "tsc -p ./typescript/tsconfig.esm2015.json", "build:types": "tsc -p ./typescript/tsconfig.types.json", "clean": "rimraf ./dist", - "test": "npm run lint && jest --config test/jest.config.js", - "test:inspect": "npm run lint && node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --config test/jest.config.js", + "test": "yarn lint && jest --config test/jest.config.js", + "test:inspect": "yarn lint && node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --config test/jest.config.js", "test:coverage": "jest --config test/jest.config.js --coverage", "lint": "eslint --ext \".js,.jsx,.ts,.tsx\" \"./src\"", "lint:fix": "eslint --fix --ext \".js,.jsx,.ts,.tsx\" \"./src\""