Skip to content

Commit 2321e89

Browse files
committed
typing_status [nfc]: Use RealmStore for server settings
1 parent 49539ff commit 2321e89

File tree

4 files changed

+27
-46
lines changed

4 files changed

+27
-46
lines changed

lib/model/store.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,9 @@ class PerAccountStore extends PerAccountStoreBase with
494494
userSettings: initialSnapshot.userSettings,
495495
savedSnippets: SavedSnippetStoreImpl(
496496
core: core, savedSnippets: initialSnapshot.savedSnippets ?? []),
497-
typingNotifier: TypingNotifier(
498-
core: core,
499-
typingStoppedWaitPeriod: Duration(
500-
milliseconds: initialSnapshot.serverTypingStoppedWaitPeriodMilliseconds),
501-
typingStartedWaitPeriod: Duration(
502-
milliseconds: initialSnapshot.serverTypingStartedWaitPeriodMilliseconds),
503-
),
497+
typingNotifier: TypingNotifier(realm: realm),
504498
users: UserStoreImpl(core: core, initialSnapshot: initialSnapshot),
505-
typingStatus: TypingStatus(core: core,
506-
typingStartedExpiryPeriod: Duration(milliseconds: initialSnapshot.serverTypingStartedExpiryPeriodMilliseconds)),
499+
typingStatus: TypingStatus(realm: realm),
507500
presence: Presence(realm: realm,
508501
initial: initialSnapshot.presences),
509502
channels: channels,

lib/model/typing_status.dart

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ import '../api/model/events.dart';
66
import '../api/route/typing.dart';
77
import 'binding.dart';
88
import 'narrow.dart';
9-
import 'store.dart';
9+
import 'realm.dart';
1010

