Skip to content

Commit

Permalink
3.1.1 Subscriptions fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Feb 13, 2017
1 parent f4bf3cc commit fac8a80
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 73 deletions.
74 changes: 54 additions & 20 deletions build/ringcentral.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/ringcentral.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions build/ringcentral.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/ringcentral.min.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions demo/apiKey.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ <h1>

</div>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.0/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pubnub/3.7.7/pubnub.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.auto.js"></script>
<script type="text/javascript" src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3.js"></script>
<script src="../build/ringcentral.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
<script type="text/javascript">
Expand Down
6 changes: 3 additions & 3 deletions demo/backbone.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ <h1>

<script type="text/javascript" src="http://underscorejs.org/underscore.js"></script>
<script type="text/javascript" src="http://backbonejs.org/backbone.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.0/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pubnub/3.7.7/pubnub.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.auto.js"></script>
<script type="text/javascript" src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3.js"></script>
<script src="../build/ringcentral.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
<script type="text/javascript">
Expand Down
18 changes: 12 additions & 6 deletions demo/core.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ <h1>
<script type="text/javascript">
window.fetch = null; // force to use polyfill
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.0/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pubnub/3.7.7/pubnub.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.auto.js"></script>
<script type="text/javascript" src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3.js"></script>
<script src="../build/ringcentral.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
<script type="text/javascript">
Expand Down Expand Up @@ -92,7 +92,10 @@ <h1>

