Comparing version 1.2.1 to 1.2.2
@@ -5,3 +5,3 @@ 'use strict'; | ||
exports.InstanceFactory = exports.AtomError = undefined; | ||
exports.createAttachMeta = createAttachMeta; | ||
exports.attachMeta = attachMeta; | ||
@@ -12,12 +12,10 @@ var _interfaces = require('./interfaces'); | ||
function createAttachMeta(selfAtom) { | ||
var oldValue = null; | ||
return function attachMeta(value) { | ||
value[_interfaces.metaKey] = selfAtom; // eslint-disable-line | ||
if (oldValue && oldValue[_interfaces.onUpdate]) { | ||
oldValue[_interfaces.onUpdate].call(oldValue, value); | ||
} | ||
oldValue = value; | ||
return value; | ||
}; | ||
function attachMeta(value) { | ||
value[_interfaces.metaKey] = this; // eslint-disable-line | ||
var oldValue = this._oldValue; | ||
if (oldValue && oldValue[_interfaces.onUpdate]) { | ||
oldValue[_interfaces.onUpdate].call(oldValue, value); | ||
} | ||
this._oldValue = value; | ||
return value; | ||
} | ||
@@ -112,3 +110,4 @@ | ||
InstanceFactory.prototype.setAtom = function setAtom(atom) { | ||
this._attachMeta = createAttachMeta(atom); | ||
atom._attachMeta = attachMeta; // eslint-disable-line | ||
this._atom = atom; | ||
return this; | ||
@@ -124,3 +123,3 @@ }; | ||
InstanceFactory.prototype._get = function _get() { | ||
return this._attachMeta(this._create(this._protoAtom.get(), this._args.get())); | ||
return this._atom._attachMeta(this._create(this._protoAtom.get(), this._args.get())); | ||
}; | ||
@@ -127,0 +126,0 @@ |
@@ -7,79 +7,98 @@ 'use strict'; | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var BoxedValue = function BoxedValue(v) { | ||
_classCallCheck(this, BoxedValue); | ||
var CellxPlugin = function () { | ||
function CellxPlugin(cellx) { | ||
_classCallCheck(this, CellxPlugin); | ||
this.v = v; | ||
}; | ||
var Cell = cellx.Cell; | ||
cellx.configure({ asynchronous: false }); | ||
this.transact = cellx.transact; | ||
var CellxValueAtom = function () { | ||
function CellxValueAtom(cellx, value) { | ||
_classCallCheck(this, CellxValueAtom); | ||
this._CellxValueAtom = function (_Cell) { | ||
_inherits(CellxValueAtom, _Cell); | ||
this._attachMeta = (0, _pluginHelpers.createAttachMeta)(this); | ||
this._value = cellx(new BoxedValue(this._attachMeta(value))); | ||
} // CellxAtom<BoxedValue<V>> | ||
function CellxValueAtom(value) { | ||
_classCallCheck(this, CellxValueAtom); | ||
var _this = _possibleConstructorReturn(this, _Cell.call(this, value)); | ||
CellxValueAtom.prototype.set = function set(value) { | ||
this._value(new BoxedValue(this._attachMeta(value))); | ||
}; | ||
_this._attachMeta = _pluginHelpers.attachMeta; | ||
_this._attachMeta(value); | ||
return _this; | ||
} | ||
CellxValueAtom.prototype.get = function get() { | ||
return this._value().v; | ||
}; | ||
CellxValueAtom.prototype.set = function set(value) { | ||
_Cell.prototype.set.call(this, this._attachMeta(value)); | ||
}; | ||
return CellxValueAtom; | ||
}(); | ||
return CellxValueAtom; | ||
}(Cell); | ||
var CellxInstanceAtom = function () { | ||
function CellxInstanceAtom(cellx, factory) { | ||
_classCallCheck(this, CellxInstanceAtom); | ||
this._CellxFunctionAtom = function (_Cell2) { | ||
_inherits(CellxFunctionAtom, _Cell2); | ||
factory.setAtom(this); | ||
this._factory = factory; | ||
this._value = cellx(factory.get); | ||
} // CellxAtom<BoxedValue<V>> | ||
function CellxFunctionAtom(v) { | ||
_classCallCheck(this, CellxFunctionAtom); | ||
/* eslint-disable */ | ||
return _possibleConstructorReturn(this, _Cell2.call(this, { v: v })); | ||
} | ||
CellxInstanceAtom.prototype.get = function get() { | ||
return this._value(); | ||
}; | ||
CellxFunctionAtom.prototype.set = function set(v) { | ||
_Cell2.prototype.set.call(this, { v: v }); | ||
}; | ||
CellxInstanceAtom.prototype.subscribe = function subscribe(fn, err) { | ||
var value = this._value; | ||
CellxFunctionAtom.prototype.get = function get() { | ||
return _Cell2.prototype.get.call(this).v; | ||
}; | ||
function listener(error, evt) { | ||
if (error && err) { | ||
err(error); | ||
} else { | ||
fn(evt.value); | ||
return CellxFunctionAtom; | ||
}(Cell); | ||
this._CellXInstanceAtom = function (_Cell3) { | ||
_inherits(CellxInstanceAtom, _Cell3); | ||
function CellxInstanceAtom(factory) { | ||
_classCallCheck(this, CellxInstanceAtom); | ||
var _this3 = _possibleConstructorReturn(this, _Cell3.call(this, factory.get)); | ||
factory.setAtom(_this3); | ||
return _this3; | ||
} | ||
} | ||
value('subscribe', listener); | ||
return function unsubscribe() { | ||
value('unsubscribe', listener); | ||
}; | ||
}; | ||
CellxInstanceAtom.prototype.subscribe = function subscribe(fn, err) { | ||
var _this4 = this; | ||
return CellxInstanceAtom; | ||
}(); | ||
function listener(error, evt) { | ||
if (error && err) { | ||
err(error); | ||
} else { | ||
fn(evt.value); | ||
} | ||
} | ||
_Cell3.prototype.subscribe.call(this, listener); | ||
var CellxPlugin = function () { | ||
function CellxPlugin(cellx) { | ||
_classCallCheck(this, CellxPlugin); | ||
var unsubscribe = function unsubscribe() { | ||
_this4.unsubscribe(listener); | ||
}; | ||
this._cellx = cellx; | ||
cellx.configure({ asynchronous: false }); | ||
this.transact = cellx.transact; | ||
return unsubscribe; | ||
}; | ||
return CellxInstanceAtom; | ||
}(Cell); | ||
} | ||
CellxPlugin.prototype.createInstanceAtom = function createInstanceAtom(factory) { | ||
return new CellxInstanceAtom(this._cellx, factory); | ||
return new this._CellXInstanceAtom(factory); | ||
}; | ||
CellxPlugin.prototype.createValueAtom = function createValueAtom(value) { | ||
return new CellxValueAtom(this._cellx, value); | ||
return typeof value === 'function' ? new this._CellxFunctionAtom(value) : new this._CellxValueAtom(value); | ||
}; | ||
@@ -86,0 +105,0 @@ |
@@ -13,3 +13,3 @@ 'use strict'; | ||
this._attachMeta = (0, _pluginHelpers.createAttachMeta)(this); | ||
this._attachMeta = _pluginHelpers.attachMeta; | ||
this._value = derivable.atom(this._attachMeta(value)); | ||
@@ -16,0 +16,0 @@ } |
@@ -19,3 +19,3 @@ 'use strict'; | ||
this._attachMeta = (0, _pluginHelpers.createAttachMeta)(this); | ||
this._attachMeta = _pluginHelpers.attachMeta; | ||
this._value = mobx.observable(new BoxedValue(this._attachMeta(value))); | ||
@@ -22,0 +22,0 @@ } |
{ | ||
"name": "atmover", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Abstraction layer on top of mobx, cellx, derivable with hot reload support", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -8,12 +8,10 @@ // @flow | ||
export function createAttachMeta<V: Object>(selfAtom: AtomGetter<V>): AttachMeta<V> { | ||
let oldValue: ?V = null | ||
return function attachMeta(value: V): V { | ||
value[metaKey] = selfAtom // eslint-disable-line | ||
if (oldValue && oldValue[onUpdate]) { | ||
oldValue[onUpdate].call(oldValue, value) | ||
} | ||
oldValue = value | ||
return value | ||
export function attachMeta<V: Object>(value: V): V { | ||
value[metaKey] = this // eslint-disable-line | ||
const oldValue = this._oldValue | ||
if (oldValue && oldValue[onUpdate]) { | ||
oldValue[onUpdate].call(oldValue, value) | ||
} | ||
this._oldValue = value | ||
return value | ||
} | ||
@@ -97,2 +95,3 @@ | ||
_isSafe: boolean = false | ||
_atom: Computed<V> | ||
@@ -110,3 +109,4 @@ constructor( | ||
setAtom(atom: Computed<V>): InstanceFactory<V> { | ||
this._attachMeta = createAttachMeta(atom) | ||
(atom: any)._attachMeta = attachMeta // eslint-disable-line | ||
this._atom = atom | ||
return this | ||
@@ -126,3 +126,3 @@ } | ||
_get(): V { | ||
return this._attachMeta(this._create(this._protoAtom.get(), this._args.get())) | ||
return (this._atom: any)._attachMeta(this._create(this._protoAtom.get(), this._args.get())) | ||
} | ||
@@ -129,0 +129,0 @@ |
// @flow | ||
import type {Computed, Atom, Transact, IInstanceFactory} from '../interfaces' | ||
import {createAttachMeta} from '../pluginHelpers' | ||
import {attachMeta} from '../pluginHelpers' | ||
type CellxEvent<V> = { | ||
type: 'change'; | ||
interface ICellEvent<V> { | ||
oldValue: V; | ||
value: V; | ||
} | ||
type ListenerName = 'addChangeListener' | 'removeChangeListener' | ||
type CellxListener<V> = (eventName: ListenerName, fn: (event: CellxEvent<V>) => void) => void | ||
type CellxSet<V> = (v: V) => void | ||
type CellxGet<V> = () => V | ||
type CellxAtom<V> = CellxListener<V> | CellxSet<V> | CellxGet<V> | ||
type CellListener<V> = (err: Error, evt: ICellEvent<V>) => boolean | void | ||
type Cellx<V> = (v: V) => CellxAtom<any> | ||
interface Cell<T> { | ||
(v: T): Cell<T>; | ||
(f: () => T): Cell<T>; | ||
subscribe(listener: CellListener<T>): Cell<T>; | ||
unsubscribe(listener: CellListener<T>): Cell<T>; | ||
get(): T; | ||
set(value: T): Cell<T>; | ||
} | ||
class BoxedValue<V> { | ||
v: V | ||
constructor(v: V) { | ||
this.v = v | ||
} | ||
interface ICellxOpts { | ||
asynchronous?: boolean; | ||
} | ||
class CellxValueAtom<V: Object | Function> { | ||
_value: any // CellxAtom<BoxedValue<V>> | ||
_attachMeta: (value: V) => V | ||
interface Cellx { | ||
configure(opts: ICellxOpts): void; | ||
transact: Transact; | ||
Cell: Class<Cell<*>>; | ||
} | ||
constructor( | ||
cellx: Cellx<*>, | ||
value: V | ||
) { | ||
this._attachMeta = createAttachMeta(this) | ||
this._value = cellx(new BoxedValue(this._attachMeta(value))) | ||
} | ||
export default class CellxPlugin { | ||
transact: Transact | ||
_CellxValueAtom: Class<Atom<any>> | ||
_CellxFunctionAtom: Class<Atom<any>> | ||
_CellXInstanceAtom: Class<Computed<any>> | ||
set(value: V): void { | ||
this._value(new BoxedValue(this._attachMeta(value))) | ||
} | ||
constructor(cellx: Cellx) { | ||
const Cell: any = cellx.Cell | ||
cellx.configure({asynchronous: false}) | ||
this.transact = cellx.transact | ||
get(): V { | ||
return this._value().v | ||
} | ||
} | ||
this._CellxValueAtom = class CellxValueAtom<V: Object> extends Cell { | ||
_attachMeta: (value: V) => V | ||
_oldValue: ?V | ||
class CellxInstanceAtom<V: Object | Function> { | ||
_value: any // CellxAtom<BoxedValue<V>> | ||
_factory: IInstanceFactory<V> | ||
constructor( | ||
value: V | ||
) { | ||
super(value) | ||
this._attachMeta = attachMeta | ||
this._attachMeta(value) | ||
} | ||
constructor( | ||
cellx: Cellx<*>, | ||
factory: IInstanceFactory<V> | ||
) { | ||
factory.setAtom(this) | ||
this._factory = factory | ||
this._value = cellx(factory.get) | ||
} | ||
set(value: V): void { | ||
super.set(this._attachMeta(value)) | ||
} | ||
} | ||
get(): V { | ||
return this._value() | ||
} | ||
this._CellxFunctionAtom = class CellxFunctionAtom<V: Function> extends Cell { | ||
constructor(v: V) { | ||
/* eslint-disable */ | ||
super({v}) | ||
} | ||
subscribe(fn: (v: V) => void, err?: (e: Error) => void): () => void { | ||
const value = this._value | ||
set(v: V): void { | ||
super.set({v}) | ||
} | ||
function listener(error: Error, evt: { | ||
type: string; | ||
value: V | ||
}): void { | ||
if (error && err) { | ||
err(error) | ||
} else { | ||
fn(evt.value) | ||
get(): V { | ||
return super.get().v | ||
} | ||
} | ||
value('subscribe', listener) | ||
return function unsubscribe(): void { | ||
value('unsubscribe', listener) | ||
} | ||
} | ||
} | ||
this._CellXInstanceAtom = class CellxInstanceAtom<V: Object | Function> extends Cell { | ||
_oldValue: ?V | ||
export default class CellxPlugin { | ||
_cellx: Cellx<*> | ||
transact: Transact | ||
constructor(factory: IInstanceFactory<V>) { | ||
super(factory.get) | ||
factory.setAtom(this) | ||
} | ||
constructor(cellx: Cellx<*>) { | ||
this._cellx = cellx | ||
cellx.configure({asynchronous: false}) | ||
this.transact = cellx.transact | ||
subscribe(fn: (v: V) => void, err?: (e: Error) => void): () => void { | ||
function listener(error: Error, evt: ICellEvent<V>): void { | ||
if (error && err) { | ||
err(error) | ||
} else { | ||
fn(evt.value) | ||
} | ||
} | ||
super.subscribe(listener) | ||
const unsubscribe = () => { | ||
this.unsubscribe(listener) | ||
} | ||
return unsubscribe | ||
} | ||
} | ||
} | ||
@@ -99,8 +105,10 @@ | ||
): Computed<V> { | ||
return new CellxInstanceAtom(this._cellx, factory) | ||
return new this._CellXInstanceAtom(factory) | ||
} | ||
createValueAtom<V: Object | Function>(value: V): Atom<V> { | ||
return new CellxValueAtom(this._cellx, value) | ||
return typeof value === 'function' | ||
? new this._CellxFunctionAtom(value) | ||
: new this._CellxValueAtom(value) | ||
} | ||
} |
// @flow | ||
import type {Computed, Atom, Transact, IInstanceFactory} from '../interfaces' | ||
import {createAttachMeta, AtomError} from '../pluginHelpers' | ||
import {attachMeta, AtomError} from '../pluginHelpers' | ||
@@ -38,3 +38,3 @@ interface LifeCycle { | ||
) { | ||
this._attachMeta = createAttachMeta(this) | ||
this._attachMeta = attachMeta | ||
this._value = derivable.atom(this._attachMeta(value)) | ||
@@ -41,0 +41,0 @@ } |
// @flow | ||
import type {Computed, Atom, Transact, IInstanceFactory} from '../interfaces' | ||
import {createAttachMeta, AtomError} from '../pluginHelpers' | ||
import {attachMeta, AtomError} from '../pluginHelpers' | ||
@@ -34,3 +34,3 @@ interface MobxAtom<V> { | ||
) { | ||
this._attachMeta = createAttachMeta(this) | ||
this._attachMeta = attachMeta | ||
this._value = mobx.observable(new BoxedValue(this._attachMeta(value))) | ||
@@ -37,0 +37,0 @@ } |
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
Sorry, the diff of this file is not supported yet
95257
1567