@hellotext/hellotext 2.5.6 → 2.5.8

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.
Files changed (113) hide show
  1. package/README.md +5 -26
  2. package/dist/hellotext.js +1 -1
  3. package/dist/hellotext.js.LICENSE.txt +1 -1
  4. package/index.d.ts +15 -6
  5. package/lib/api/acks.cjs +17 -31
  6. package/lib/api/acks.js +17 -42
  7. package/lib/api/businesses.cjs +14 -28
  8. package/lib/api/businesses.js +13 -36
  9. package/lib/api/events.cjs +26 -40
  10. package/lib/api/events.js +28 -52
  11. package/lib/api/forms.cjs +24 -40
  12. package/lib/api/forms.js +24 -57
  13. package/lib/api/identifications.cjs +15 -29
  14. package/lib/api/identifications.js +15 -40
  15. package/lib/api/index.cjs +26 -46
  16. package/lib/api/index.js +28 -49
  17. package/lib/api/push/identities.cjs +43 -0
  18. package/lib/api/push/identities.js +35 -0
  19. package/lib/api/response.cjs +30 -52
  20. package/lib/api/response.js +31 -60
  21. package/lib/api/submissions.cjs +18 -34
  22. package/lib/api/submissions.js +18 -48
  23. package/lib/api/webchat/messages.cjs +44 -64
  24. package/lib/api/webchat/messages.js +44 -80
  25. package/lib/api/webchats.cjs +37 -56
  26. package/lib/api/webchats.js +37 -65
  27. package/lib/api/whatsapp_widgets.cjs +49 -72
  28. package/lib/api/whatsapp_widgets.js +48 -91
  29. package/lib/builders/input_builder.cjs +36 -48
  30. package/lib/builders/input_builder.js +36 -48
  31. package/lib/builders/logo_builder.cjs +10 -29
  32. package/lib/builders/logo_builder.js +20 -28
  33. package/lib/channels/application_channel.cjs +136 -178
  34. package/lib/channels/application_channel.js +137 -180
  35. package/lib/channels/webchat_channel.cjs +125 -170
  36. package/lib/channels/webchat_channel.js +125 -170
  37. package/lib/controllers/form_controller.cjs +82 -121
  38. package/lib/controllers/form_controller.js +82 -130
  39. package/lib/controllers/message_controller.cjs +211 -282
  40. package/lib/controllers/message_controller.js +209 -284
  41. package/lib/controllers/mixins/usePopover.cjs +3 -6
  42. package/lib/controllers/mixins/usePopover.js +17 -22
  43. package/lib/controllers/webchat/emoji_picker_controller.cjs +106 -150
  44. package/lib/controllers/webchat/emoji_picker_controller.js +105 -170
  45. package/lib/controllers/webchat/useBehaviour.js +5 -5
  46. package/lib/controllers/webchat/useOpeningSequence.js +6 -6
  47. package/lib/controllers/webchat/useTeaser.js +9 -9
  48. package/lib/controllers/webchat_controller.cjs +916 -1088
  49. package/lib/controllers/webchat_controller.js +926 -1152
  50. package/lib/core/configuration/forms.cjs +21 -35
  51. package/lib/core/configuration/forms.js +20 -35
  52. package/lib/core/configuration/locale.cjs +36 -71
  53. package/lib/core/configuration/locale.js +36 -70
  54. package/lib/core/configuration/push.cjs +16 -0
  55. package/lib/core/configuration/push.js +10 -0
  56. package/lib/core/configuration/webchat.cjs +142 -182
  57. package/lib/core/configuration/webchat.js +145 -189
  58. package/lib/core/configuration/whatsapp.cjs +76 -102
  59. package/lib/core/configuration/whatsapp.js +76 -105
  60. package/lib/core/configuration.cjs +62 -75
  61. package/lib/core/configuration.js +61 -75
  62. package/lib/core/event.cjs +36 -59
  63. package/lib/core/event.js +35 -61
  64. package/lib/core/sanitize_html.js +4 -4
  65. package/lib/errors/invalid_event.cjs +5 -26
  66. package/lib/errors/invalid_event.js +5 -26
  67. package/lib/errors/not_initialized_error.cjs +5 -26
  68. package/lib/errors/not_initialized_error.js +5 -26
  69. package/lib/hellotext.cjs +186 -211
  70. package/lib/hellotext.js +189 -236
  71. package/lib/index.js +1 -1
  72. package/lib/models/business.cjs +115 -152
  73. package/lib/models/business.js +118 -162
  74. package/lib/models/cookies.cjs +34 -51
  75. package/lib/models/cookies.js +34 -51
  76. package/lib/models/fingerprint.cjs +8 -23
  77. package/lib/models/fingerprint.js +25 -58
  78. package/lib/models/form.cjs +106 -142
  79. package/lib/models/form.js +105 -153
  80. package/lib/models/form_collection.cjs +63 -101
  81. package/lib/models/form_collection.js +61 -107
  82. package/lib/models/index.cjs +7 -0
  83. package/lib/models/index.js +1 -0
  84. package/lib/models/page.cjs +106 -129
  85. package/lib/models/page.js +106 -131
  86. package/lib/models/push.cjs +345 -0
  87. package/lib/models/push.js +339 -0
  88. package/lib/models/query.cjs +21 -41
  89. package/lib/models/query.js +21 -41
  90. package/lib/models/session.cjs +33 -65
  91. package/lib/models/session.js +30 -63
  92. package/lib/models/user.cjs +29 -51
  93. package/lib/models/user.js +29 -51
  94. package/lib/models/utm.cjs +15 -27
  95. package/lib/models/utm.js +17 -32
  96. package/lib/models/webchat.cjs +52 -76
  97. package/lib/models/webchat.js +52 -90
  98. package/lib/models/whatsapp_widget.cjs +41 -59
  99. package/lib/models/whatsapp_widget.js +41 -73
  100. package/lib/vanilla.cjs +4 -7
  101. package/lib/vanilla.js +2 -1
  102. package/package.json +2 -10
  103. package/src/api/index.js +5 -0
  104. package/src/api/push/identities.js +42 -0
  105. package/src/core/configuration/push.js +12 -0
  106. package/src/core/configuration.js +5 -0
  107. package/src/hellotext.js +15 -1
  108. package/src/index.bundle.js +0 -1
  109. package/src/models/business.js +1 -0
  110. package/src/models/index.js +1 -0
  111. package/src/models/push.js +397 -0
  112. package/src/vanilla.js +2 -1
  113. package/styles/index.css +0 -30