function getPresenceSubscription(event, cb) {

var subscription = sdk.createCachedSubscription('demo');
var subscription = sdk.createCachedSubscription({
cacheKey: 'demo',
renewHandicapMs: 14 * 60 * 1000 // set quicker subscription refresh
});

subscription.on(subscription.events.notification, function(msg) {
cb(msg.body);
Expand All @@ -102,12 +105,15 @@ <h1>
.restore([event])
.register()
.then(function(ajax) {
console.log('Subscribed', ajax);
console.log('Subscribed, renews in', Math.ceil(ajax.json().expiresIn / 60));
console.log(ajax);
})
.catch(function(e) {
console.error(e);
});

window.subscription = subscription;

}

function presenceUpdate(presence) {
Expand Down Expand Up @@ -149,7 +155,7 @@ <h1>

}

platform.ensureLoggedIn().then(showExtension).catch(function(){
platform.ensureLoggedIn().then(showExtension).catch(function() {
location.assign('login.htnl');
});

Expand Down
6 changes: 3 additions & 3 deletions demo/fax.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ <h1>
<script type="text/javascript">
window.fetch = null; // force to use polyfill
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.0/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pubnub/3.7.7/pubnub.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.auto.js"></script>
<script type="text/javascript" src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3.js"></script>
<script src="../build/ringcentral.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
<script type="text/javascript">
Expand Down
6 changes: 3 additions & 3 deletions demo/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ <h1>
<script type="text/javascript">
window.fetch = null; // force to use polyfill
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.0/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pubnub/3.7.7/pubnub.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.auto.js"></script>
<script type="text/javascript" src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3.js"></script>
<script src="../build/ringcentral.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
<script type="text/javascript">
Expand Down
6 changes: 3 additions & 3 deletions demo/refresh.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ <h1>
<script type="text/javascript">
window.fetch = null; // force to use polyfill
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.0/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pubnub/3.7.7/pubnub.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.auto.js"></script>
<script type="text/javascript" src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3.js"></script>
<script src="../build/ringcentral.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
<script type="text/javascript">
Expand Down
10 changes: 4 additions & 6 deletions demo/requirejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ <h4>RingCentral JS SDK Properties</h4>
requirejs.config({
paths: {
'ringcentral': '../build/ringcentral',
'pubnub': 'https://cdnjs.cloudflare.com/ajax/libs/pubnub/3.7.7/pubnub',
'fetch': 'https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.0/fetch',
'promise': 'https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise'
'pubnub': 'https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3',
'fetch': 'hhttps://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch',
'promise': 'https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.0.5/es6-promise.auto'
}
});

require(['ringcentral', 'promise', 'fetch'], function(SDK, promise) {

promise.polyfill();
require(['ringcentral', 'promise', 'fetch'], function(SDK) {

var sdk = new SDK({server: SDK.server.sandbox});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ringcentral",
"version": "3.1.0",
"version": "3.1.1",
"scripts": {
"clean": "rm -rf build/*",
"build": "npm run clean && ./node_modules/.bin/webpack --display-modules --progress --colors --bail",
Expand Down
2 changes: 1 addition & 1 deletion src/SDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ SDK.prototype.createSubscription = function(options) {
*/
SDK.prototype.createCachedSubscription = function(options) {

if (typeof arguments[0] == 'string') {
if (typeof arguments[0] === 'string') {
options = {cacheKey: arguments[0].toString()};
} else {
options = options || {};
Expand Down
72 changes: 53 additions & 19 deletions src/subscription/Subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var EventEmitter = require("events").EventEmitter;
* @property {Platform} _platform
* @property {int} _pollInterval
* @property {int} _renewHandicapMs
* @property {PUBNUB} _pubnub
* @property {PubNub} _pubnub
* @property {string} _pubnubLastChannel
* @property {int} _timeout
* @property {ISubscription} _subscription
Expand Down Expand Up @@ -49,6 +49,9 @@ function Subscription(options) {
/** @private */
this._pubnubLastChannel = null;

/** @private */
this._pubnubLastSubscribeKey = null;

/** @private */
this._timeout = null;

Expand Down Expand Up @@ -278,7 +281,7 @@ Subscription.prototype.resubscribe = function() {
*/
Subscription.prototype.reset = function() {
this._clearTimeout();
if (this.subscribed() && this._pubnub) this._pubnub.unsubscribe({channel: this.subscription().deliveryMode.address});
this._unsubscribeAtPubnub();
this._setSubscription(null);
return this;
};
Expand Down Expand Up @@ -379,35 +382,66 @@ Subscription.prototype._subscribeAtPubnub = function() {

if (this._pubnub) {

if (this._pubnubLastChannel == deliveryMode.address) { // Nothing to update, keep listening to same channel
if (this._pubnubLastChannel === deliveryMode.address) {

// Nothing to update, keep listening to same channel
return this;
} else if (this._pubnubLastChannel) { // Need to subscribe to new channel
this._pubnub.unsubscribe({channel: this._pubnubLastChannel});

} else if (this._pubnubLastSubscribeKey && this._pubnubLastSubscribeKey !== deliveryMode.subscriberKey) {

// Subscribe key changed, need to reset everything
this._unsubscribeAtPubnub();

} else if (this._pubnubLastChannel) {

// Need to subscribe to new channel
this._pubnub.unsubscribeAll();

}

// Re-init for new data
this._pubnub = this._pubnub.init({
ssl: true,
subscribe_key: deliveryMode.subscriberKey
});
}

} else {
if (!this._pubnub) {

// Init from scratch
this._pubnub = new this._externals.PubNub({
this._pubnubLastSubscribeKey = deliveryMode.subscriberKey;

var PubNub = this._externals.PubNub;

this._pubnub = new PubNub({
ssl: true,
subscribe_key: deliveryMode.subscriberKey
subscribeKey: deliveryMode.subscriberKey
});

this._pubnub.addListener({
status: function(statusEvent) {},
message: function(m) {
this._notify(m.message); // all other props are ignored
}.bind(this)
});

}

this._pubnubLastChannel = deliveryMode.address;
this._pubnub.subscribe({channels: [deliveryMode.address]});

return this;

};

/**
* @return {Subscription}
* @private
*/
Subscription.prototype._unsubscribeAtPubnub = function() {

if (!this.subscribed() || this._pubnub) return this;

this._pubnub.subscribe({
channel: deliveryMode.address,
message: this._notify.bind(this),
connect: function() {}
});
this._pubnub.removeAllListeners();
this._pubnub.destroy(); // this will unsubscribe from all

this._pubnubLastSubscribeKey = null;
this._pubnubLastChannel = null;
this._pubnub = null;

return this;

Expand Down

0 comments on commit fac8a80

Please sign in to comment.