@cerebral/fluent
Advanced tools
Comparing version 1.0.1 to 1.0.2-1523464214894
@@ -1,2 +0,4 @@ | ||
import { computed as mobxComputed } from 'mobx'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var mobx_1 = require("mobx"); | ||
var ComputedClass = /** @class */ (function () { | ||
@@ -10,3 +12,3 @@ function ComputedClass(callback) { | ||
this.instance || | ||
mobxComputed(function () { | ||
mobx_1.computed(function () { | ||
return _this.callback(_this.getState().state, _this.getState().root); | ||
@@ -18,5 +20,7 @@ }); | ||
}()); | ||
export { ComputedClass }; | ||
export function Computed(callback) { | ||
exports.ComputedClass = ComputedClass; | ||
function Computed(callback) { | ||
return new ComputedClass(callback); | ||
} | ||
exports.Computed = Computed; | ||
//# sourceMappingURL=Computed.js.map |
@@ -1,7 +0,10 @@ | ||
import Hoc from './Hoc'; | ||
import { CerebralError } from 'cerebral'; | ||
export default function connect(dependencies, passedComponent) { | ||
return Hoc(dependencies, passedComponent); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Hoc_1 = require("./Hoc"); | ||
var cerebral_1 = require("cerebral"); | ||
function connect(dependencies, passedComponent) { | ||
return Hoc_1.default(dependencies, passedComponent); | ||
} | ||
export function ConnectFactory() { | ||
exports.default = connect; | ||
function ConnectFactory() { | ||
function returned() { | ||
@@ -26,3 +29,4 @@ return { | ||
} | ||
export function ModuleConnectFactory() { | ||
exports.ConnectFactory = ConnectFactory; | ||
function ModuleConnectFactory() { | ||
function To(stateLookup, signalsLookup) { | ||
@@ -49,6 +53,7 @@ return function returned() { | ||
} | ||
exports.ModuleConnectFactory = ModuleConnectFactory; | ||
function getFakeProps() { | ||
var handler = { | ||
get: function (target, name) { | ||
throw new CerebralError('The props lambda parameter should only be used to conveniently get the type of the components generic props parameter. Use `this.props` in the component instead.'); | ||
throw new cerebral_1.CerebralError('The props lambda parameter should only be used to conveniently get the type of the components generic props parameter. Use `this.props` in the component instead.'); | ||
}, | ||
@@ -79,1 +84,2 @@ }; | ||
} | ||
//# sourceMappingURL=connect.js.map |
"use strict"; | ||
//# sourceMappingURL=connect.test.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
@@ -11,5 +12,6 @@ var extendStatics = Object.setPrototypeOf || | ||
})(); | ||
import * as React from 'react'; | ||
import * as PropTypes from 'prop-types'; | ||
import { throwError } from 'cerebral/internal'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var React = require("react"); | ||
var PropTypes = require("prop-types"); | ||
var internal_1 = require("cerebral/internal"); | ||
var Container = /** @class */ (function (_super) { | ||
@@ -23,3 +25,3 @@ __extends(Container, _super); | ||
if (!controller) { | ||
throwError('You are not passing controller to Container'); | ||
internal_1.throwError('You are not passing controller to Container'); | ||
} | ||
@@ -40,2 +42,3 @@ return { controller: controller }; | ||
}(React.Component)); | ||
export default Container; | ||
exports.default = Container; | ||
//# sourceMappingURL=Container.js.map |
@@ -38,1 +38,2 @@ "use strict"; | ||
}) */ | ||
//# sourceMappingURL=fluent.test.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
@@ -11,6 +12,7 @@ var extendStatics = Object.setPrototypeOf || | ||
})(); | ||
import * as React from 'react'; | ||
import { throwError } from 'cerebral/internal'; | ||
import View from './View'; | ||
import * as PropTypes from 'prop-types'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var React = require("react"); | ||
var internal_1 = require("cerebral/internal"); | ||
var View_1 = require("./View"); | ||
var PropTypes = require("prop-types"); | ||
var BaseComponent = /** @class */ (function (_super) { | ||
@@ -21,6 +23,6 @@ __extends(BaseComponent, _super); | ||
if (!controller) { | ||
throwError('Can not find controller, did you remember to use the Container component? Read more at: http://cerebraljs.com/docs/api/components.html#react'); | ||
internal_1.throwError('Can not find controller, did you remember to use the Container component? Read more at: http://cerebraljs.com/docs/api/components.html#react'); | ||
} | ||
_this.onUpdate = _this.onUpdate.bind(_this); | ||
_this.view = new View({ | ||
_this.view = new View_1.default({ | ||
dependencies: dependencies, | ||
@@ -54,3 +56,3 @@ props: props, | ||
}(React.Component)); | ||
export default function HOC(dependencies, Component) { | ||
function HOC(dependencies, Component) { | ||
return _a = /** @class */ (function (_super) { | ||
@@ -77,1 +79,3 @@ __extends(class_1, _super); | ||
} | ||
exports.default = HOC; | ||
//# sourceMappingURL=Hoc.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
@@ -11,18 +12,30 @@ var extendStatics = Object.setPrototypeOf || | ||
})(); | ||
import { observable, untracked, transaction, extras, ObservableMap as MobxObservableMap, useStrict, } from 'mobx'; | ||
import { CreateModuleProvider } from './providers'; | ||
import { BaseControllerClass } from 'cerebral'; | ||
import { extractModuleProp, getModule, throwError } from 'cerebral/internal'; | ||
import { updateIn } from './utils'; | ||
import Model from './Model'; | ||
export { ConnectFactory, ModuleConnectFactory } from './connect'; | ||
export { pathFor } from './paths'; | ||
export { SequenceFactory, SequenceWithPropsFactory, } from 'function-tree/fluent'; | ||
export { Computed } from './Computed'; | ||
export { default as Container } from './Container'; | ||
export function Dictionary(values) { | ||
return new MobxObservableMap(values); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var mobx_1 = require("mobx"); | ||
var providers_1 = require("./providers"); | ||
var cerebral_1 = require("cerebral"); | ||
var internal_1 = require("cerebral/internal"); | ||
var utils_1 = require("./utils"); | ||
var Model_1 = require("./Model"); | ||
var connect_1 = require("./connect"); | ||
exports.ConnectFactory = connect_1.ConnectFactory; | ||
exports.ModuleConnectFactory = connect_1.ModuleConnectFactory; | ||
var paths_1 = require("./paths"); | ||
exports.pathFor = paths_1.pathFor; | ||
var fluent_1 = require("function-tree/fluent"); | ||
exports.SequenceFactory = fluent_1.SequenceFactory; | ||
exports.SequenceWithPropsFactory = fluent_1.SequenceWithPropsFactory; | ||
var Computed_1 = require("./Computed"); | ||
exports.Computed = Computed_1.Computed; | ||
var Container_1 = require("./Container"); | ||
exports.Container = Container_1.default; | ||
function Dictionary(values) { | ||
return new mobx_1.ObservableMap(values); | ||
} | ||
exports.Dictionary = Dictionary; | ||
// mobx uses extra fields in modules and therefore needs to declare it's own `Module` constructor. | ||
export { Module, Provider, CerebralError } from 'cerebral'; | ||
var cerebral_2 = require("cerebral"); | ||
exports.Module = cerebral_2.Module; | ||
exports.Provider = cerebral_2.Provider; | ||
exports.CerebralError = cerebral_2.CerebralError; | ||
var FluentController = /** @class */ (function (_super) { | ||
@@ -33,8 +46,8 @@ __extends(FluentController, _super); | ||
executeBranchWrapper: function (cb) { | ||
extras.allowStateChanges(true, function () { return untracked(function () { return transaction(cb); }); }); | ||
mobx_1.extras.allowStateChanges(true, function () { return mobx_1.untracked(function () { return mobx_1.transaction(cb); }); }); | ||
}, | ||
}) || this; | ||
useStrict(typeof options.useStrict === 'undefined' ? true : options.useStrict); | ||
mobx_1.useStrict(typeof options.useStrict === 'undefined' ? true : options.useStrict); | ||
_this.state = _this.model.state; | ||
_this.signals = extractModuleProp(_this.module, 'signals', function (signals, module) { | ||
_this.signals = internal_1.extractModuleProp(_this.module, 'signals', function (signals, module) { | ||
return Object.keys(signals).reduce(function (runableSignals, key) { | ||
@@ -50,3 +63,3 @@ if ('run' in signals[key]) { | ||
}); | ||
_this.contextProviders.module = CreateModuleProvider(_this.model.state, _this.devtools, _this.model, options.useLegacyStateApi || false); | ||
_this.contextProviders.module = providers_1.CreateModuleProvider(_this.model.state, _this.devtools, _this.model, options.useLegacyStateApi || false); | ||
return _this; | ||
@@ -58,8 +71,8 @@ } | ||
if (!moduleKey) { | ||
return throwError('Can not find module path to add module'); | ||
return internal_1.throwError('Can not find module path to add module'); | ||
} | ||
var parentModule = getModule(pathArray, this.module); | ||
var parentModule = internal_1.getModule(pathArray, this.module); | ||
var newModule = module.create(this, pathArray.concat(moduleKey)); | ||
if (!parentModule.modules || !parentModule.modules[moduleKey]) { | ||
return throwError('Can not find module path to add module'); | ||
return internal_1.throwError('Can not find module path to add module'); | ||
} | ||
@@ -70,6 +83,6 @@ parentModule.modules[moduleKey] = newModule; | ||
} | ||
updateIn(this.state, pathArray.concat(moduleKey), function (parentState, key) { | ||
parentState[key] = observable(newModule.state); | ||
utils_1.updateIn(this.state, pathArray.concat(moduleKey), function (parentState, key) { | ||
parentState[key] = mobx_1.observable(newModule.state); | ||
}); | ||
updateIn(this.signals, pathArray.concat(moduleKey), function (parentState, key) { | ||
utils_1.updateIn(this.signals, pathArray.concat(moduleKey), function (parentState, key) { | ||
parentState[key] = Object.keys(newModule.signals).reduce(function (signals, key) { | ||
@@ -90,5 +103,5 @@ signals[key] = newModule.signals[key]; | ||
var moduleKey = pathArray.pop(); | ||
var parentModule = getModule(pathArray, this.module); | ||
var parentModule = internal_1.getModule(pathArray, this.module); | ||
if (!moduleKey || !parentModule || !parentModule.modules) { | ||
return throwError('Module you are trying to remove does not exist'); | ||
return internal_1.throwError('Module you are trying to remove does not exist'); | ||
} | ||
@@ -102,6 +115,6 @@ var module = parentModule.modules[moduleKey]; | ||
delete parentModule.modules[moduleKey]; | ||
updateIn(this.state, pathArray.concat(moduleKey), function (parentState) { | ||
utils_1.updateIn(this.state, pathArray.concat(moduleKey), function (parentState) { | ||
delete parentState[moduleKey]; | ||
}); | ||
updateIn(this.signals, pathArray.concat(moduleKey), function (parentSignals) { | ||
utils_1.updateIn(this.signals, pathArray.concat(moduleKey), function (parentSignals) { | ||
delete parentSignals[moduleKey]; | ||
@@ -112,9 +125,11 @@ }); | ||
return FluentController; | ||
}(BaseControllerClass)); | ||
export { FluentController }; | ||
export function Controller(rootModule, options) { | ||
}(cerebral_1.BaseControllerClass)); | ||
exports.FluentController = FluentController; | ||
function Controller(rootModule, options) { | ||
if (options === void 0) { options = {}; } | ||
options.Model = Model; | ||
options.Model = Model_1.default; | ||
options.Model.useLegacyStateApi = options.useLegacyStateApi; | ||
return new FluentController(rootModule, options); | ||
} | ||
exports.Controller = Controller; | ||
//# sourceMappingURL=index.js.map |
@@ -0,9 +1,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* eslint-env mocha */ | ||
import { Controller, Module, Dictionary, SequenceFactory, SequenceWithPropsFactory, Computed, } from './'; | ||
import View from './View'; | ||
import { autorun } from 'mobx'; | ||
import * as assert from 'assert'; | ||
var _1 = require("./"); | ||
var View_1 = require("./View"); | ||
var mobx_1 = require("mobx"); | ||
var assert = require("assert"); | ||
describe('Fluent', function () { | ||
it('should instantiate with initial state', function () { | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -13,7 +15,7 @@ foo: 'bar', | ||
}); | ||
var controller = Controller(rootModule); | ||
var controller = _1.Controller(rootModule); | ||
assert.deepEqual(controller.state, { foo: 'bar' }); | ||
}); | ||
it('should expose traditional model API', function () { | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -23,3 +25,3 @@ foo: 'bar', | ||
list: ['foo', 'bar', 'baz'], | ||
object: Dictionary({ | ||
object: _1.Dictionary({ | ||
foo: 'bar', | ||
@@ -48,3 +50,3 @@ }), | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
useLegacyStateApi: true, | ||
@@ -64,5 +66,5 @@ }); | ||
it('should instantiate with observable maps', function () { | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
map: Dictionary({ | ||
map: _1.Dictionary({ | ||
foo: 'bar', | ||
@@ -72,7 +74,7 @@ }), | ||
}); | ||
var controller = Controller(rootModule); | ||
var controller = _1.Controller(rootModule); | ||
assert.deepEqual(controller.state.map.get('foo'), 'bar'); | ||
}); | ||
it('should not throw when trying to mutate state outside actions', function () { | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -82,6 +84,6 @@ foo: 'bar', | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
useStrict: false, | ||
}); | ||
autorun(function () { | ||
mobx_1.autorun(function () { | ||
return controller.state.foo; | ||
@@ -94,3 +96,3 @@ }); | ||
it('should allow state changes in actions', function () { | ||
var SequenceWithProps = SequenceWithPropsFactory(); | ||
var SequenceWithProps = _1.SequenceWithPropsFactory(); | ||
var sequence = SequenceWithProps(function (s) { | ||
@@ -102,3 +104,3 @@ return s.action(function test(_a) { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -111,3 +113,3 @@ foo: 'bar', | ||
}); | ||
var controller = Controller(rootModule); | ||
var controller = _1.Controller(rootModule); | ||
controller.signals.test({ | ||
@@ -119,3 +121,3 @@ foo: 'bar', | ||
it('should allow computed', function () { | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var signal = Sequence(function (s) { | ||
@@ -131,6 +133,6 @@ return s.action(function test(_a) { | ||
}; | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
foo: 'bar', | ||
test: Computed(computed), | ||
test: _1.Computed(computed), | ||
}, | ||
@@ -141,4 +143,4 @@ signals: { | ||
}); | ||
var controller = Controller(rootModule); | ||
autorun(function () { | ||
var controller = _1.Controller(rootModule); | ||
mobx_1.autorun(function () { | ||
controller.state.test.get(); | ||
@@ -152,3 +154,3 @@ hasUpdated++; | ||
it('should allow getters', function () { | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var signal = Sequence(function (s) { | ||
@@ -161,3 +163,3 @@ return s.action(function test(_a) { | ||
var hasUpdated = 0; | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -173,4 +175,4 @@ foo: 'bar', | ||
}); | ||
var controller = Controller(rootModule); | ||
autorun(function () { | ||
var controller = _1.Controller(rootModule); | ||
mobx_1.autorun(function () { | ||
controller.state.test; | ||
@@ -184,3 +186,3 @@ hasUpdated++; | ||
it('should work with fluent api', function () { | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -192,3 +194,3 @@ return s.action(function testAction(_a) { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -201,3 +203,3 @@ foo: 'bar', | ||
}); | ||
var controller = Controller(rootModule); | ||
var controller = _1.Controller(rootModule); | ||
controller.signals.test(); | ||
@@ -208,3 +210,3 @@ assert.equal(controller.state.foo, 'bar2'); | ||
var actionCount = 0; | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -241,3 +243,3 @@ return s | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -250,3 +252,3 @@ _set: 'foo', | ||
_unshift: ['foo'], | ||
_observableMapSet: Dictionary({}), | ||
_observableMapSet: _1.Dictionary({}), | ||
}, | ||
@@ -257,3 +259,3 @@ signals: { | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
devtools: { | ||
@@ -331,3 +333,3 @@ init: function () { }, | ||
var hasUpdated = false; | ||
var SequenceWithProps = SequenceWithPropsFactory(); | ||
var SequenceWithProps = _1.SequenceWithPropsFactory(); | ||
var sequence = SequenceWithProps(function (s) { | ||
@@ -339,3 +341,3 @@ return s.action(function test(_a) { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -348,4 +350,4 @@ foo: 'bar', | ||
}); | ||
var controller = Controller(rootModule); | ||
var view = new View({ | ||
var controller = _1.Controller(rootModule); | ||
var view = new View_1.default({ | ||
controller: controller, | ||
@@ -371,3 +373,3 @@ onUpdate: function () { | ||
it('should handle nested map property', function () { | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -382,5 +384,5 @@ return s.action(function test(_a) { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
observableMapSet: Dictionary({ | ||
observableMapSet: _1.Dictionary({ | ||
item: { | ||
@@ -395,3 +397,3 @@ foo: 'string', | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
devtools: { | ||
@@ -417,3 +419,3 @@ init: function () { }, | ||
it('should handle removing map property', function () { | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -425,5 +427,5 @@ return s.action(function test(_a) { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
observableMapSet: Dictionary({ | ||
observableMapSet: _1.Dictionary({ | ||
item: { | ||
@@ -438,3 +440,3 @@ foo: 'string', | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
devtools: { | ||
@@ -458,3 +460,3 @@ init: function () { }, | ||
var actionCount = 0; | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -468,3 +470,3 @@ return s.action(function test(_a) { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
@@ -484,3 +486,3 @@ foo: { | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
devtools: { | ||
@@ -514,3 +516,3 @@ init: function () { }, | ||
var actionCount = 0; | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -534,5 +536,5 @@ return s.action(function test(_a) { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
state: { | ||
observableMapSet: Dictionary({ | ||
observableMapSet: _1.Dictionary({ | ||
item: { | ||
@@ -548,3 +550,3 @@ foo: 'string', | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
devtools: { | ||
@@ -616,3 +618,3 @@ init: function () { }, | ||
var actionCount = 0; | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -649,3 +651,3 @@ return s | ||
}); | ||
var fooModule = Module({ | ||
var fooModule = _1.Module({ | ||
state: { | ||
@@ -658,3 +660,3 @@ _set: 'foo', | ||
_unshift: ['foo'], | ||
_observableMapSet: Dictionary({}), | ||
_observableMapSet: _1.Dictionary({}), | ||
}, | ||
@@ -665,3 +667,3 @@ signals: { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
modules: { | ||
@@ -671,3 +673,3 @@ foo: fooModule, | ||
}); | ||
var controller = Controller(rootModule, { | ||
var controller = _1.Controller(rootModule, { | ||
devtools: { | ||
@@ -744,3 +746,3 @@ init: function () { }, | ||
it('should manage module state without devtools', function () { | ||
var Sequence = SequenceFactory(); | ||
var Sequence = _1.SequenceFactory(); | ||
var sequence = Sequence(function (s) { | ||
@@ -777,3 +779,3 @@ return s | ||
}); | ||
var fooModule = Module({ | ||
var fooModule = _1.Module({ | ||
state: { | ||
@@ -786,3 +788,3 @@ _set: 'foo', | ||
_unshift: ['foo'], | ||
_observableMapSet: Dictionary({}), | ||
_observableMapSet: _1.Dictionary({}), | ||
}, | ||
@@ -793,3 +795,3 @@ signals: { | ||
}); | ||
var rootModule = Module({ | ||
var rootModule = _1.Module({ | ||
modules: { | ||
@@ -799,3 +801,3 @@ foo: fooModule, | ||
}); | ||
var controller = Controller(rootModule); | ||
var controller = _1.Controller(rootModule); | ||
controller.signals.foo.test(); | ||
@@ -811,1 +813,2 @@ assert.equal(controller.state.foo._set, 'bar2'); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
@@ -11,7 +12,8 @@ var extendStatics = Object.setPrototypeOf || | ||
})(); | ||
import { extractModuleProp, isObject, BaseModel } from 'cerebral/internal'; | ||
import { observable, isObservable, isObservableMap, extendObservable, ObservableMap, } from 'mobx'; | ||
import { ComputedClass } from './Computed'; | ||
import { updateIn, traverse } from './utils'; | ||
import { CreateStateProvider } from './providers'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var internal_1 = require("cerebral/internal"); | ||
var mobx_1 = require("mobx"); | ||
var Computed_1 = require("./Computed"); | ||
var utils_1 = require("./utils"); | ||
var providers_1 = require("./providers"); | ||
var Model = /** @class */ (function (_super) { | ||
@@ -21,7 +23,7 @@ __extends(Model, _super); | ||
var _this = _super.call(this, controller) || this; | ||
_this.state = extractModuleProp(controller.module, 'state', function (state, module) { | ||
_this.state = internal_1.extractModuleProp(controller.module, 'state', function (state, module) { | ||
return _this.observeState(state); | ||
}); | ||
_this.StateProvider = function (devtools) { | ||
return CreateStateProvider(_this.state, devtools, _this, Model.useLegacyStateApi); | ||
return providers_1.CreateStateProvider(_this.state, devtools, _this, Model.useLegacyStateApi); | ||
}; | ||
@@ -32,4 +34,4 @@ return _this; | ||
Model.prototype.set = function (path, value) { | ||
updateIn(this.state, path, function (parentState, key) { | ||
if (isObservableMap(parentState)) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
if (mobx_1.isObservableMap(parentState)) { | ||
parentState.set(key, value); | ||
@@ -43,3 +45,3 @@ } | ||
Model.prototype.toggle = function (path) { | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key] = !parentState[key]; | ||
@@ -49,3 +51,3 @@ }); | ||
Model.prototype.push = function (path, value) { | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key].push(value); | ||
@@ -60,4 +62,4 @@ }); | ||
var value = Object.assign.apply(Object, [mergeValue].concat(values)); | ||
updateIn(this.state, path, function (parentState, key) { | ||
if (isObservableMap(parentState[key])) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
if (mobx_1.isObservableMap(parentState[key])) { | ||
parentState[key].merge(value); | ||
@@ -71,3 +73,3 @@ } | ||
Model.prototype.pop = function (path) { | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key].pop(); | ||
@@ -77,3 +79,3 @@ }); | ||
Model.prototype.shift = function (path) { | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key].shift(); | ||
@@ -83,3 +85,3 @@ }); | ||
Model.prototype.unshift = function (path, value) { | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key].unshift(value); | ||
@@ -93,3 +95,3 @@ }); | ||
} | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
(_a = parentState[key]).splice.apply(_a, args); | ||
@@ -101,3 +103,3 @@ var _a; | ||
var deleteKey = path.pop(); | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key].delete(deleteKey); | ||
@@ -107,3 +109,3 @@ }); | ||
Model.prototype.concat = function (path, value) { | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key] = parentState[key].concat(value); | ||
@@ -114,3 +116,3 @@ }); | ||
if (delta === void 0) { delta = 1; } | ||
updateIn(this.state, path, function (parentState, key) { | ||
utils_1.updateIn(this.state, path, function (parentState, key) { | ||
parentState[key] += delta; | ||
@@ -127,3 +129,3 @@ }); | ||
} | ||
if (currentState instanceof ObservableMap) { | ||
if (currentState instanceof mobx_1.ObservableMap) { | ||
return currentState.get(key); | ||
@@ -136,13 +138,13 @@ } | ||
var _this = this; | ||
var root = observable({}); | ||
var extension = traverse(state, function (parent, key, path) { | ||
if (isObservable(parent[key])) { | ||
var root = mobx_1.observable({}); | ||
var extension = utils_1.traverse(state, function (parent, key, path) { | ||
if (mobx_1.isObservable(parent[key])) { | ||
return parent[key]; | ||
} | ||
else if (isObject(parent[key])) { | ||
return observable(parent[key]); | ||
else if (internal_1.isObject(parent[key])) { | ||
return mobx_1.observable(parent[key]); | ||
} | ||
return parent[key]; | ||
}); | ||
var extended = extendObservable(root, Object.keys(extension).reduce(function (root, key) { | ||
var extended = mobx_1.extendObservable(root, Object.keys(extension).reduce(function (root, key) { | ||
var propertyDescriptor = Object.getOwnPropertyDescriptor(extension, key); | ||
@@ -152,3 +154,3 @@ if (propertyDescriptor && 'get' in propertyDescriptor) { | ||
} | ||
else if (extension[key] instanceof ComputedClass) { | ||
else if (extension[key] instanceof Computed_1.ComputedClass) { | ||
extension[key].getState = function () { return ({ | ||
@@ -160,4 +162,4 @@ state: extended, | ||
} | ||
else if (isObservable(extension[key])) { | ||
root[key] = observable.ref(extension[key]); | ||
else if (mobx_1.isObservable(extension[key])) { | ||
root[key] = mobx_1.observable.ref(extension[key]); | ||
} | ||
@@ -173,3 +175,4 @@ else { | ||
return Model; | ||
}(BaseModel)); | ||
export default Model; | ||
}(internal_1.BaseModel)); | ||
exports.default = Model; | ||
//# sourceMappingURL=Model.js.map |
@@ -1,3 +0,5 @@ | ||
var pathLambdaRegex = /function\s*\((\w)\)\s*{\s*return\s*\1[.]?([\w.[\]]+);\s*}/g; | ||
export function cerebralPathFromFunction(func, args) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var pathLambdaRegex = /function\s*\((\w)\)\s*{\s*return\s*\1[.]?([\w.[\]]+);?\s*}/g; | ||
function cerebralPathFromFunction(func, args) { | ||
pathLambdaRegex.lastIndex = 0; | ||
@@ -24,3 +26,4 @@ var str = func.toString().replace(/"use strict";/, ''); | ||
} | ||
export function pathFrom() { | ||
exports.cerebralPathFromFunction = cerebralPathFromFunction; | ||
function pathFrom() { | ||
return function (func) { | ||
@@ -34,3 +37,4 @@ var args = []; | ||
} | ||
export function pathFor(func) { | ||
exports.pathFrom = pathFrom; | ||
function pathFor(func) { | ||
var args = []; | ||
@@ -42,3 +46,4 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
export function combinePaths() { | ||
exports.pathFor = pathFor; | ||
function combinePaths() { | ||
return function (input) { | ||
@@ -68,1 +73,3 @@ var args = []; | ||
} | ||
exports.combinePaths = combinePaths; | ||
//# sourceMappingURL=paths.js.map |
@@ -1,4 +0,6 @@ | ||
import { Provider } from 'cerebral'; | ||
import { isObservable, isObservableMap } from 'mobx'; | ||
import { ComputedClass } from './Computed'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var cerebral_1 = require("cerebral"); | ||
var mobx_1 = require("mobx"); | ||
var Computed_1 = require("./Computed"); | ||
var mutationMethods = [ | ||
@@ -20,3 +22,3 @@ 'concat', | ||
} | ||
if (isObservableMap(currentState)) { | ||
if (mobx_1.isObservableMap(currentState)) { | ||
return currentState.get(pathKey.toString()); | ||
@@ -45,3 +47,3 @@ } | ||
} | ||
if (isObservableMap(target)) { | ||
if (mobx_1.isObservableMap(target)) { | ||
switch (key) { | ||
@@ -167,3 +169,3 @@ case 'size': | ||
} | ||
else if (target[key] instanceof ComputedClass) { | ||
else if (target[key] instanceof Computed_1.ComputedClass) { | ||
return target[key]; | ||
@@ -203,3 +205,3 @@ } | ||
basePath.concat(path), | ||
isObservable(value) ? value.value : value, | ||
mobx_1.isObservable(value) ? value.value : value, | ||
], | ||
@@ -227,3 +229,3 @@ }, execution, functionDetails, props); | ||
]; | ||
export function CreateStateProvider(state, devtools, model, useLegacyStateApi) { | ||
function CreateStateProvider(state, devtools, model, useLegacyStateApi) { | ||
if (!devtools && useLegacyStateApi) { | ||
@@ -242,3 +244,3 @@ legacyApi.forEach(function (method) { | ||
} | ||
return Provider(state, { | ||
return cerebral_1.Provider(state, { | ||
wrap: devtools | ||
@@ -259,3 +261,5 @@ ? function (context) { | ||
method: method, | ||
args: [path.split('.')].concat(args.map(function (value) { return (isObservable(value) ? value.value : value); })), | ||
args: [path.split('.')].concat(args.map(function (value) { | ||
return mobx_1.isObservable(value) ? value.value : value; | ||
})), | ||
}, context.execution, context.functionDetails, context.props); | ||
@@ -274,4 +278,5 @@ } | ||
} | ||
export function CreateModuleProvider(state, devtools, model, useLegacyStateApi) { | ||
return Provider(function (context) { | ||
exports.CreateStateProvider = CreateStateProvider; | ||
function CreateModuleProvider(state, devtools, model, useLegacyStateApi) { | ||
return cerebral_1.Provider(function (context) { | ||
var signalPath = context.execution.name.split('.'); | ||
@@ -295,3 +300,3 @@ var modulePath = signalPath.splice(0, signalPath.length - 1); | ||
method: 'module.' + method, | ||
args: [modulePath.concat(path.split('.'))].concat(args.map(function (value) { return (isObservable(value) ? value.value : value); })), | ||
args: [modulePath.concat(path.split('.'))].concat(args.map(function (value) { return (mobx_1.isObservable(value) ? value.value : value); })), | ||
}, context.execution, context.functionDetails, context.props); | ||
@@ -326,1 +331,3 @@ } | ||
} | ||
exports.CreateModuleProvider = CreateModuleProvider; | ||
//# sourceMappingURL=providers.js.map |
@@ -1,8 +0,10 @@ | ||
import { isObservable, isObservableMap } from 'mobx'; | ||
import { ComputedClass } from './Computed'; | ||
import { throwError, isObject } from 'cerebral/internal'; | ||
export function updateIn(obj, path, cb) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var mobx_1 = require("mobx"); | ||
var Computed_1 = require("./Computed"); | ||
var internal_1 = require("cerebral/internal"); | ||
function updateIn(obj, path, cb) { | ||
return path.reduce(function (currentValue, key, index) { | ||
if (index > 0 && currentValue === undefined) { | ||
throwError("You are setting to path \"" + path + "\", but it is not valid"); | ||
internal_1.throwError("You are setting to path \"" + path + "\", but it is not valid"); | ||
} | ||
@@ -12,3 +14,3 @@ if (index === path.length - 1) { | ||
} | ||
return isObservableMap(currentValue) | ||
return mobx_1.isObservableMap(currentValue) | ||
? currentValue.get(key) | ||
@@ -18,3 +20,4 @@ : currentValue[key]; | ||
} | ||
export function traverse(obj, cb, path) { | ||
exports.updateIn = updateIn; | ||
function traverse(obj, cb, path) { | ||
if (path === void 0) { path = []; } | ||
@@ -26,6 +29,6 @@ return Object.keys(obj).reduce(function (newObj, key) { | ||
} | ||
else if (isObservable(obj[key]) || obj[key] instanceof ComputedClass) { | ||
else if (mobx_1.isObservable(obj[key]) || obj[key] instanceof Computed_1.ComputedClass) { | ||
newObj[key] = obj[key]; | ||
} | ||
else if (isObject(obj[key])) { | ||
else if (internal_1.isObject(obj[key])) { | ||
path.push(key); | ||
@@ -42,1 +45,3 @@ newObj[key] = traverse(obj[key], cb, path); | ||
} | ||
exports.traverse = traverse; | ||
//# sourceMappingURL=utils.js.map |
@@ -1,5 +0,7 @@ | ||
import { Reaction } from 'mobx'; | ||
import { getChangedProps, throwError, isObject, noop } from 'cerebral/internal'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var mobx_1 = require("mobx"); | ||
var internal_1 = require("cerebral/internal"); | ||
function autorun(view) { | ||
var reaction = new Reaction(view.name || 'Autorun', function () { | ||
var reaction = new mobx_1.Reaction(view.name || 'Autorun', function () { | ||
this.track(view); | ||
@@ -14,6 +16,6 @@ }); | ||
if (!dependencies) { | ||
throwError('There is no reason to connect a component that has no dependencies'); | ||
internal_1.throwError('There is no reason to connect a component that has no dependencies'); | ||
} | ||
if (typeof dependencies !== 'function') { | ||
throwError('You have to define a function for the dependencies'); | ||
internal_1.throwError('You have to define a function for the dependencies'); | ||
} | ||
@@ -25,3 +27,3 @@ this.props = props; | ||
this.isUnmounted = false; | ||
this.updateComponent = onUpdate || noop; | ||
this.updateComponent = onUpdate || internal_1.noop; | ||
this.propKeys = Object.keys(props || {}); | ||
@@ -50,4 +52,4 @@ this._verifyPropsWarned = false; | ||
this.isMounted = true; | ||
if (!isObject(this.cerebralProps)) { | ||
throwError('You have to return dependencies from the dependency function'); | ||
if (!internal_1.isObject(this.cerebralProps)) { | ||
internal_1.throwError('You have to return dependencies from the dependency function'); | ||
} | ||
@@ -74,3 +76,3 @@ if (this.controller.devtools) { | ||
View.prototype.onPropsUpdate = function (props, nextProps) { | ||
var propsChanges = getChangedProps(props, nextProps); | ||
var propsChanges = internal_1.getChangedProps(props, nextProps); | ||
if (propsChanges.length) { | ||
@@ -100,2 +102,3 @@ this.props = nextProps; | ||
}()); | ||
export default View; | ||
exports.default = View; | ||
//# sourceMappingURL=View.js.map |
{ | ||
"name": "@cerebral/fluent", | ||
"version": "1.0.1", | ||
"version": "1.0.2-1523464214894", | ||
"description": "Makes Cerebral typescript friendly", | ||
"main": "index.js", | ||
"module": "./es/index.js", | ||
"jsnext:main": "./es/index.js", | ||
"typings": "./lib/index.d.ts", | ||
"author": "Christian Alfoni <christianalfoni@gmail.com>", | ||
@@ -10,3 +13,2 @@ "contributors": [ | ||
], | ||
"typings": "./index.d.ts", | ||
"license": "MIT", | ||
@@ -25,13 +27,16 @@ "repository": { | ||
"jest": "^22.1.1", | ||
"prop-types": "^15.6.1" | ||
"prop-types": "^15.6.1", | ||
"ts-node": "^5.0.1" | ||
}, | ||
"dependencies": { | ||
"cerebral": "^4.2.1", | ||
"cerebral": "^4.2.1-1523464214894", | ||
"mobx": "^3.4.1" | ||
}, | ||
"scripts": { | ||
"build": "cross-env tsc", | ||
"build": "run-p build:*", | ||
"build:cjs": "cross-env tsc", | ||
"build:es": "cross-env tsc -p ./tsconfig.es2015.json", | ||
"coverage": "nyc --reporter=lcov --reporter=json npm run test", | ||
"prepublish": "npm run build", | ||
"test": "cross-env mocha --require ts-runtime.js \"src/**/*.test.ts\"", | ||
"test": "cross-env TS_NODE_IGNORE=false mocha --require ts-node/register \"src/**/*.test.ts\"", | ||
"test:watch": "npm run test -- --watch" | ||
@@ -38,0 +43,0 @@ }, |
116
README.md
@@ -8,2 +8,3 @@ # @cerebral/fluent | ||
## Description | ||
This addon gives you full typesafety using [TypeScript](https://www.typescriptlang.org/). It exposes a chainable API and types to be used with defining your modules, sequences, actions and connecting to components. | ||
@@ -21,5 +22,3 @@ | ||
export const doThis = sequence(s => s | ||
.action(actions.someAction) | ||
) | ||
export const doThis = sequence((s) => s.action(actions.someAction)) | ||
``` | ||
@@ -33,7 +32,6 @@ | ||
export const doThis = Sequence(s => s | ||
.branch(actions.someBranchAction) | ||
.paths({ | ||
pathA: s => s, | ||
pathB: s => s | ||
export const doThis = Sequence((s) => | ||
s.branch(actions.someBranchAction).paths({ | ||
pathA: (s) => s, | ||
pathB: (s) => s | ||
}) | ||
@@ -50,4 +48,4 @@ ) | ||
type State = { | ||
items: string[], | ||
currentItemIndex: number, | ||
items: string[] | ||
currentItemIndex: number | ||
currentItem: ComputedValue<string> | ||
@@ -59,3 +57,3 @@ } | ||
currentItemIndex: 0, | ||
currentItem: Computed(state => state.items[state.currentItemIndex]) | ||
currentItem: Computed((state) => state.items[state.currentItemIndex]) | ||
} | ||
@@ -89,16 +87,15 @@ | ||
})) | ||
.to( | ||
function App ({ foo, onClick, externalProp }) { | ||
return <div></div> | ||
} | ||
) | ||
.to(function App({ foo, onClick, externalProp }) { | ||
return <div /> | ||
}) | ||
// Alternatively | ||
.toClass(props => | ||
class App extends React.Component<typeof props> { | ||
render () { | ||
const { foo, onClick, externalProp } = this.props | ||
.toClass( | ||
(props) => | ||
class App extends React.Component<typeof props> { | ||
render() { | ||
const { foo, onClick, externalProp } = this.props | ||
return <div></div> | ||
return <div /> | ||
} | ||
} | ||
} | ||
) | ||
@@ -152,5 +149,3 @@ ``` | ||
export const doThis = sequence(s => s | ||
.debounce(100) | ||
) | ||
export const doThis = sequence((s) => s.debounce(100)) | ||
``` | ||
@@ -163,8 +158,7 @@ | ||
export const doThis = sequence(s => s | ||
.equals(({ state }) => state.user.role) | ||
.paths({ | ||
admin: s => s, | ||
user: s => s, | ||
otherwise: s => s | ||
export const doThis = sequence((s) => | ||
s.equals(({ state }) => state.user.role).paths({ | ||
admin: (s) => s, | ||
user: (s) => s, | ||
otherwise: (s) => s | ||
}) | ||
@@ -177,14 +171,21 @@ ) | ||
```ts | ||
import { IContext, IBranchContext, ConnectFacory, SequenceFactory, SequenceWithPropsFactory } from '@cerebral/fluent' | ||
import { | ||
IContext, | ||
IBranchContext, | ||
ConnectFacory, | ||
SequenceFactory, | ||
SequenceWithPropsFactory | ||
} from '@cerebral/fluent' | ||
import { State, Signals, Providers } from './app' | ||
export type Context<Props> = IContext<Props> & Providers; | ||
export type Context<Props> = IContext<Props> & Providers | ||
export type BranchContext<Paths, Props> = IBranchContext<Paths, Props> & Providers; | ||
export type BranchContext<Paths, Props> = IBranchContext<Paths, Props> & | ||
Providers | ||
export const connect = ConnectFactory<State, Signals>(); | ||
export const connect = ConnectFactory<State, Signals>() | ||
export const sequence = SequenceFactory<SignalContext>(); | ||
export const sequence = SequenceFactory<SignalContext>() | ||
export const sequenceWithProps = SequenceWithPropsFactory<SignalContext>(); | ||
export const sequenceWithProps = SequenceWithPropsFactory<SignalContext>() | ||
``` | ||
@@ -203,7 +204,7 @@ | ||
export interface Providers { | ||
http: HttpProvider, | ||
http: HttpProvider | ||
state: State | ||
} | ||
export type Context<Props> = IContext<Props> & Providers; | ||
export type Context<Props> = IContext<Props> & Providers | ||
``` | ||
@@ -222,7 +223,8 @@ | ||
export interface Providers { | ||
http: HttpProvider, | ||
http: HttpProvider | ||
state: State | ||
} | ||
export type BranchContext<Paths, Props> = IBranchContext<Paths, Props> & Providers | ||
export type BranchContext<Paths, Props> = IBranchContext<Paths, Props> & | ||
Providers | ||
``` | ||
@@ -260,7 +262,6 @@ | ||
export const doThis = sequence(s => s | ||
.branch(actions.doThis) | ||
.paths({ | ||
success: s => s, | ||
error: s => s | ||
export const doThis = sequence((s) => | ||
s.branch(actions.doThis).paths({ | ||
success: (s) => s, | ||
error: (s) => s | ||
}) | ||
@@ -270,3 +271,2 @@ ) | ||
## sequence | ||
@@ -281,3 +281,3 @@ | ||
// Sequence with expected output props | ||
export const composeThis = sequence<{ foo: string, bar: string }>(s | ||
export const composeThis = sequence<{ foo: string, bar: string }>(s | ||
``` | ||
@@ -294,3 +294,3 @@ | ||
// Sequence with expected input and output props | ||
export const composeThis = sequence<{ foo: string }, { foo: string, bar: string }>(s | ||
export const composeThis = sequence<{ foo: string }, { foo: string, bar: string }>(s | ||
``` | ||
@@ -301,7 +301,5 @@ | ||
```ts | ||
import { Sequence } from '@cerebral/fluent' | ||
import { Sequence } from '@cerebral/fluent' | ||
export const doThis = Sequence(s => s | ||
.wait(1000) | ||
) | ||
export const doThis = Sequence((s) => s.wait(1000)) | ||
``` | ||
@@ -312,9 +310,8 @@ | ||
```ts | ||
import { Sequence } from '@cerebral/fluent' | ||
import { Sequence } from '@cerebral/fluent' | ||
export const doThis = Sequence(s => s | ||
.when(({ state }) => state.isAwesome) | ||
.paths({ | ||
true: s => s, | ||
false: s => s | ||
export const doThis = Sequence((s) => | ||
s.when(({ state }) => state.isAwesome).paths({ | ||
true: (s) => s, | ||
false: (s) => s | ||
}) | ||
@@ -347,2 +344,1 @@ ) | ||
``` | ||
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
256033
68
3630
5
2
327