-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone.sse.js
More file actions
169 lines (145 loc) · 4.63 KB
/
backbone.sse.js
File metadata and controls
169 lines (145 loc) · 4.63 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***************************************************
*
* Backbone.SSE
*
* Effortless Backbone EventSource Integration
*
* Copyright 2015, Jeremy Blalock & Synack, Inc.
*
* v0.0.1
*
***************************************************/
!function(Backbone, _, $) {
var SSE = Backbone.SSE = {};
var noop = function(key) {
return function() {
throw key + ' is not available in Backbone.SSE';
}
}
SSE.Model = Backbone.Model.extend({
constructor: function(attributes, options) {
_.bindAll(this, '_receiveMessage', '_receiveError', '_receiveOpen');
// Setup some instance variables:
this._activeEvents = [];
this._partials = {};
// Call super constructor
Backbone.Model.prototype.constructor.apply(this, arguments);
},
/* Override default prefix for clarity. */
cidPrefix: 'sse',
/*
* Initialize EventSource.
* Meant to take the place of standard Model fetch.
*/
start: function(url) {
url = url || _.result(this, 'url');
if (this._eventSource) {
this.stop();
};
this._eventSource = new window.EventSource(url);
this._eventSource.addEventListener('message', this._receiveMessage);
this._eventSource.addEventListener('error', this._receiveError);
this._eventSource.addEventListener('open', this._receiveOpen);
// Listen to user-defined events
for (var i = 0; i < this._activeEvents.length; i += 1) {
this._listenToEvent(this._activeEvents[i]);
}
},
/*
* Teardown SSE connection.
* Prevents memory leaks caused by starting many connections.
*/
stop: function() {
if (this._eventSource) {
// Close connection
this._eventSource.close();
// Cleanup
this._eventSource.removeEventListener('message', this._receiveMessage);
this._eventSource.removeEventListener('error', this._receiveError);
this._eventSource.removeEventListener('open', this._receiveOpen);
// Cleanup to user-defined events
for (var i = 0; i < this._activeEvents.length; i += 1) {
this._stopListeningToEvent(this._activeEvents[i]);
}
// Unset
this._eventSource = null;
}
},
/*
* Stop and start connection, to reset if failing.
*/
restart: function() {
this.stop();
this.start();
},
/*
* Capture events we're trying to listen to, to make sure we're
* triggering those events.
*/
on: function(name) {
if (this._activeEvents.indexOf(name) == -1) {
this._activeEvents.push(name);
this._listenToEvent(name);
}
Backbone.Model.prototype.on.apply(this, arguments);
},
/* Receive data.
* Triggers new sync event. */
_receiveMessage: function(e) {
var data = JSON.parse(e.data);
this.set(this.parse(data));
this.trigger('sync', this);
},
/* Receive arbitrary event.
* Triggers that event. */
_receiveEvent: function(evt, e) {
var data = null;
if (e.data) {
data = JSON.parse(e.data);
}
this.trigger(evt, data, e);
},
/* Error Occurred.
* Will happen every ~1s because of how SSE retries. */
_receiveError: function(e) {
this.trigger('eventSourceError', e);
},
/* Successfully established connection.
* Trigger open event. */
_receiveOpen: function(e) {
this.trigger('opened', this);
},
/* Wrap underscore.js partial, using singleton pattern.
* Will only work with string and numeric arguments. */
_partial: function(functionName) {
var args = _.values(arguments),
key = args.join(',');
if (key in this._partials) {
return this._partials[key];
}
var func = this[functionName];
if (!func) return null;
var partial = _.bind.apply(_, [func, this].concat(args.slice(1)));
this._partials[key] = partial;
return partial
},
/* Listen to an arbitrary SSE event.
* Useful for listening to heartbeats, updates, etc. */
_listenToEvent: function(evt) {
if (!this._eventSource) return;
var func = this._partial('_receiveEvent', evt);
this._eventSource.addEventListener(evt, func);
},
/* Stop listening to arbitrary SSE event.
* Follows same pattern as _listenToEvent. */
_stopListeningToEvent: function(evt) {
var func = this._partial('_receiveEvent', evt);
this._eventSource.removeEventListener(evt, func);
},
/* Disable invalid methods: */
sync: noop('sync'),
fetch: noop('fetch'),
destroy: noop('destroy'),
save: noop('save')
});
}(Backbone, _, jQuery);