Skip to content

Commit

Permalink
feat(connectionState): The state of the connection can be accessed by…
Browse files Browse the repository at this point in the history
… the promise on the ngStomp service

If you want to know if the system is connected or not, you can subscribe to the promise `ngStomp.connectionState.then()` and execute some code. `then` first function will be called if connection is up, second `then` function  or `catch` will be called if connection is down.

Don't forget, if you set a long timeOut in setting, the promise won't be fulfilled until we reach this timeOut. If the connection have already been open, the catch method won't be called

Linked to #43
  • Loading branch information
davinkevin committed Jul 4, 2016
1 parent 6ec0593 commit d57501c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
23 changes: 14 additions & 9 deletions core/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,38 @@ export default class ngStompWebSocket {
this.$timeout = $timeout;
this.connections = [];

this.initConnectionState();
this.connect();
}

initConnectionState() {
this.deferred = this.$q.defer();
this.connectionState = this.deferred.promise;
}

connect() {
this.$setConnection();

this.stompClient.connect(
this.settings.login,
this.settings.password,
() => {
this.deferred.resolve();
},
() => this.deferred.resolve(),
() => {
this.deferred.reject();
this.initConnectionState();
this.settings.timeOut >= 0 && this.$timeout(() => {
this.connect();
this.$reconnectAll();
}, this.settings.timeOut);
},
this.settings.vhost
);
return this.promiseResult;

return this.connectionState;
}

subscribe(queue, callback, header = {}, scope, json = false, digest) {
this.promiseResult
this.connectionState
.then(
() => this.$stompSubscribe(queue, callback, header, scope, json, digest),
() => this.$$addToConnectionQueue({ queue, callback, header, scope, json, digest })
Expand All @@ -55,14 +62,14 @@ export default class ngStompWebSocket {

/* Deprecated */
unsubscribe(url) {
this.promiseResult.then(() => this.$stompUnSubscribe(url));
this.connectionState.then(() => this.$stompUnSubscribe(url));
return this;
}

send(queue, data, header = {}) {
let sendDeffered = this.$q.defer();

this.promiseResult.then(() => {
this.connectionState.then(() => {
this.stompClient.send(queue, header, JSON.stringify(data));
sendDeffered.resolve();
});
Expand Down Expand Up @@ -110,8 +117,6 @@ export default class ngStompWebSocket {
this.stompClient.heartbeat.outgoing = this.settings.heartbeat.outgoing;
this.stompClient.heartbeat.incoming = this.settings.heartbeat.incoming;
}
this.deferred = this.$q.defer();
this.promiseResult = this.deferred.promise;
}

$unRegisterScopeOnDestroy(connection) {
Expand Down
30 changes: 22 additions & 8 deletions dist/angular-stomp.js

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

2 changes: 1 addition & 1 deletion dist/angular-stomp.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/angular-stomp.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"prebuild": "jspm install",
"build": "gulp build",
"release": "gulp release --type $(type)",
"postrelease": "npm publish",
"test": "karma start --single-run",
"test-tdd": "karma start"
},
Expand Down

0 comments on commit d57501c

Please sign in to comment.