Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 11.1.0

- Added getRepo method to presenters to allow greater control over
repo assignment
- Actions may now, intentionally, be set to undefined

## 11.0.0

- Fix bug where Presenter given stateless view component as an inline
Expand Down
39 changes: 23 additions & 16 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import coroutine from './coroutine'
import tag from './tag'
import { inherit } from './utils'

// What types are disposable?
const DISPOSABLE = {
cancelled : true,
done : true,
error : true
}

/**
* Actions encapsulate the process of resolving an action creator. Create an
* action using `Microcosm::push`:
Expand All @@ -16,7 +23,7 @@ export default function Action (behavior) {

inherit(Action, Emitter, {
type : null,
payload : null,
payload : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

disabled : false,
diposable : false,
parent : null,
Expand Down Expand Up @@ -45,16 +52,16 @@ inherit(Action, Emitter, {
/**
* If defined, sets the payload for the action and triggers a "change" event.
*/
set (type, payload, disposable) {
set (type, payload) {
// Ignore set if the action is already disposed.
if (this.disposable) {
return false
}

this.type = this.behavior[type]
this.disposable = disposable
this.disposable = DISPOSABLE.hasOwnProperty(type)

if (payload != undefined) {
if (arguments.length > 1) {
this.payload = payload
}

Expand All @@ -67,8 +74,8 @@ inherit(Action, Emitter, {
* Set the action state to "open", then set a payload if provided. Triggers
* the "open" event.
*/
open (payload) {
if (this.set('open', payload, false)) {
open (...params) {
if (this.set('open', ...params)) {
this._emit('open', this.payload)
}

Expand All @@ -79,9 +86,9 @@ inherit(Action, Emitter, {
* Set the action state to "loading", then set a payload if provided.
* Triggers the "update" event.
*/
send (payload) {
if (this.set('loading', payload, false)) {
this._emit('update', payload)
send (...params) {
if (this.set('loading', ...params)) {
this._emit('update', this.payload)
}

return this
Expand All @@ -91,9 +98,9 @@ inherit(Action, Emitter, {
* Set the action state to "error" and marks the action for clean up, then
* set a payload if provided. Triggers the "error" event.
*/
reject (payload) {
if (this.set('error', payload, true)) {
this._emit('error', payload)
reject (...params) {
if (this.set('error', ...params)) {
this._emit('error', this.payload)
}

return this
Expand All @@ -103,8 +110,8 @@ inherit(Action, Emitter, {
* Set the action state to "done" and marks the action for clean up, then set
* a payload if provided. Triggers the "done" event.
*/
resolve (payload) {
if (this.set('done', payload, true)) {
resolve (...params) {
if (this.set('done', ...params)) {
this._emit('done', this.payload)
}

Expand All @@ -115,8 +122,8 @@ inherit(Action, Emitter, {
* Set the action state to "cancelled" and marks the action for clean up,
* then set a payload if provided. Triggers the "cancel" event.
*/
cancel (payload) {
if (this.set('cancelled', payload, true)) {
cancel (...params) {
if (this.set('cancelled', ...params)) {
this._emit('cancel', this.payload)
}

Expand Down
15 changes: 15 additions & 0 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ test('accommodates string actions', function () {
expect(action.type).toBe('test')
})

test('an action payload is undefined by default', function () {
const action = new Action('test').resolve()

expect(action.payload).toBe(undefined)
})

test('an action can intentionally be set to undefined', function () {
const action = new Action('test')

action.open(true)
action.resolve(undefined)

expect(action.payload).toBe(undefined)
})

test('actions can be tested externally', function () {
const repo = new Microcosm()
const identity = n => n
Expand Down