Skip to content

Commit e5393ba

Browse files
Gourav DwivediGourav Dwivedi
authored andcommitted
code refactoring
1 parent ef4c23d commit e5393ba

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as appSignalR from './lib/app.signalr';
2+
3+
var sr = null;
4+
class AppLogic {
5+
6+
constructor() {
7+
this.isTokenRequired = false;
8+
this.isBasicView = true;
9+
}
10+
11+
Init(options) {
12+
13+
if(this.isBasicView !== true && this.isTokenRequired === true) {
14+
//adding new property to options object
15+
options["isTokenRequired"] = true;
16+
}
17+
else {
18+
options["isTokenRequired"] = false;
19+
}
20+
21+
sr = new appSignalR.SignalRApp(options.url);
22+
sr.Init(options);
23+
}
24+
25+
OnConnect() {
26+
sr.OnConnect();
27+
}
28+
29+
OnSend(options) {
30+
sr.OnSend(options);
31+
}
32+
33+
OnReceive(callback) {
34+
sr.OnReceive(callback);
35+
}
36+
37+
OnDisConnect() {
38+
sr.OnDisConnect();
39+
}
40+
41+
EnableAuth() {
42+
this.isTokenRequired = true;
43+
}
44+
45+
DisableAuth() {
46+
this.isTokenRequired = false;
47+
}
48+
49+
IsAuthEnabled() {
50+
return this.isTokenRequired;
51+
}
52+
53+
GetCurrentView() {
54+
return this.isBasicView;
55+
}
56+
57+
SetCurrentViewAsBasic() {
58+
this.isBasicView = true;
59+
}
60+
61+
SetCurrentViewAsAdvance() {
62+
this.isBasicView = false;
63+
}
64+
}
65+
66+
export { AppLogic }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import mitt from 'mitt';
2+
3+
const ContentType = {
4+
TEXT : "Text",
5+
NUMBER : "Number",
6+
JSON : "JSON"
7+
}
8+
9+
const AppEvents = mitt();
10+
11+
export { ContentType, AppEvents };
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as signalR from "@aspnet/signalr";
2+
import { AppEvents } from './app.common';
3+
4+
var storeFunc = null;
5+
class SignalRApp {
6+
constructor(url) {
7+
this.url = url;
8+
this.connection = null;
9+
this.isConnected = false;
10+
}
11+
12+
Init(options) {
13+
14+
if(options.isTokenRequired === true) {
15+
options.accessTokenFactory = () => options.getToken();
16+
}
17+
18+
this.connection = new signalR.HubConnectionBuilder()
19+
.withUrl(options.url, options)
20+
.configureLogging(signalR.LogLevel.Information)
21+
.build();
22+
23+
24+
//Receive Data
25+
//Reading the raw response
26+
var self = this;
27+
storeFunc = self.connection.processIncomingData;
28+
self.connection.processIncomingData = function (data) {
29+
console.log('processIncomingData'+ data);
30+
storeFunc.call(self.connection, data);
31+
}
32+
debugger;
33+
AppEvents.emit('Init', options);
34+
}
35+
36+
37+
OnConnect() {
38+
var self = this;
39+
self.connection.start()
40+
.then(function () {
41+
AppEvents.emit('OnConnect', { url: self.url });
42+
})
43+
.catch(function (err) {
44+
return console.error(err.toString());
45+
});
46+
}
47+
48+
OnSend(options) {
49+
var methodArguments = new Array();
50+
methodArguments = options.methodArguments;
51+
52+
AppEvents.emit('OnSend', options);
53+
this.connection.invoke(options.methodName, ...methodArguments)
54+
.catch(function (err) {
55+
return console.log(err);
56+
});
57+
}
58+
59+
OnReceive(callback) {
60+
this.connection.on("ReceiveData", function (data) {
61+
callback(data);
62+
});
63+
}
64+
65+
OnDisConnect() {
66+
this.connection.stop()
67+
.then(function () {
68+
console.log('Disconnected');
69+
AppEvents.emit('OnDisconnected');
70+
})
71+
.catch(function (err) {
72+
return console.error(err.toString());
73+
});
74+
}
75+
}
76+
77+
export { SignalRApp }

0 commit comments

Comments
 (0)