Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved onError and onComplete zone handling #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 103 additions & 104 deletions lib/sails.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,123 +124,121 @@ export class SailsService {
var self = this;
let subject = new Subject<ISailsConnection>();

this.zone.runOutsideAngular(() => {
if (this._io && this._io.sails) {
if (this._io && this._io.socket && this._io.socket.isConnected) {
this._io.disconnect();
}

if (this._io && this._io.sails) {
if (this._io && this._io.socket && this._io.socket.isConnected) {
this._io.disconnect();
}

}
// Make URL optional
if ('object' === typeof url) {
this._opts = Object.assign({}, url);
url = null;
}

// Make URL optional
if ('object' === typeof url) {
this._opts = Object.assign({}, url);
url = null;
}
// this._url = url || null;
// this._opts = opts || {}

// this._url = url || null;
// this._opts = opts || {}
if (url) {
this.serverUrl = url;
} else if (this._opts.url) {
this.serverUrl = this._opts.url;
} else if (!(this._serverUrl && this._serverUrl.length > 0)) {
this._serverUrl = undefined;
}
this._opts.url = this._serverUrl;

if (url) {
this.serverUrl = url;
} else if (this._opts.url) {
this.serverUrl = this._opts.url;
} else if (!(this._serverUrl && this._serverUrl.length > 0)) {
this._serverUrl = undefined;
}
this._opts.url = this._serverUrl;
// // If explicit connection url is specified, save it to options
// this._opts.url = url || this._opts.url || this._serverUrl;

// // If explicit connection url is specified, save it to options
// this._opts.url = url || this._opts.url || this._serverUrl;
this._io = io.sails.connect(this._opts);

this._io = io.sails.connect(this._opts);
if (this._io && this._io.sails) {
if (this._io && this._io.socket && this._io.socket.isConnected) {
this._connected = true;
} else {
this._connected = false;
this.zone.run(() => subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
}));
}

if (this._io && this._io.sails) {
if (this._io && this._io.socket && this._io.socket.isConnected) {
this._connected = true;
} else {
this._connected = false;
subject.next({
this.zone.run(() => subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
});
}));
}

} else {
this._connected = false;
subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
});
}

this._io.on('connect_error', function () {

if (io.sails.environment != "production" && self.silent !== true) {
console.log('Connection failed');
}
this._io.on('connect_error', () => {


subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
if (io.sails.environment != "production" && self.silent !== true) {
console.log('Connection failed');
}
this.zone.run(() => subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
}));
});
});
this._io.on('reconnect_failed', function () {
this._io.on('reconnect_failed', () => {

if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has not reconnected to the server!');
}
subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has not reconnected to the server!');
}
this.zone.run(() => subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
}));
});
});

this._io.on('reconnected', function () {
this._io.on('reconnected', () => {

if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has reconnected to the server!');
}
subject.next({
connected: true,
url: self.serverUrl,
opts: self._opts
if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has reconnected to the server!');
}
this.zone.run(() => subject.next({
connected: true,
url: self.serverUrl,
opts: self._opts
}));
});
});


// Add a connect listener
this._io.on('connect', function () {
if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has connected to the server!');
}
subject.next({
connected: true,
url: self.serverUrl,
opts: self._opts
// Add a connect listener
this._io.on('connect', () => {
if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has connected to the server!');
}
this.zone.run(() => subject.next({
connected: true,
url: self.serverUrl,
opts: self._opts
}));
//subject.complete();

});
//subject.complete();

});
this._io.on('disconnect', () => {
if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has disconnected to the server!');
}
this.zone.run(() => subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
}));
//subject.complete();

this._io.on('disconnect', function () {
if (io.sails.environment != "production" && self.silent !== true) {
console.log('Client has disconnected to the server!');
}
subject.next({
connected: false,
url: self.serverUrl,
opts: self._opts
});
//subject.complete();

});

return <Observable<ISailsConnection>>subject.asObservable();
}

Expand Down Expand Up @@ -291,20 +289,21 @@ export class SailsService {
console.log("request:jwr", jwres)
}
if (jwres.statusCode < 200 || jwres.statusCode >= 400) {
subject.error({
this.zone.run(() => subject.error({
data: resData,
statusCode: jwres.statusCode,
response: jwres,
error: jwres.error
})
);
} else {
this.zone.run(() => subject.next({
data: resData,
statusCode: jwres.statusCode,
response: jwres
}));
}
subject.complete();
this.zone.run(() => subject.complete());
})
})
return subject.asObservable();
Expand All @@ -326,20 +325,20 @@ export class SailsService {
console.log("get:jwr", jwres)
}
if (jwres.statusCode < 200 || jwres.statusCode >= 400) {
subject.error({
this.zone.run(() => subject.error({
data: resData,
statusCode: jwres.statusCode,
response: jwres,
error: jwres.error
})
}));
} else {
this.zone.run(() => subject.next({
data: resData,
statusCode: jwres.statusCode,
response: jwres
}));
}
subject.complete();
this.zone.run(() => subject.complete());
})
});
return subject.asObservable();
Expand All @@ -363,20 +362,20 @@ export class SailsService {
console.log("post:jwr", jwres);
}
if (jwres.statusCode < 200 || jwres.statusCode >= 400) {
subject.error({
this.zone.run(() => subject.error({
data: resData,
statusCode: jwres.statusCode,
response: jwres,
error: jwres.error
})
}));
} else {
this.zone.run(() => subject.next({
data: resData,
statusCode: jwres.statusCode,
response: jwres
}));
}
subject.complete();
this.zone.run(() => subject.complete());
})
});
return subject.asObservable();
Expand All @@ -399,12 +398,12 @@ export class SailsService {
console.log("put:jwr", jwres);
}
if (jwres.statusCode < 200 || jwres.statusCode >= 400) {
subject.error({
this.zone.run(() => subject.error({
data: resData,
statusCode: jwres.statusCode,
response: jwres,
error: jwres.error
})
}));
} else {
//subject.next(resData);
this.zone.run(() => subject.next({
Expand All @@ -413,7 +412,7 @@ export class SailsService {
response: jwres
}));
}
subject.complete();
this.zone.run(() => subject.complete());
})
});
return subject.asObservable();
Expand All @@ -436,12 +435,12 @@ export class SailsService {
console.log("patch:jwr", jwres);
}
if (jwres.statusCode < 200 || jwres.statusCode >= 400) {
subject.error({
this.zone.run(() => subject.error({
data: resData,
statusCode: jwres.statusCode,
response: jwres,
error: jwres.error
})
}));
} else {
//subject.next(resData);
this.zone.run(() => subject.next({
Expand All @@ -450,7 +449,7 @@ export class SailsService {
response: jwres
}));
}
subject.complete();
this.zone.run(() => subject.complete());
})
});
return subject.asObservable();
Expand All @@ -472,20 +471,20 @@ export class SailsService {
console.log("delete:jwr", jwres);
}
if (jwres.statusCode < 200 || jwres.statusCode >= 400) {
subject.error({
this.zone.run(() => subject.error({
data: resData,
statusCode: jwres.statusCode,
response: jwres,
error: jwres.error
})
}));
} else {
this.zone.run(() => subject.next({
data: resData,
statusCode: jwres.statusCode,
response: jwres
}));
}
subject.complete();
this.zone.run(() => subject.complete());
})
});
return subject.asObservable();
Expand Down