1111
/// The model for tracking the typing status organized by narrows.
1212
///
1313
/// Listeners are notified when a typist is added or removed from any narrow.
14-
class TypingStatus extends PerAccountStoreBase with ChangeNotifier {
15-
TypingStatus({
16-
required super.core,
17-
required this.typingStartedExpiryPeriod,
18-
});
19-
20-
final Duration typingStartedExpiryPeriod;
14+
class TypingStatus extends HasRealmStore with ChangeNotifier {
15+
TypingStatus({required super.realm});
2116

2217
Iterable<SendableNarrow> get debugActiveNarrows => _timerMapsByNarrow.keys;
2318

@@ -47,7 +42,7 @@ class TypingStatus extends PerAccountStoreBase with ChangeNotifier {
4742
final typistTimer = narrowTimerMap[typistUserId];
4843
final isNewTypist = typistTimer == null;
4944
typistTimer?.cancel();
50-
narrowTimerMap[typistUserId] = Timer(typingStartedExpiryPeriod, () {
45+
narrowTimerMap[typistUserId] = Timer(serverTypingStartedExpiryPeriod, () {
5146
if (_removeTypist(narrow, typistUserId)) {
5247
notifyListeners();
5348
}
@@ -92,15 +87,8 @@ class TypingStatus extends PerAccountStoreBase with ChangeNotifier {
9287
/// See also:
9388
/// * https://github.com/zulip/zulip/blob/52a9846cdf4abfbe937a94559690d508e95f4065/web/shared/src/typing_status.ts
9489
/// * https://zulip.readthedocs.io/en/latest/subsystems/typing-indicators.html
95-
class TypingNotifier extends PerAccountStoreBase {
96-
TypingNotifier({
97-
required super.core,
98-
required this.typingStoppedWaitPeriod,
99-
required this.typingStartedWaitPeriod,
100-
});
101-
102-
final Duration typingStoppedWaitPeriod;
103-
final Duration typingStartedWaitPeriod;
90+
class TypingNotifier extends HasRealmStore {
91+
TypingNotifier({required super.realm});
10492

10593
SendableNarrow? _currentDestination;
10694

@@ -137,7 +125,7 @@ class TypingNotifier extends PerAccountStoreBase {
137125
if (destination == _currentDestination) {
138126
// Nothing has really changed, except we may need
139127
// to send a ping to the server and extend out our idle time.
140-
if (_sinceLastPing!.elapsed > typingStartedWaitPeriod) {
128+
if (_sinceLastPing!.elapsed > serverTypingStartedWaitPeriod) {
141129
_actuallyPingServer();
142130
}
143131
_startOrExtendIdleTimer();
@@ -179,7 +167,7 @@ class TypingNotifier extends PerAccountStoreBase {
179167

180168
void _startOrExtendIdleTimer() {
181169
_idleTimer?.cancel();
182-
_idleTimer = Timer(typingStoppedWaitPeriod, _stopLastNotification);
170+
_idleTimer = Timer(serverTypingStoppedWaitPeriod, _stopLastNotification);
183171
}
184172

185173
void _actuallyPingServer() {

test/model/typing_status_test.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ void main() {
297297
const waitTime = Duration(milliseconds: 100);
298298
// [waitTime] should not be long enough
299299
// to trigger a "typing stopped" notice.
300-
assert(waitTime < model.typingStoppedWaitPeriod);
300+
assert(waitTime < store.serverTypingStoppedWaitPeriod);
301301

302302
async.elapse(waitTime);
303303
// t = 100ms: The idle timer is reset to typingStoppedWaitPeriod.
@@ -306,7 +306,7 @@ void main() {
306306
check(connection.lastRequest).isNull();
307307
check(async.pendingTimers).single;
308308

309-
async.elapse(model.typingStoppedWaitPeriod - const Duration(milliseconds: 1));
309+
async.elapse(store.serverTypingStoppedWaitPeriod - const Duration(milliseconds: 1));
310310
// t = typingStoppedWaitPeriod + 99ms:
311311
// Since the timer was reset at t = 100ms, the "typing stopped" notice has
312312
// not been sent yet.
@@ -326,12 +326,12 @@ void main() {
326326
const waitInterval = Duration(milliseconds: 2000);
327327
// [waitInterval] should not be long enough
328328
// to trigger a "typing stopped" notice.
329-
assert(waitInterval < model.typingStoppedWaitPeriod);
329+
assert(waitInterval < store.serverTypingStoppedWaitPeriod);
330330
// [waitInterval] should be short enough
331331
// that the loop below runs more than once.
332-
assert(waitInterval < model.typingStartedWaitPeriod);
332+
assert(waitInterval < store.serverTypingStartedWaitPeriod);
333333

334-
while (async.elapsed <= model.typingStartedWaitPeriod) {
334+
while (async.elapsed <= store.serverTypingStartedWaitPeriod) {
335335
// t <= typingStartedWaitPeriod: "Typing started" notices are throttled.
336336
model.keystroke(narrow);
337337
check(connection.lastRequest).isNull();
@@ -354,7 +354,7 @@ void main() {
354354
await prepareStartTyping(async);
355355

356356
connection.prepare(json: {});
357-
async.elapse(model.typingStoppedWaitPeriod);
357+
async.elapse(store.serverTypingStoppedWaitPeriod);
358358
checkTypingRequest(TypingOp.stop, narrow);
359359
check(async.pendingTimers).isEmpty();
360360
}));
@@ -406,7 +406,7 @@ void main() {
406406
const waitTime = Duration(milliseconds: 100);
407407
// [waitTime] should not be long enough
408408
// to trigger a "typing stopped" notice.
409-
assert(waitTime < model.typingStoppedWaitPeriod);
409+
assert(waitTime < store.serverTypingStoppedWaitPeriod);
410410

411411
// t = 0ms: Start typing. The idle timer is set to typingStoppedWaitPeriod.
412412
connection.prepare(json: {});
@@ -429,7 +429,7 @@ void main() {
429429
async.elapse(Duration.zero);
430430
check(async.pendingTimers).single;
431431

432-
async.elapse(model.typingStoppedWaitPeriod - waitTime);
432+
async.elapse(store.serverTypingStoppedWaitPeriod - waitTime);
433433
// t = typingStoppedPeriod:
434434
// Because the old timer has been canceled at t = 100ms,
435435
// no "typing stopped" notice has been sent yet.
@@ -452,7 +452,7 @@ void main() {
452452
const waitInterval = Duration(milliseconds: 2000);
453453
// [waitInterval] should not be long enough
454454
// to trigger a "typing stopped" notice.
455-
assert(waitInterval < model.typingStoppedWaitPeriod);
455+
assert(waitInterval < store.serverTypingStoppedWaitPeriod);
456456

457457
// t = 0ms: Start typing. The typing started time is set to 0ms.
458458
connection.prepare(json: {});
@@ -471,16 +471,16 @@ void main() {
471471
checkSetTypingStatusRequests(connection.takeRequests(),
472472
[(TypingOp.stop, topicNarrow), (TypingOp.start, dmNarrow)]);
473473

474-
while (async.elapsed <= model.typingStartedWaitPeriod) {
474+
while (async.elapsed <= store.serverTypingStartedWaitPeriod) {
475475
// t <= typingStartedWaitPeriod: "still typing" requests are throttled.
476476
model.keystroke(dmNarrow);
477477
check(connection.lastRequest).isNull();
478478

479479
async.elapse(waitInterval);
480480
}
481481

482-
assert(async.elapsed > model.typingStartedWaitPeriod);
483-
assert(async.elapsed <= model.typingStartedWaitPeriod + waitInterval);
482+
assert(async.elapsed > store.serverTypingStartedWaitPeriod);
483+
assert(async.elapsed <= store.serverTypingStartedWaitPeriod + waitInterval);
484484
// typingStartedWaitPeriod < t <= typingStartedWaitPeriod + waitInterval * 1:
485485
// The "still typing" requests are still throttled, because it hasn't
486486
// been a full typingStartedWaitPeriod since the last time we sent

test/widgets/compose_box_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ void main() {
761761
await checkStartTyping(tester, narrow);
762762

763763
connection.prepare(json: {});
764-
await tester.pump(store.typingNotifier.typingStoppedWaitPeriod);
764+
await tester.pump(store.serverTypingStoppedWaitPeriod);
765765
checkTypingRequest(TypingOp.stop, narrow);
766766
});
767767

@@ -773,7 +773,7 @@ void main() {
773773
await checkStartTyping(tester, narrow);
774774

775775
connection.prepare(json: {});
776-
await tester.pump(store.typingNotifier.typingStoppedWaitPeriod);
776+
await tester.pump(store.serverTypingStoppedWaitPeriod);
777777
checkTypingRequest(TypingOp.stop, narrow);
778778
});
779779

@@ -786,7 +786,7 @@ void main() {
786786
await checkStartTyping(tester, destinationNarrow);
787787

788788
connection.prepare(json: {});
789-
await tester.pump(store.typingNotifier.typingStoppedWaitPeriod);
789+
await tester.pump(store.serverTypingStoppedWaitPeriod);
790790
checkTypingRequest(TypingOp.stop, destinationNarrow);
791791
});
792792

@@ -866,7 +866,7 @@ void main() {
866866
await checkStartTyping(tester, narrow);
867867

868868
connection.prepare(json: {});
869-
await tester.pump(store.typingNotifier.typingStoppedWaitPeriod);
869+
await tester.pump(store.serverTypingStoppedWaitPeriod);
870870
checkTypingRequest(TypingOp.stop, narrow);
871871

872872
connection.prepare(json: {});
@@ -876,7 +876,7 @@ void main() {
876876

877877
// Ensures that a "typing stopped" notice is sent when the test ends.
878878
connection.prepare(json: {});
879-
await tester.pump(store.typingNotifier.typingStoppedWaitPeriod);
879+
await tester.pump(store.serverTypingStoppedWaitPeriod);
880880
checkTypingRequest(TypingOp.stop, narrow);
881881
});
882882

0 commit comments

Comments
 (0)