Skip to content

Commit

Permalink
class extends Store
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Jun 21, 2015
1 parent 00de8a9 commit 72a987b
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 432 deletions.
28 changes: 28 additions & 0 deletions foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Alt, { Store } from './'

const alt = new Alt()

const actions = alt.generateActions('foo')

class MyStore extends Store {
constructor() {
super(alt, {
displayName: 'MyStore'
})

this.state = { x: 1 }

this.bindAction(actions.foo, () => {
this.setState({ x: this.state.x + 1 })
})
}
}

const store = new MyStore()

store.listen(state => console.log('CHANGED', state))

actions.foo()
actions.foo()
actions.foo()
actions.foo()
43 changes: 4 additions & 39 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { Dispatcher } from 'flux'

import * as StateFunctions from './utils/StateFunctions'
import * as fn from '../utils/functions'
import * as store from './store'
import * as utils from './utils/AltUtils'
import makeAction from './actions'

import Store from './store'

export { Store }

class Alt {
constructor(config = {}) {
this.config = config
Expand All @@ -27,44 +30,6 @@ class Alt {
this.batchingFunction(() => this.dispatcher.dispatch({ action, data, details }))
}

createUnsavedStore(StoreModel, ...args) {
const key = StoreModel.displayName || ''
store.createStoreConfig(this.config, StoreModel)
const Store = store.transformStore(this.storeTransforms, StoreModel)

return fn.isFunction(Store)
? store.createStoreFromClass(this, Store, key, ...args)
: store.createStoreFromObject(this, Store, key)
}

createStore(StoreModel, iden, ...args) {
let key = iden || StoreModel.displayName || StoreModel.name || ''
store.createStoreConfig(this.config, StoreModel)
const Store = store.transformStore(this.storeTransforms, StoreModel)

if (this.stores[key] || !key) {
if (this.stores[key]) {
utils.warn(
`A store named ${key} already exists, double check your store ` +
`names or pass in your own custom identifier for each store`
)
} else {
utils.warn('Store name was not specified')
}

key = utils.uid(this.stores, key)
}

const storeInstance = fn.isFunction(Store)
? store.createStoreFromClass(this, Store, key, ...args)
: store.createStoreFromObject(this, Store, key)

this.stores[key] = storeInstance
StateFunctions.saveInitialSnapshot(this, key)

return storeInstance
}

generateActions(...actionNames) {
const actions = { name: 'global' }
return this.createActions(actionNames.reduce((obj, action) => {
Expand Down
94 changes: 0 additions & 94 deletions src/alt/store/AltStore.js

This file was deleted.

180 changes: 0 additions & 180 deletions src/alt/store/StoreMixin.js

This file was deleted.

Loading

0 comments on commit 72a987b

Please sign in to comment.