annotate core/src/js/messaging/Listener.js @ 30:2dfba21cd879

sync
author cin
date Wed, 27 Jun 2018 02:46:14 +0300
parents acdcdf1a8d21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define([ "dojo/_base/declare", "dojo/_base/lang", "./Client" ],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 function(declare, lang, Client) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 return declare([ Client ], {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 _listener : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 constructor : function(session, destination, options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 if (!options || !options.listener)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 throw new Error("A listener is required");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 this._listener = options.listener;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 if (options.transform)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 this._transform = options.transform;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 getMode : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 return "listener";
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 process : function(result) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 switch (result.type) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 case "message":
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 this._handleMessage(result.message);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 } catch (ex) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 var err = new Error("Failed to handle message");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 err.envelope = result.message;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 err.innerException = ex;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 this._handleError(err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 break;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 case "error":
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 this._handleError(result.error);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 break;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 _transform : function(envelope) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 return envelope;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 _handleMessage : function(envelope) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 this.log(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 "MESSAGE type = ${0}, headers = ${2}: ${1}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 envelope.bodyType,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 envelope.body,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 JSON.stringify(envelope.headers));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 var data = this._transform(envelope);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 this._listener(data);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 this.emit("message", data);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 _handleError : function(ex) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 if (ex.innerException)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 this.error(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 "ERROR: ${0} -> ${1}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 ex.message,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 ex.innerException.message);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 this.error("ERROR: ${0}", ex.message);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 this.emit("error", ex);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 });