Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _utils = require('./utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
Expand Down Expand Up @@ -108,12 +110,12 @@ function local() {
var id = getId(_this.props),
storeState = _this.store.getState();

if (!storeState.local) {
if (!(0, _utils.getLocalState)(storeState)) {
throw new Error('did you forget to include the `local` reducer?');
}
return {
id: id,
value: whenUndefined(storeState.local.get(id), getInitial(_this.props))
value: whenUndefined((0, _utils.getLocalState)(storeState).get(id), getInitial(_this.props))
};
}(), _this.$ = function (action) {
// 'localize' an event. super convenient for making actions 'local' to this component
Expand Down Expand Up @@ -175,7 +177,7 @@ function local() {

this.setState({
id: id,
value: whenUndefined(this.store.getState().local.get(id), init)
value: whenUndefined((0, _utils.getLocalState)(this.store.getState()).get(id), init)
});
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var _tree = require('./tree');

var T = _interopRequireWildcard(_tree);

var _utils = require('./utils');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
Expand Down Expand Up @@ -67,7 +69,7 @@ var Root = (_temp2 = _class = function (_Component) {

if (isBrowserLike) {
this.dispose = this.context.store.subscribe(function () {
var state = _this2.context.store.getState().local,
var state = (0, _utils.getLocalState)(_this2.context.store.getState()),
changed = false;
T.entries(state.$$changed).forEach(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 2),
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getLocalState = getLocalState;
function getLocalState(state) {
if (typeof state.get === 'function') {
return state.get('local');
}
return state.local;
};
10 changes: 6 additions & 4 deletions src/decorator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { PropTypes, Component } from 'react'

import { getLocalState } from './utils';

const isBrowserLike = typeof navigator !== 'undefined'

function whenUndefined(o, orElse) {
Expand Down Expand Up @@ -77,13 +79,13 @@ export default function local({
state = (() => {
let id = getId(this.props),
storeState = this.store.getState()

if(!storeState.local) {
if(!getLocalState( storeState )) {
throw new Error('did you forget to include the `local` reducer?')
}
return {
id,
value: whenUndefined(storeState.local.get(id), getInitial(this.props))
value: whenUndefined( getLocalState( storeState ).get( id ), getInitial( this.props ) )
}
})()

Expand Down Expand Up @@ -145,7 +147,7 @@ export default function local({

this.setState({
id,
value: whenUndefined(this.store.getState().local.get(id), init)
value: whenUndefined( getLocalState( this.store.getState() ).get( id ), init )
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/root.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, PropTypes } from 'react'

import * as T from './tree'
import {getLocalState} from './utils';

const isBrowserLike = typeof navigator !== 'undefined'

Expand All @@ -25,7 +26,7 @@ export default class Root extends Component {
componentWillMount() {
if(isBrowserLike) {
this.dispose = this.context.store.subscribe(() => {
let state = this.context.store.getState().local, changed = false
let state = getLocalState(this.context.store.getState()), changed = false
T.entries(state.$$changed).forEach(([ key, value ]) => {
changed = true;
(this.fns[key] || []).forEach(fn => fn(value))
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function getLocalState( state ){
if(typeof state.get === 'function') {
return state.get( 'local' );
}
return state.local;
};