mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
fix: doc state refresh (#5086)
This commit is contained in:
parent
83b18c4825
commit
f0d8eee8a0
@ -16,6 +16,7 @@ import 'package:appflowy/user/application/auth/auth_service.dart';
|
||||
import 'package:appflowy/util/color_generator/color_generator.dart';
|
||||
import 'package:appflowy/util/color_to_hex_string.dart';
|
||||
import 'package:appflowy/util/debounce.dart';
|
||||
import 'package:appflowy/util/throttle.dart';
|
||||
import 'package:appflowy/workspace/application/view/view_listener.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-document/protobuf.dart';
|
||||
@ -66,7 +67,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
StreamSubscription? _transactionSubscription;
|
||||
|
||||
final _updateSelectionDebounce = Debounce();
|
||||
final _syncDocDebounce = Debounce();
|
||||
final _syncThrottle = Throttler(duration: const Duration(milliseconds: 500));
|
||||
|
||||
bool get isLocalMode {
|
||||
final userProfilePB = state.userProfilePB;
|
||||
@ -155,7 +156,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
/// subscribe to the document content change
|
||||
void _onDocumentChanged() {
|
||||
_documentListener.start(
|
||||
onDocEventUpdate: _debounceSyncDoc,
|
||||
onDocEventUpdate: _throttleSyncDoc,
|
||||
onDocAwarenessUpdate: _onAwarenessStatesUpdate,
|
||||
);
|
||||
|
||||
@ -290,8 +291,8 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
_updateSelectionDebounce.call(_onSelectionUpdate);
|
||||
}
|
||||
|
||||
void _debounceSyncDoc(DocEventPB docEvent) {
|
||||
_syncDocDebounce.call(() {
|
||||
void _throttleSyncDoc(DocEventPB docEvent) {
|
||||
_syncThrottle.call(() {
|
||||
_onDocumentStateUpdate(docEvent);
|
||||
});
|
||||
}
|
||||
|
@ -77,17 +77,30 @@ class DocumentCollabAdapter {
|
||||
|
||||
final ops = diffNodes(editorState.document.root, document.root);
|
||||
if (ops.isEmpty) {
|
||||
debugPrint('[collab] received empty ops');
|
||||
Log.info('Doc diff, no changes');
|
||||
return;
|
||||
}
|
||||
|
||||
debugPrint('[collab] received ops: $ops');
|
||||
prettyPrintJson(ops.map((op) => op.toJson()).toList());
|
||||
|
||||
final transaction = editorState.transaction;
|
||||
for (final op in ops) {
|
||||
transaction.add(op);
|
||||
}
|
||||
await editorState.apply(transaction, isRemote: true);
|
||||
|
||||
// Use for debugging, DO NOT REMOVE
|
||||
// assert(() {
|
||||
// final local = editorState.document.root.toJson();
|
||||
// final remote = document.root.toJson();
|
||||
// if (!const DeepCollectionEquality().equals(local, remote)) {
|
||||
// Log.error('Invalid diff status');
|
||||
// Log.error('Local: $local');
|
||||
// Log.error('Remote: $remote');
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
// }());
|
||||
}
|
||||
|
||||
Future<void> _syncUpdated(
|
||||
|
@ -1,7 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Debounce {
|
||||
Debounce({
|
||||
this.duration = const Duration(milliseconds: 1000),
|
||||
@ -10,8 +8,9 @@ class Debounce {
|
||||
final Duration duration;
|
||||
Timer? _timer;
|
||||
|
||||
void call(VoidCallback action) {
|
||||
void call(Function action) {
|
||||
dispose();
|
||||
|
||||
_timer = Timer(duration, () {
|
||||
action();
|
||||
});
|
||||
|
23
frontend/appflowy_flutter/lib/util/throttle.dart
Normal file
23
frontend/appflowy_flutter/lib/util/throttle.dart
Normal file
@ -0,0 +1,23 @@
|
||||
import 'dart:async';
|
||||
|
||||
class Throttler {
|
||||
Throttler({
|
||||
this.duration = const Duration(milliseconds: 1000),
|
||||
});
|
||||
|
||||
final Duration duration;
|
||||
Timer? _timer;
|
||||
|
||||
void call(Function callback) {
|
||||
if (_timer?.isActive ?? false) return;
|
||||
|
||||
_timer = Timer(duration, () {
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
}
|
||||
}
|
@ -53,8 +53,8 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "."
|
||||
ref: a9c77a9
|
||||
resolved-ref: a9c77a918b05c02f134128813b1c04a5b6856ae4
|
||||
ref: c8cd407
|
||||
resolved-ref: c8cd4071e36ca6b1fb6d9ef803abb61e9a743c8b
|
||||
url: "https://github.com/AppFlowy-IO/appflowy-editor.git"
|
||||
source: git
|
||||
version: "2.3.3"
|
||||
|
@ -169,7 +169,7 @@ dependency_overrides:
|
||||
appflowy_editor:
|
||||
git:
|
||||
url: https://github.com/AppFlowy-IO/appflowy-editor.git
|
||||
ref: "a9c77a9"
|
||||
ref: "c8cd407"
|
||||
|
||||
sheet:
|
||||
git:
|
||||
|
Loading…
Reference in New Issue
Block a user