Skip to content

Commit

Permalink
Fix coverage add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 14, 2015
1 parent f0185f2 commit f572a96
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 90 deletions.
50 changes: 20 additions & 30 deletions dist/alt-with-runtime.js
Expand Up @@ -37,6 +37,24 @@ function uid(container, name) {
return key;
}

function doSetState(store, storeInstance, nextState) {
if (!nextState) {
return;
}

storeInstance[NEXT_STATE] = storeInstance[NEXT_STATE] || {};
if (typeof nextState === "function") {
assign(storeInstance[NEXT_STATE], nextState(storeInstance[STATE_CONTAINER]));
} else {
assign(storeInstance[NEXT_STATE], nextState);
}

if (!storeInstance[IS_DISPATCHING]) {
storeInstance[STATE_CONTAINER] = storeInstance[NEXT_STATE];
store.emitChange();
}
}

/* istanbul ignore next */
function NoopClass() {}

Expand Down Expand Up @@ -332,21 +350,7 @@ var createStoreFromObject = function (alt, StoreModel, key, saveStore) {
return storeInstance;
},
setState: function setState(nextState) {
if (!nextState) {
return;
}

storeInstance[NEXT_STATE] = storeInstance[NEXT_STATE] || {};
if (typeof state === "function") {
assign(storeInstance[NEXT_STATE], nextState(storeInstance[STATE_CONTAINER]));
} else {
assign(storeInstance[NEXT_STATE], nextState);
}

if (!storeInstance[IS_DISPATCHING]) {
storeInstance[STATE_CONTAINER] = storeInstance[NEXT_STATE];
this.emitChange();
}
doSetState(this, storeInstance, nextState);
}
}, StoreMixinListeners, StoreMixinEssentials, StoreModel);

Expand Down Expand Up @@ -442,21 +446,7 @@ var Alt = (function () {
return storeInstance;
},
setState: function setState(nextState) {
if (!nextState) {
return;
}

storeInstance[NEXT_STATE] = storeInstance[NEXT_STATE] || {};
if (typeof state === "function") {
assign(storeInstance[NEXT_STATE], nextState(storeInstance[STATE_CONTAINER]));
} else {
assign(storeInstance[NEXT_STATE], nextState);
}

if (!storeInstance[IS_DISPATCHING]) {
storeInstance[STATE_CONTAINER] = storeInstance[NEXT_STATE];
this.emitChange();
}
doSetState(this, storeInstance, nextState);
}
});

Expand Down
48 changes: 24 additions & 24 deletions docs/createStore.md
Expand Up @@ -120,6 +120,30 @@ class MyStore {
}
```

## StoreModel#setState

> (state = {}: function | object): undefined
`setState` is the recommended way to set state for a store. This method automatically emits a change event. `setState` also accepts a function which returns an object of the state to be set. For convenience, calls to `setState` are automatically batched as they are in React and only one change event will be emitted once the action finishes dispatching.

```js
class MyStore {
handleFoo() {
this.foo = 0;

setTimeout(() => {
// set foo to 1 and emit a change.
this.setState({
foo: 1
});
}, 100);

// supress emitting a change.
return false;
}
}
```

## StoreModel#exportPublicMethods

> (methods: object): undefined
Expand Down Expand Up @@ -192,30 +216,6 @@ class MyStore {
}
```

## StoreModel#setState

> (state = {}: object): false
`setState` is syntactic sugar for setting state. This method automatically emits a change event. This can be particularly useful when performing async in your stores when you wish to update the state and emit a change event all in one.

```js
class MyStore {
handleFoo() {
this.foo = 0;

setTimeout(() => {
// set foo to 1 and emit a change.
this.setState({
foo: 1
});
}, 100);

// supress emitting a change.
return false;
}
}
```

## StoreModel#dispatcher

This is a reference to the dispatcher used by alt which is flux's own dispatcher. You may use this to listen in on all the global dispatches yourself.
Expand Down
59 changes: 23 additions & 36 deletions src/alt.js
Expand Up @@ -36,6 +36,27 @@ function uid(container, name) {
return key
}

function doSetState(store, storeInstance, nextState) {
if (!nextState) {
return
}

storeInstance[NEXT_STATE] = storeInstance[NEXT_STATE] || {}
if (typeof nextState === 'function') {
assign(
storeInstance[NEXT_STATE],
nextState(storeInstance[STATE_CONTAINER])
)
} else {
assign(storeInstance[NEXT_STATE], nextState)
}

if (!storeInstance[IS_DISPATCHING]) {
storeInstance[STATE_CONTAINER] = storeInstance[NEXT_STATE]
store.emitChange()
}
}

/* istanbul ignore next */
function NoopClass() { }

Expand Down Expand Up @@ -319,24 +340,7 @@ const createStoreFromObject = (alt, StoreModel, key, saveStore) => {
return storeInstance
},
setState(nextState) {
if (!nextState) {
return
}

storeInstance[NEXT_STATE] = storeInstance[NEXT_STATE] || {}
if (typeof state === 'function') {
assign(
storeInstance[NEXT_STATE],
nextState(storeInstance[STATE_CONTAINER])
)
} else {
assign(storeInstance[NEXT_STATE], nextState)
}

if (!storeInstance[IS_DISPATCHING]) {
storeInstance[STATE_CONTAINER] = storeInstance[NEXT_STATE]
this.emitChange()
}
doSetState(this, storeInstance, nextState)
}
}, StoreMixinListeners, StoreMixinEssentials, StoreModel)

Expand Down Expand Up @@ -424,24 +428,7 @@ class Alt {
return storeInstance
},
setState(nextState) {
if (!nextState) {
return
}

storeInstance[NEXT_STATE] = storeInstance[NEXT_STATE] || {}
if (typeof state === 'function') {
assign(
storeInstance[NEXT_STATE],
nextState(storeInstance[STATE_CONTAINER])
)
} else {
assign(storeInstance[NEXT_STATE], nextState)
}

if (!storeInstance[IS_DISPATCHING]) {
storeInstance[STATE_CONTAINER] = storeInstance[NEXT_STATE]
this.emitChange()
}
doSetState(this, storeInstance, nextState)
}
})

Expand Down
50 changes: 50 additions & 0 deletions test/setting-state.js
Expand Up @@ -59,5 +59,55 @@ export default {

myStore.unlisten(spy)
},

'transactional setState'() {
const alt = new Alt()

const actions = alt.generateActions('fire')
class SetState {
constructor() {
this.bindActions(actions)
this.x = 0
}

fire() {
this.setState(() => {
return {
x: 1
}
})
}
}

const store = alt.createStore(SetState)

assert(store.getState().x === 0, 'x is initially 0')
actions.fire()
assert(store.getState().x === 1, 'x is 1')
},

'transactional setState with failure'() {
const alt = new Alt()

const actions = alt.generateActions('fire')
class SetState {
constructor() {
this.bindActions(actions)
this.x = 0
}

fire() {
this.setState(() => {
throw new Error('error')
})
}
}

const store = alt.createStore(SetState)

assert(store.getState().x === 0, 'x is initially 0')
assert.throws(() => actions.fire())
assert(store.getState().x === 0, 'x remains 0')
},
}
}

0 comments on commit f572a96

Please sign in to comment.