Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
reactive-di
Advanced tools
Typesafe dependency injection container for react-like components.
npm install --save reactive-di lom_atom babel-plugin-transform-metadata
Example .babelrc:
{
"presets": [
"flow",
"react",
["es2015", {"loose": true}]
],
"plugins": [
"transform-metadata",
"transform-decorators-legacy",
["transform-react-jsx", {"pragma": "lom_h"}]
]
}
babel-plugin-transform-metadata is optional, used for metadata generation. ReactiveDI use type annotations for dependency resolving, without this plugin you will need to provide metadata manually.
Build rdi and copy to ../app-project/node_modules/reactive-di
npm run watch --reactive-di:dest=../app-project
ReactiveDI has no static dependencies and not a zero-setup library. Setup is usually about 30-50 SLOC, but you do it once per application. But you can integrate into ReactiveDI any component react-like, state management and css-in-js library via adapters.
// @flow
import {detached, mem, AtomWait, action} from 'lom_atom'
import {createReactWrapper, createCreateElement} from 'reactive-di'
import {render, h, Component} from 'preact'
function ErrorableView({error}: {error: Error}) {
return <div>
{error instanceof AtomWait
? <div>
Loading...
</div>
: <div>
<h3>Fatal error !</h3>
<div>{error.message}</div>
<pre>
{error.stack.toString()}
</pre>
</div>
}
</div>
}
const lomCreateElement = createCreateElement(
createReactWrapper(
Component,
ErrorableView,
detached
),
(h: React$CreateElement)
)
global['lom_h'] = lomCreateElement
Usage:
import {mem} from 'lom_atom'
import {props} from 'reactive-di'
import {render} from 'preact'
interface IHelloProps {
name: string;
}
class HelloContext {
@mem name: string
@props set props({name}: IHelloProps) {
this.name = name
}
}
export function HelloView(
_: IHelloProps,
{context}: {context: HelloContext}
) {
return <div>
Hello, {context.name}
<br/><input value={context.name} onInput={({target}) => {
context.name = (target: any).value
}} />
</div>
}
render(<HelloView name="John" />, document.body)
// @flow
import {Reaction} from 'mobx'
import {createReactWrapper, createCreateElement, createMobxDetached} from 'reactive-di'
import {createElement, Component} from 'react'
import {render} from 'react-dom'
function ErrorableView({error}: {error: Error}) {
return <div>
{error instanceof AtomWait
? <div>
Loading...
</div>
: <div>
<h3>Fatal error !</h3>
<div>{error.message}</div>
<pre>
{error.stack.toString()}
</pre>
</div>
}
</div>
}
const lomCreateElement = createCreateElement(
createReactWrapper(
Component,
ErrorableView,
createMobxDetached(Reaction)
),
createElement
)
global['lom_h'] = lomCreateElement
Usage:
import {observable} from 'mobx'
import {props} from 'reactive-di'
import {render} from 'preact'
interface IHelloProps {
name: string;
}
class HelloContext {
@observable name: string
@props set props({name}: IHelloProps) {
this.name = name
}
}
export function HelloView(
_: IHelloProps,
{context}: {context: HelloContext}
) {
return <div>
Hello, {context.name}
<br/><input value={context.name} onInput={({target}) => {
context.name = (target: any).value
}} />
</div>
}
render(<HelloView name="John" />, document.body)
You can use context in stateless functional components. With babel-plugin-transform-metadata you do not need to provide metadata (like Button.contextTypes = {color: PropTypes.string};
).
Context signature generated from second argument:
// @flow
export function HelloView(
_: IHelloProps,
{context}: {context: HelloContext}
) { ... }
Or
function HelloView(
_: {},
context: HelloComponent
) {
/// ...
}
Class definitions used as keys for dependency resolving. For generation dependency metadata ReactiveDI do not use any library (like props-types), raw metadata only expose function arguments to injector. Without plugin, you will need to provide them manually:
HelloView.deps = [{context: HelloContext}]
Injector in createElement (lom_h) automatically initializes HelloContext and pass it to HelloView in
render(<HelloView prefix="Hello" />, document.body)
ReactiveDI is state management agnostic library. You can use mobx or lom_atom (like mobx, but much simpler and with some killer features). In ReactiveDI all components are pure functional.
All errors are isolated in components. They do not breaks whole application. You don't need to manually catch errors via componentDidCatch.
class HelloContext {
@mem get name() {
throw new Error('oops')
}
}
function HelloView(
_: {},
{context}: {context: HelloContext}
) {
return <input value={context.name} onInput={({target}: Event) => {
context.name = (target: any).value
}} />
}
Exception in get name
intercepted by try/catch in HelloView wrapper and displays by default ErrorableView, registered in ReactiveDI setup:
// ...
function ErrorableView({error}: {error: Error}) {
return <div>
{error instanceof AtomWait
? <div>
Loading...
</div>
: <div>
<h3>Fatal error !</h3>
<div>{error.message}</div>
<pre>
{error.stack.toString()}
</pre>
</div>
}
</div>
}
const lomCreateElement = createCreateElement(
createReactWrapper(
Component,
ErrorableView,
detached
),
h
)
global['lom_h'] = lomCreateElement
You can provide custom error component handler:
function HelloView(
_: {},
{context}: {context: HelloContext}
) {
let name: string
try {
name = context.name
} catch (e) {
name = 'Error:' + e.message
}
return <input value={name} onInput={{target}: Event) => {
context.name = (target: any).value
}} />
}
HelloView.onError = ({error: Error}) => (
<div>{error.message}</div>
)
Or manually handle error:
function HelloView(
_: {},
{context}: {context: HelloContext}
) {
let name: string
try {
name = context.name
} catch (e) {
name = 'Error:' + e.message
}
return <input value={name} onInput={{target}: Event) => {
context.name = (target: any).value
}} />
}
In ReactiveDI pending/complete status realized via exceptions. Special user defined "Wait" exception can be handled in ErrorableView.
function ErrorableView({error}: {error: Error}) {
return <div>
{error instanceof AtomWait
? <div>
Loading...
</div>
: ...
}
</div>
}
In component model throw new AtomWait()
catched in HelloComponent wrapper and default ErrorableView shows Loading...
instead of HelloView.
class HelloContext {
@mem set name(next: string | Error) {}
@mem get name(): string {
// fetch some data and update name
throw new AtomWait()
}
}
On fetch complete fetch().then((data: string) => {this.name = data})
sets new data and render HelloView instead of ErrorableView.
Class SomeAbstract used somewhere in the application, but at ReactiveDI setup you can redefine them to SomeConcrete class instance with same interface.
class SomeAbstract {}
class SomeConcrete extends SomeAbstract {}
class C {
a: SomeAbstract
constructor(a: SomeAbstract) {
this.a = a
}
}
const injector = new Injector(
[
[SomeAbstract, new SomeConcrete()]
]
)
injector.value(SomeAbstract).a instanceof SomeConcrete
In vue you can use content distribution with slots. ReactiveDI helps you to do same thing in react-applications. Slot is a component itself or its id.
Create slightly modified component, based on FirstCounterView.
import {cloneComponent} from 'reactive-di'
class FirstCounterService {
@mem value = 0
}
function CounterMessageView({value}: {value: string}) {
return <div>count: {value}</div>
}
function FirstCounterView(
_: {},
counter: FirstCounterService
) {
return <div>
<CounterMessageView value={counter.value}/>
<button id="FirstCounterAddButton" onClick={() => { counter.value++ }}>Add</button>
</div>
}
class SecondCounterService {
@mem value = 1
}
// Create FirstCounterView copy, but remove FirstCounterAddButton and replace FirstCounterService to SecondCounterService.
const SecondCounterView = cloneComponent(FirstCounterView, [
[FirstCounterService, SecondCounterService],
['FirstCounterAddButton', null],
], 'SecondCounterView')
Works like inheritance in classes, but you don't need to extract each component detail in methods. Any component part is open for extension bу default. Be careful, do not violate Liskov substitution principle.
Each component instance has an own injector. Injector - is a type to instance map, which types described in component context.
When Parent and Child components depends on on same SharedService - DI injects one instance to them. And this instance live while Parent component mounted to DOM.
class SharedService {}
function Parent(
props: {},
context: {sharedService: SharedService}
) {
return <Child parentService={context.sharedService} />
}
function Child(
_: {},
context: {sharedService: SharedService}
) {
// context.sharedService instance cached in parent
}
If only Child component depends on SharedService, DI creates separated SharedService instance per Child.
class SharedService {}
function Parent() {
return <Child/>
}
function Child(
props: {},
context: {sharedService: SharedService}
) {
// sharedService - cached in child
}
Css-in-js with reactivity and dependency injection power. ReactiveDI not statically depended on Jss, you can integrate another css-in-js solution, realizing described below interface.
Setup:
// @flow
import {detached, mem} from 'lom_atom'
import {createReactWrapper, createCreateElement, Injector} from 'reactive-di'
import {h, Component} from 'preact'
import {create as createJss} from 'jss'
import ErrorableView from './ErrorableView'
const jss = createJss()
/*
jss must implements IProcessor interface:
export interface IProcessor {
createStyleSheet<V: Object>(_cssObj: V, options: any): ISheet<V>;
}
export interface ISheet<V: Object> {
attach(): ISheet<V>;
classes: {+[id: $Keys<V>]: string};
}
*/
const lomCreateElement = createCreateElement(
createReactWrapper(
Component,
ErrorableView,
detached,
new Injector([], jss)
),
h
)
global['lom_h'] = lomCreateElement
Reactive style usage:
import {mem} from 'lom_atom'
import {theme} from 'reactive-di'
class ThemeVars {
@mem color = 'red'
}
class MyTheme {
vars: ThemeVars
constructor(vars: ThemeVars) {
this._vars = vars
}
@mem @theme get css() {
return {
wrapper: {
backgroundColor: this._vars.color
}
}
}
}
function MyView(
props: {},
{theme: {css}, vars}: {theme: MyTheme, vars: ThemeVars}
) {
return <div class={css.wrapper}>...
<button onClick={() => vars.color = 'green'}>Change color</button>
</div>
}
Styles automatically mounts/unmounts together with component. Changing vars.color
automatically rebuilds and remounts css.
With mobx, unmount feature does not works at current moment. But still no memory leaks, due to unique theme id.
Without any state management library works only css mounting without reactivity updates.
import {theme} from 'reactive-di'
class MyTheme {
@theme get css() {
return {
wrapper: {
backgroundColor: 'red'
}
}
}
}
function MyView(
props: {},
{theme: {css}, vars}: {theme: MyTheme}
) {
return <div class={css.wrapper}>...</div>
}
By default one css block generated per component. But you can generate unique css block per component instance too. Just use theme.self
decorator:
import {mem} from 'lom_atom'
import {props, theme} from 'reactive-di'
interface MyProps {
color: string;
}
class MyTheme {
@mem @props _props: MyProps
@mem @theme.self get css() {
return {
wrapper: {
backgroundColor: this.props.color
}
}
}
}
function MyView(
props: MyProps,
{theme: {css}}: {theme: MyTheme}
) {
return <div class={css.wrapper}>...
</div>
}
<MyView color="red"/>
<MyView color="blue"/>
You can pass component properties to its dependencies via prop
decorator.
import {mem} from 'lom_atom'
import {props} from 'reactive-di'
interface MyProps {
some: string;
}
class MyViewService {
@props _props: MyProps;
// @mem @props _props: MyProps; // for reactive props
@mem get some(): string {
return this._props.some + '-suffix'
}
}
function MyView(
props: MyProps,
{service}: {service: MyViewService}
) {
return <div>{service.some}</div>
}
If you need to react on props changes - just use combination @mem
and @props
decorators.
class MyViewService {
@mem @props _props: MyProps;
// @mem @props _props: MyProps; // for reactive props
@mem get some(): string {
return this._props.some + '-suffix'
}
}
You can use any react/preact/inferno components together with rdi components.
Not ReactiveDI part, in state management libraries you can monitor state changes and user actions.
Console logger in lom_atom:
import {defaultContext, BaseLogger, ConsoleLogger} from 'lom_atom'
import type {ILogger} from 'lom_atom'
defaultContext.setLogger(new ConsoleLogger())
For custom loggers, implement interface ILogger.
Experimental feature - you can restore state on client side by providing data to class map in Injector.
// @flow
import {mem} from 'lom_atom'
import {Injector} from 'reactive-di'
const defaultDeps = []
const injector = new Injector([], undefined, {
SomeService: {
name: 'test',
id: 123
}
})
class SomeService {
// setup babel-plugin-transform-metadata or define displayName, if js-uglify used
static displayName = 'SomeService'
@mem name = ''
id = 0
}
const someService: SomeService = injector.value(SomeService)
someService.name === 'test'
someService.id === 123
displayName in class used as a key for data mapping. babel-plugin-transform-metadata can generate displayName. To enable it, add ["transform-metadata", {"addDisplayName": true}]
into .babelrc.
Example .babelrc:
{
"presets": [
"flow",
"react",
["es2015", {"loose": true}]
],
"plugins": [
["transform-metadata", {"addDisplayName": true}],
"transform-decorators-legacy",
["transform-react-jsx", {"pragma": "lom_h"}]
]
}
6.1.0 (2018-02-03)
<a name="6.0.9"></a>
FAQs
Reactive dependency injection
The npm package reactive-di receives a total of 14 weekly downloads. As such, reactive-di popularity was classified as not popular.
We found that reactive-di demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.