Skip to content

Commit

Permalink
Action statuses are now registerable
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Feb 14, 2017
1 parent 0903506 commit 5c16447
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 38 deletions.
5 changes: 4 additions & 1 deletion src/get-registration.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
export default function getRegistration (entity, behavior, status) {
let type = behavior[status]

if (entity.register != null) {
if (entity.register) {
var registry = entity.register()

if (typeof registry[behavior] === 'object') {

return registry[behavior][status]
} else {
return registry[type]
}
}

return null
}
49 changes: 17 additions & 32 deletions src/microcosm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Action from './action'
import Emitter from './emitter'
import History from './history'
import Realm from './realm'
import createEffect from './create-effect'
import tag from './tag'
import coroutine from './coroutine'
import Action from './action'
import Emitter from './emitter'
import History from './history'
import Realm from './realm'
import createEffect from './create-effect'
import tag from './tag'
import coroutine from './coroutine'
import getRegistration from './get-registration'

import {
RESET,
Expand Down Expand Up @@ -228,7 +229,6 @@ inherit(Microcosm, Emitter, {

/**
* Publish a release if anything changed
* @param {Action} action
*/
release (action) {
if (action) {
Expand All @@ -245,8 +245,6 @@ inherit(Microcosm, Emitter, {
/**
* Append an action to history and return it. This is used by push,
* but also useful for testing action states.
* @param {Function} behavior - An action function
* @return {Action} action representation of the invoked function
*/
append (behavior) {
return this.history.append(behavior)
Expand All @@ -255,9 +253,6 @@ inherit(Microcosm, Emitter, {
/**
* Push an action into Microcosm. This will trigger the lifecycle for updating
* state.
* @param {Function} behavior - An action function
* @param {...Any} params - Parameters to invoke the type with
* @return {Action} action representaftion of the invoked function
*/
push (behavior/*, ... params */) {
let action = this.append(behavior)
Expand All @@ -270,8 +265,6 @@ inherit(Microcosm, Emitter, {

/**
* Partially apply push
* @param {...Any} params - Parameters to invoke the type with
* @return {Function} A partially applied push function
*/
prepare (/*... params */) {
let params = toArray(arguments)
Expand All @@ -286,32 +279,25 @@ inherit(Microcosm, Emitter, {

/**
* Adds a domain to the Microcosm instance. A domain informs the
* microcosm how to process various action types. If no key
* is given, the domain will operate on all application state.
* @param {String|null} key - The namespace within application state for the domain.
* @param {Object|Function} domain Configuration options to create a domain
* @return {Microcosm} self
* Microcosm how to process various action types. If no key is
* given, the domain will operate on all application state.
*/
addDomain (key, domain, options) {
addDomain (key, config, options) {
this.follower = false

this.realm.add(key, domain, options)
let domain = this.realm.add(key, config, options)

this.rebase(key)

return this
return domain
},

/**
* An effect is a one-time handler that fires whenever an action changes. Callbacks
* will only ever fire once, and can not modify state.
* @param {Object|Function} effect - Configuration options to create an effect
* @param {Object} options - Options to pass to the effect
* @return {Microcosm} self
*/
addEffect (effect, options) {
createEffect(this, effect, options)

return this
addEffect (config, options) {
return createEffect(this, config, options)
},

/**
Expand Down Expand Up @@ -350,7 +336,6 @@ inherit(Microcosm, Emitter, {

/**
* Alias serialize for JS interoperability.
* @return {Object} result of `serialize`.
*/
toJSON () {
return this.serialize()
Expand Down Expand Up @@ -473,4 +458,4 @@ inherit(Microcosm, Emitter, {

export default Microcosm

export { Microcosm, Action, History, tag, get, set, merge, inherit }
export { Microcosm, Action, History, tag, get, set, merge, inherit, getRegistration }
66 changes: 61 additions & 5 deletions test/register.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Microcosm from '../src/microcosm'
import lifecycle from '../src/lifecycle'

const action = a => a
let action = a => a

test('sends actions in the context of the domain', function (done) {
const repo = new Microcosm()
test('sends actions in the context of the domain', function () {
expect.assertions(1)

let repo = new Microcosm()

repo.addDomain('test', {
test: true,
Expand All @@ -12,7 +15,6 @@ test('sends actions in the context of the domain', function (done) {
return {
[action]() {
expect(this.test).toBe(true)
done()
}
}
}
Expand All @@ -22,7 +24,7 @@ test('sends actions in the context of the domain', function (done) {
})

test('returns the same state if a handler is not provided', function () {
const repo = new Microcosm()
let repo = new Microcosm()

repo.addDomain('test', {
getInitialState() {
Expand All @@ -34,3 +36,57 @@ test('returns the same state if a handler is not provided', function () {
expect(repo.state.test).toEqual('test')
})
})

describe('nesting', function () {

test('allows domains nested registration methods', function () {
let repo = new Microcosm()
let handler = jest.fn()

let domain = repo.addDomain('test', {
register () {
return {
[action]: {
open : handler,
update : handler,
reject : handler,
resolve : handler,
cancel : handler
}
}
}
})

expect(domain).toRegister(action, 'open')
expect(domain).toRegister(action, 'update')
expect(domain).toRegister(action, 'reject')
expect(domain).toRegister(action, 'resolve')
expect(domain).toRegister(action, 'cancel')
})

test('allows domains nested registration methods', function () {
let repo = new Microcosm()
let handler = jest.fn()

let effect = repo.addEffect({
register () {
return {
[action]: {
open : handler,
update : handler,
reject : handler,
resolve : handler,
cancel : handler
}
}
}
})

expect(effect).toRegister(action, 'open')
expect(effect).toRegister(action, 'update')
expect(effect).toRegister(action, 'reject')
expect(effect).toRegister(action, 'resolve')
expect(effect).toRegister(action, 'cancel')
})

})

0 comments on commit 5c16447

Please sign in to comment.