forked from marcuswestin/store.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
95 lines (86 loc) · 1.89 KB
/
events.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
var util = require('../src/util')
var bind = util.bind
var each = util.each
var create = util.create
var slice = util.slice
module.exports = eventsPlugin
function eventsPlugin() {
var pubsub = _newPubSub()
return {
watch: watch,
unwatch: unwatch,
once: once,
set: set,
remove: remove,
clearAll: clearAll
}
// new pubsub functions
function watch(_, key, listener) {
return pubsub.on(key, bind(this, listener))
}
function unwatch(_, subId) {
pubsub.off(subId)
}
function once(_, key, listener) {
pubsub.once(key, bind(this, listener))
}
// overwrite function to fire when appropriate
function set(super_fn, key, val) {
var oldVal = this.get(key)
super_fn()
pubsub.fire(key, val, oldVal)
}
function remove(super_fn, key) {
var oldVal = this.get(key)
super_fn()
pubsub.fire(key, undefined, oldVal)
}
function clearAll(super_fn) {
var oldVals = {}
this.each(function(val, key) {
oldVals[key] = val
})
super_fn()
each(oldVals, function(oldVal, key) {
pubsub.fire(key, undefined, oldVal)
})
}
}
function _newPubSub() {
return create(_pubSubBase, {
_id: 0,
_subSignals: {},
_subCallbacks: {}
})
}
var _pubSubBase = {
_id: null,
_subCallbacks: null,
_subSignals: null,
on: function(signal, callback) {
if (!this._subCallbacks[signal]) {
this._subCallbacks[signal] = {}
}
this._id += 1
this._subCallbacks[signal][this._id] = callback
this._subSignals[this._id] = signal
return this._id
},
off: function(subId) {
var signal = this._subSignals[subId]
delete this._subCallbacks[signal][subId]
delete this._subSignals[subId]
},
once: function(signal, callback) {
var subId = this.on(signal, bind(this, function() {
callback.apply(this, arguments)
this.off(subId)
}))
},
fire: function(signal) {
var args = slice(arguments, 1)
each(this._subCallbacks[signal], function(callback) {
callback.apply(this, args)
})
}
}