@urso/core 0.3.3 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,13 @@
1
1
  class ModulesTransportConfig {
2
- constructor() {
3
- this._setDefaultConfig();
4
- }
5
2
 
6
3
  getConfig() {
7
- return this._config;
8
- };
9
-
10
- _setDefaultConfig() {
11
- this._config = {
12
- autoReconnection: true,
13
- autoReconnectionDelay: 5000,
14
- hosts: {
15
- wsHost: Urso.helper.parseGetParams('wsHost') || 'ws://localhost:9100/',
16
- xhrHost: 'https://reqres.in/api/users/2'
4
+ return {
5
+ autoReconnect: true,
6
+ reconnectTimeout: 5000,
7
+ type: 'websocket', // websocket | xhr
8
+ host: Urso.helper.parseGetParams('wsHost')
17
9
  }
18
- };
19
- };
10
+ }
20
11
  }
21
12
 
22
13
  module.exports = ModulesTransportConfig;
@@ -4,6 +4,7 @@ class ModulesTransportConnectionTypesXhr extends Urso.Core.Modules.Transport.Bas
4
4
 
5
5
  this._xhr = null;
6
6
  this._ready = true;
7
+ this._runCallback('ready');
7
8
  }
8
9
 
9
10
  send(message){
@@ -13,13 +14,15 @@ class ModulesTransportConnectionTypesXhr extends Urso.Core.Modules.Transport.Bas
13
14
  log('[XHR] SEND:', preparedMessage);
14
15
  };
15
16
 
17
+ _getMethod(){
18
+ return 'POST';
19
+ };
16
20
 
17
21
  _createXhr(){
18
22
  this._xhr = new XMLHttpRequest();
19
23
  this._xhr.onerror = this._onError.bind(this);
20
24
  this._xhr.onreadystatechange = this._onReadyStateChange.bind(this);
21
- this._xhr.open('GET', this._host, true);
22
- log('[XHR]: CREATED');
25
+ this._xhr.open(this._getMethod(), this._host, true);
23
26
  };
24
27
 
25
28
  _onMessage(message){
@@ -1,5 +1,6 @@
1
1
  class ModulesTransportDecorator {
2
- constructor() {
2
+ constructor({ callbacks }) {
3
+ this._callbacks = callbacks;
3
4
  this.singleton = true;
4
5
  }
5
6
 
@@ -2,10 +2,6 @@ class ModulesTransportService {
2
2
  constructor() {
3
3
  this._callbacks = {};
4
4
  this._config = this.getInstance('Config').getConfig();
5
-
6
- this.WEBSOCKET = 'websocket';
7
- this.XHR = 'xhr';
8
-
9
5
  this._communicator = null;
10
6
  }
11
7
 
@@ -15,17 +11,25 @@ class ModulesTransportService {
15
11
  };
16
12
 
17
13
  init() {
18
- if(Urso.config.useTransport){
19
- this._createConnection();
20
- }
14
+ this._createConnection();
21
15
  };
22
16
 
23
17
  send(message) {
24
18
  if (!this._checkCommunicator() || !this._checkCommunicatorReady())
25
19
  return false;
26
20
 
27
- const decoratedMessage = this.getInstance('Decorator').toServer(message);
21
+ const decoratedMessage = this.getInstance('Decorator', { callbacks: this._callbacks }).toServer(message);
22
+
23
+ if(!decoratedMessage) {
24
+ return false;
25
+ }
26
+
28
27
  const validatedMessage = this._validateMessage(decoratedMessage);
28
+
29
+ if(!validatedMessage) {
30
+ return false;
31
+ }
32
+
29
33
  this._communicator.send(validatedMessage);
30
34
 
31
35
  return true;
@@ -35,7 +39,7 @@ class ModulesTransportService {
35
39
  if (!this._checkCommunicator())
36
40
  return false;
37
41
 
38
- this._communicator.reconnect(this._config.autoReconnectionDelay || 0);
42
+ this._communicator.reconnect(this._getAutoReconnetionDelay());
39
43
 
40
44
  return true;
41
45
  };
@@ -85,7 +89,12 @@ class ModulesTransportService {
85
89
  _runMiddleWare(event, data) {
86
90
  switch (event) {
87
91
  case 'response':
88
- const decoratedMessage = this.getInstance('Decorator').toFront(JSON.parse(data));
92
+ const decoratedMessage = this.getInstance('Decorator', { callbacks: this._callbacks }).toFront(data);
93
+
94
+ if(!decoratedMessage) {
95
+ return;
96
+ }
97
+
89
98
  return this._validateMessage(decoratedMessage);
90
99
 
91
100
  case 'close':
@@ -96,11 +105,19 @@ class ModulesTransportService {
96
105
  }
97
106
  };
98
107
 
108
+ _autoReconnectCheck() {
109
+ return this._config.reconnectTimeout;
110
+ }
111
+
112
+ _getAutoReconnetionDelay() {
113
+ return this._config.autoReconnect || 0;
114
+ }
115
+
99
116
  _tryReconnect() {
100
- if (!this._config.autoReconnection)
117
+ if (!this._autoReconnectCheck())
101
118
  return false;
102
119
 
103
- this._communicator.reconnect(this._config.autoReconnectionDelay);
120
+ this._communicator.reconnect(this._getAutoReconnetionDelay());
104
121
  };
105
122
 
106
123
  _setCallback(event, callback) {
@@ -112,24 +129,20 @@ class ModulesTransportService {
112
129
  return { ...message };
113
130
  };
114
131
 
115
- _getHost(type) {
116
- const hosts = this._config.hosts;
117
-
118
- switch (type) {
119
- case this.WEBSOCKET:
120
- return hosts.wsHost;
121
- case this.XHR:
122
- return hosts.xhrHost;
123
- default:
124
- return false;
125
- }
132
+ _getHost() {
133
+ return this._config.host;
126
134
  };
127
135
 
128
- _createConnection(type = this.WEBSOCKET) {
129
- const host = this._getHost(type);
136
+ _getType() {
137
+ return this._config.type;
138
+ }
139
+
140
+ _createConnection() {
141
+ const host = this._getHost();
142
+ const type = this._getType();
130
143
  const callbacks = this._callbacks;
131
144
  const capitalizedType = Urso.helper.capitaliseFirstLetter(type);
132
-
145
+
133
146
  this._communicator = this.getInstance(`ConnectionTypes.${capitalizedType}`, { callbacks, host });
134
147
 
135
148
  if (!this._communicator)