owja / ioc Goto Github PK
View Code? Open in Web Editor NEW:unicorn: lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
License: MIT License
:unicorn: lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
License: MIT License
Missing in Documentation (Readme):
a simple plugin functionality could fix some needs to get rid of some boilerplate code if for example a service should trigger a listener, subscribe and unsubscribe automatically on mount and unmount of components. Plugins are more versatile and easier to adapt than application specific code.
This is a replacement for #22
The README.md has to be checked. There is at least one error.
export const TYPE = {
"MyService" = Symbol("MyService"),
"MyOtherService" = Symbol("MyOtherService"),
};
should be
export const TYPE = {
MyService: Symbol("MyService"),
MyOtherService: Symbol("MyOtherService"),
};
2.7.0
to 2.8.0
.This version is covered by your current version range and after updating it in your project the build failed.
@typescript-eslint/parser is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
The new version differs by 20 commits.
a9117f5
chore: publish v2.8.0
46b58b4
feat(eslint-plugin): add rule restrict-template-expressions (#850)
42a48de
fix(eslint-plugin): [no-unnec-type-arg] throwing on call/new expr (#1217)
ba89168
fix(typescript-estree): correctly account for trailing slash in… (#1205)
eb83af1
fix(eslint-plugin): [require-await] better handle nesting (#1193)
9829dd3
fix(eslint-plugin): [nuta] correctly handle null/undefined separation (#1201)
d1de3a7
fix(eslint-plugin): [unified-signatures] crash: cannot read pro… (#1096)
9aee06c
fix(eslint-plugin): [indent] handle empty generic declarations (#1211)
74192f8
chore: bump dependencies (#1208)
e2008e3
fix(eslint-plugin): [indent] fix decorator type (#1189)
4fac6c5
fix(eslint-plugin): [no-unnecessary-cond] fix naked type param (#1207)
259ff20
feat(eslint-plugin): [no-type-alias] handle conditional types (#953)
9c8203f
fix(eslint-plugin): [camelcase] handle optional member expr (#1204)
d8b07a7
feat(eslint-plugin): add space-before-function-paren [extension] (#924)
ca41dcf
docs(eslint-plugin): brace-style as a replacement for one-line (#1202)
There are 20 commits in total.
See the full diff
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
I could need some help with testing and some people doing code review of my own work. Feel free to open Issue, make pull requests or just comment here. :)
22.9.0
to 22.10.0
.This version is covered by your current version range and after updating it in your project the build failed.
eslint-plugin-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
The new version differs by 7 commits.
28bd1dc
feat(rules): adds no-if
rule (#293)
7ebdc0e
chore: enforce import destructure order
31c7cef
chore: convert to import/export (#302)
9f858cb
chore: delete tests instead of ignoring them with babel
c595ba0
chore: do not include tests in published tarball
4b4eb78
chore: fix lint error in md file
d3ea720
chore(docs): fix typo (#304)
See the full diff
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
Some bugs causes eslint to fail. Unpin it in package.json after it is working again.
22.6.1
to 22.6.2
.This version is covered by your current version range and after updating it in your project the build failed.
eslint-plugin-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
The new version differs by 1 commits.
331457d
fix: move @typescript-eslint/experimental-utils to dependencies (#267)
See the full diff
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
24.0.19
to 24.0.20
.This version is covered by your current version range and after updating it in your project the build failed.
@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
People suggested to add some inline documentation because they have problems to understand how the library works. This should be fixed.
Add all needed unit tests
Is your feature request something you would just nice to see implemented?
It would be nice if a dependency which got injected will not resolve on every call on the propery which resolves the dependency. It would be nice too if this behavior can be configured.
Describe the solution you'd like
I am not shure which solution i wuld prefer. For myself i normaly use dependencies in singelton scope. But sometimes it could be usefull to resolve a dependency only once per instance. And this feature will improof performance too if caching is the default behavior.
Which implementation whould you prefer? Or keep the current state? Or any other suggestion?
1.9.0
to 1.10.0
.This version is covered by your current version range and after updating it in your project the build failed.
@typescript-eslint/eslint-plugin is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
Id like to be able to inject a class that requires arguments for it's constructor which would give more flexibility for some use cases.
Describe the solution you'd like
I think a rework of the Factory
type could do the trick, for now it doesn't accept any parameters 🤔
the idea, if possible, would be to usesomething like this
container.bind(<symbol>).toFactory( (...args: InjectedConstructorArgs) => new Injected(...args)) )
and then
resolveContainer(<symbol>)(...args)
Describe alternatives you've considered
put the constructor logic in another function that is called manually after injection
Hi, i use @owja/ioc
in Node.js app.
In my app i have a service (for example named MyService
) that has one dependency.
I use wire
function for define dependency.
In my case i have two containers in two apps and i want to use MyService
in both of containers
But the wire
function is created for a specific container strictly binds each service to the container.
In my case i act like this:
const createMyService = wire => class MyService {
constructor () {
wire(this, 'myDependency', TYPE.myDependency)
}
};
But it looks more like a trick.
How can i properly create service for use it in any container?
22.5.1
to 22.6.0
.This version is covered by your current version range and after updating it in your project the build failed.
eslint-plugin-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
The new version differs by 9 commits.
14d83ef
feat(rules): add no-commented-out rule (#262)
83ff198
chore: migrate no-jest-import to typescript (#259)
718c08c
chore: upgrade @typescript-eslint
ca2aa27
chore: port lowercase-name to TypeScript (#258)
3df0058
chore(ci): run danger with lts version of node
48e3a59
chore: precompile with babel (#257)
8670804
chore: bump deps
05eb11a
chore: fix lint error
dff6446
docs: link to eslint-plugin-jest-formatting
See the full diff
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
Make it possible to bind in in singleton scope.
.to().inSingeltonScope()
.toFactory().inSingeltonScope()
.toValue()
is not affected.
Hey, I might need to download a bundle containing a service when resolving it so I need a way to .get
asynchronously and have async factories (like in InversifyJS for example).
Alternatively if plugins could return a Promise I guess that would also work for this use case.
This should also be possible:
@inject(SERVICE, TAG);
Currently tags hav to be a array:
@inject(SERVICE, [TAG]);
Describe the bug
The Container.bind(...).toValue(...) consider the a given function as a fatory.
To Reproduce
import { Container, token } from "@owja/ioc"
const TYPE = {
myType: token("MyType")
}
const container = new Container()
container.bind(TYPE.myType).toValue((text) => {
if (text === undefined) console.log("You cannot give undefined.")
else console.log(text)
})
const myFunc = container.get(TYPE.myType)
myFunc("hello, world!")
the console output is
You cannot give undefined.
file:///test.js:11
myFunc("hello, world!")
^
TypeError: myFunc is not a function
Expected behavior
I want to bind a Getter function, such as (options) => create(options) with toValue
, however, the container considered it as a factory and tried to call it and retrieved the return value.
I don't think toValue()
should detect its type under the hood, which made me confused.
Instead, toFactory()
is more suitable for this.
Environment (please complete the following information):
Additional context
The workaround is to wrap the function with a new function.
container.bind(TYPE.myType).toValue(() => (text) => {
if (text === undefined) console.log("You cannot give undefined.")
else console.log(text)
})
But it makes the typing sucks in typescript.
Because the token should be token<()=>(text:string)=>void>("MyType")
, then the return value from container.get(...)
is typed by ()=>(text:string)=>void
but (text:string)=>void
is actually expected.
This is how I personally like to use this library in my project:
import {Container} from "@owja/ioc";
const container = new Container();
const BAR = Symbol.for("bar")
const FOO = Symbol.for("foo")
interface Foo {
doThing(): void
tellBarToDoThing(): void
}
class AppFoo implements Foo {
constructor(private bar: Bar = container.get(BAR)) {}
doThing(): void {
console.log('foo do thing')
}
tellBarToDoThing() {
this.bar.doThing()
}
}
container.bind<Foo>(FOO).to(AppFoo)
interface Bar {
doThing(): void
tellFooToDoThing(): void
}
class AppBar implements Bar {
constructor(private foo: Foo = container.get(FOO)) {}
doThing() {
console.log('bar do thing')
}
tellFooToDoThing() {
this.foo.doThing()
}
}
// You don't need to use `toFactory()` below, but here just for example sake.
container.bind<Bar>(BAR).toFactory(() => { return new AppBar() }).inSingletonScope()
let foo: Foo = container.get(FOO)
foo.doThing()
let bar: Bar = container.get(BAR)
bar.doThing()
The code to look at here is the constructors of the classes. This is how I like to resolve the dependencies for my classes instead of wire()
or resolve()
that the readme suggests.
Wanted to see what you thought. Do you see anything wrong with this? Do you see it as a value add to add this technique to the readme as another option to resolving dependencies?
Allready in progress: #51
Hi, first of all thanks for pretty good library.
I noticed that container can have an implicit behaviour.
According to api, i can bind service to symbol and save options
to the variable.
Later I can start get services from the container by symbol.
But after that I can use options to change type of service by options.toSingletonScope()
After changing service type I will receive only one instance.
I don't know, maybe it is a regular behaviour but it can confuse the developer.
Maybe I'm wrong and this behaviour implies that at the time of binding, the developer may not know what the service should be.
P.S. Sorry for bug
label...
2.10.0
to 2.11.0
.This version is covered by your current version range and after updating it in your project the build failed.
@typescript-eslint/eslint-plugin is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
The new version differs by 6 commits.
9f76095
chore: publish v2.11.0
324f155
docs(eslint-plugin): ROADMAP note some jsdoc rules (#1319)
3ddf1a2
fix(eslint-plugin): [brace-style] handle enum declarations (#1281)
b5a52a3
feat(eslint-plugin): [member-ordering] add index signature (#1190)
39f654b
test(eslint-plugin): [unified-signatures] add tests for #740 (#1074)
5362561
docs(eslint-plugin): typo in space-before-function-paren docs (#1297)
See the full diff
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
1.9.0
to 1.10.0
.This version is covered by your current version range and after updating it in your project the build failed.
@typescript-eslint/parser is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
Hello,
First of all, I want to thank you for your awesome project !
I'm currently using the version 2.0.0-alpha.3
on a REACT game project.
For my project I would like to be able to test multiple JSX versions of the same component on the fly.
I'd like for all my components to come with a SVG
version, a WebGL
and canvas
one.
For this issue I assumed I have two redering method svg
and svg2
(canvas
and webGL
cause issues due to useEffect being used).
I have the logic for my component in a vanilla class and injected render
method based on a token.
I think (and hope) the code is kinda self explanatory.
Here are the files I use for now
container.ts
containing tokens for the rendering methods.export type renderingMethods = 'canvas' | 'svg' | 'svg2' | 'webGL';
const container = new Container();
export const resolve = createResolve(container);
export const get = container.get;
export const RENDERERS: { [K in renderingMethods]: Token<FunctionComponent<Props>> } = {
canvas: token('canvas'),
svg: token('svg'),
svg2: token('svg2'),
webGL: token('webGL')
};
container.bind(RENDERERS.canvas).toValue(BoardCanvas);
container.bind(RENDERERS.svg).toValue(BoardSVG);
container.bind(RENDERERS.svg2).toValue(BoardSVG2);
container.bind(RENDERERS.webGL).toValue(BoardWebGL);
App.tsx
with a select in the header to choose the rendering method.function App() {
const board = new Board({ x: 200, y: 200 });
const [renderer, setRenderer] = useState<renderingMethods>('svg');
return (
<>
<header>
<label>
Rendering Method
<br />
<select value={renderer} onChange={({ target: { value } }) => setRenderer(value as renderingMethods)}>
<option value="canvas">Canvas</option>
<option value="svg">SVG</option>
<option value="svg2">SVG2</option>
<option value="webGL">WebGL</option>
</select>
</label>
{renderer}
</header>
<div className="App">{board.render(renderer)}</div>
</>
);
}
Board.ts
export class Board implements board {
bounds: BoardRanges;
constructor(bounds: BoardRanges) {
this.bounds = {
x: Math.abs(bounds.x),
y: Math.abs(bounds.y)
};
}
readonly render = (renderer: renderingMethods) => resolve(RENDERERS[renderer])()({ bounds: this.bounds });
}
svg
rendererexport const BoardSVG: FunctionComponent<Props> = ({ bounds }: Props) => {
console.log('using board svg');
return (
<svg width={bounds.x} height={bounds.y} className="game-board">
<g transform={`matrix(1 0 0 -1 ${bounds.x / 2} ${bounds.y / 2})`}>
<line x1={-bounds.x / 2} x2={bounds.x / 2} strokeWidth="1px" stroke="#000" />
<line y1={-bounds.y / 2} y2={bounds.y / 2} strokeWidth="1px" stroke="#000" />
</g>
</svg>
);
};
svg2
rendererexport const BoardSVG2: FunctionComponent<Props> = ({ bounds }: Props) => {
console.log('using board svg2');
return (
<svg width={bounds.x} height={bounds.y} className="game-board">
<g transform={`matrix(1 0 0 -1 ${bounds.x / 2} ${bounds.y / 2})`}>
<line x1={-bounds.x / 2} x2={bounds.x / 2} strokeWidth="1px" stroke="#333" />
<line y1={-bounds.y / 2} y2={bounds.y / 2} strokeWidth="1px" stroke="#333" />
</g>
</svg>
);
};
the UI/injection doesn't seem to reload as it always giving me the console.log('using board svg');
output.
I'm not really sure if it's a problem with the lib or with me not doing things correctly
create a new repository with examples
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.