@@ -5,199 +5,157 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _core = require("../core");
8
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
9
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
10
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
11
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
12
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
13
- let ApplicationChannel = /*#__PURE__*/function () {
14
- function ApplicationChannel() {
15
- _classCallCheck(this, ApplicationChannel);
8
+ class ApplicationChannel {
9
+ static webSocket;
10
+ static channels = new Set();
11
+ static messageHandlers = new Set();
12
+ static disconnectHandlers = new Set();
13
+ static subscriptionConfirmHandlers = new Set();
14
+ static reconnectTimeout = null;
15
+ static reconnectAttempts = 0;
16
+ static reconnectBaseDelay = 500;
17
+ static reconnectMaxDelay = 10000;
18
+ static reconnectJitter = 0.3;
19
+ static needsResubscribe = false;
20
+ constructor() {
16
21
  ApplicationChannel.channels.add(this);
17
22
  }
18
- _createClass(ApplicationChannel, [{
19
- key: "send",
20
- value: function send({
23
+ send({
24
+ command,
25
+ identifier,
26
+ data
27
+ }) {
28
+ const payload = {
21
29
  command,
22
- identifier,
23
- data
24
- }) {
25
- const payload = {
26
- command,
27
- identifier: JSON.stringify(identifier),
28
- data: JSON.stringify(data || {})
29
- };
30
- const socket = ApplicationChannel.ensureWebSocket();
31
- const message = JSON.stringify(payload);
32
- if (socket.readyState === WebSocket.OPEN) {
30
+ identifier: JSON.stringify(identifier),
31
+ data: JSON.stringify(data || {})
32
+ };
33
+ const socket = ApplicationChannel.ensureWebSocket();
34
+ const message = JSON.stringify(payload);
35
+ if (socket.readyState === WebSocket.OPEN) {
36
+ socket.send(message);
37
+ } else {
38
+ socket.addEventListener('open', () => {
33
39
  socket.send(message);
34
- } else {
35
- socket.addEventListener('open', () => {
36
- socket.send(message);
37
- });
38
- }
39
- }
40
- }, {
41
- key: "onMessage",
42
- value: function onMessage(callback) {
43
- const handler = event => {
44
- const data = JSON.parse(event.data);
45
- const {
46
- type,
47
- message
48
- } = data;
49
- if (this.ignoredEvents.includes(type)) {
50
- return;
51
- }
52
- callback(message);
53
- };
54
- ApplicationChannel.messageHandlers.add(handler);
55
- ApplicationChannel.ensureWebSocket().addEventListener('message', handler);
56
- }
57
- }, {
58
- key: "onDisconnect",
59
- value: function onDisconnect(callback) {
60
- ApplicationChannel.disconnectHandlers.add(callback);
61
- }
62
- }, {
63
- key: "onSubscriptionConfirmed",
64
- value: function onSubscriptionConfirmed(callback) {
65
- ApplicationChannel.subscriptionConfirmHandlers.add(callback);
66
- }
67
- }, {
68
- key: "webSocket",
69
- get: function () {
70
- return ApplicationChannel.ensureWebSocket();
71
- }
72
- }, {
73
- key: "ignoredEvents",
74
- get: function () {
75
- return ['ping', 'confirm_subscription', 'welcome'];
76
- }
77
- }], [{
78
- key: "ensureWebSocket",
79
- value: function ensureWebSocket() {
80
- if (this.webSocket && !this.closedWebSocket(this.webSocket)) {
81
- return this.webSocket;
82
- }
83
- if (this.webSocket) {
84
- this.needsResubscribe = true;
85
- }
86
- return this.openWebSocket();
87
- }
88
- }, {
89
- key: "openWebSocket",
90
- value: function openWebSocket() {
91
- this.clearReconnectTimeout();
92
- const socket = new WebSocket(_core.Configuration.actionCableUrl);
93
- this.webSocket = socket;
94
- this.installWebSocketHandlers(socket);
95
- return socket;
96
- }
97
- }, {
98
- key: "installWebSocketHandlers",
99
- value: function installWebSocketHandlers(socket) {
100
- socket.addEventListener('open', () => this.handleOpen(socket));
101
- socket.addEventListener('close', () => this.handleDisconnect(socket));
102
- socket.addEventListener('error', () => this.handleDisconnect(socket));
103
- socket.addEventListener('message', event => this.handleControlMessage(event));
104
- this.messageHandlers.forEach(handler => {
105
- socket.addEventListener('message', handler);
106
40
  });
107
41
  }
108
- }, {
109
- key: "handleOpen",
110
- value: function handleOpen(socket) {
111
- if (socket !== this.webSocket) {
112
- return;
113
- }
114
- this.reconnectAttempts = 0;
115
- if (!this.needsResubscribe) {
116
- return;
117
- }
118
- this.needsResubscribe = false;
119
- this.resubscribeChannels();
120
- }
121
- }, {
122
- key: "handleControlMessage",
123
- value: function handleControlMessage(event) {
124
- let data;
125
- try {
126
- data = JSON.parse(event.data);
127
- } catch {
128
- return;
129
- }
130
- if (data.type !== 'confirm_subscription') {
42
+ }
43
+ onMessage(callback) {
44
+ const handler = event => {
45
+ const data = JSON.parse(event.data);
46
+ const {
47
+ type,
48
+ message
49
+ } = data;
50
+ if (this.ignoredEvents.includes(type)) {
131
51
  return;
132
52
  }
133
- this.subscriptionConfirmHandlers.forEach(callback => callback(data.identifier));
53
+ callback(message);
54
+ };
55
+ ApplicationChannel.messageHandlers.add(handler);
56
+ ApplicationChannel.ensureWebSocket().addEventListener('message', handler);
57
+ }
58
+ onDisconnect(callback) {
59
+ ApplicationChannel.disconnectHandlers.add(callback);
60
+ }
61
+ onSubscriptionConfirmed(callback) {
62
+ ApplicationChannel.subscriptionConfirmHandlers.add(callback);
63
+ }
64
+ get webSocket() {
65
+ return ApplicationChannel.ensureWebSocket();
66
+ }
67
+ static ensureWebSocket() {
68
+ if (this.webSocket && !this.closedWebSocket(this.webSocket)) {
69
+ return this.webSocket;
134
70
  }
135
- }, {
136
- key: "handleDisconnect",
137
- value: function handleDisconnect(socket) {
138
- if (socket !== this.webSocket) {
139
- return;
140
- }
141
- this.disconnectHandlers.forEach(callback => callback());
142
- this.webSocket = null;
71
+ if (this.webSocket) {
143
72
  this.needsResubscribe = true;
144
- this.scheduleReconnect();
145
73
  }
146
- }, {
147
- key: "scheduleReconnect",
148
- value: function scheduleReconnect() {
149
- if (this.reconnectTimeout) {
150
- return;
151
- }
152
- this.reconnectTimeout = setTimeout(() => {
153
- this.reconnectTimeout = null;
154
- this.reconnectAttempts += 1;
155
- this.openWebSocket();
156
- }, this.reconnectDelay);
74
+ return this.openWebSocket();
75
+ }
76
+ static openWebSocket() {
77
+ this.clearReconnectTimeout();
78
+ const socket = new WebSocket(_core.Configuration.actionCableUrl);
79
+ this.webSocket = socket;
80
+ this.installWebSocketHandlers(socket);
81
+ return socket;
82
+ }
83
+ static installWebSocketHandlers(socket) {
84
+ socket.addEventListener('open', () => this.handleOpen(socket));
85
+ socket.addEventListener('close', () => this.handleDisconnect(socket));
86
+ socket.addEventListener('error', () => this.handleDisconnect(socket));
87
+ socket.addEventListener('message', event => this.handleControlMessage(event));
88
+ this.messageHandlers.forEach(handler => {
89
+ socket.addEventListener('message', handler);
90
+ });
91
+ }
92
+ static handleOpen(socket) {
93
+ if (socket !== this.webSocket) {
94
+ return;
157
95
  }
158
- }, {
159
- key: "clearReconnectTimeout",
160
- value: function clearReconnectTimeout() {
161
- if (this.reconnectTimeout) {
162
- clearTimeout(this.reconnectTimeout);
163
- this.reconnectTimeout = null;
164
- }
96
+ this.reconnectAttempts = 0;
97
+ if (!this.needsResubscribe) {
98
+ return;
165
99
  }
166
- }, {
167
- key: "resubscribeChannels",
168
- value: function resubscribeChannels() {
169
- this.channels.forEach(channel => {
170
- const resubscribe = channel.resubscribe || channel.subscribe;
171
- if (typeof resubscribe === 'function') {
172
- resubscribe.call(channel);
173
- }
174
- });
100
+ this.needsResubscribe = false;
101
+ this.resubscribeChannels();
102
+ }
103
+ static handleControlMessage(event) {
104
+ let data;
105
+ try {
106
+ data = JSON.parse(event.data);
107
+ } catch {
108
+ return;
175
109
  }
176
- }, {
177
- key: "closedWebSocket",
178
- value: function closedWebSocket(socket) {
179
- return socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING;
110
+ if (data.type !== 'confirm_subscription') {
111
+ return;
180
112
  }
181
- }, {
182
- key: "reconnectDelay",
183
- get: function () {
184
- const delay = Math.min(this.reconnectMaxDelay, this.reconnectBaseDelay * 2 ** this.reconnectAttempts);
185
- const jitter = Math.round(delay * this.reconnectJitter * Math.random());
186
- return delay + jitter;
113
+ this.subscriptionConfirmHandlers.forEach(callback => callback(data.identifier));
114
+ }
115
+ static handleDisconnect(socket) {
116
+ if (socket !== this.webSocket) {
117
+ return;
118
+ }
119
+ this.disconnectHandlers.forEach(callback => callback());
120
+ this.webSocket = null;
121
+ this.needsResubscribe = true;
122
+ this.scheduleReconnect();
123
+ }
124
+ static scheduleReconnect() {
125
+ if (this.reconnectTimeout) {
126
+ return;
127
+ }
128
+ this.reconnectTimeout = setTimeout(() => {
129
+ this.reconnectTimeout = null;
130
+ this.reconnectAttempts += 1;
131
+ this.openWebSocket();
132
+ }, this.reconnectDelay);
133
+ }
134
+ static clearReconnectTimeout() {
135
+ if (this.reconnectTimeout) {
136
+ clearTimeout(this.reconnectTimeout);
137
+ this.reconnectTimeout = null;
187
138
  }
188
- }]);
189
- return ApplicationChannel;
190
- }();
191
- ApplicationChannel.webSocket = void 0;
192
- ApplicationChannel.channels = new Set();
193
- ApplicationChannel.messageHandlers = new Set();
194
- ApplicationChannel.disconnectHandlers = new Set();
195
- ApplicationChannel.subscriptionConfirmHandlers = new Set();
196
- ApplicationChannel.reconnectTimeout = null;
197
- ApplicationChannel.reconnectAttempts = 0;
198
- ApplicationChannel.reconnectBaseDelay = 500;
199
- ApplicationChannel.reconnectMaxDelay = 10000;
200
- ApplicationChannel.reconnectJitter = 0.3;
201
- ApplicationChannel.needsResubscribe = false;
139
+ }
140
+ static resubscribeChannels() {
141
+ this.channels.forEach(channel => {
142
+ const resubscribe = channel.resubscribe || channel.subscribe;
143
+ if (typeof resubscribe === 'function') {
144
+ resubscribe.call(channel);
145
+ }
146
+ });
147
+ }
148
+ static closedWebSocket(socket) {
149
+ return socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING;
150
+ }
151
+ static get reconnectDelay() {
152
+ const delay = Math.min(this.reconnectMaxDelay, this.reconnectBaseDelay * 2 ** this.reconnectAttempts);
153
+ const jitter = Math.round(delay * this.reconnectJitter * Math.random());
154
+ return delay + jitter;
155
+ }
156
+ get ignoredEvents() {
157
+ return ['ping', 'confirm_subscription', 'welcome'];
158
+ }
159
+ }
202
160
  var _default = ApplicationChannel;
203
161
  exports.default = _default;
@@ -1,197 +1,154 @@
1
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
3
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
4
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
5
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
6
1
  import { Configuration } from '../core';
7
- var ApplicationChannel = /*#__PURE__*/function () {
8
- function ApplicationChannel() {
9
- _classCallCheck(this, ApplicationChannel);
2
+ class ApplicationChannel {
3
+ static webSocket;
4
+ static channels = new Set();
5
+ static messageHandlers = new Set();
6
+ static disconnectHandlers = new Set();
7
+ static subscriptionConfirmHandlers = new Set();
8
+ static reconnectTimeout = null;
9
+ static reconnectAttempts = 0;
10
+ static reconnectBaseDelay = 500;
11
+ static reconnectMaxDelay = 10000;
12
+ static reconnectJitter = 0.3;
13
+ static needsResubscribe = false;
14
+ constructor() {
10
15
  ApplicationChannel.channels.add(this);
11
16
  }
12
- _createClass(ApplicationChannel, [{
13
- key: "send",
14
- value: function send(_ref) {
15
- var {
16
- command,
17
- identifier,
18
- data
19
- } = _ref;
20
- var payload = {
21
- command,
22
- identifier: JSON.stringify(identifier),
23
- data: JSON.stringify(data || {})
24
- };
25
- var socket = ApplicationChannel.ensureWebSocket();
26
- var message = JSON.stringify(payload);
27
- if (socket.readyState === WebSocket.OPEN) {
17
+ send({
18
+ command,
19
+ identifier,
20
+ data
21
+ }) {
22
+ const payload = {
23
+ command,
24
+ identifier: JSON.stringify(identifier),
25
+ data: JSON.stringify(data || {})
26
+ };
27
+ const socket = ApplicationChannel.ensureWebSocket();
28
+ const message = JSON.stringify(payload);
29
+ if (socket.readyState === WebSocket.OPEN) {
30
+ socket.send(message);
31
+ } else {
32
+ socket.addEventListener('open', () => {
28
33
  socket.send(message);
29
- } else {
30
- socket.addEventListener('open', () => {
31
- socket.send(message);
32
- });
33
- }
34
- }
35
- }, {
36
- key: "onMessage",
37
- value: function onMessage(callback) {
38
- var handler = event => {
39
- var data = JSON.parse(event.data);
40
- var {
41
- type,
42
- message
43
- } = data;
44
- if (this.ignoredEvents.includes(type)) {
45
- return;
46
- }
47
- callback(message);
48
- };
49
- ApplicationChannel.messageHandlers.add(handler);
50
- ApplicationChannel.ensureWebSocket().addEventListener('message', handler);
51
- }
52
- }, {
53
- key: "onDisconnect",
54
- value: function onDisconnect(callback) {
55
- ApplicationChannel.disconnectHandlers.add(callback);
56
- }
57
- }, {
58
- key: "onSubscriptionConfirmed",
59
- value: function onSubscriptionConfirmed(callback) {
60
- ApplicationChannel.subscriptionConfirmHandlers.add(callback);
61
- }
62
- }, {
63
- key: "webSocket",
64
- get: function get() {
65
- return ApplicationChannel.ensureWebSocket();
66
- }
67
- }, {
68
- key: "ignoredEvents",
69
- get: function get() {
70
- return ['ping', 'confirm_subscription', 'welcome'];
71
- }
72
- }], [{
73
- key: "ensureWebSocket",
74
- value: function ensureWebSocket() {
75
- if (this.webSocket && !this.closedWebSocket(this.webSocket)) {
76
- return this.webSocket;
77
- }
78
- if (this.webSocket) {
79
- this.needsResubscribe = true;
80
- }
81
- return this.openWebSocket();
82
- }
83
- }, {
84
- key: "openWebSocket",
85
- value: function openWebSocket() {
86
- this.clearReconnectTimeout();
87
- var socket = new WebSocket(Configuration.actionCableUrl);
88
- this.webSocket = socket;
89
- this.installWebSocketHandlers(socket);
90
- return socket;
91
- }
92
- }, {
93
- key: "installWebSocketHandlers",
94
- value: function installWebSocketHandlers(socket) {
95
- socket.addEventListener('open', () => this.handleOpen(socket));
96
- socket.addEventListener('close', () => this.handleDisconnect(socket));
97
- socket.addEventListener('error', () => this.handleDisconnect(socket));
98
- socket.addEventListener('message', event => this.handleControlMessage(event));
99
- this.messageHandlers.forEach(handler => {
100
- socket.addEventListener('message', handler);
101
34
  });
102
35
  }
103
- }, {
104
- key: "handleOpen",
105
- value: function handleOpen(socket) {
106
- if (socket !== this.webSocket) {
107
- return;
108
- }
109
- this.reconnectAttempts = 0;
110
- if (!this.needsResubscribe) {
111
- return;
112
- }
113
- this.needsResubscribe = false;
114
- this.resubscribeChannels();
115
- }
116
- }, {
117
- key: "handleControlMessage",
118
- value: function handleControlMessage(event) {
119
- var data;
120
- try {
121
- data = JSON.parse(event.data);
122
- } catch (_unused) {
123
- return;
124
- }
125
- if (data.type !== 'confirm_subscription') {
36
+ }
37
+ onMessage(callback) {
38
+ const handler = event => {
39
+ const data = JSON.parse(event.data);
40
+ const {
41
+ type,
42
+ message
43
+ } = data;
44
+ if (this.ignoredEvents.includes(type)) {
126
45
  return;
127
46
  }
128
- this.subscriptionConfirmHandlers.forEach(callback => callback(data.identifier));
47
+ callback(message);
48
+ };
49
+ ApplicationChannel.messageHandlers.add(handler);
50
+ ApplicationChannel.ensureWebSocket().addEventListener('message', handler);
51
+ }
52
+ onDisconnect(callback) {
53
+ ApplicationChannel.disconnectHandlers.add(callback);
54
+ }
55
+ onSubscriptionConfirmed(callback) {
56
+ ApplicationChannel.subscriptionConfirmHandlers.add(callback);
57
+ }
58
+ get webSocket() {
59
+ return ApplicationChannel.ensureWebSocket();
60
+ }
61
+ static ensureWebSocket() {
62
+ if (this.webSocket && !this.closedWebSocket(this.webSocket)) {
63
+ return this.webSocket;
129
64
  }
130
- }, {
131
- key: "handleDisconnect",
132
- value: function handleDisconnect(socket) {
133
- if (socket !== this.webSocket) {
134
- return;
135
- }
136
- this.disconnectHandlers.forEach(callback => callback());
137
- this.webSocket = null;
65
+ if (this.webSocket) {
138
66
  this.needsResubscribe = true;
139
- this.scheduleReconnect();
140
67
  }
141
- }, {
142
- key: "scheduleReconnect",
143
- value: function scheduleReconnect() {
144
- if (this.reconnectTimeout) {
145
- return;
146
- }
147
- this.reconnectTimeout = setTimeout(() => {
148
- this.reconnectTimeout = null;
149
- this.reconnectAttempts += 1;
150
- this.openWebSocket();
151
- }, this.reconnectDelay);
68
+ return this.openWebSocket();
69
+ }
70
+ static openWebSocket() {
71
+ this.clearReconnectTimeout();
72
+ const socket = new WebSocket(Configuration.actionCableUrl);
73
+ this.webSocket = socket;
74
+ this.installWebSocketHandlers(socket);
75
+ return socket;
76
+ }
77
+ static installWebSocketHandlers(socket) {
78
+ socket.addEventListener('open', () => this.handleOpen(socket));
79
+ socket.addEventListener('close', () => this.handleDisconnect(socket));
80
+ socket.addEventListener('error', () => this.handleDisconnect(socket));
81
+ socket.addEventListener('message', event => this.handleControlMessage(event));
82
+ this.messageHandlers.forEach(handler => {
83
+ socket.addEventListener('message', handler);
84
+ });
85
+ }
86
+ static handleOpen(socket) {
87
+ if (socket !== this.webSocket) {
88
+ return;
152
89
  }
153
- }, {
154
- key: "clearReconnectTimeout",
155
- value: function clearReconnectTimeout() {
156
- if (this.reconnectTimeout) {
157
- clearTimeout(this.reconnectTimeout);
158
- this.reconnectTimeout = null;
159
- }
90
+ this.reconnectAttempts = 0;
91
+ if (!this.needsResubscribe) {
92
+ return;
160
93
  }
161
- }, {
162
- key: "resubscribeChannels",
163
- value: function resubscribeChannels() {
164
- this.channels.forEach(channel => {
165
- var resubscribe = channel.resubscribe || channel.subscribe;
166
- if (typeof resubscribe === 'function') {
167
- resubscribe.call(channel);
168
- }
169
- });
94
+ this.needsResubscribe = false;
95
+ this.resubscribeChannels();
96
+ }
97
+ static handleControlMessage(event) {
98
+ let data;
99
+ try {
100
+ data = JSON.parse(event.data);
101
+ } catch {
102
+ return;
170
103
  }
171
- }, {
172
- key: "closedWebSocket",
173
- value: function closedWebSocket(socket) {
174
- return socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING;
104
+ if (data.type !== 'confirm_subscription') {
105
+ return;
175
106
  }
176
- }, {
177
- key: "reconnectDelay",
178
- get: function get() {
179
- var delay = Math.min(this.reconnectMaxDelay, this.reconnectBaseDelay * 2 ** this.reconnectAttempts);
180
- var jitter = Math.round(delay * this.reconnectJitter * Math.random());
181
- return delay + jitter;
107
+ this.subscriptionConfirmHandlers.forEach(callback => callback(data.identifier));
108
+ }
109
+ static handleDisconnect(socket) {
110
+ if (socket !== this.webSocket) {
111
+ return;
112
+ }
113
+ this.disconnectHandlers.forEach(callback => callback());
114
+ this.webSocket = null;
115
+ this.needsResubscribe = true;
116
+ this.scheduleReconnect();
117
+ }
118
+ static scheduleReconnect() {
119
+ if (this.reconnectTimeout) {
120
+ return;
121
+ }
122
+ this.reconnectTimeout = setTimeout(() => {
123
+ this.reconnectTimeout = null;
124
+ this.reconnectAttempts += 1;
125
+ this.openWebSocket();
126
+ }, this.reconnectDelay);
127
+ }
128
+ static clearReconnectTimeout() {
129
+ if (this.reconnectTimeout) {
130
+ clearTimeout(this.reconnectTimeout);
131
+ this.reconnectTimeout = null;
182
132
  }
183
- }]);
184
- return ApplicationChannel;
185
- }();
186
- ApplicationChannel.webSocket = void 0;
187
- ApplicationChannel.channels = new Set();
188
- ApplicationChannel.messageHandlers = new Set();
189
- ApplicationChannel.disconnectHandlers = new Set();
190
- ApplicationChannel.subscriptionConfirmHandlers = new Set();
191
- ApplicationChannel.reconnectTimeout = null;
192
- ApplicationChannel.reconnectAttempts = 0;
193
- ApplicationChannel.reconnectBaseDelay = 500;
194
- ApplicationChannel.reconnectMaxDelay = 10000;
195
- ApplicationChannel.reconnectJitter = 0.3;
196
- ApplicationChannel.needsResubscribe = false;
133
+ }
134
+ static resubscribeChannels() {
135
+ this.channels.forEach(channel => {
136
+ const resubscribe = channel.resubscribe || channel.subscribe;
137
+ if (typeof resubscribe === 'function') {
138
+ resubscribe.call(channel);
139
+ }
140
+ });
141
+ }
142
+ static closedWebSocket(socket) {
143
+ return socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING;
144
+ }
145
+ static get reconnectDelay() {
146
+ const delay = Math.min(this.reconnectMaxDelay, this.reconnectBaseDelay * 2 ** this.reconnectAttempts);
147
+ const jitter = Math.round(delay * this.reconnectJitter * Math.random());
148
+ return delay + jitter;
149
+ }
150
+ get ignoredEvents() {
151
+ return ['ping', 'confirm_subscription', 'welcome'];
152
+ }
153
+ }
197
154
  export default ApplicationChannel;