reactive-di
Advanced tools
Comparing version 2.3.3 to 2.3.4
@@ -14,2 +14,8 @@ 'use strict'; | ||
var _ComponentControllable = require('../core/ComponentControllable'); | ||
var _ComponentControllable2 = _interopRequireDefault(_ComponentControllable); | ||
var _common = require('../core/common'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -37,10 +43,8 @@ | ||
}; | ||
var controllable = this._controllable = ctx.createControllable(setState, this); | ||
this._createElement = controllable.createElement; | ||
var controllable = this._controllable = new _ComponentControllable2.default(ctx.info, setState); | ||
this._createElement = controllable.contextify(ctx.createElement); | ||
var state = controllable.getState(); | ||
if (state) { | ||
this.state = state; | ||
} | ||
this._target = ctx.target; | ||
var state = this.state = controllable.getState(); | ||
this._target = ctx.info.target; | ||
this._controllable.onWillMount(this); | ||
}; | ||
@@ -53,3 +57,3 @@ | ||
ComponentMixin.prototype.componentDidUpdate = function componentDidUpdate(props, state) { | ||
this._controllable.onUpdate(); | ||
this._controllable.onUpdate(this); | ||
}; | ||
@@ -88,9 +92,10 @@ | ||
this.createElement = createElement; | ||
this._createElement = createElement; | ||
this._Component = Component; | ||
} | ||
ReactComponentFactory.prototype.wrapComponent = function wrapComponent(target, createControllable) { | ||
ReactComponentFactory.prototype.wrapComponent = function wrapComponent(info) { | ||
var _class, _temp; | ||
var createElement = this._createElement; | ||
var WrappedComponent = (_temp = _class = function (_ref2) { | ||
@@ -106,5 +111,5 @@ _inherits(WrappedComponent, _ref2); | ||
return WrappedComponent; | ||
}(this._Component), _class.displayName = '' + (0, _debugName2.default)(target), _class.__rdiCtx = { | ||
target: target, | ||
createControllable: createControllable | ||
}(this._Component), _class.displayName = '' + (0, _debugName2.default)(info.target), _class.__rdiCtx = { | ||
info: info, | ||
createElement: createElement | ||
}, _temp); | ||
@@ -111,0 +116,0 @@ |
@@ -98,2 +98,4 @@ 'use strict'; | ||
var cloneNumber = 0; | ||
function cloneComponent(src, rec) { | ||
@@ -103,3 +105,4 @@ function target(arg1, arg2, arg3) { | ||
} | ||
target.displayName = src.displayName || src.name; | ||
target.displayName = (src.displayName || src.name) + '#clone-' + (cloneNumber || '0'); | ||
cloneNumber++; | ||
Object.setPrototypeOf(target, src); | ||
@@ -106,0 +109,0 @@ if (rec) { |
@@ -135,2 +135,6 @@ 'use strict'; | ||
InternalLifeCycle.prototype.onWillMount = function onWillMount() { | ||
this._lc.onWillMount && this._lc.onWillMount(this._entity); | ||
}; | ||
InternalLifeCycle.prototype.onMount = function onMount() { | ||
@@ -137,0 +141,0 @@ this._count++; |
@@ -35,3 +35,3 @@ 'use strict'; | ||
var ComponentControllable = function () { | ||
function ComponentControllable(_ref, setState, createElement) { | ||
function ComponentControllable(_ref, setState) { | ||
var deps = _ref.deps; | ||
@@ -55,3 +55,2 @@ var meta = _ref.meta; | ||
this._container = container; | ||
this.createElement = createElement; | ||
var ad = ctx.adapter; | ||
@@ -73,2 +72,35 @@ this._isDisposed = ad.atom(false); | ||
ComponentControllable.prototype.contextify = function contextify(createElement) { | ||
var context = this._container; | ||
function ce(tag, props) { | ||
switch (arguments.length) { | ||
case 2: | ||
return createElement(context.wrapComponent(tag), props); | ||
case 3: | ||
return createElement(context.wrapComponent(tag), props, arguments[2]); | ||
case 4: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3]); | ||
case 5: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4]); | ||
case 6: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5]); | ||
case 7: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]); | ||
case 8: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7]); | ||
case 9: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]); | ||
default: | ||
var args = [context.wrapComponent(tag), props]; | ||
for (var i = 2, l = arguments.length; i < l; i++) { | ||
args.push(arguments[i]); | ||
} | ||
return createElement.apply(undefined, args); | ||
} | ||
} | ||
ce.displayName = 'h#' + context.displayName; | ||
return ce; | ||
}; | ||
ComponentControllable.prototype.getState = function getState() { | ||
@@ -112,2 +144,15 @@ if (this._isDisposed.get()) { | ||
ComponentControllable.prototype.onWillMount = function onWillMount(component) { | ||
if (this._isDisposed.get()) { | ||
throw new Error('componentDidMount called after componentWillUnmount: ' + this.displayName); | ||
} | ||
this._isMounted.set(true); | ||
var lcs = this._lcs; | ||
for (var i = 0, l = lcs.length; i < l; i++) { | ||
var lc = lcs[i]; | ||
lc.onWillMount(); | ||
} | ||
this._cls.onWillMount(); | ||
}; | ||
return ComponentControllable; | ||
@@ -114,0 +159,0 @@ }(); |
@@ -8,2 +8,4 @@ 'use strict'; | ||
var _class, _temp; | ||
var _common = require('./common'); | ||
@@ -54,7 +56,7 @@ | ||
wrapComponent: function wrapComponent() { | ||
throw new Error('Can\'t create widget: provide widget factory to di'); | ||
throw new Error('dummyComponentFactory, can\'t create widget: provide widget factory to di'); | ||
} | ||
}; | ||
var Di = function () { | ||
var Di = (_temp = _class = function () { | ||
function Di(componentFactory, createStyleSheet, handlers, adapter, metaRegistry, displayName, collector, mdlFactory) { | ||
@@ -67,3 +69,3 @@ _classCallCheck(this, Di); | ||
this._componentFactory = componentFactory || dummyComponentFactory; | ||
this.displayName = displayName || 'rootDi'; | ||
this.displayName = (displayName || 'root') + String(Di.uniqId++); | ||
this._mdlFactory = mdlFactory; | ||
@@ -77,40 +79,4 @@ this.adapter = adapter || _derivableAtomAdapter2.default; | ||
this.stopped = this.adapter.atom(false); | ||
this._createElement = this._getCreateElement(); | ||
} | ||
Di.prototype._getCreateElement = function _getCreateElement() { | ||
var _this = this; | ||
var ce = this._componentFactory.createElement; | ||
var createWrappedElement = function createWrappedElement(tag, props) { | ||
for (var _len = arguments.length, ch = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
ch[_key - 2] = arguments[_key]; | ||
} | ||
switch (ch.length) { | ||
case 0: | ||
return ce(_this.wrapComponent(tag), props); | ||
case 1: | ||
return ce(_this.wrapComponent(tag), props, ch[0]); | ||
case 2: | ||
return ce(_this.wrapComponent(tag), props, ch[0], ch[1]); | ||
case 3: | ||
return ce(_this.wrapComponent(tag), props, ch[0], ch[1], ch[2]); | ||
case 4: | ||
return ce(_this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3]); | ||
case 5: | ||
return ce(_this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3], ch[4]); | ||
case 6: | ||
return ce(_this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3], ch[4], ch[5]); | ||
case 7: | ||
return ce(_this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3], ch[4], ch[5], ch[6]); | ||
default: | ||
return ce.apply(undefined, [_this.wrapComponent(tag), props].concat(ch)); | ||
} | ||
}; | ||
return createWrappedElement; | ||
}; | ||
Di.prototype.stop = function stop() { | ||
@@ -132,3 +98,3 @@ this.stopped.set(true); | ||
Di.prototype.create = function create(displayName) { | ||
return new Di(null, null, this._handlers, this.adapter, this._metaRegistry.copy(), displayName, this._collector, this._mdlFactory).values(this.defaults); | ||
return new Di(this._componentFactory, null, this._handlers, this.adapter, this._metaRegistry.copy(), this.displayName + '.' + displayName, this._collector, this._mdlFactory).values(this.defaults); | ||
}; | ||
@@ -148,4 +114,2 @@ | ||
Di.prototype.wrapComponent = function wrapComponent(key) { | ||
var _this2 = this; | ||
if (!(0, _common.isComponent)(key)) { | ||
@@ -156,9 +120,3 @@ return key; | ||
if (!info.value) { | ||
(function () { | ||
var createElement = _this2._createElement; | ||
var createControllable = function _createControllable(setState) { | ||
return new _ComponentControllable2.default(info, setState, createElement); | ||
}; | ||
info.value = _this2._componentFactory.wrapComponent(info.target, createControllable); | ||
})(); | ||
info.value = this._componentFactory.wrapComponent(info); | ||
} | ||
@@ -179,7 +137,2 @@ | ||
if (key === this.constructor) { | ||
info.value = this.adapter.atom(this); | ||
collector.addCached(info.lcs); | ||
return info.value; | ||
} | ||
var cache = this._metaRegistry; | ||
@@ -290,4 +243,3 @@ var ctx = info.ctx; | ||
return Di; | ||
}(); | ||
}(), _class.uniqId = 1, _temp); | ||
exports.default = Di; | ||
@@ -294,0 +246,0 @@ |
@@ -19,2 +19,6 @@ 'use strict'; | ||
this._context = context; | ||
var key = context.constructor; | ||
var di = new _common.DepInfo(key, key, context); | ||
di.value = context; | ||
this._metaMap.set(key, di); | ||
return this; | ||
@@ -32,7 +36,5 @@ }; | ||
var ctx = this._context; | ||
var metaMap = this._metaMap; | ||
var key = void 0; | ||
var target = void 0; | ||
var di = void 0; | ||
for (var i = 0, l = registered.length; i < l; i++) { | ||
@@ -57,9 +59,10 @@ var pr = registered[i]; | ||
if (target !== key && (0, _common.isAbstract)(key)) { | ||
if (key !== target) { | ||
var rec = metaMap.get(target); | ||
di = (rec ? rec.ctx : null) || ctx; | ||
var depInfo = new _common.DepInfo(target, key, (rec ? rec.ctx : null) || ctx); | ||
metaMap.set(key, depInfo); | ||
metaMap.set(target, depInfo); | ||
} else { | ||
di = ctx; | ||
metaMap.set(key, new _common.DepInfo(target, key, ctx)); | ||
} | ||
metaMap.set(key, new _common.DepInfo(target, key, di)); | ||
} | ||
@@ -66,0 +69,0 @@ }; |
@@ -48,2 +48,5 @@ 'use strict'; | ||
if (!target) { | ||
throw new Error('Target Class is not set for SingleUpdateBucket'); | ||
} | ||
if (Array.isArray(rawUpdate)) { | ||
@@ -200,3 +203,5 @@ if (rawUpdate.length !== 2) { | ||
}); | ||
this._run(); | ||
if (this.size) { | ||
this._run(); | ||
} | ||
}; | ||
@@ -235,3 +240,2 @@ | ||
this._rollbackOnError = rollbackOnError; | ||
this.displayName = di.displayName + '#UpdaterObserver'; | ||
this._adapter = di.adapter; | ||
@@ -250,4 +254,3 @@ this._qeue = new AsyncQeue(this, maxSize); | ||
this.next(updates); | ||
} | ||
if (this._qeue.size === 0) { | ||
} else if (this._qeue.size === 0) { | ||
this.status.set(new UpdaterStatus('complete')); | ||
@@ -315,4 +318,4 @@ this._pendingUpdates = []; | ||
var rec = updates[i]; | ||
if (needRollback) { | ||
pd.push(rec); | ||
if (!Array.isArray(rec)) { | ||
throw new Error('Wrong updates rec: must by an NormalizedSyncUpdate tuple'); | ||
} | ||
@@ -322,4 +325,17 @@ var key = rec[0]; | ||
if (!key) { | ||
throw new Error('NormalizedSyncUpdate key can\'t be null'); | ||
} | ||
if (!props) { | ||
throw new Error('NormalizedSyncUpdate props can\'t be null'); | ||
} | ||
if (needRollback) { | ||
pd.push(rec); | ||
} | ||
di.val(key).swap(cloneInstance, props); | ||
} | ||
if (!needRollback) { | ||
this.status.set(new UpdaterStatus('complete')); | ||
this._pendingUpdates = []; | ||
} | ||
}; | ||
@@ -338,5 +354,6 @@ | ||
this.displayName = di.displayName + '#Updater'; | ||
var c = this.constructor; | ||
this.displayName = di.displayName + '#' + (0, _debugName2.default)(c); | ||
this._uo = new UpdaterObserver(di, c.maxSize, c.rollbackOnError); | ||
this._uo.displayName = this.displayName + '#updater'; | ||
this.status = this._uo.status; | ||
@@ -343,0 +360,0 @@ var unsubscribeUpdater = function unsubscribeUpdater(isStopped) { |
@@ -22,2 +22,3 @@ 'use strict'; | ||
us.pending = true; | ||
us.complete = false; | ||
} | ||
@@ -27,2 +28,4 @@ if (updater.error) { | ||
us.retry = updater.retry; | ||
us.complete = false; | ||
us.pending = false; | ||
break; | ||
@@ -29,0 +32,0 @@ } |
{ | ||
"name": "reactive-di", | ||
"version": "2.3.3", | ||
"version": "2.3.4", | ||
"description": "Reactive dependency injection", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -21,2 +21,3 @@ // @flow | ||
import type {SingleUpdate} from 'reactive-di/interfaces/updater' | ||
import ComponentControllable from 'reactive-di/core/ComponentControllable' | ||
@@ -65,2 +66,3 @@ describe('InitializerTest', () => { | ||
} | ||
@component() | ||
@@ -71,10 +73,12 @@ @deps({m: ModelA}) | ||
ctl: IComponentControllable<*, *> | ||
static createControllable: CreateControllable<*, *> | ||
static info: any | ||
constructor() { | ||
this.ctl = this.constructor.createControllable((state: Object) => { | ||
Object.assign(this, state) | ||
}) | ||
// console.log(this.constructor.info) | ||
this.ctl = new ComponentControllable(this.constructor.info, (state) => this.setState(state)) | ||
Object.assign(this, this.ctl.getState()) | ||
} | ||
setState(state) { | ||
Object.assign(this, state) | ||
} | ||
} | ||
@@ -86,8 +90,8 @@ | ||
}, | ||
wrapComponent(target: Function, createControllable: CreateControllable<any, any>): any { | ||
target.createControllable = createControllable | ||
return (target: any) | ||
wrapComponent(info: any): any { | ||
info.target.info = info | ||
return (info.target: any) | ||
} | ||
}) | ||
const C: Class<Component> = di.wrapComponent(Component) | ||
}).register([Updater]) | ||
const C: Class<Component> = di.wrapComponent((Component: any)) | ||
const c = new C() | ||
@@ -94,0 +98,0 @@ return c |
@@ -50,3 +50,3 @@ // @flow | ||
assert(di.val(MyUpdaterStatus).get().complete) | ||
di.val(MyUpdater1).get().setSingle(() => promise) | ||
di.val(MyUpdater1).get().setSingle(() => promise, ModelA) | ||
assert(di.val(MyUpdaterStatus).get().pending) | ||
@@ -67,3 +67,3 @@ | ||
const promise = Promise.resolve({val: 'testA'}) | ||
di.val(MyUpdater1).get().setSingle(() => promise) | ||
di.val(MyUpdater1).get().setSingle(() => promise, ModelA) | ||
@@ -86,3 +86,3 @@ const u: UpdaterStatus = di.val(MyUpdaterStatus).get() | ||
const promiseA = Promise.resolve({val: 'testA'}) | ||
di.val(MyUpdater1).get().setSingle(() => promiseA) | ||
di.val(MyUpdater1).get().setSingle(() => promiseA, ModelA) | ||
@@ -93,3 +93,3 @@ let resolveFn: Function | ||
}) | ||
di.val(MyUpdater2).get().setSingle(() => promiseB) | ||
di.val(MyUpdater2).get().setSingle(() => promiseB, ModelB) | ||
@@ -96,0 +96,0 @@ const u: UpdaterStatus = di.val(MyUpdaterStatus).get() |
@@ -6,4 +6,4 @@ // @flow | ||
SrcComponent, | ||
IComponentControllable, | ||
CreateControllable, | ||
IComponentControllable, | ||
SetState | ||
@@ -14,2 +14,4 @@ } from 'reactive-di/interfaces/component' | ||
import shallowEqual, {shallowStrictEqual} from 'reactive-di/utils/shallowEqual' | ||
import ComponentControllable from 'reactive-di/core/ComponentControllable' | ||
import {DepInfo} from 'reactive-di/core/common' | ||
@@ -24,4 +26,4 @@ type ReactElement = React$Element<any> | ||
interface StaticContext<Props, State> { | ||
target: SrcComponent<Props, State>; | ||
createControllable: CreateReactControllable<Props, State>; | ||
info: DepInfo<SrcComponent<Props, State>, *>; | ||
createElement: CreateElement<*, *>; | ||
} | ||
@@ -35,6 +37,6 @@ | ||
state: State | ||
state: ?State | ||
props: Props | ||
_target: SrcComponent<Props, State> | ||
_target: SrcComponent<Props, ?State> | ||
_controllable: IComponentControllable<State, ReactComponentClass<Props, State>> | ||
@@ -48,10 +50,8 @@ _createElement: ReactCreateElement | ||
} | ||
const controllable = this._controllable = ctx.createControllable(setState, this) | ||
this._createElement = controllable.createElement | ||
const controllable = this._controllable = new ComponentControllable(ctx.info, setState) | ||
this._createElement = controllable.contextify(ctx.createElement) | ||
const state: ?State = controllable.getState() | ||
if (state) { | ||
this.state = state | ||
} | ||
this._target = ctx.target | ||
const state: ?State = this.state = controllable.getState() | ||
this._target = ctx.info.target | ||
this._controllable.onWillMount((this: any)) | ||
} | ||
@@ -64,3 +64,3 @@ | ||
componentDidUpdate(props: Props, state: State): void { | ||
this._controllable.onUpdate() | ||
this._controllable.onUpdate((this: any)) | ||
} | ||
@@ -97,6 +97,6 @@ | ||
_Component: ReactComponentClass<*, *> | ||
createElement: CreateElement<*, *> | ||
_createElement: CreateElement<*, *> | ||
constructor({Component, createElement}: React) { | ||
this.createElement = (createElement: any) | ||
this._createElement = (createElement: any) | ||
this._Component = Component | ||
@@ -106,10 +106,10 @@ } | ||
wrapComponent<Props, State>( | ||
target: SrcComponent<Props, State>, | ||
createControllable: CreateReactControllable<Props, State> | ||
info: DepInfo<SrcComponent<Props, State>, *> | ||
): ReactComponentClass<Props, State> { | ||
const createElement = this._createElement | ||
class WrappedComponent extends (this._Component: any)<Props, State> { | ||
static displayName: string = `${debugName(target)}` | ||
static displayName: string = `${debugName(info.target)}` | ||
static __rdiCtx: StaticContext<Props, State> = { | ||
target, | ||
createControllable | ||
info, | ||
createElement | ||
} | ||
@@ -116,0 +116,0 @@ state: State |
@@ -82,2 +82,4 @@ // @flow | ||
let cloneNumber: number = 0 | ||
export function cloneComponent<C: Function>(src: C, rec?: ComponentMetaRec): C { | ||
@@ -87,3 +89,4 @@ function target(arg1: any, arg2: any, arg3: any) { | ||
} | ||
target.displayName = src.displayName || src.name | ||
target.displayName = (src.displayName || src.name) + '#clone-' + (cloneNumber || '0') | ||
cloneNumber++ | ||
Object.setPrototypeOf(target, src) | ||
@@ -90,0 +93,0 @@ if (rec) { |
@@ -111,2 +111,6 @@ // @flow | ||
onWillMount() { | ||
this._lc.onWillMount && this._lc.onWillMount(this._entity) | ||
} | ||
onMount() { | ||
@@ -113,0 +117,0 @@ this._count++ |
@@ -31,8 +31,7 @@ //@flow | ||
createElement: CreateElement<Component, *> | ||
_cls: ComponentLifeCycle<*> | ||
constructor<V>( | ||
{deps, meta, ctx, name, lc}: DepInfo<V, ComponentMeta>, | ||
setState: (state: State) => void, | ||
createElement: CreateElement<Component, *> | ||
setState: (state: State) => void | ||
) { | ||
@@ -49,3 +48,2 @@ this.displayName = name | ||
this._container = container | ||
this.createElement = createElement | ||
const ad: Adapter = ctx.adapter | ||
@@ -68,2 +66,38 @@ this._isDisposed = ad.atom(false) | ||
contextify(createElement: CreateElement<*, *>): CreateElement<*, *> { | ||
const context: IContext = this._container | ||
function ce( | ||
tag: any, | ||
props?: ?{[id: string]: mixed} | ||
) { | ||
switch (arguments.length) { | ||
case 2: | ||
return createElement(context.wrapComponent(tag), props) | ||
case 3: | ||
return createElement(context.wrapComponent(tag), props, arguments[2]) | ||
case 4: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3]) | ||
case 5: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4]) | ||
case 6: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5]) | ||
case 7: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]) | ||
case 8: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7]) | ||
case 9: | ||
return createElement(context.wrapComponent(tag), props, arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]) | ||
default: | ||
const args = [context.wrapComponent(tag), props] | ||
for (let i = 2, l = arguments.length; i < l; i++) { | ||
args.push(arguments[i]) | ||
} | ||
return createElement.apply(undefined, args) | ||
} | ||
} | ||
ce.displayName = `h#${context.displayName}` | ||
return ce | ||
} | ||
getState(): ?State { | ||
@@ -106,3 +140,16 @@ if (this._isDisposed.get()) { | ||
} | ||
onWillMount(component: Component): void { | ||
if (this._isDisposed.get()) { | ||
throw new Error(`componentDidMount called after componentWillUnmount: ${this.displayName}`) | ||
} | ||
this._isMounted.set(true) | ||
const lcs = this._lcs | ||
for (let i = 0, l = lcs.length; i < l; i++) { | ||
const lc = lcs[i] | ||
lc.onWillMount() | ||
} | ||
this._cls.onWillMount() | ||
} | ||
} | ||
if (0) ((new ComponentControllable(...(0: any))): IComponentControllable<*, *>) |
@@ -30,3 +30,3 @@ // @flow | ||
wrapComponent() { | ||
throw new Error('Can\'t create widget: provide widget factory to di') | ||
throw new Error('dummyComponentFactory, can\'t create widget: provide widget factory to di') | ||
} | ||
@@ -47,5 +47,4 @@ } | ||
_path: string[] = [] | ||
_createElement: CreateElement<*, *> | ||
_mdlFactory: ?MiddlewareFactory | ||
static uniqId: number = 1 | ||
constructor( | ||
@@ -63,3 +62,3 @@ componentFactory?: ?ComponentFactory, | ||
this._componentFactory = componentFactory || dummyComponentFactory | ||
this.displayName = displayName || 'rootDi' | ||
this.displayName = (displayName || 'root') + String(Di.uniqId++) | ||
this._mdlFactory = mdlFactory | ||
@@ -73,38 +72,4 @@ this.adapter = adapter || derivableAtomAdapter | ||
this.stopped = this.adapter.atom(false) | ||
this._createElement = this._getCreateElement() | ||
} | ||
_getCreateElement(): CreateElement<*, *> { | ||
const ce = this._componentFactory.createElement | ||
const createWrappedElement = ( | ||
tag: Function, | ||
props?: ?{[id: string]: mixed}, | ||
...ch: any | ||
) => { | ||
switch (ch.length) { | ||
case 0: | ||
return ce(this.wrapComponent(tag), props) | ||
case 1: | ||
return ce(this.wrapComponent(tag), props, ch[0]) | ||
case 2: | ||
return ce(this.wrapComponent(tag), props, ch[0], ch[1]) | ||
case 3: | ||
return ce(this.wrapComponent(tag), props, ch[0], ch[1], ch[2]) | ||
case 4: | ||
return ce(this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3]) | ||
case 5: | ||
return ce(this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3], ch[4]) | ||
case 6: | ||
return ce(this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3], ch[4], ch[5]) | ||
case 7: | ||
return ce(this.wrapComponent(tag), props, ch[0], ch[1], ch[2], ch[3], ch[4], ch[5], ch[6]) | ||
default: | ||
return ce(this.wrapComponent(tag), props, ...ch) | ||
} | ||
} | ||
return createWrappedElement | ||
} | ||
stop(): IContext { | ||
@@ -127,8 +92,8 @@ this.stopped.set(true) | ||
return (new Di( | ||
this._componentFactory, | ||
null, | ||
null, | ||
this._handlers, | ||
this.adapter, | ||
this._metaRegistry.copy(), | ||
displayName, | ||
this.displayName + '.' + displayName, | ||
this._collector, | ||
@@ -158,9 +123,4 @@ this._mdlFactory | ||
if (!info.value) { | ||
const createElement = this._createElement | ||
const createControllable = function _createControllable(setState: SetState<*>) { | ||
return new ComponentControllable(info, setState, createElement) | ||
} | ||
info.value = (this._componentFactory.wrapComponent( | ||
info.target, | ||
createControllable | ||
info | ||
): any) | ||
@@ -182,7 +142,2 @@ } | ||
if (key === this.constructor) { | ||
info.value = this.adapter.atom(((this: any): V)) | ||
collector.addCached(info.lcs) | ||
return (info.value: any) | ||
} | ||
const cache = this._metaRegistry | ||
@@ -189,0 +144,0 @@ const {ctx, target, meta, name} = info |
@@ -18,2 +18,6 @@ // @flow | ||
this._context = context | ||
const key: Function = context.constructor | ||
const di = new DepInfo(key, key, context) | ||
di.value = (context: any) | ||
this._metaMap.set(key, di) | ||
return this | ||
@@ -31,7 +35,5 @@ } | ||
const ctx = this._context | ||
const metaMap = this._metaMap | ||
let key: Key | ||
let target: Function | ||
let di: ?IContext | ||
for (let i = 0, l = registered.length; i < l; i++) { | ||
@@ -56,9 +58,10 @@ const pr: RegisterDepItem = registered[i] | ||
if (target !== key && isAbstract(key)) { | ||
if (key !== target) { | ||
const rec: ?DepInfo<*, *> = metaMap.get(target) | ||
di = (rec ? rec.ctx : null) || ctx | ||
const depInfo = new DepInfo(target, key, (rec ? rec.ctx : null) || ctx) | ||
metaMap.set(key, depInfo) | ||
metaMap.set(target, depInfo) | ||
} else { | ||
di = ctx | ||
metaMap.set(key, new DepInfo(target, key, ctx)) | ||
} | ||
metaMap.set(key, new DepInfo(target, key, di)) | ||
} | ||
@@ -65,0 +68,0 @@ } |
@@ -52,2 +52,5 @@ // @flow | ||
constructor(rawUpdate: SingleUpdate, target: Key) { | ||
if (!target) { | ||
throw new Error('Target Class is not set for SingleUpdateBucket') | ||
} | ||
if (Array.isArray(rawUpdate)) { | ||
@@ -239,3 +242,5 @@ if (rawUpdate.length !== 2) { | ||
this._subscriptions = this._subscriptions.filter((target) => target !== item) | ||
this._run() | ||
if (this.size) { | ||
this._run() | ||
} | ||
} | ||
@@ -273,3 +278,2 @@ | ||
this._rollbackOnError = rollbackOnError | ||
this.displayName = `${di.displayName}#UpdaterObserver` | ||
this._adapter = di.adapter | ||
@@ -288,4 +292,3 @@ this._qeue = new AsyncQeue((this: Observer<UpdateBucket, OperationErrorRec>), maxSize) | ||
this.next(updates) | ||
} | ||
if (this._qeue.size === 0) { | ||
} else if (this._qeue.size === 0) { | ||
this.status.set(new UpdaterStatus('complete')) | ||
@@ -338,8 +341,21 @@ this._pendingUpdates = [] | ||
const rec = updates[i] | ||
if (!Array.isArray(rec)) { | ||
throw new Error('Wrong updates rec: must by an NormalizedSyncUpdate tuple') | ||
} | ||
const [key, props] = rec | ||
if (!key) { | ||
throw new Error('NormalizedSyncUpdate key can\'t be null') | ||
} | ||
if (!props) { | ||
throw new Error('NormalizedSyncUpdate props can\'t be null') | ||
} | ||
if (needRollback) { | ||
pd.push(rec) | ||
} | ||
const [key, props] = rec | ||
di.val(key).swap(cloneInstance, props) | ||
} | ||
if (!needRollback) { | ||
this.status.set(new UpdaterStatus('complete')) | ||
this._pendingUpdates = [] | ||
} | ||
} | ||
@@ -359,4 +375,4 @@ } | ||
constructor(di: IContext) { | ||
this.displayName = `${di.displayName}#Updater` | ||
const c = this.constructor | ||
this.displayName = `${di.displayName}#${debugName(c)}` | ||
this._uo = new UpdaterObserver( | ||
@@ -367,2 +383,3 @@ di, | ||
) | ||
this._uo.displayName = this.displayName + '#updater' | ||
this.status = this._uo.status | ||
@@ -369,0 +386,0 @@ const unsubscribeUpdater = (isStopped: boolean) => { |
@@ -13,2 +13,3 @@ //@flow | ||
us.pending = true | ||
us.complete = false | ||
} | ||
@@ -18,2 +19,4 @@ if (updater.error) { | ||
us.retry = updater.retry | ||
us.complete = false | ||
us.pending = false | ||
break | ||
@@ -20,0 +23,0 @@ } |
// @flow | ||
import type {IContext} from 'reactive-di/interfaces/internal' | ||
import type {DepInfo} from 'reactive-di/core/common' | ||
@@ -26,7 +27,7 @@ export type StyleSheet = { | ||
export interface IComponentControllable<State, Component> { | ||
createElement: CreateElement<Component, *>; | ||
getState(): ?State; | ||
onUnmount(): void; | ||
onMount(): void; | ||
onUpdate(): void; | ||
onUpdate(c: Component): void; | ||
onWillMount(c: Component): void; | ||
} | ||
@@ -41,7 +42,3 @@ | ||
export interface ComponentFactory { | ||
createElement: CreateElement<*, *>; | ||
wrapComponent<Props, State>( | ||
target: SrcComponent<Props, State>, | ||
createControllable: CreateControllable<State, *> | ||
): mixed; | ||
wrapComponent<Props, State>(info: DepInfo<SrcComponent<Props, State>, *>): mixed; | ||
} |
@@ -16,8 +16,13 @@ // @flow | ||
/** | ||
* Called on first mount of any component, which uses description | ||
* Called before first mount of any component, works on server side | ||
*/ | ||
onWillMount?: (dep: Dep) => void; | ||
/** | ||
* Called on first mount of any component | ||
*/ | ||
onMount?: (dep: Dep) => void; | ||
/** | ||
* Called on last unmount of any component, which uses description | ||
* Called on last unmount of any component | ||
*/ | ||
@@ -24,0 +29,0 @@ onUnmount?: (dep: Dep) => void; |
// @flow | ||
export function shallowStrictEqual(objA: Object, objB: Object): boolean { | ||
export function shallowStrictEqual(objA: ?Object, objB: Object): boolean { | ||
if (objA === objB) { | ||
@@ -5,0 +5,0 @@ return true |
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
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
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
321823
4972