-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
137 lines (125 loc) · 3.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var createEmitter = require('namespace-emitter')
var isPlainObject = require('is-plain-object')
var extend = require('xtend')
/**
* Create the store
* @name createStoreEmitter
* @param {function} modifier
* @param {object} [initialState]
* @example
* var createStore = require('store-emitter')
* var store = createStore(function (action, state) {
* if (action.type === 'change_something') {
* return { something: 'changed' }
* }
* })
*/
module.exports = function createStore (modifier, initialState) {
if (typeof modifier !== 'function') {
throw new Error('first argument must be a function')
}
var emitter = createEmitter()
initialState = initialState || {}
var isEmitting = false
var state = extend(initialState)
store.initialState = getInitialState
store.getState = getState
store.emit = store
store.on = on
store.once = once
store.off = off
return store
/**
* Send an action to the store. Takes a single object parameter. Object must include a `type` property with a string value, and can contain any other properties.
* @name store
* @param {object} action
* @param {string} action.type
* @example
* store({
* type: 'example'
* exampleValue: 'anything'
* })
*/
function store (action) {
if (!action || !isPlainObject(action)) {
throw new Error('action parameter is required and must be a plain object')
}
if (!action.type || typeof action.type !== 'string') {
throw new Error('type property of action is required and must be a string')
}
if (isEmitting) {
throw new Error('modifiers may not emit actions')
}
isEmitting = true
var oldState = extend(state)
state = modifier(action, oldState)
var newState = extend(state)
emitter.emit(action.type, action, newState, oldState)
isEmitting = false
}
/**
* Get the initial state of the store
* @name store.initialState
* @example
* var state = store.initialState()
*/
function getInitialState () {
return initialState
}
/**
* Get the current state of the store
* @name store.getState
* @example
* var state = store.getState()
*/
function getState () {
return state
}
/**
* Listen for changes to the store
* @name store.on
* @param {string} event – an action type
* @param {Function} callback
* @example
* store.on('*', function (action, state, oldState) {
*
* })
*
* store.on('article', function (action, state, oldState) {
*
* })
*
* store.on('article:delete', function (action, state, oldState) {
*
* })
*/
function on (event, callback) {
emitter.on(event, callback)
}
/**
* Listen for a single change to the store
* @name store.once
* @param {string} event – an action type
* @param {Function} callback
* @example
* store.once('article', function (action, state, oldState) {
*
* })
*/
function once (event, callback) {
emitter.once(event, callback)
}
/**
* Stop listening for changes to the store. Passing just the action type will remove all listeners for that action type.
* @name store.off
* @param {string} event – an action type
* @param {Function} [callback] – optional callback
* @example
* store.off('article', function (action, state, oldState) {
*
* })
*/
function off (event, callback) {
emitter.off(event, callback)
}
}