Skip to content

Commit

Permalink
Merge pull request #194 from goatslacker/add-actions-to-alt
Browse files Browse the repository at this point in the history
Add all actions to this.actions
  • Loading branch information
goatslacker committed Apr 29, 2015
2 parents 1dfd8a2 + 6d341e9 commit 1d32ad9
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 158 deletions.
68 changes: 17 additions & 51 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@
2,
"always"
],
"func-names": [
0
],
"func-names": 0,
"func-style": [
0,
"expression"
Expand Down Expand Up @@ -148,18 +146,10 @@
"capIsNew": true
}
],
"new-parens": [
2
],
"no-array-constructor": [
2
],
"no-inline-comments": [
2
],
"no-lonely-if": [
2
],
"new-parens": 2,
"no-array-constructor": 2,
"no-inline-comments": 2,
"no-lonely-if": 2,
"no-mixed-spaces-and-tabs": [
2,
"always"
Expand All @@ -170,46 +160,22 @@
"max": 2
}
],
"no-nested-ternary": [
2
],
"no-new": [
0
],
"no-new-object": [
2
],
"no-nested-ternary": 2,
"no-new": 0,
"no-new-object": 2,
"no-return-assign": 0,
"no-sequences": [
0
],
"no-sequences": 0,
"no-spaced-func": [
2
],
"no-ternary": [
0
],
"no-trailing-spaces": [
2
],
"no-underscore-dangle": [
0
],
"no-wrap-func": [
2
],
"no-var": [
0
],
"one-var": [
0
],
"operator-assignment": [
0
],
"padded-blocks": [
0
],
"no-ternary": 0,
"no-trailing-spaces": 2,
"no-underscore-dangle": 0,
"no-wrap-func": 2,
"no-var": 0,
"one-var": 0,
"operator-assignment": 0,
"padded-blocks": 0,
"quote-props": [
2,
"as-needed"
Expand Down
97 changes: 49 additions & 48 deletions src/alt/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import AltAction from './AltAction'
import Symbol from 'es-symbol'
import assign from 'object-assign'
import formatAsConstant from './utils/formatAsConstant'
import getInternalMethods from './utils/getInternalMethods'
import uid from './utils/uid'
import { Dispatcher } from 'flux'
import { warn } from './utils/warnings'
import * as StoreUtils from './utils/StoreUtils'
import * as Sym from './symbols/symbols'
import * as StateFunctions from './utils/StateFunctions'
import createStoreConfig from './utils/createStoreConfig'

const {
import makeAction from './utils/makeAction'
import {
filterSnapshots,
saveInitialSnapshot,
setAppState,
snapshot
} from './utils/StateFunctions'
import {
createStoreConfig,
createStoreFromObject,
createStoreFromClass,
transformStore
} = StoreUtils
const {
ACTION_HANDLER,
} from './utils/StoreUtils'
import {
ACTION_KEY,
ACTIONS_REGISTRY,
INIT_SNAPSHOT,
LAST_SNAPSHOT,
LIFECYCLE
} = Sym
const {
filterSnapshots,
saveInitialSnapshot,
setAppState,
snapshot
} = StateFunctions

const ACTIONS_REGISTRY = Symbol()
} from './symbols/symbols'
import {
dispatchIdentity,
formatAsConstant,
getInternalMethods,
uid,
warn
} from './utils/AltUtils'

class Alt {
constructor(config = {}) {
this.config = config
this.serialize = config.serialize || JSON.stringify
this.deserialize = config.deserialize || JSON.parse
this.dispatcher = config.dispatcher || new Dispatcher()
this.actions = {}
this.actions = { global: {} }
this.stores = {}
this.storeTransforms = config.storeTransforms || []
this[ACTIONS_REGISTRY] = {}
Expand Down Expand Up @@ -89,32 +86,23 @@ class Alt {
}

generateActions(...actionNames) {
return this.createActions(function () {
this.generateActions(...actionNames)
})
const actions = { name: 'global' }
return this.createActions(actionNames.reduce((obj, action) => {
obj[action] = dispatchIdentity
return obj
}, actions))
}

createAction(name, implementation, obj) {
const actionId = uid(this[ACTIONS_REGISTRY], `Alt.${name}`)
this[ACTIONS_REGISTRY][actionId] = 1
const actionName = Symbol.for(actionId)

// Wrap the action so we can provide a dispatch method
const newAction = new AltAction(this, actionName, implementation, obj)

const action = newAction[ACTION_HANDLER]
action.defer = (...args) => {
setTimeout(() => {
newAction[ACTION_HANDLER].apply(null, args)
})
}
action[ACTION_KEY] = actionName
return action
return makeAction(this, 'global', name, implementation, obj)
}

createActions(ActionsClass, exportObj = {}, ...argsForConstructor) {
const actions = {}
const key = ActionsClass.displayName || ActionsClass.name || ''
const key = uid(
this[ACTIONS_REGISTRY],
ActionsClass.displayName || ActionsClass.name || 'Unknown'
)

if (typeof ActionsClass === 'function') {
assign(actions, getInternalMethods(ActionsClass.prototype, true))
Expand All @@ -125,10 +113,7 @@ class Alt {

generateActions(...actionNames) {
actionNames.forEach((actionName) => {
// This is a function so we can later bind this to AltAction
actions[actionName] = function (x, ...a) {
this.dispatch(a.length ? [x].concat(a) : x)
}
actions[actionName] = dispatchIdentity
})
}
}
Expand All @@ -138,10 +123,26 @@ class Alt {
assign(actions, ActionsClass)
}

this.actions[key] = this.actions[key] || {}

return Object.keys(actions).reduce((obj, action) => {
obj[action] = this.createAction(`${key}#${action}`, actions[action], obj)
if (typeof actions[action] !== 'function') {
return obj
}

// create the action
obj[action] = makeAction(
this,
key,
action,
actions[action],
obj
)

// generate a constant
const constant = formatAsConstant(action)
obj[constant] = obj[action][ACTION_KEY]

return obj
}, exportObj)
}
Expand Down
3 changes: 3 additions & 0 deletions src/alt/symbols/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const ACTION_HANDLER = Symbol()
// the action's uid symbol for listening
export const ACTION_KEY = Symbol()

// per instance registry of actions
export const ACTIONS_REGISTRY = Symbol()

// the action's name
export const ACTION_UID = Symbol()

Expand Down
43 changes: 43 additions & 0 deletions src/alt/utils/AltUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* istanbul ignore next */
function NoopClass() { }

const builtIns = Object.getOwnPropertyNames(NoopClass)
const builtInProto = Object.getOwnPropertyNames(NoopClass.prototype)

export function getInternalMethods(obj, isProto) {
const excluded = isProto ? builtInProto : builtIns
return Object.getOwnPropertyNames(obj).reduce((value, m) => {
if (excluded.indexOf(m) !== -1) {
return value
}

value[m] = obj[m]
return value
}, {})
}

export function warn(msg) {
/* istanbul ignore else */
if (typeof console !== 'undefined') {
console.warn(new ReferenceError(msg))
}
}

export function uid(container, name) {
let count = 0
let key = name
while (Object.hasOwnProperty.call(container, key)) {
key = name + String(++count)
}
return key
}

export function formatAsConstant(name) {
return name.replace(/[a-z]([A-Z])/g, (i) => {
return `${i[0]}_${i[1].toLowerCase()}`
}).toUpperCase()
}

export function dispatchIdentity(x, ...a) {
this.dispatch(a.length ? [x].concat(a) : x)
}
27 changes: 20 additions & 7 deletions src/alt/utils/StoreUtils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import assign from 'object-assign'
import AltStore from '../AltStore'
import * as StoreMixins from './StoreMixins'
import getInternalMethods from './getInternalMethods'
import * as Sym from '../symbols/symbols'

const { StoreMixinListeners, StoreMixinEssentials } = StoreMixins
const {
import AltStore from '../AltStore'
import { getInternalMethods } from './AltUtils'
import {
StoreMixinEssentials,
StoreMixinListeners
} from './StoreMixins'
import {
ALL_LISTENERS,
LIFECYCLE,
LISTENERS,
PUBLIC_METHODS,
STATE_CONTAINER
} = Sym
} from '../symbols/symbols'

function doSetState(store, storeInstance, state) {
if (!state) {
Expand All @@ -35,6 +36,18 @@ function doSetState(store, storeInstance, state) {
}
}

export function createStoreConfig(globalConfig, StoreModel) {
StoreModel.config = assign({
getState(state) {
return Object.keys(state).reduce((obj, key) => {
obj[key] = state[key]
return obj
}, {})
},
setState: assign
}, globalConfig, StoreModel.config)
}

export function transformStore(transforms, StoreModel) {
return transforms.reduce((Store, transform) => transform(Store), StoreModel)
}
Expand Down
15 changes: 0 additions & 15 deletions src/alt/utils/createStoreConfig.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/alt/utils/formatAsConstant.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/alt/utils/getInternalMethods.js

This file was deleted.

Loading

0 comments on commit 1d32ad9

Please sign in to comment.