/
watcher.js
95 lines (89 loc) · 2.34 KB
/
watcher.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
/*
* Watcher v1.2.2 - 12-09-2015, https://github.com/davegillem/watcher-pubsub
* ===================================
* A jQuery plugin for enabling publish/subscribe (Observer) functionality
*
* (c) Copyright 2015 Dave Gillem, http://davegillem.com
* MIT Licensed
*/
;(function ($, window) {
"use strict";
var watchers = {},
watcherID = -1,
_addWatcher = function(event, callback) {
if(!watchers[event]){
watchers[event] = [];
}
var whichWatcher, watcherRef,
isCopy = false,
currEvent = watchers[event] || [],
evtLen = currEvent.length;
// check for duplicates
for(var i=0, loopcount=evtLen; i<loopcount; i++){
whichWatcher = currEvent[i];
if(whichWatcher.callback === callback) {
isCopy = true;
watcherRef = whichWatcher.id;
}
}
if(!isCopy){
++watcherID;
watchers[event].push({
callback : callback,
id : watcherID
});
watcherRef = watcherID;
}
return watcherRef;
},
_removeWatcher = function(event, id) {
var whichWatcher,
isRemoved = false,
currEvent = watchers[event] || [],
evtLen = currEvent.length;
for(var i=0, loopcount=evtLen; i<loopcount; i++){
whichWatcher = currEvent[i];
if(whichWatcher.id == id) {
currEvent.splice(i, 1);
isRemoved = true;
break; // jump out once/if ID is found
}
}
return isRemoved;
},
_removeAllWatchers = function(event) {
delete watchers[event];
},
_notifyWatchers = function(event, data) {
var whichEvent = watchers[event];
if(whichEvent){
/*
// if whichEvent then $.each
whichEvent && $.each(whichEvent, function() {
this.callback.call($, data);
});
*/
$.each(whichEvent, function() {
this.callback.call($, data);
});
}
},
_getWatchers = function (event){
return event ? watchers[event] : watchers;
},
_pubMap = {
'getWatchers' : _getWatchers,
'publish' : _notifyWatchers,
'subscribe' : _addWatcher,
'unsubscribe' : _removeWatcher,
'unsubscribeAll': _removeAllWatchers
};
$.watcher = function(method, event, data) {
var methodCall = _pubMap[method];
if (methodCall) {
return methodCall(event, data);
}else{
throw new Error("There is not a "+method+" that can be called for $.watcher");
}
};
})(jQuery, this);