This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmust.js
151 lines (140 loc) · 4.09 KB
/
must.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
;(function(window, $) {
if (window.must !== undefined) {
return window.must;
}
var mu = {};
var supportsWebsockets = window.WebSocket || window.MozWebSocket;
/** Streams public RSVP data from http://www.meetup.com/meetup_api/docs/stream/2/rsvps/#polling */
mu.Rsvps = function(callback, params, error) {
return new mu.Stream({
path: "/2/rsvps",
callback: callback,
error: error,
params: params
});
};
/** Streams public photo data from http://www.meetup.com/meetup_api/docs/stream/2/photos/#polling */
mu.Photos = function(callback, params, error) {
return new mu.Stream({
path: "/2/photos",
callback: callback,
error: error,
params: params
});
};
/** Streams public checkin data from http://www.meetup.com/meetup_api/docs/stream/2/checkins/#polling */
mu.Checkins = function(callback, params, error) {
return new mu.Stream({
path: "/2/checkins",
callback: callback,
error: error,
params: params
});
};
/** Streams public Meetup comments from http://www.meetup.com/meetup_api/docs/stream/2/event_comments/#polling */
mu.Comments = function(callback, params, error) {
return new mu.Stream({
path: "/2/event_comments",
callback: callback,
params: params,
error: error
});
};
/**
* @param config object that defines the following options
* host - base host
* path - path of stream endpoint
* url - http url for the stream
* wsUrl - websockets url for the stream
* callback - callback for messages, parameter is one JS object
* log - function that logs a msg
* error - function called when an error occurs
*/
mu.Stream = function(config) {
var $ = jQuery,
defaults = {
host: "http://stream.meetup.com",
path: "/2/rsvps",
log: function(msg) { },
stopping: false,
error: function(msg) {
alert(msg);
}
}
$.extend(this, defaults, config);
this.url = this.host + this.path;
this.wsUrl = this.wsUrl || this.url.replace(/^http/, 'ws');
this.stop = this.close; // backward compatibility
if (!this.noOpen) {
this.open();
}
return this;
};
mu.Stream.prototype = {
handleJson: function(json) {
if (typeof this.callback === "function") {
this.callback(json);
} else {
this.error("callback is not a function");
}
},
close: function() {
this.stopping = true;
},
open: function() {
var self = this;
if (supportsWebsockets) {
if (this.params) { this.wsUrl = this.wsUrl + "?" + $.param(this.params) };
var s = window.WebSocket ? new WebSocket(this.wsUrl) : new MozWebSocket(this.wsUrl);
s.onmessage = function(e) {
if (this.stopping) {
s.close();
} else {
var ary = JSON.parse(e.data);
if (ary.errors) {
error(ary.errors);
} else {
self.handleJson(ary);
}
}
};
} else {
var newest = 0;
this.successCallback = function(ary) {
if (!this.stopping) {
if (ary) {
if (ary.errors) {
stopping = true;
error(ary.errors);
} else {
$.each(ary, function() {
self.handleJson(this);
newest = Math.max(newest, this.mtime);
});
}
}
if (!stopping) {
var params = self.params || {};
if (newest > 0) {
params.since_mtime = newest;
delete params.since_count;
}
$.ajax({
url: self.url,
data: params,
dataType: 'jsonp',
success: self.successCallback,
scriptCharset: 'UTF-8'
});
}
}
};
mu.Loader.defer(function () {
self.successCallback();
});
}
}
}
// export to window
window.must = mu;
}(this, jQuery));