
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
immutable-di
Advanced tools
Simple, promise-based dependency injection container with some state handling functions (for facebook flux-like state-management)
Simple, dependency injection container with some state handling functions.
npm install --save immutable-di immutable-di-react
npm test
npm run dev.examples
Our main focus make Flux-like API as less and simple as possible. Which with less words you can express more. The ideas behind similar to the redux, baobab, nuclear-js, but implementation based on dependency injection. And of course you can use dependency injection as standalone.
import {default as React, Component} from 'react';
import {Getter, Facet, Factory, Setter} from 'immutable-di/define'
import root from 'immutable-di-react/root'
import statefull from 'immutable-di-react/statefull'
import Container from 'immutable-di'
import NativeCursor from 'immutable-di/cursors/native'
const cursor = new NativeCursor({
tis: {
a: 1,
b: 2
}
})
const container = new Container(cursor)
var abbaFacet = Facet({
a: ['tis', 'a']
})(function bloomyFacet({a}) {
return a + 10;
})
var ChangeAction = Factory({
setA: Setter(['tis', 'a']),
setIsLoading: Setter(['tis', 'isLoading'])
})(function ({setA, setIsLoading}) {
return function (num) {
// Here will be all mutate state logic. for example server side request
// communication with API layer and etc.
setIsLoading(true);
$.get('/server/route').then((data) => {
setIsLoading(false);
var a = num + data.a;
setA(a);
})
}
});
@root()
@statefull({
abba: abbaFacet,
changeAction: ChangeAction
})
class Application extends React.Component {
handleClick () {
this.props.changeAction(100);
}
render () {
return <div onClick={this.handleClick.bind(this)}>Bloom: {this.props.abba}</div>
}
}
export default function () {
React.render(<Application container={container} />, document.querySelector('.app'));
}
import {Facet, Factory, Class} from 'immutable-di/define'
// A, B - functions or classes with di definitions
// For functions:
Factory([A, B])(C) // resolve functions A, B and pass them as arguments to C
Factory({a: A, b: B})(C) // resolve functions A, B and pass them as object {a, b} to C
//Facet - same as Factory, but do not cache factory return value
// For classes:
@Class([A, B])
class C {
constructor(a, b) {
}
}
// or
class C {
constructor(a, b) {
}
}
export default Class([A, B])(C)
// or
@Class({
a: A,
b: B
})
class C {
constructor({a, b}) {
}
}
// for State
@Class([
A,
B,
options: ['config', 'c']
])
class C {
constructor(a, b, options) {
}
}
import Container from 'immutable-di'
import NativeCursor from 'immutable-di/cursors/native'
const cursor = new NativeCursor({
config: {
logger: {
opt1: 'test1'
},
mod2: {
opt1: 'test2'
}
}
})
// define di container with state:
const container = new Container(cursor)
// dep 1:
function MyFaset(state) {
return 'data'
}
Factory()(MyFaset)
// dep 2:
function myHandler({state, faset}) {
console.log(state, faset)
}
// bind listener:
const listener = container.on({
state: ['config', 'logger'],
faset: MyFaset
}, myHandler)
// trigger my hander
cursor.select(['config', 'logger', 'opt1']).set('test')
// path config.logger not affected, myHandler is not triggered
cursor.select(['config', 'mod2', 'opt1']).set('1')
// unbind listener:
container.off(listener)
import Container from 'immutable-di'
import {Factory, Class} from 'immutable-di/define'
import NativeCursor from 'immutable-di/cursors/native'
const cursor = new NativeCursor({
config: {
logger: {
opt1: 'test1'
}
}
})
const container = new Container(cursor)
function ConsoleOutputDriver() {
return function consoleOutputDriver(str) {
console.log(str)
}
}
Factory()(ConsoleOutputDriver)
@Class([
ConsoleOutputDriver,
['config', 'logger']
])
class Logger {
constructor(outputDriver, dep, config) {
this._outputDriver = outputDriver
this._config = config
}
log(val) {
this._outputDriver('val:' + val + ', opt:' + this._config.opt1)
}
}
function SomeDep() {
return 'dep'
}
Factory()(SomeDep)
function App({logger, someDep}) {
return function app(val) {
logger.log(val + someDep)
}
}
Factory({
logger: logger,
someDep: SomeDep
})(App)
container.get(App)('test') // outputs: val: testdep, opt: test1
import Container from 'immutable-di'
import {Factory, Class} from 'immutable-di/define'
import NativeCursor from 'immutable-di/cursors/native'
const cursor = new NativeCursor({
config: {
myModule: {
opt1: 'test1'
}
}
})
const container = new Container(cursor)
function MyModule({opt1}) {
console.log('init', opt1)
return function myModule(val) {
console.log('out', opt1, ', val', val)
}
}
Factory([
['config', 'myModule']
])(MyModule)
container.get(MyModule) // outputs init test1
container.get(MyModule) // no outputs, return from cache
const cursor = cursor.select(['config', 'myModule', 'opt1'])
cursor.set('test2') // outputs test2
container.get(MyModule) // no outputs: return from cache
container.get(MyModule)('test3') // outputs out test2, val test3
// my-faset.js
function myFaset(todos) {
return todos.map(todo => todo.id + '-mapped')
}
export default Factory([
['todoApp', 'todos']
])(myFaset)
// todo-list.js
import statefull from 'immutable-di-react/statefull'
import root from 'immutable-di-react/root'
import TodoListItem from './todo-list-item'
import myFaset from './my-faset'
import TodoActions from './todo-actions'
// set container from props to context:
@root()
// bind to setState:
@statefull({
todos: ['todoApp', 'todos'], // state path
query: ['todoApp', 'query'], // state path
mapped: myFaset, // faset
actions: TodoActions // class with actions
})
export default class TodoList extends React.Component {
render({todos, mapped, actions}) {
return (
<div>
<div>
<h3>Mapped todo ids:</h3>
{mapped.toString()}
</div>
<ul>
{todos.map(todo => (
<TodoListItem todo={todo}/>
))}
</ul>
</div>
)
}
}
// todo-list-item.js
import React from 'react'
import widget from 'immutable-di-react/widget'
import di from 'immutable-di-react/di'
import TodoActions from './todo-actions'
function TodoListItem({todo, editMode, actions}) {
return (
<li className='todos-list-item'>
{todo.title}
</li>
)
}
export default Di({
actions: TodoActions
})(widget(TodoListItem))
// todo-actions.js
import Container from 'immutable-di'
import {Class} from 'immutable-di/define'
@Class([Container])
export default class TodoActions {
constructor(container) {
this._cursor = cursor.select(['todoApp'])
}
addTodo(todo) {
this._cursor.apply(['todos'] => todos.concat(todo))
}
}
// index.js
import React from 'react'
import Container from 'immutable-di'
import NativeCursor from 'immutable-di/cursors/native'
import TodoList from './todo-list'
// define di container with state:
const cursor = new NativeCursor({
todoApp: {
todos: [],
query: {
}
}
})
const container = new Container(cursor)
const initialProps = cursor.select(['todoApp']).get()
React.render(<TodoList ...initialProps container={container}/>, document.querySelector('body'))
import {Factory, Setter} from 'immutable-di/define'
import Container from 'immutable-di'
import NativeCursor from 'immutable-di/cursors/native'
import MonitorFactory from 'immutable-di/history/MonitorFactory'
Factory.extend = MonitorFactory
function showChanges(history) {
console.log(history)
}
const action = Factory([
Setter(['tis', 'a'])
])(function MyAction(setA) {
return function myAction(value) {
setA(value)
}
})
const cursor = new NativeCursor({
tis: {
a: 1,
b: 2
}
})
const container = new Container(cursor)
const listener = container.on([
['__history']
], showChanges)
container.get(action)(123)
// Will produce:
/*
[
{ "displayName": "MyAction", "id": 11, "args": [ 123 ], "diff": {} }
]
*/
container.off(listener)
NativeCursor:diff used for diff generation, this dummy, but NativeCursor can be extended.
FAQs
Simple, promise-based dependency injection container with some state handling functions (for facebook flux-like state-management)
The npm package immutable-di receives a total of 8 weekly downloads. As such, immutable-di popularity was classified as not popular.
We found that immutable-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.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.