/
telepathy.enchant.js
327 lines (308 loc) · 8.59 KB
/
telepathy.enchant.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @fileOverview
* telepathy.enchant.js
* @version 0.1.0
* @require enchant.js v0.8.0+
* @author UEI Corporation
*/
(function() {
/**
* @namespace enchant.telepathy
*/
enchant.telepathy = {};
/**
[lang:ja]
* デフォルトの送信APIのURL.
[/lang]
[lang:en]
* The default URL of sending API.
[/lang]
* @type String
*/
enchant.telepathy.SEND_API_URL = 'http://skylab.enchantmoon.com/moonblock/add_data/';
/**
[lang:ja]
* デフォルトの受信APIのURL.
[/lang]
[lang:en]
* The default URL of receiving API.
[/lang]
* @type String
*/
enchant.telepathy.RECV_API_URL = 'http://skylab.enchantmoon.com/moonblock/watch/';
/**
[lang:ja]
* オブジェクトににアプリケーション外からのメッセージ(テレパシー)を与えるイベント.
* 発行するオブジェクト: {@link enchant.telepathy.TelepathySence}
[/lang]
[lang:en]
* Event which gives foreign message to objects.
* Issued by {@link enchant.telepathy.TelepathySence}
[/lang]
* @type String
*/
enchant.Event.TELEPATHY = 'telepathy';
/**
* @scope enchant.telepathy.Telepathy.prototype
*/
enchant.telepathy.Telepathy = enchant.Class.create(enchant.Event, {
/**
* @name enchant.telepathy.Telepathy
* @class
[lang:ja]
* アプリケーション外からのメッセージを表すイベントオブジェクト.
* @param {String} channel チャンネル.
* @param {*} data データ.
[/lang]
[lang:en]
* Event which represents foreign message.
* @param {String} channel channel.
* @param {*} data content.
[/lang]
* @constructs
* @extends enchant.Event
*/
initialize: function(channel, data) {
enchant.Event.call(this, enchant.Event.TELEPATHY);
/**
[lang:ja]
* テレパシーのチャンネルを表す.
[/lang]
[lang:en]
* Telepathy channel.
[/lang]
* @type String
*/
this.channel = channel;
/**
[lang:ja]
* テレパシーの内容を表す.
[/lang]
[lang:en]
* Telepathy content.
[/lang]
* @type *
*/
this.data = data;
}
});
/**
* @scope enchant.telepathy.TelepathySense.prototype
*/
enchant.telepathy.TelepathySense = enchant.Class.create(enchant.EventTarget, {
/**
* @name enchant.telepathy.TelepathySense
* @class
[lang:ja]
* テレパシーの送受信を行うためのオブジェクト.
[/lang]
[lang:en]
* Object for sending and receiving telepathy.
[/lang]
* @construct
* @extends enchant.EventTarget
*/
initialize: function() {
enchant.EventTarget.call(this);
/**
[lang:ja]
* Telepathyの受信者を保持するオブジェクト.
[/lang]
[lang:en]
* Object which stores channelers.
[/lang]
* @type Object
*/
this.channelers = {};
/**
[lang:ja]
* Telepathyの受信に使用するEventSourceを保持するオブジェクト.
[/lang]
[lang:en]
* Object which stores EventSource.
[/lang]
* @type Object
*/
this.eventSources = {};
/**
[lang:ja]
* 最後に受信したTelepathyオブジェクト.
[/lang]
[lang:en]
* The telepathy object which received lastly.
[/lang]
* @type enchant.telepathy.Telepathy
*/
this.lastTelepathy = null;
/**
[lang:ja]
* テレパシー送信APIのURL.
[/lang]
[lang:en]
* URL of the send API.
[/lang]
* @type String
*/
this.sendAPI = enchant.telepathy.SEND_API_URL;
/**
[lang:ja]
* テレパシー受信APIのURL.
[/lang]
[lang:en]
* URL of the receive API.
[/lang]
* @type String
*/
this.recvAPI = enchant.telepathy.RECV_API_URL;
this.addEventListener(enchant.Event.TELEPATHY, this._ontelepathy);
},
/**
* @param {enchant.Event} evt
* @private
*/
_ontelepathy: function(evt) {
var i, l,
channelers = this.channelers[evt.channel];
this.lastTelepathy = evt;
if (!channelers) {
return;
}
for (i = 0, l = channelers.length; i < l; i++) {
channelers[i].dispatchEvent(evt);
}
},
/**
[lang:ja]
* オブジェクトをTelepathyの受信者に設定する.
* @param {String} channel 対象のチャンネル.
* @param {enchant.EventTarget} target 対象のオブジェクト.
[/lang]
[lang:en]
* Adds objet as telepathy channeler.
* @param {String} channel target channel.
* @param {enchant.EventTarget} target target object.
[/lang]
*/
addChanneler: function(channel, target) {
if (this.channelers[channel]) {
if (this.channelers[channel].indexOf(target) === -1) {
this.channelers[channel].push(target);
}
} else {
this.channelers[channel] = [ target ];
}
},
/**
[lang:ja]
* オブジェクトをTelepathyの受信者から除外する.
* @param {String} channel 対象のチャンネル.
* @param {enchant.EventTarget} target 対象のオブジェクト.
[/lang]
[lang:en]
* Removes objet from telepathy channeler.
* @param {String} channel target channel.
* @param {enchant.EventTarget} target target object.
[/lang]
*/
removeChanneler: function(channel, target) {
var i;
if (!this.channelers[channel]) {
return;
}
i = this.channelers[channel].indexOf(target);
if (i !== -1) {
this.channelers[channel].splice(i, 1);
}
},
/**
[lang:ja]
* テレパシーを送信する.
* @param {String} channel 対象のチャンネル.
* @param {*} message 送信するデータ.
[/lang]
[lang:en]
* Sends the telepathy.
* @param {String} channel target channel.
* @param {*} message data to send.
[/lang]
*/
send: function(channel, message) {
var xhr = new XMLHttpRequest(),
data = JSON.stringify({ value: message });
xhr.open('POST', this.sendAPI + channel, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.addEventListener('error', function(evt) {
var errorEvent = new enchant.Event(enchant.Event.ERROR);
errorEvent.message = 'Cannot send telepathy: ' + channel;
this.dispatchEvent(errorEvent);
}.bind(this));
xhr.send('object=' + data);
},
/**
[lang:ja]
* テレパシーの受信を開始する.
* @param {String} channel 対象のチャンネル.
[/lang]
[lang:en]
* Begins to receive the telepathy.
* @param {String} channel target channel.
[/lang]
*/
open: function(channel) {
var eventSource = new EventSource(this.recvAPI + channel);
eventSource.addEventListener('message', function(evt) {
var i, l,
data = JSON.parse(evt.data),
stream = data.objects;
for (i = 0, l = stream.length; i < l; i++) {
this.dispatchEvent(new enchant.telepathy.Telepathy(channel, JSON.parse(stream[i]).value));
}
}.bind(this));
this.eventSources[channel] = eventSource;
},
/**
[lang:ja]
* テレパシーの受信を終了する.
* @param {String} channel 対象のチャンネル.
[/lang]
[lang:en]
* Ends to receive the telepathy.
* @param {String} channel target channel.
[/lang]
*/
close: function(channel) {
var eventSource = this.eventSources[channel];
if (eventSource) {
eventSource.close();
delete this.eventSources[channel];
delete this.channelers[channel];
}
},
/**
[lang:ja]
* 全てのチャンネルについてテレパシーの受信を終了する.
[/lang]
[lang:en]
* Closes all telepathy channels.
[/lang]
*/
closeAll: function() {
for (var channel in this.eventSources) {
this.close(channel);
}
},
/**
[lang:ja]
* TelepathySenseの後処理を行う.
[/lang]
[lang:en]
* Finalizes TelepathySense.
[/lang]
*/
finalize: function() {
this.clearEventListener();
this.closeAll();
this.channelers = {};
}
});
}());