@viewberapp/chat 0.1.12 → 0.1.17

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 (75) hide show
  1. package/LICENSE +20 -20
  2. package/dist/API.d.ts +93 -91
  3. package/dist/API.js +243 -241
  4. package/dist/API.js.map +1 -1
  5. package/dist/CachedChat.d.ts +67 -64
  6. package/dist/CachedChat.js +248 -241
  7. package/dist/CachedChat.js.map +1 -1
  8. package/dist/Chat.d.ts +66 -66
  9. package/dist/Chat.js +273 -270
  10. package/dist/Chat.js.map +1 -1
  11. package/dist/ChatError.d.ts +4 -4
  12. package/dist/ChatError.js +2 -2
  13. package/dist/ChatSubscription.d.ts +84 -82
  14. package/dist/ChatSubscription.js +134 -128
  15. package/dist/ChatSubscription.js.map +1 -1
  16. package/dist/Message.d.ts +25 -25
  17. package/dist/Message.js +35 -35
  18. package/dist/OnlineUser.d.ts +8 -8
  19. package/dist/OnlineUser.js +9 -9
  20. package/dist/PendingMessage.d.ts +23 -23
  21. package/dist/PendingMessage.js +23 -23
  22. package/dist/index.d.ts +8 -8
  23. package/dist/index.js +27 -27
  24. package/dist/logger/ConsoleLogger.d.ts +8 -8
  25. package/dist/logger/ConsoleLogger.js +20 -20
  26. package/dist/logger/ConsoleLogger.js.map +1 -1
  27. package/dist/logger/NullLogger.d.ts +8 -8
  28. package/dist/logger/NullLogger.js +10 -10
  29. package/dist/logger/NullLogger.js.map +1 -1
  30. package/dist/logger/RollbarLogger.d.ts +11 -11
  31. package/dist/logger/RollbarLogger.js +23 -23
  32. package/dist/logger/RollbarLogger.js.map +1 -1
  33. package/dist/logger/index.d.ts +12 -12
  34. package/dist/logger/index.js +12 -12
  35. package/dist/logger/index.js.map +1 -1
  36. package/dist/react/components/AdminMessage.d.ts +6 -6
  37. package/dist/react/components/AdminMessage.js +59 -59
  38. package/dist/react/components/ConnectionAndChannelStatusLabel.d.ts +7 -7
  39. package/dist/react/components/ConnectionAndChannelStatusLabel.js +59 -59
  40. package/dist/react/components/Message.d.ts +23 -23
  41. package/dist/react/components/Message.js +87 -87
  42. package/dist/react/components/MessageDateTime.d.ts +8 -8
  43. package/dist/react/components/MessageDateTime.js +31 -31
  44. package/dist/react/components/MessageList.d.ts +46 -46
  45. package/dist/react/components/MessageList.js +107 -107
  46. package/dist/react/components/MessageRow.d.ts +5 -5
  47. package/dist/react/components/MessageRow.js +34 -34
  48. package/dist/react/components/index.d.ts +6 -6
  49. package/dist/react/components/index.js +13 -13
  50. package/dist/react/index.d.ts +3 -3
  51. package/dist/react/index.js +16 -16
  52. package/dist/react/useAppointmentChat.d.ts +3 -3
  53. package/dist/react/useAppointmentChat.js +10 -10
  54. package/dist/react/useChat.d.ts +40 -38
  55. package/dist/react/useChat.js +127 -125
  56. package/dist/react/useChat.js.map +1 -1
  57. package/dist/react/useRealtimeTotalUserUnreadChatMessages.d.ts +13 -12
  58. package/dist/react/useRealtimeTotalUserUnreadChatMessages.js +138 -137
  59. package/dist/react/useRealtimeTotalUserUnreadChatMessages.js.map +1 -1
  60. package/dist/util/getLatestMessage.d.ts +3 -3
  61. package/dist/util/getLatestMessage.js +17 -17
  62. package/dist/util/groupMessages.d.ts +8 -8
  63. package/dist/util/groupMessages.js +30 -30
  64. package/dist/util/index.d.ts +4 -4
  65. package/dist/util/index.js +9 -9
  66. package/dist/util/isNil.d.ts +1 -1
  67. package/dist/util/isNil.js +7 -7
  68. package/dist/util/jwtTokenChanged.d.ts +1 -1
  69. package/dist/util/jwtTokenChanged.js +10 -10
  70. package/dist/util/useIsMounted.d.ts +1 -1
  71. package/dist/util/useIsMounted.js +13 -13
  72. package/package.json +2 -2
  73. package/dist/util/getLatestMessageId.d.ts +0 -3
  74. package/dist/util/getLatestMessageId.js +0 -18
  75. package/dist/util/getLatestMessageId.js.map +0 -1
@@ -1,64 +1,67 @@
1
- import Ably from 'ably';
2
- import API from "./API";
3
- import { Logger } from "./logger";
4
- import OnlineUser from "./OnlineUser";
5
- import { ChannelStatus, ConnectionStatus, DebugOptions } from './Chat';
6
- import PendingMessage from "./PendingMessage";
7
- import Message from "./Message";
8
- import ChatSubscription from "./ChatSubscription";
9
- import ChatError from './ChatError';
10
- export interface CachedChatParams {
11
- id: number;
12
- api: API;
13
- channel: Ably.Types.RealtimeChannelCallbacks;
14
- connectionStatus: ConnectionStatus;
15
- logger: Logger;
16
- debugLogger: Logger;
17
- debug?: DebugOptions;
18
- }
19
- export interface SendMessageOptions {
20
- isAdminMessage: boolean;
21
- }
22
- export declare type Messages = (Message | PendingMessage)[];
23
- export declare type GetLatestMessagesStatus = 'initialized' | 'fetching' | 'success' | 'failed';
24
- export default class CachedChat {
25
- private id;
26
- private api;
27
- messages: Messages;
28
- onlineUsers: OnlineUser[];
29
- private subscriptions;
30
- private channel;
31
- private connectionStatus;
32
- private channelStatus;
33
- private getLatestMessagesStatus;
34
- private logger;
35
- private debugLogger;
36
- private debug;
37
- private disabled_at;
38
- constructor({ id, api, channel, connectionStatus, logger, debugLogger, debug }: CachedChatParams);
39
- sendMessage(message: string, options?: SendMessageOptions): Promise<void>;
40
- reportChat(reason: string): Promise<void>;
41
- toggleChatEnabled(): Promise<void>;
42
- sendPendingMessage(pendingMessage: PendingMessage): Promise<void>;
43
- getLatestMessages(): void;
44
- markMessagesReadUpTo(message: Message): Promise<{
45
- error: ChatError | undefined;
46
- }>;
47
- setDisabledAt(disabled_at?: Date | null | undefined): void;
48
- getDisabledAt(): Date | null | undefined;
49
- addMessage(message: Message | PendingMessage): void;
50
- private replacePendingMessage;
51
- private updatePendingMessageStatus;
52
- addSubscription(subscription: ChatSubscription): void;
53
- getConnectionStatus(): ConnectionStatus;
54
- setConnectionStatus(status: ConnectionStatus): void;
55
- getChannelStatus(): ChannelStatus;
56
- private handleChannelStateChange;
57
- getGetLatestMessagesStatus(): GetLatestMessagesStatus;
58
- setGetLatestMessagesStatus(status: GetLatestMessagesStatus): void;
59
- detachChannel(callback: Ably.Types.errorCallback): void;
60
- private fireMessagesEventOnEachNonCancelledSubscription;
61
- private fireEventOnEachSubscription;
62
- cancelSubscription(subscription: ChatSubscription): void;
63
- cancelAllSubscriptions(): void;
64
- }
1
+ import Ably from 'ably';
2
+ import API from "./API";
3
+ import { Logger } from "./Logger";
4
+ import OnlineUser from "./OnlineUser";
5
+ import { ChannelStatus, ConnectionStatus, DebugOptions } from './Chat';
6
+ import PendingMessage from "./PendingMessage";
7
+ import Message from "./Message";
8
+ import ChatSubscription from "./ChatSubscription";
9
+ import ChatError from './ChatError';
10
+ export interface CachedChatParams {
11
+ id: number;
12
+ api: API;
13
+ channel: Ably.Types.RealtimeChannelCallbacks;
14
+ connectionStatus: ConnectionStatus;
15
+ logger: Logger;
16
+ debugLogger: Logger;
17
+ debug?: DebugOptions;
18
+ }
19
+ export interface SendMessageOptions {
20
+ isAdminMessage: boolean;
21
+ }
22
+ export declare type Messages = (Message | PendingMessage)[];
23
+ export declare type GetLatestMessagesStatus = 'initialized' | 'fetching' | 'success' | 'failed';
24
+ export default class CachedChat {
25
+ private id;
26
+ private api;
27
+ messages: Messages;
28
+ onlineUsers: OnlineUser[];
29
+ private subscriptions;
30
+ private channel;
31
+ private connectionStatus;
32
+ private channelStatus;
33
+ private getLatestMessagesStatus;
34
+ private logger;
35
+ private debugLogger;
36
+ private debug;
37
+ private disabled_at;
38
+ private is_member;
39
+ constructor({ id, api, channel, connectionStatus, logger, debugLogger, debug }: CachedChatParams);
40
+ sendMessage(message: string, options?: SendMessageOptions): Promise<void>;
41
+ reportChat(reason: string): Promise<void>;
42
+ toggleChatEnabled(): Promise<void>;
43
+ sendPendingMessage(pendingMessage: PendingMessage): Promise<void>;
44
+ getLatestMessages(): void;
45
+ markMessagesReadUpTo(message: Message): Promise<{
46
+ error: ChatError | undefined;
47
+ }>;
48
+ setDisabledAt(disabled_at?: Date | null | undefined): void;
49
+ getDisabledAt(): Date | null | undefined;
50
+ setIsMember(is_member: boolean): void;
51
+ getIsMember(): boolean;
52
+ addMessage(message: Message | PendingMessage): void;
53
+ private replacePendingMessage;
54
+ private updatePendingMessageStatus;
55
+ addSubscription(subscription: ChatSubscription): void;
56
+ getConnectionStatus(): ConnectionStatus;
57
+ setConnectionStatus(status: ConnectionStatus): void;
58
+ getChannelStatus(): ChannelStatus;
59
+ private handleChannelStateChange;
60
+ getGetLatestMessagesStatus(): GetLatestMessagesStatus;
61
+ setGetLatestMessagesStatus(status: GetLatestMessagesStatus): void;
62
+ detachChannel(callback: Ably.Types.errorCallback): void;
63
+ private fireMessagesEventOnEachNonCancelledSubscription;
64
+ private fireEventOnEachSubscription;
65
+ cancelSubscription(subscription: ChatSubscription): void;
66
+ cancelAllSubscriptions(): void;
67
+ }
@@ -1,242 +1,249 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- const uuid_1 = require("uuid");
27
- const PendingMessage_1 = __importStar(require("./PendingMessage"));
28
- const Message_1 = __importStar(require("./Message"));
29
- class CachedChat {
30
- constructor({ id, api, channel, connectionStatus, logger, debugLogger, debug }) {
31
- this.getLatestMessagesStatus = 'initialized';
32
- this.id = id;
33
- this.api = api;
34
- this.messages = [];
35
- this.onlineUsers = [];
36
- this.subscriptions = [];
37
- this.channel = channel;
38
- this.channelStatus = this.channel.state;
39
- this.connectionStatus = connectionStatus;
40
- this.logger = logger;
41
- this.debugLogger = debugLogger;
42
- this.debug = debug;
43
- this.disabled_at = null;
44
- this.channel.on(this.handleChannelStateChange.bind(this));
45
- this.channel.subscribe((message) => {
46
- var _a;
47
- if ((_a = this.debug) === null || _a === void 0 ? void 0 : _a.inboundAblyMessages) {
48
- this.debugLogger.debug('[Ably] [InboundMessage]', message);
49
- }
50
- if (message.name === 'chat.message.created') {
51
- this.addMessage(new Message_1.default({
52
- id: message.data.id,
53
- chatId: message.data.chat_id,
54
- message: message.data.message,
55
- senderId: message.data.sender_id,
56
- sender: message.data.sender,
57
- isAdminMessage: message.data.is_admin_message,
58
- createdAt: new Date(message.data.created_at)
59
- }));
60
- this.fireMessagesEventOnEachNonCancelledSubscription();
61
- }
62
- });
63
- }
64
- sendMessage(message, options) {
65
- return new Promise((resolve, reject) => {
66
- const pendingMessage = new PendingMessage_1.default({
67
- id: (0, uuid_1.v4)(),
68
- message,
69
- status: 'initialized',
70
- options,
71
- createdAt: new Date
72
- });
73
- this.addMessage(pendingMessage);
74
- this.fireMessagesEventOnEachNonCancelledSubscription();
75
- this.sendPendingMessage(pendingMessage)
76
- .then(() => resolve())
77
- .catch(err => reject(err));
78
- });
79
- }
80
- reportChat(reason) {
81
- return new Promise((resolve, reject) => {
82
- this.api.reportChat(this.id, reason)
83
- .then(() => resolve())
84
- .catch(err => reject(err));
85
- });
86
- }
87
- toggleChatEnabled() {
88
- return new Promise((resolve, reject) => {
89
- this.api.toggleChatEnabled(this.id)
90
- .then(() => resolve())
91
- .catch(err => reject(err));
92
- });
93
- }
94
- sendPendingMessage(pendingMessage) {
95
- return new Promise((resolve, reject) => {
96
- if (!(0, PendingMessage_1.isPendingMessage)(pendingMessage) || pendingMessage.status === 'pending') {
97
- return resolve();
98
- }
99
- this.updatePendingMessageStatus(pendingMessage, 'pending');
100
- this.fireMessagesEventOnEachNonCancelledSubscription();
101
- this.api.storeMessage(this.id, pendingMessage.message, pendingMessage.options).then(({ message, error }) => {
102
- if (error) {
103
- this.updatePendingMessageStatus(pendingMessage, 'failed');
104
- this.fireMessagesEventOnEachNonCancelledSubscription();
105
- return reject(new Error(error.message));
106
- }
107
- this.replacePendingMessage(pendingMessage, message);
108
- this.fireMessagesEventOnEachNonCancelledSubscription();
109
- resolve();
110
- }).catch(err => {
111
- this.updatePendingMessageStatus(pendingMessage, 'failed');
112
- this.fireMessagesEventOnEachNonCancelledSubscription();
113
- reject(err);
114
- });
115
- });
116
- }
117
- getLatestMessages() {
118
- this.setGetLatestMessagesStatus('fetching');
119
- this.api.getLatestMessages(this.id).then(({ messages, error }) => {
120
- if (messages) {
121
- messages.forEach(m => this.addMessage(m));
122
- this.setGetLatestMessagesStatus('success');
123
- this.fireMessagesEventOnEachNonCancelledSubscription();
124
- return;
125
- }
126
- this.setGetLatestMessagesStatus('failed');
127
- }).catch(err => {
128
- this.setGetLatestMessagesStatus('failed');
129
- });
130
- }
131
- markMessagesReadUpTo(message) {
132
- return this.api.markChatMessagesReadUpTo(this.id, message);
133
- }
134
- setDisabledAt(disabled_at) {
135
- this.disabled_at = disabled_at;
136
- }
137
- getDisabledAt() {
138
- return this.disabled_at;
139
- }
140
- addMessage(message) {
141
- if (message instanceof PendingMessage_1.default) {
142
- this.messages.push(message);
143
- return;
144
- }
145
- let index = -1;
146
- for (let i = (this.messages.length - 1); i >= 0; i--) {
147
- if ((0, Message_1.isMessageAnd)(this.messages[i], m => m.id < message.id) && index === -1) {
148
- index = i;
149
- }
150
- if ((0, Message_1.isMessageAnd)(this.messages[i], m => m.id === message.id)) {
151
- return;
152
- }
153
- }
154
- if (index === -1) {
155
- this.messages.unshift(message);
156
- this.messages = this.messages.concat([]);
157
- }
158
- else {
159
- this.messages.splice(index + 1, 0, message);
160
- this.messages = this.messages.concat([]);
161
- }
162
- }
163
- replacePendingMessage(pendingMessage, message) {
164
- this.messages = this.messages.filter((m) => {
165
- return m.id !== pendingMessage.id;
166
- });
167
- this.addMessage(message);
168
- }
169
- updatePendingMessageStatus(pendingMessage, status) {
170
- this.messages = this.messages.map((m) => {
171
- if ((0, PendingMessage_1.isPendingMessage)(m) && m.id === pendingMessage.id) {
172
- return m.clone({ status });
173
- }
174
- return m;
175
- });
176
- }
177
- addSubscription(subscription) {
178
- this.subscriptions.push(subscription);
179
- }
180
- getConnectionStatus() {
181
- return this.connectionStatus;
182
- }
183
- setConnectionStatus(status) {
184
- this.connectionStatus = status;
185
- this.subscriptions.forEach(subscription => {
186
- if (subscription.getStatus() !== 'cancelled') {
187
- subscription.setConnectionStatus(this.connectionStatus);
188
- }
189
- });
190
- }
191
- getChannelStatus() {
192
- return this.channelStatus;
193
- }
194
- handleChannelStateChange(stateChange) {
195
- this.channelStatus = stateChange.current;
196
- this.subscriptions.forEach(subscription => {
197
- if (subscription.getStatus() !== 'cancelled') {
198
- subscription.setChannelStatus(this.channelStatus);
199
- }
200
- });
201
- }
202
- getGetLatestMessagesStatus() {
203
- return this.getLatestMessagesStatus;
204
- }
205
- setGetLatestMessagesStatus(status) {
206
- this.getLatestMessagesStatus = status;
207
- this.subscriptions.forEach(subscription => {
208
- if (subscription.getStatus() !== 'cancelled') {
209
- subscription.setGetLatestMessagesStatus(this.getLatestMessagesStatus);
210
- }
211
- });
212
- }
213
- detachChannel(callback) {
214
- this.channel.detach(callback);
215
- }
216
- fireMessagesEventOnEachNonCancelledSubscription() {
217
- this.fireEventOnEachSubscription('messages', {
218
- messages: this.messages.concat()
219
- }, {
220
- whereStatusNotIn: ['cancelled']
221
- });
222
- }
223
- fireEventOnEachSubscription(name, data, options) {
224
- for (let i = 0, il = this.subscriptions.length; i < il; i++) {
225
- const subscription = this.subscriptions[i];
226
- if (options && Array.isArray(options.whereStatusNotIn) && options.whereStatusNotIn.includes(subscription.getStatus())) {
227
- continue;
228
- }
229
- subscription.fireEvent(name, data);
230
- }
231
- }
232
- cancelSubscription(subscription) {
233
- subscription.setStatus('cancelled');
234
- }
235
- cancelAllSubscriptions() {
236
- this.subscriptions.forEach(subscription => {
237
- this.cancelSubscription(subscription);
238
- });
239
- }
240
- }
241
- exports.default = CachedChat;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const uuid_1 = require("uuid");
27
+ const PendingMessage_1 = __importStar(require("./PendingMessage"));
28
+ const Message_1 = __importStar(require("./Message"));
29
+ class CachedChat {
30
+ constructor({ id, api, channel, connectionStatus, logger, debugLogger, debug }) {
31
+ this.getLatestMessagesStatus = 'initialized';
32
+ this.is_member = true;
33
+ this.id = id;
34
+ this.api = api;
35
+ this.messages = [];
36
+ this.onlineUsers = [];
37
+ this.subscriptions = [];
38
+ this.channel = channel;
39
+ this.channelStatus = this.channel.state;
40
+ this.connectionStatus = connectionStatus;
41
+ this.logger = logger;
42
+ this.debugLogger = debugLogger;
43
+ this.debug = debug;
44
+ this.disabled_at = null;
45
+ this.channel.on(this.handleChannelStateChange.bind(this));
46
+ this.channel.subscribe((message) => {
47
+ var _a;
48
+ if ((_a = this.debug) === null || _a === void 0 ? void 0 : _a.inboundAblyMessages) {
49
+ this.debugLogger.debug('[Ably] [InboundMessage]', message);
50
+ }
51
+ if (message.name === 'chat.message.created') {
52
+ this.addMessage(new Message_1.default({
53
+ id: message.data.id,
54
+ chatId: message.data.chat_id,
55
+ message: message.data.message,
56
+ senderId: message.data.sender_id,
57
+ sender: message.data.sender,
58
+ isAdminMessage: message.data.is_admin_message,
59
+ createdAt: new Date(message.data.created_at)
60
+ }));
61
+ this.fireMessagesEventOnEachNonCancelledSubscription();
62
+ }
63
+ });
64
+ }
65
+ sendMessage(message, options) {
66
+ return new Promise((resolve, reject) => {
67
+ const pendingMessage = new PendingMessage_1.default({
68
+ id: (0, uuid_1.v4)(),
69
+ message,
70
+ status: 'initialized',
71
+ options,
72
+ createdAt: new Date
73
+ });
74
+ this.addMessage(pendingMessage);
75
+ this.fireMessagesEventOnEachNonCancelledSubscription();
76
+ this.sendPendingMessage(pendingMessage)
77
+ .then(() => resolve())
78
+ .catch(err => reject(err));
79
+ });
80
+ }
81
+ reportChat(reason) {
82
+ return new Promise((resolve, reject) => {
83
+ this.api.reportChat(this.id, reason)
84
+ .then(() => resolve())
85
+ .catch(err => reject(err));
86
+ });
87
+ }
88
+ toggleChatEnabled() {
89
+ return new Promise((resolve, reject) => {
90
+ this.api.toggleChatEnabled(this.id)
91
+ .then(() => resolve())
92
+ .catch(err => reject(err));
93
+ });
94
+ }
95
+ sendPendingMessage(pendingMessage) {
96
+ return new Promise((resolve, reject) => {
97
+ if (!(0, PendingMessage_1.isPendingMessage)(pendingMessage) || pendingMessage.status === 'pending') {
98
+ return resolve();
99
+ }
100
+ this.updatePendingMessageStatus(pendingMessage, 'pending');
101
+ this.fireMessagesEventOnEachNonCancelledSubscription();
102
+ this.api.storeMessage(this.id, pendingMessage.message, pendingMessage.options).then(({ message, error }) => {
103
+ if (error) {
104
+ this.updatePendingMessageStatus(pendingMessage, 'failed');
105
+ this.fireMessagesEventOnEachNonCancelledSubscription();
106
+ return reject(new Error(error.message));
107
+ }
108
+ this.replacePendingMessage(pendingMessage, message);
109
+ this.fireMessagesEventOnEachNonCancelledSubscription();
110
+ resolve();
111
+ }).catch(err => {
112
+ this.updatePendingMessageStatus(pendingMessage, 'failed');
113
+ this.fireMessagesEventOnEachNonCancelledSubscription();
114
+ reject(err);
115
+ });
116
+ });
117
+ }
118
+ getLatestMessages() {
119
+ this.setGetLatestMessagesStatus('fetching');
120
+ this.api.getLatestMessages(this.id).then(({ messages, error }) => {
121
+ if (messages) {
122
+ messages.forEach(m => this.addMessage(m));
123
+ this.setGetLatestMessagesStatus('success');
124
+ this.fireMessagesEventOnEachNonCancelledSubscription();
125
+ return;
126
+ }
127
+ this.setGetLatestMessagesStatus('failed');
128
+ }).catch(err => {
129
+ this.setGetLatestMessagesStatus('failed');
130
+ });
131
+ }
132
+ markMessagesReadUpTo(message) {
133
+ return this.api.markChatMessagesReadUpTo(this.id, message);
134
+ }
135
+ setDisabledAt(disabled_at) {
136
+ this.disabled_at = disabled_at;
137
+ }
138
+ getDisabledAt() {
139
+ return this.disabled_at;
140
+ }
141
+ setIsMember(is_member) {
142
+ this.is_member = is_member;
143
+ }
144
+ getIsMember() {
145
+ return this.is_member;
146
+ }
147
+ addMessage(message) {
148
+ if (message instanceof PendingMessage_1.default) {
149
+ this.messages.push(message);
150
+ return;
151
+ }
152
+ let index = -1;
153
+ for (let i = (this.messages.length - 1); i >= 0; i--) {
154
+ if ((0, Message_1.isMessageAnd)(this.messages[i], m => m.id < message.id) && index === -1) {
155
+ index = i;
156
+ }
157
+ if ((0, Message_1.isMessageAnd)(this.messages[i], m => m.id === message.id)) {
158
+ return;
159
+ }
160
+ }
161
+ if (index === -1) {
162
+ this.messages.unshift(message);
163
+ this.messages = this.messages.concat([]);
164
+ }
165
+ else {
166
+ this.messages.splice(index + 1, 0, message);
167
+ this.messages = this.messages.concat([]);
168
+ }
169
+ }
170
+ replacePendingMessage(pendingMessage, message) {
171
+ this.messages = this.messages.filter((m) => {
172
+ return m.id !== pendingMessage.id;
173
+ });
174
+ this.addMessage(message);
175
+ }
176
+ updatePendingMessageStatus(pendingMessage, status) {
177
+ this.messages = this.messages.map((m) => {
178
+ if ((0, PendingMessage_1.isPendingMessage)(m) && m.id === pendingMessage.id) {
179
+ return m.clone({ status });
180
+ }
181
+ return m;
182
+ });
183
+ }
184
+ addSubscription(subscription) {
185
+ this.subscriptions.push(subscription);
186
+ }
187
+ getConnectionStatus() {
188
+ return this.connectionStatus;
189
+ }
190
+ setConnectionStatus(status) {
191
+ this.connectionStatus = status;
192
+ this.subscriptions.forEach(subscription => {
193
+ if (subscription.getStatus() !== 'cancelled') {
194
+ subscription.setConnectionStatus(this.connectionStatus);
195
+ }
196
+ });
197
+ }
198
+ getChannelStatus() {
199
+ return this.channelStatus;
200
+ }
201
+ handleChannelStateChange(stateChange) {
202
+ this.channelStatus = stateChange.current;
203
+ this.subscriptions.forEach(subscription => {
204
+ if (subscription.getStatus() !== 'cancelled') {
205
+ subscription.setChannelStatus(this.channelStatus);
206
+ }
207
+ });
208
+ }
209
+ getGetLatestMessagesStatus() {
210
+ return this.getLatestMessagesStatus;
211
+ }
212
+ setGetLatestMessagesStatus(status) {
213
+ this.getLatestMessagesStatus = status;
214
+ this.subscriptions.forEach(subscription => {
215
+ if (subscription.getStatus() !== 'cancelled') {
216
+ subscription.setGetLatestMessagesStatus(this.getLatestMessagesStatus);
217
+ }
218
+ });
219
+ }
220
+ detachChannel(callback) {
221
+ this.channel.detach(callback);
222
+ }
223
+ fireMessagesEventOnEachNonCancelledSubscription() {
224
+ this.fireEventOnEachSubscription('messages', {
225
+ messages: this.messages.concat()
226
+ }, {
227
+ whereStatusNotIn: ['cancelled']
228
+ });
229
+ }
230
+ fireEventOnEachSubscription(name, data, options) {
231
+ for (let i = 0, il = this.subscriptions.length; i < il; i++) {
232
+ const subscription = this.subscriptions[i];
233
+ if (options && Array.isArray(options.whereStatusNotIn) && options.whereStatusNotIn.includes(subscription.getStatus())) {
234
+ continue;
235
+ }
236
+ subscription.fireEvent(name, data);
237
+ }
238
+ }
239
+ cancelSubscription(subscription) {
240
+ subscription.setStatus('cancelled');
241
+ }
242
+ cancelAllSubscriptions() {
243
+ this.subscriptions.forEach(subscription => {
244
+ this.cancelSubscription(subscription);
245
+ });
246
+ }
247
+ }
248
+ exports.default = CachedChat;
242
249
  //# sourceMappingURL=CachedChat.js.map