forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsent_messages.js
184 lines (141 loc) · 4.31 KB
/
sent_messages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var sent_messages = (function () {
var exports = {};
exports.messages = {};
exports.reset_id_state = function () {
exports.next_local_id = 0;
};
exports.get_new_local_id = function () {
exports.next_local_id += 1;
var local_id = exports.next_local_id;
return 'loc-' + local_id.toString();
};
function report_send_time(send_time, receive_time,
locally_echoed, rendered_changed) {
var data = {
time: send_time.toString(),
received: receive_time.toString(),
locally_echoed: locally_echoed,
};
if (locally_echoed) {
data.rendered_content_disparity = rendered_changed;
}
channel.post({
url: '/json/report_send_time',
data: data,
});
}
exports.start_tracking_message = function (opts) {
var local_id = opts.local_id;
if (!opts.local_id) {
blueslip.error('You must supply a local_id');
return;
}
if (exports.messages[local_id] !== undefined) {
blueslip.error('We are re-using a local_id');
return;
}
var state = exports.message_state(opts);
exports.messages[local_id] = state;
};
exports.message_state = function (opts) {
var self = {};
self.data = {};
self.data.start = new Date();
self.data.local_id = opts.local_id;
self.data.locally_echoed = opts.locally_echoed;
self.data.received = undefined;
self.data.send_finished = undefined;
self.data.rendered_content_disparity = false;
self.start_resend = function () {
self.data.start = new Date();
self.data.received = undefined;
self.data.send_finished = undefined;
self.data.rendered_content_disparity = false;
};
self.maybe_restart_event_loop = function () {
if (self.data.received) {
// We got our event, no need to do anything
return;
}
blueslip.log("Restarting get_events due to " +
"delayed receipt of sent message " +
self.data.local_id);
server_events.restart_get_events();
};
self.maybe_report_send_times = function () {
if (!self.ready()) {
return;
}
var data = self.data;
report_send_time(data.send_finished - data.start,
data.received - data.start,
data.locally_echoed,
data.rendered_content_disparity);
};
self.report_event_received = function () {
self.data.received = new Date();
self.maybe_report_send_times();
};
self.mark_disparity = function () {
self.data.rendered_content_disparity = true;
};
self.report_server_ack = function () {
self.data.send_finished = new Date();
self.maybe_report_send_times();
// We only start our timer for events coming in here,
// since it's plausible the server rejected our message,
// or took a while to process it, but there is nothing
// wrong with our event loop.
if (!self.data.received) {
setTimeout(self.maybe_restart_event_loop, 5000);
}
};
self.ready = function () {
return (self.data.send_finished !== undefined) &&
(self.data.received !== undefined);
};
return self;
};
exports.get_message_state = function (local_id) {
var state = exports.messages[local_id];
if (!state) {
blueslip.warn('Unknown local_id: ' + local_id);
}
return state;
};
exports.mark_disparity = function (local_id) {
var state = exports.get_message_state(local_id);
if (!state) {
return;
}
state.mark_disparity();
};
exports.report_event_received = function (local_id) {
var state = exports.get_message_state(local_id);
if (!state) {
return;
}
state.report_event_received();
};
exports.start_resend = function (local_id) {
var state = exports.get_message_state(local_id);
if (!state) {
return;
}
state.start_resend();
};
exports.report_server_ack = function (local_id) {
var state = exports.get_message_state(local_id);
if (!state) {
return;
}
state.report_server_ack();
};
exports.initialize = function () {
exports.reset_id_state();
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = sent_messages;
}