| 1 | --- /home/brendonh/installers/dojo-release-0.9.0/dojox/_cometd/cometd.js 2007-08-21 01:38:15.000000000 +0800 |
|---|
| 2 | +++ cometd.js 2007-09-19 20:28:07.000000000 +0800 |
|---|
| 3 | @@ -35,6 +35,8 @@ |
|---|
| 4 | this.backlog = []; |
|---|
| 5 | this.handleAs="json-comment-optional"; |
|---|
| 6 | this.advice; |
|---|
| 7 | + this.pendingSubscriptions = {} |
|---|
| 8 | + this.pendingUnsubscriptions = {} |
|---|
| 9 | |
|---|
| 10 | this._subscriptions = []; |
|---|
| 11 | |
|---|
| 12 | @@ -48,11 +50,6 @@ |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | this.init = function(props, root, bargs){ |
|---|
| 16 | - if(dojo.isString(props)){ |
|---|
| 17 | - var oldRoot = root; |
|---|
| 18 | - root = props; |
|---|
| 19 | - props = oldRoot; |
|---|
| 20 | - } |
|---|
| 21 | // FIXME: if the root isn't from the same host, we should automatically |
|---|
| 22 | // try to select an XD-capable transport |
|---|
| 23 | props = props||{}; |
|---|
| 24 | @@ -185,18 +182,34 @@ |
|---|
| 25 | // check for various meta topic actions that we need to respond to |
|---|
| 26 | switch(message.channel){ |
|---|
| 27 | case "/meta/subscribe": |
|---|
| 28 | + var pendingDef = this.pendingSubscriptions[message.subscription]; |
|---|
| 29 | if(!message.successful){ |
|---|
| 30 | - console.debug("cometd subscription error for channel", message.channel, ":", message.error); |
|---|
| 31 | + if(pendingDef !== undefined) { |
|---|
| 32 | + pendingDef.errback(new Error(message.error)); |
|---|
| 33 | + delete this.pendingSubscriptions[message.subscription]; |
|---|
| 34 | + } |
|---|
| 35 | return; |
|---|
| 36 | } |
|---|
| 37 | dojox.cometd.subscribed(message.subscription, message); |
|---|
| 38 | + if(pendingDef !== undefined) { |
|---|
| 39 | + pendingDef.callback(true); |
|---|
| 40 | + delete this.pendingSubscriptions[message.subscription]; |
|---|
| 41 | + } |
|---|
| 42 | break; |
|---|
| 43 | case "/meta/unsubscribe": |
|---|
| 44 | + var pendingDef = this.pendingUnsubscriptions[message.subscription]; |
|---|
| 45 | if(!message.successful){ |
|---|
| 46 | - console.debug("cometd unsubscription error for channel", message.channel, ":", message.error); |
|---|
| 47 | + if(pendingDef !== undefined) { |
|---|
| 48 | + pendingDef.errback(new Error(message.error)); |
|---|
| 49 | + delete this.pendingUnsubscriptions[message.subscription]; |
|---|
| 50 | + } |
|---|
| 51 | return; |
|---|
| 52 | } |
|---|
| 53 | this.unsubscribed(message.subscription, message); |
|---|
| 54 | + if(pendingDef !== undefined) { |
|---|
| 55 | + pendingDef.callback(true); |
|---|
| 56 | + delete this.pendingUnsubscriptions[message.subscription]; |
|---|
| 57 | + } |
|---|
| 58 | break; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | @@ -271,18 +284,22 @@ |
|---|
| 62 | // the second half of the objOrFunc/funcName pair for identifying |
|---|
| 63 | // a callback function to notifiy upon channel message delivery |
|---|
| 64 | |
|---|
| 65 | + if (this.pendingSubscriptions[channel] !== undefined) { |
|---|
| 66 | + // We already asked to subscribe to this channel, and |
|---|
| 67 | + // haven't heard back yet. Fail the previous attempt. |
|---|
| 68 | + var oldDef = this.pendingSubscriptions[channel]; |
|---|
| 69 | + oldDef.cancel(); |
|---|
| 70 | + delete this.pendingSubscriptions[channel]; |
|---|
| 71 | + } |
|---|
| 72 | + |
|---|
| 73 | + var pendingDef = new dojo.Deferred(); |
|---|
| 74 | + this.pendingSubscriptions[channel] = pendingDef; |
|---|
| 75 | + |
|---|
| 76 | if(!this.currentTransport){ |
|---|
| 77 | this.backlog.push(["subscribe", channel, useLocalTopics, objOrFunc, funcName]); |
|---|
| 78 | - return; |
|---|
| 79 | + return pendingDef; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | - if((useLocalTopics !== true)||(useLocalTopcis !== false)){ |
|---|
| 83 | - // similar to: function(channel, objOrFunc, funcName, useLocalTopics); |
|---|
| 84 | - var ofn = funcName; |
|---|
| 85 | - funcName = objOrFunc; |
|---|
| 86 | - objOrFunc = useLocalTopics; |
|---|
| 87 | - useLocalTopics = ofn; |
|---|
| 88 | - } |
|---|
| 89 | // console.debug(objOrFunc, funcName); |
|---|
| 90 | |
|---|
| 91 | if(objOrFunc){ |
|---|
| 92 | @@ -297,10 +314,13 @@ |
|---|
| 93 | } |
|---|
| 94 | // FIXME: would we handle queuing of the subscription if not connected? |
|---|
| 95 | // Or should the transport object? |
|---|
| 96 | - return this.currentTransport.sendMessage({ |
|---|
| 97 | + this.currentTransport.sendMessage({ |
|---|
| 98 | channel: "/meta/subscribe", |
|---|
| 99 | subscription: channel |
|---|
| 100 | }); |
|---|
| 101 | + |
|---|
| 102 | + return pendingDef; |
|---|
| 103 | + |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | this.subscribed = function( /*string*/ channel, |
|---|
| 107 | @@ -327,9 +347,21 @@ |
|---|
| 108 | // channel |
|---|
| 109 | // funcName: |
|---|
| 110 | // the second half of the objOrFunc/funcName pair for identifying |
|---|
| 111 | + |
|---|
| 112 | + if (this.pendingUnsubscriptions[channel] !== undefined) { |
|---|
| 113 | + // We already asked to subscribe to this channel, and |
|---|
| 114 | + // haven't heard back yet. Fail the previous attempt. |
|---|
| 115 | + var oldDef = this.pendingUnsubscriptions[channel]; |
|---|
| 116 | + oldDef.cancel(); |
|---|
| 117 | + delete this.pendingUnsubscriptions[channel]; |
|---|
| 118 | + } |
|---|
| 119 | + |
|---|
| 120 | + var pendingDef = new dojo.Deferred(); |
|---|
| 121 | + this.pendingUnsubscriptions[channel] = pendingDef; |
|---|
| 122 | + |
|---|
| 123 | if(!this.currentTransport){ |
|---|
| 124 | this.backlog.push(["unsubscribe", channel, useLocalTopics, objOrFunc, funcName]); |
|---|
| 125 | - return; |
|---|
| 126 | + return pendingDef; |
|---|
| 127 | } |
|---|
| 128 | // a callback function to notifiy upon channel message delivery |
|---|
| 129 | if(objOrFunc){ |
|---|
| 130 | @@ -341,10 +373,13 @@ |
|---|
| 131 | // FIXME: we're not passing a valid handle!! |
|---|
| 132 | dojo.unsubscribe(tname, objOrFunc, funcName); |
|---|
| 133 | } |
|---|
| 134 | - return this.currentTransport.sendMessage({ |
|---|
| 135 | + this.currentTransport.sendMessage({ |
|---|
| 136 | channel: "/meta/unsubscribe", |
|---|
| 137 | subscription: channel |
|---|
| 138 | }); |
|---|
| 139 | + |
|---|
| 140 | + return pendingDef; |
|---|
| 141 | + |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | this.unsubscribed = function(/*string*/ channel, |
|---|
| 145 | @@ -520,14 +555,6 @@ |
|---|
| 146 | return; |
|---|
| 147 | } |
|---|
| 148 | break; |
|---|
| 149 | - case "/meta/subscribe": |
|---|
| 150 | - if(!message.successful){ |
|---|
| 151 | - console.debug("cometd subscription error for channel", message.channel, ":", message.error); |
|---|
| 152 | - return; |
|---|
| 153 | - } |
|---|
| 154 | - dojox.cometd.subscribed(message.channel); |
|---|
| 155 | - // console.debug(message.channel); |
|---|
| 156 | - break; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | } |
|---|