Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { EventEmitter } from 'events'
- import Dispatcher from '../dispatcher/dispatcher.js'
- import { ActionTypes } from '../constants/actions'
- const state = {
- health: 100,
- fatigue: 100
- }
- const stat = {
- strength: 1, // affects rof and damage
- agility: 1, // affects speed
- endurance: 1 // affects rate of fatigue
- }
- /**
- * A store registers itself with the dispatcher and provides it with a callback.
- * This callback receives the action as a parameter.
- * Within the store's registered callback, a switch statement based on the action's
- * type is used to interpret the action and to provide the proper hooks into the store's internal methods.
- * This allows an action to result in an update to the state of the store, via the dispatcher.
- * After the stores are updated, they broadcast an event declaring that their state has changed,
- * so the views may query the new state and update themselves.
- *
- * Stores contain the application state and logic.
- * Their role is somewhat similar to a model in a traditional MVC,
- * but they manage the state of many objects:
- * they do not represent a single record of data like ORM models do.
- * Nor are they the same as Backbone's collections.
- * More than simply managing a collection of ORM-style objects,
- * ##stores manage the application state for a particular domain within the application##.
- */
- class PlayerStore extends EventEmitter {
- constructor() {
- super()
- // register this store with the dispatcher
- this.dispatchToken = Dispatcher.register(this.onAction.bind(this))
- }
- onAction(payload) {
- // provide hooks to each respective action
- switch(payload.type) {
- case ActionTypes.PLAYER_HEALED:
- this.increaseHealth()
- break
- case ActionTypes.PLAYER_INJURED:
- this.noIHaveNoMadeThisBitYet()
- break
- }
- }
- get health() {
- return state.health
- }
- increaseHealth() {
- state.health += 6
- // broadcast an event declaring that the state has changed
- // so that whoever is listening to this store can update themself
- this.emit('change')
- }
- }
- module.exports = new PlayerStore();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement