Comparing version 1.1.2 to 1.1.3
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="1.1.3"></a> | ||
## [1.1.3](https://github.com/zerkalica/lom_atom/compare/v1.1.2...v1.1.3) (2017-09-20) | ||
<a name="1.1.2"></a> | ||
@@ -7,0 +12,0 @@ ## [1.1.2](https://github.com/zerkalica/lom_atom/compare/v1.1.1...v1.1.2) (2017-09-13) |
@@ -545,3 +545,3 @@ // eslint-disable-line | ||
forcedFn.displayName = name + "*"; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
@@ -583,2 +583,10 @@ return { | ||
function setFunctionName(fn, name) { | ||
Object.defineProperty(fn, 'name', { | ||
value: name, | ||
writable: false | ||
}); | ||
fn.displayName = name; | ||
} | ||
function memProp(proto, name, descr, normalize) { | ||
@@ -591,3 +599,7 @@ var handlerKey = name + "$"; | ||
proto[handlerKey] = descr.get === undefined && descr.set === undefined ? createValueHandler(descr.initializer) : createGetSetHandler(descr.get, descr.set); | ||
if (descr.initializer) setFunctionName(descr.initializer, name); | ||
if (descr.get) setFunctionName(descr.get, "get#" + name); | ||
if (descr.set) setFunctionName(descr.set, "set#" + name); | ||
var handler = proto[handlerKey] = descr.get === undefined && descr.set === undefined ? createValueHandler(descr.initializer) : createGetSetHandler(descr.get, descr.set); | ||
setFunctionName(handler, name + "()"); | ||
var hostAtoms = new WeakMap(); | ||
@@ -676,3 +688,3 @@ Object.defineProperty(proto, name + "()", { | ||
forcedFn.displayName = name + "*"; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
@@ -794,3 +806,3 @@ return { | ||
action.displayName = hk; | ||
setFunctionName(action, hk); | ||
return action; | ||
@@ -837,3 +849,3 @@ } | ||
action.displayName = name || fn.displayName || fn.name; | ||
setFunctionName(action, name || fn.displayName || fn.name); | ||
return action; | ||
@@ -840,0 +852,0 @@ } |
@@ -549,3 +549,3 @@ 'use strict'; | ||
forcedFn.displayName = name + "*"; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
@@ -587,2 +587,10 @@ return { | ||
function setFunctionName(fn, name) { | ||
Object.defineProperty(fn, 'name', { | ||
value: name, | ||
writable: false | ||
}); | ||
fn.displayName = name; | ||
} | ||
function memProp(proto, name, descr, normalize) { | ||
@@ -595,3 +603,7 @@ var handlerKey = name + "$"; | ||
proto[handlerKey] = descr.get === undefined && descr.set === undefined ? createValueHandler(descr.initializer) : createGetSetHandler(descr.get, descr.set); | ||
if (descr.initializer) setFunctionName(descr.initializer, name); | ||
if (descr.get) setFunctionName(descr.get, "get#" + name); | ||
if (descr.set) setFunctionName(descr.set, "set#" + name); | ||
var handler = proto[handlerKey] = descr.get === undefined && descr.set === undefined ? createValueHandler(descr.initializer) : createGetSetHandler(descr.get, descr.set); | ||
setFunctionName(handler, name + "()"); | ||
var hostAtoms = new WeakMap(); | ||
@@ -680,3 +692,3 @@ Object.defineProperty(proto, name + "()", { | ||
forcedFn.displayName = name + "*"; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
@@ -798,3 +810,3 @@ return { | ||
action.displayName = hk; | ||
setFunctionName(action, hk); | ||
return action; | ||
@@ -841,3 +853,3 @@ } | ||
action.displayName = name || fn.displayName || fn.name; | ||
setFunctionName(action, name || fn.displayName || fn.name); | ||
return action; | ||
@@ -844,0 +856,0 @@ } |
@@ -551,3 +551,3 @@ (function (global, factory) { | ||
forcedFn.displayName = name + "*"; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
@@ -589,2 +589,10 @@ return { | ||
function setFunctionName(fn, name) { | ||
Object.defineProperty(fn, 'name', { | ||
value: name, | ||
writable: false | ||
}); | ||
fn.displayName = name; | ||
} | ||
function memProp(proto, name, descr, normalize) { | ||
@@ -597,3 +605,7 @@ var handlerKey = name + "$"; | ||
proto[handlerKey] = descr.get === undefined && descr.set === undefined ? createValueHandler(descr.initializer) : createGetSetHandler(descr.get, descr.set); | ||
if (descr.initializer) setFunctionName(descr.initializer, name); | ||
if (descr.get) setFunctionName(descr.get, "get#" + name); | ||
if (descr.set) setFunctionName(descr.set, "set#" + name); | ||
var handler = proto[handlerKey] = descr.get === undefined && descr.set === undefined ? createValueHandler(descr.initializer) : createGetSetHandler(descr.get, descr.set); | ||
setFunctionName(handler, name + "()"); | ||
var hostAtoms = new WeakMap(); | ||
@@ -682,3 +694,3 @@ Object.defineProperty(proto, name + "()", { | ||
forcedFn.displayName = name + "*"; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
@@ -800,3 +812,3 @@ return { | ||
action.displayName = hk; | ||
setFunctionName(action, hk); | ||
return action; | ||
@@ -843,3 +855,3 @@ } | ||
action.displayName = name || fn.displayName || fn.name; | ||
setFunctionName(action, name || fn.displayName || fn.name); | ||
return action; | ||
@@ -846,0 +858,0 @@ } |
{ | ||
"name": "lom_atom", | ||
"version": "1.1.2", | ||
"description": "Observable state management", | ||
"version": "1.1.3", | ||
"description": "Alternative implementation of eigenmethod mol_atom state management library", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "registry": "https://registry.npmjs.org" |
180
README.md
@@ -7,3 +7,3 @@ # lom_atom | ||
* Tiny size (about 7kb minified) | ||
* About 11kb minified | ||
* Memory-efficient | ||
@@ -13,15 +13,110 @@ * Simpler, less core concept than mobx | ||
Usage examples with [reactive-di](https://github.com/zerkalica/reactive-di): [example source](https://github.com/zerkalica/rdi-examples), [demo](http://zerkalica.github.io/rdi-examples/), [todomvc benchmark](http://mol.js.org/app/bench/#bench=https%3A%2F%2Fzerkalica.github.io%2Ftodomvc%2Fbenchmark%2F/sample=preact-lom-rdi~preact-raw~preact-mobx) | ||
Install ``` npm install --save lom_atom ``` | ||
## Observable state | ||
<!-- TOC depthFrom:2 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 --> | ||
- [Observable property](#observable-property) | ||
- [Observable get/set](#observable-getset) | ||
- [Force mode cache management](#force-mode-cache-management) | ||
- [Method-style properties](#method-style-properties) | ||
- [Computed values](#computed-values) | ||
- [Side effects](#side-effects) | ||
- [Key-value](#key-value) | ||
- [Actions](#actions) | ||
<!-- /TOC --> | ||
## Observable property | ||
```js | ||
import {mem} from 'lom_atom' | ||
class Todo { | ||
id = Math.random() | ||
@mem title = '' | ||
@mem finished = false | ||
} | ||
const todo = new Todo() | ||
todo.title = '123' | ||
``` | ||
## Observable get/set | ||
```js | ||
import {mem} from 'lom_atom' | ||
class Todo { | ||
@mem set title(next: string) { | ||
// test next | ||
} | ||
@mem get title(): string { | ||
return 'default' | ||
} | ||
} | ||
const todo = new Todo() | ||
todo.title = '123' | ||
``` | ||
## Force mode cache management | ||
Killer feature, grabbed from [mol_atom](https://github.com/eigenmethod/mol). We can reset cache on get or obviously set cache value, using magic force property. | ||
On set value: force mode talk lom to pass value through set handler. On get value: invoke handler with ``` undefined, true ``` and reset cache. | ||
```js | ||
import {force, mem} from 'lom_atom' | ||
class TodoList { | ||
@force force: TodoList | ||
@mem set todos(todos: Todo | Error) { | ||
console.log('set handler') | ||
} | ||
@mem get todos() { | ||
console.log('get handler') | ||
return [someTodo] | ||
} | ||
} | ||
const list = new TodoList() | ||
list.todos = [someTodo] // console: set handler | ||
list.todos = [someTodo] // someTodo already set - no handler call | ||
list.force.todos = [someTodo] // force, console: set handler | ||
list.todos // console: get handler | ||
list.todos // return cached value | ||
list.force.todos // console: get handler | ||
``` | ||
## Method-style properties | ||
In this form we can change value on set. Less magic, than regular properties. | ||
```js | ||
import {action, mem} from 'lom_atom' | ||
class Some { | ||
@force $: Some | ||
@mem name(next?: string, force?: boolean): string { | ||
// if next !== undefined - set mode | ||
if (next !== undefined) return next | ||
return 'default value' | ||
} | ||
} | ||
const some = new Some() | ||
some.name() === 'default value' | ||
some.name('new value') // Set value directly into atom cache, some.name() handler not called | ||
some.name() === 'new value' | ||
some.name('val', true) // Pass value through some.name() handler and set result into cache | ||
some.name(undefined, true) === 'default value' // Invoke some.name() handler and reset to default value | ||
``` | ||
``` some.force.name(val) ``` alias of ``` some.name(val, true) ``` | ||
And ``` some.force.name() ``` alias of ``` some.name(undefined, true) ``` | ||
## Computed values | ||
@@ -46,2 +141,4 @@ | ||
Listener.listen throws errors on todo list store property access, if todo list loading finished with erorr or loading in progress. | ||
```js | ||
@@ -87,38 +184,5 @@ class TodoList { | ||
## Cache management | ||
Killer feature, grabbed from [mol_atom](https://github.com/eigenmethod/mol). We can reset cache on get or obviously set cache value, using magic force property. | ||
```js | ||
import {force, mem} from 'lom_atom' | ||
class TodoList { | ||
@force force: TodoList | ||
@mem set todos(todos: Todo | Error) { | ||
console.log('set handler') | ||
} | ||
@mem get todos() { | ||
console.log('get handler') | ||
return [someTodo] | ||
} | ||
} | ||
const list = new TodoList() | ||
list.todos = [someTodo] // console: set handler | ||
list.todos = [someTodo] // someTodo already set - no handler call | ||
list.force.todos = [someTodo] // force, console: set handler | ||
list.todos // console: get handler | ||
list.todos // return cached value | ||
list.force.todos // console: get handler | ||
``` | ||
## Key-value | ||
Basic dictionary support. See eigenmethod [mol_mem](https://github.com/eigenmethod/mol/tree/master/mem). | ||
Basic dictionary support. First argument is an key of any type. See eigenmethod [mol_mem](https://github.com/eigenmethod/mol/tree/master/mem). | ||
@@ -140,30 +204,28 @@ ```js | ||
## State load/save | ||
## Actions | ||
```js | ||
class TodosStore { | ||
@serializable @mem todos [] | ||
} | ||
``` | ||
State updates are asynchronous, but sometime we need to do transactional synced updates via action helper: | ||
save: | ||
```js | ||
const store = new TodosStore() | ||
store.todos.push({id: '1', title: 'todo one'}) | ||
import {action, mem} from 'lom_atom' | ||
class Some { | ||
@mem name = '' | ||
@mem id = '' | ||
store.__lom_state.todos[0].id === '1' | ||
@action set(id: string, name: string) { | ||
this.id = id | ||
this.name = name | ||
} | ||
} | ||
const some = new Some() | ||
``` | ||
// Transactionally changed in current tick: | ||
action(() => { | ||
some.name = 'test' | ||
some.id = '123' | ||
}) | ||
load: | ||
// or | ||
```js | ||
const store = new TodosStore() | ||
// setup initial state | ||
store.__lom_state = { | ||
todos: [{id: 1, title: 'todo one'}] | ||
} | ||
store.todos[0] | ||
some.set('123', 'test') | ||
``` |
@@ -39,3 +39,3 @@ // @flow | ||
} | ||
forcedFn.displayName = `${name}*` | ||
setFunctionName(forcedFn, `${name}*`) | ||
proto[`${name}*`] = forcedFn | ||
@@ -83,2 +83,7 @@ | ||
function setFunctionName(fn: Function, name: string) { | ||
Object.defineProperty(fn, 'name', {value: name, writable: false}) | ||
fn.displayName = name | ||
} | ||
function memProp<V, P: Object>( | ||
@@ -95,6 +100,11 @@ proto: P, | ||
proto[handlerKey] = descr.get === undefined && descr.set === undefined | ||
if (descr.initializer) setFunctionName(descr.initializer, name) | ||
if (descr.get) setFunctionName(descr.get, `get#${name}`) | ||
if (descr.set) setFunctionName(descr.set, `set#${name}`) | ||
const handler = proto[handlerKey] = descr.get === undefined && descr.set === undefined | ||
? createValueHandler(descr.initializer) | ||
: createGetSetHandler(descr.get, descr.set) | ||
setFunctionName(handler, `${name}()`) | ||
const hostAtoms: WeakMap<Object, IAtom<V>> = new WeakMap() | ||
@@ -181,3 +191,3 @@ | ||
} | ||
forcedFn.displayName = `${name}*` | ||
setFunctionName(forcedFn, `${name}*`) | ||
proto[`${name}*`] = forcedFn | ||
@@ -311,3 +321,3 @@ | ||
} | ||
action.displayName = hk | ||
setFunctionName(action, hk) | ||
@@ -334,3 +344,3 @@ return action | ||
} | ||
action.displayName = name || fn.displayName || fn.name | ||
setFunctionName(action, name || fn.displayName || fn.name) | ||
@@ -337,0 +347,0 @@ return (action: any) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
258044
3231
227