mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
config view repository
This commit is contained in:
parent
bfdc0b6ee9
commit
3bb12d50bd
@ -1,3 +1,7 @@
|
||||
import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
|
||||
|
||||
typedef ViewUpdatedCallback = void Function(View view);
|
||||
|
||||
abstract class IViewWatch {
|
||||
void startWatching({ViewUpdatedCallback? updatedCallback});
|
||||
|
||||
|
@ -1,23 +1,46 @@
|
||||
import 'package:app_flowy/workspace/domain/i_view.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-observable/subject.pb.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_infra/flowy_logger.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-observable/subject.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-workspace/observable.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-workspace/view_query.pb.dart';
|
||||
import 'package:flowy_sdk/rust_stream.dart';
|
||||
|
||||
import 'package:app_flowy/workspace/domain/i_view.dart';
|
||||
|
||||
class ViewRepository {
|
||||
String viewId;
|
||||
ViewRepository({
|
||||
required this.viewId,
|
||||
});
|
||||
|
||||
Future<Either<View, WorkspaceError>> getView() {
|
||||
final request = QueryViewRequest.create()..viewId = viewId;
|
||||
return WorkspaceEventReadView(request).send();
|
||||
}
|
||||
}
|
||||
|
||||
class ViewWatchRepository {
|
||||
StreamSubscription<ObservableSubject>? _subscription;
|
||||
ViewUpdatedCallback? _updatedCallback;
|
||||
String viewId;
|
||||
late ViewRepository _repo;
|
||||
ViewWatchRepository({
|
||||
required this.viewId,
|
||||
});
|
||||
}) {
|
||||
_repo = ViewRepository(viewId: viewId);
|
||||
}
|
||||
|
||||
void startWatching({
|
||||
ViewUpdatedCallback? updatedCallback,
|
||||
}) {
|
||||
_addViewCallback = addViewCallback;
|
||||
_updatedCallback = updatedCallback;
|
||||
_subscription = RustStreamReceiver.listen((observable) {
|
||||
if (observable.subjectId != appId) {
|
||||
if (observable.subjectId != viewId) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -27,4 +50,26 @@ class ViewWatchRepository {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _handleObservableType(WorkspaceObservable ty) {
|
||||
switch (ty) {
|
||||
case WorkspaceObservable.ViewUpdateDesc:
|
||||
if (_updatedCallback == null) {
|
||||
return;
|
||||
}
|
||||
_repo.getView().then((result) {
|
||||
result.fold(
|
||||
(view) => _updatedCallback!(view),
|
||||
(error) => Log.error(error),
|
||||
);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> close() async {
|
||||
await _subscription?.cancel();
|
||||
}
|
||||
}
|
||||
|
@ -152,6 +152,40 @@ class WorkspaceEventCreateView {
|
||||
}
|
||||
}
|
||||
|
||||
class WorkspaceEventReadView {
|
||||
QueryViewRequest request;
|
||||
WorkspaceEventReadView(this.request);
|
||||
|
||||
Future<Either<View, WorkspaceError>> send() {
|
||||
final request = FFIRequest.create()
|
||||
..event = WorkspaceEvent.ReadView.toString()
|
||||
..payload = requestToBytes(this.request);
|
||||
|
||||
return Dispatch.asyncRequest(request)
|
||||
.then((bytesResult) => bytesResult.fold(
|
||||
(okBytes) => left(View.fromBuffer(okBytes)),
|
||||
(errBytes) => right(WorkspaceError.fromBuffer(errBytes)),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class WorkspaceEventUpdateView {
|
||||
UpdateViewRequest request;
|
||||
WorkspaceEventUpdateView(this.request);
|
||||
|
||||
Future<Either<Unit, WorkspaceError>> send() {
|
||||
final request = FFIRequest.create()
|
||||
..event = WorkspaceEvent.UpdateView.toString()
|
||||
..payload = requestToBytes(this.request);
|
||||
|
||||
return Dispatch.asyncRequest(request)
|
||||
.then((bytesResult) => bytesResult.fold(
|
||||
(bytes) => left(unit),
|
||||
(errBytes) => right(WorkspaceError.fromBuffer(errBytes)),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class UserEventGetStatus {
|
||||
UserEventGetStatus();
|
||||
|
||||
|
@ -33,6 +33,6 @@ pub enum WorkspaceEvent {
|
||||
ReadView = 202,
|
||||
|
||||
#[display(fmt = "UpdateView")]
|
||||
#[event(input = "UpdateViewRequest", output = "View")]
|
||||
#[event(input = "UpdateViewRequest")]
|
||||
UpdateView = 203,
|
||||
}
|
||||
|
@ -47,4 +47,6 @@ pub fn create(user: Arc<dyn WorkspaceUser>, database: Arc<dyn WorkspaceDatabase>
|
||||
.event(WorkspaceEvent::CreateApp, create_app)
|
||||
.event(WorkspaceEvent::GetApp, get_app)
|
||||
.event(WorkspaceEvent::CreateView, create_view)
|
||||
.event(WorkspaceEvent::ReadView, read_view)
|
||||
.event(WorkspaceEvent::UpdateView, update_view)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user