@mreinlaender/i-inject
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

i-inject

A scoped injection-container intended for use in a Routed SPA, written in TypeScript.

Concept

Register stores with n-scopes

class Test {
 public static typeName = "Test";

}

// register(type: T, ...scopes[])
inject.register(Test, '/test/*', '/something/differen/test'

Use the wildcard '*' to permit everything further "down" the current scope.

Leading to

inject.register(Test, '*');

being allways available

Keep the current-scope in line with window.location.pathname using the changeTo-function

React example using react-router-dom:

const location = useLocation();

useEffect(() => {
    inject.changeTo(location.pathname)
}, [location])

Enjoy accessing singleton instances from where you want, and only from there.

Instances and contained data will be dropped after a change to the current-scope, providing a particularly safe / clean working environment.

inject.register(Test, '/test/:id', '/something/differen/test'
inject.enter('/test/123');

expect(inject.get(Test)).toBeDefined();

inject.changeTo('/wont/be/active);

expect(inject.get(Test)).toBeUndefined();

Readme

Keywords

none

Package Sidebar

Install

npm i @mreinlaender/i-inject

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

9.04 kB

Total Files

5

Last publish

Collaborators

  • mreinlaender