-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.Event.js
executable file
·137 lines (114 loc) · 2.8 KB
/
crafity.Event.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
/*jslint bitwise: true, unparam: true, maxerr: 50, white: true */
/*globals window, setTimeout*/
/*!
* crafity.Event
* Copyright(c) 2011 Crafity
* Copyright(c) 2011 Bart Riemens
* Copyright(c) 2011 Galina Slavova
* MIT Licensed
*/
(function (crafity) {
"use strict";
if (!crafity.arrays) {
throw new Error("Missing dependency 'crafity.arrays'");
}
var arrays = crafity.arrays;
crafity.Event = function Event(type) {
var self = this;
this.handlers = [];
this.types = arrays.toArray(arguments);
this.sync = arrays.contains(self.types, "sync");
if (self.sync) {
self.types.splice(self.types.indexOf('sync'), 1);
}
if (arrays.contains.not(self.types, "unobservable")) {
this.onListenerSubscribed = new Event("unobservable");
this.onListenerUnsubscribed = new Event("unobservable");
} else {
self.types.splice(self.types.indexOf('unobservable'), 1);
}
this.raiseFunctions = {
cancel: function (args) {
var h = self.handlers.slice()
, next, raise;
next = function next() {
if (h && h.length > 0) {
var handler = h[0];
h.splice(0, 1);
raise(handler);
}
};
raise = function raise(handler) {
function innerRaise() {
if (handler.apply(self, args) !== false) {
next();
}
}
if (self.sync) {
innerRaise();
} else {
setTimeout(function () {
innerRaise();
}, 1);
}
};
next();
}
};
if (!self.types || self.types.length === 0 || self.types.indexOf('cancel') > -1) {
self.raiseFunction = self.raiseFunctions.cancel;
} else {
throw new TypeError("Unknown Event Type");
}
/**
*
*/
this.listenerCount = self.handlers.length;
/**
*
* @param args
*/
this.raise = function (args) {
self.raiseFunction(arrays.toArray(arguments));
};
/**
*
* @param handler
*/
this.subscribe = function (handler) {
if (handler && handler instanceof Function) {
self.handlers.push(handler);
} else {
throw new Error("Invalid or undefined handler");
}
self.listenerCount = self.handlers.length;
if (self.onListenerSubscribed) {
self.onListenerSubscribed.raise(self, handler);
}
};
/**
*
* @param handler
*/
this.unsubscribe = function (handler) {
var handlerIndex;
self.handlers.forEach(function (existingHandler, index) {
if (existingHandler === handler) {
handlerIndex = index;
}
});
if (handlerIndex < 0) {
throw new Error("Invalid or undefined handler");
}
self.handlers.splice(handlerIndex, 1);
self.listenerCount = self.handlers.length;
if (self.onListenerUnsubscribed) {
self.onListenerUnsubscribed.raise(self, handler);
}
};
this.clear = function () {
self.handlers = [];
self.listenerCount = 0;
};
};
}(window.crafity = window.crafity || {}));