Skip to content

Commit

Permalink
decorators for instances
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed May 11, 2015
1 parent a9c35e0 commit 3865214
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
38 changes: 26 additions & 12 deletions src/utils/decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ function addMeta(description, decoration) {
return description
}

export function createActions(alt, ...args) {
return function (Actions) {
return alt.createActions(Actions, {}, ...args)
}
}

export function createStore(alt, ...args) {
return function (Store) {
export function decorate(context) {
return (Store) => {
const proto = Store.prototype
const publicMethods = {}
const bindListeners = {}
Expand All @@ -31,8 +25,10 @@ export function createStore(alt, ...args) {
}

/* istanbul ignore else */
if (meta.action) {
bindListeners[name] = meta.action
if (meta.actions) {
bindListeners[name] = meta.actions
} else if (meta.actionsWithContext) {
bindListeners[name] = meta.actionsWithContext(context)
} else if (meta.publicMethod) {
publicMethods[name] = proto[name]
}
Expand All @@ -43,13 +39,31 @@ export function createStore(alt, ...args) {
publicMethods
}, Store.config)

return alt.createStore(Store, undefined, ...args)
return Store
}
}

export function createActions(alt, ...args) {
return function (Actions) {
return alt.createActions(Actions, {}, ...args)
}
}

export function createStore(alt, ...args) {
return function (Store) {
return alt.createStore(decorate(alt)(Store), undefined, ...args)
}
}

export function bind(...actionIds) {
return (obj, name, description) => {
return addMeta(description, { action: actionIds })
return addMeta(description, { actions: actionIds })
}
}

export function bindWithContext(fn) {
return (obj, name, description) => {
return addMeta(description, { actionsWithContext: fn })
}
}

Expand Down
44 changes: 41 additions & 3 deletions test/decorators-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { assert } from 'chai'
import Alt from '../'
import { createActions, createStore, bind, expose } from '../utils/decorators'
import {
createActions,
createStore,
bind,
expose,
bindWithContext,
decorate
} from '../utils/decorators'

const alt = new Alt()

Expand Down Expand Up @@ -55,8 +62,8 @@ export default {
'decorating action listening and public methods'() {
const TodoActions = alt.generateActions('addTodo')

@createStore(alt)
class TodoStore {
@decorate(alt)
class TodoStoreClass {
static displayName = 'TodoStore'

constructor() {
Expand All @@ -79,11 +86,42 @@ export default {
}
}

const TodoStore = alt.createStore(TodoStoreClass)

TodoActions.addTodo('hello')

assert(TodoStore.getState().id === 1)
assert.isFunction(TodoStore.getTodo)
assert(TodoStore.getTodo(0) === 'hello')
},

'decorating instances using context'() {
class TodoStore {
constructor() {
this.todos = []
}

@bindWithContext(alt => alt.actions.TodoActions.addTodo)
addTodo(item) {
this.todos.push(item)
}
}

class Flux extends Alt {
constructor() {
super()
this.addActions('TodoActions', [
'addTodo'
])
this.addStore('TodoStore', decorate(this)(TodoStore))
}
}

const flux = new Flux()
assert(flux.stores.TodoStore.getState().todos.length === 0, 'state is empty')
flux.actions.TodoActions.addTodo('hello world')
assert(flux.stores.TodoStore.getState().todos.length === 1, 'we have an item')
assert(flux.stores.TodoStore.getState().todos[0] === 'hello world', 'it is hello world')
},
}
}

0 comments on commit 3865214

Please sign in to comment.