chore: notification parser (#1745)

This commit is contained in:
Nathan.fooo 2023-01-27 22:57:23 +08:00 committed by GitHub
parent 7a750e5255
commit 4fb2afe82e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 143 additions and 78 deletions

View File

@ -10,22 +10,22 @@ import 'notification_helper.dart';
// GridPB
typedef GridNotificationCallback = void Function(
GridDartNotification, Either<Uint8List, FlowyError>);
GridNotification, Either<Uint8List, FlowyError>);
class GridNotificationParser
extends NotificationParser<GridDartNotification, FlowyError> {
extends NotificationParser<GridNotification, FlowyError> {
GridNotificationParser(
{String? id, required GridNotificationCallback callback})
: super(
id: id,
callback: callback,
tyParser: (ty) => GridDartNotification.valueOf(ty),
tyParser: (ty) => GridNotification.valueOf(ty),
errorParser: (bytes) => FlowyError.fromBuffer(bytes),
);
}
typedef GridNotificationHandler = Function(
GridDartNotification ty, Either<Uint8List, FlowyError> result);
GridNotification ty, Either<Uint8List, FlowyError> result);
class GridNotificationListener {
StreamSubscription<SubscribeObject>? _subscription;

View File

@ -32,18 +32,18 @@ class BoardListener {
}
void _handler(
GridDartNotification ty,
GridNotification ty,
Either<Uint8List, FlowyError> result,
) {
switch (ty) {
case GridDartNotification.DidUpdateGroupView:
case GridNotification.DidUpdateGroupView:
result.fold(
(payload) => _groupUpdateNotifier?.value =
left(GroupViewChangesetPB.fromBuffer(payload)),
(error) => _groupUpdateNotifier?.value = right(error),
);
break;
case GridDartNotification.DidGroupByNewField:
case GridNotification.DidGroupByNewField:
result.fold(
(payload) => _groupByNewFieldNotifier?.value =
left(GroupViewChangesetPB.fromBuffer(payload).newGroups),

View File

@ -27,11 +27,11 @@ class GroupListener {
}
void _handler(
GridDartNotification ty,
GridNotification ty,
Either<Uint8List, FlowyError> result,
) {
switch (ty) {
case GridDartNotification.DidUpdateGroup:
case GridNotification.DidUpdateGroup:
result.fold(
(payload) => _groupNotifier?.value =
left(GroupRowsNotificationPB.fromBuffer(payload)),

View File

@ -22,9 +22,9 @@ class CellListener {
objectId: "$rowId:$fieldId", handler: _handler);
}
void _handler(GridDartNotification ty, Either<Uint8List, FlowyError> result) {
void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) {
switch (ty) {
case GridDartNotification.DidUpdateCell:
case GridNotification.DidUpdateCell:
result.fold(
(payload) => _updateCellNotifier?.value = left(unit),
(error) => _updateCellNotifier?.value = right(error),

View File

@ -27,11 +27,11 @@ class SingleFieldListener {
}
void _handler(
GridDartNotification ty,
GridNotification ty,
Either<Uint8List, FlowyError> result,
) {
switch (ty) {
case GridDartNotification.DidUpdateField:
case GridNotification.DidUpdateField:
result.fold(
(payload) =>
_updateFieldNotifier?.value = left(FieldPB.fromBuffer(payload)),

View File

@ -25,9 +25,9 @@ class GridFieldsListener {
);
}
void _handler(GridDartNotification ty, Either<Uint8List, FlowyError> result) {
void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) {
switch (ty) {
case GridDartNotification.DidUpdateGridFields:
case GridNotification.DidUpdateGridFields:
result.fold(
(payload) => updateFieldsNotifier?.value =
left(GridFieldChangesetPB.fromBuffer(payload)),

View File

@ -30,11 +30,11 @@ class FiltersListener {
}
void _handler(
GridDartNotification ty,
GridNotification ty,
Either<Uint8List, FlowyError> result,
) {
switch (ty) {
case GridDartNotification.DidUpdateFilter:
case GridNotification.DidUpdateFilter:
result.fold(
(payload) => _filterNotifier?.value =
left(FilterChangesetNotificationPB.fromBuffer(payload)),
@ -100,11 +100,11 @@ class FilterListener {
}
void _handler(
GridDartNotification ty,
GridNotification ty,
Either<Uint8List, FlowyError> result,
) {
switch (ty) {
case GridDartNotification.DidUpdateFilter:
case GridNotification.DidUpdateFilter:
result.fold(
(payload) => handleChangeset(
FilterChangesetNotificationPB.fromBuffer(payload)),

View File

@ -23,9 +23,9 @@ class RowListener {
_listener = GridNotificationListener(objectId: rowId, handler: _handler);
}
void _handler(GridDartNotification ty, Either<Uint8List, FlowyError> result) {
void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) {
switch (ty) {
case GridDartNotification.DidUpdateRow:
case GridNotification.DidUpdateRow:
result.fold(
(payload) =>
updateRowNotifier?.value = left(RowPB.fromBuffer(payload)),

View File

@ -24,9 +24,9 @@ class SettingListener {
_listener = GridNotificationListener(objectId: gridId, handler: _handler);
}
void _handler(GridDartNotification ty, Either<Uint8List, FlowyError> result) {
void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) {
switch (ty) {
case GridDartNotification.DidUpdateGridSetting:
case GridNotification.DidUpdateGridSetting:
result.fold(
(payload) => _updateSettingNotifier?.value = left(
GridSettingPB.fromBuffer(payload),

View File

@ -27,11 +27,11 @@ class SortsListener {
}
void _handler(
GridDartNotification ty,
GridNotification ty,
Either<Uint8List, FlowyError> result,
) {
switch (ty) {
case GridDartNotification.DidUpdateSort:
case GridNotification.DidUpdateSort:
result.fold(
(payload) => _notifier?.value =
left(SortChangesetNotificationPB.fromBuffer(payload)),

View File

@ -51,30 +51,30 @@ class GridViewListener {
_reorderSingleRow?.addPublishListener(onReorderSingleRow);
}
void _handler(GridDartNotification ty, Either<Uint8List, FlowyError> result) {
void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) {
switch (ty) {
case GridDartNotification.DidUpdateGridViewRowsVisibility:
case GridNotification.DidUpdateGridViewRowsVisibility:
result.fold(
(payload) => _rowsVisibility?.value =
left(GridRowsVisibilityChangesetPB.fromBuffer(payload)),
(error) => _rowsVisibility?.value = right(error),
);
break;
case GridDartNotification.DidUpdateGridViewRows:
case GridNotification.DidUpdateGridViewRows:
result.fold(
(payload) => _rowsNotifier?.value =
left(GridViewRowsChangesetPB.fromBuffer(payload)),
(error) => _rowsNotifier?.value = right(error),
);
break;
case GridDartNotification.DidReorderRows:
case GridNotification.DidReorderRows:
result.fold(
(payload) => _reorderAllRows?.value =
left(ReorderAllRowsPB.fromBuffer(payload).rowOrders),
(error) => _reorderAllRows?.value = right(error),
);
break;
case GridDartNotification.DidReorderSingleRow:
case GridNotification.DidReorderSingleRow:
result.fold(
(payload) => _reorderSingleRow?.value =
left(ReorderSingleRowPB.fromBuffer(payload)),

View File

@ -16,10 +16,14 @@ function App() {
name: "abc",
});
let listener = await new UserNotificationListener("", (userProfile) => {
let listener = await new UserNotificationListener({
onUserSignIn: (userProfile) => {
console.log(userProfile);
}, onProfileUpdate(userProfile) {
console.log(userProfile);
// stop listening the changes
listener.stop();
});
}});
listener.start();

View File

@ -0,0 +1,26 @@
import { GridNotification } from "../../../../../services/backend";
import { NotificationParser, OnNotificationError } from "../../../../../services/backend/notifications/parser";
declare type GridNotificationCallback = (ty: GridNotification, payload: Uint8Array) => void;
export class GridNotificationParser extends NotificationParser<GridNotification> {
constructor(params: { id?: String; callback: GridNotificationCallback; onError?: OnNotificationError }) {
super(
params.callback,
(ty) => {
let notification = GridNotification[ty];
if (isGridNotification(notification)) {
return GridNotification[notification];
} else {
return GridNotification.Unknown;
}
},
params.id,
params.onError
);
}
}
const isGridNotification = (notification: string): notification is keyof typeof GridNotification => {
return Object.values(GridNotification).indexOf(notification) !== -1;
};

View File

@ -1,13 +1,12 @@
import { Result } from "ts-results/result";
import { UserNotification, FlowyError } from "../../../../../services/backend";
import { UserNotification } from "../../../../../services/backend";
import { NotificationParser, OnNotificationError } from "../../../../../services/backend/notifications/parser";
declare type UserNotificationCallback = (ty: UserNotification, payload: Uint8Array) => void;
export class UserNotificationParser extends NotificationParser<UserNotification> {
constructor(callback: UserNotificationCallback, id?: String, onError?: OnNotificationError) {
constructor(params: { id?: String; callback: UserNotificationCallback; onError?: OnNotificationError }) {
super(
callback,
params.callback,
(ty) => {
let notification = UserNotification[ty];
if (isUserNotification(notification)) {
@ -16,8 +15,8 @@ export class UserNotificationParser extends NotificationParser<UserNotification>
return UserNotification.Unknown;
}
},
id,
onError
params.id,
params.onError
);
}
}

View File

@ -3,32 +3,40 @@ import { AFNotificationListener, OnNotificationError } from "../../../../../serv
import { UserNotificationParser } from "./parser";
declare type OnUserProfileUpdate = (userProfile: UserProfilePB) => void;
declare type OnUserSignIn = (userProfile: UserProfilePB) => void;
export class UserNotificationListener extends AFNotificationListener<UserNotification> {
onProfileUpdate?: OnUserProfileUpdate;
onUserSignIn?: OnUserSignIn;
constructor(userId?: String, onProfileUpdate?: OnUserProfileUpdate, onError?: OnNotificationError) {
let parser = new UserNotificationParser(
(notification, payload) => {
constructor(params: {
userId?: String;
onUserSignIn?: OnUserSignIn;
onProfileUpdate?: OnUserProfileUpdate;
onError?: OnNotificationError;
}) {
let parser = new UserNotificationParser({
callback: (notification, payload) => {
switch (notification) {
case UserNotification.UserAuthChanged:
break;
case UserNotification.UserProfileUpdated:
this.onProfileUpdate?.(UserProfilePB.deserializeBinary(payload));
break;
case UserNotification.UserUnauthorized:
break;
case UserNotification.UserSignIn:
let userProfile = UserProfilePB.deserializeBinary(payload);
this.onProfileUpdate?.(userProfile);
this.onUserSignIn?.(UserProfilePB.deserializeBinary(payload));
break;
default:
break;
}
},
userId,
onError
);
id: params.userId,
onError: params.onError,
});
super(parser);
this.onProfileUpdate = onProfileUpdate;
this.onProfileUpdate = params.onProfileUpdate;
this.onUserSignIn = params.onUserSignIn;
}
}

View File

@ -0,0 +1,26 @@
import { FolderNotification } from "../../../../services/backend";
import { NotificationParser, OnNotificationError } from "../../../../services/backend/notifications/parser";
declare type FolderNotificationCallback = (ty: FolderNotification, payload: Uint8Array) => void;
export class FolderNotificationParser extends NotificationParser<FolderNotification> {
constructor(params: { id?: String; callback: FolderNotificationCallback; onError?: OnNotificationError }) {
super(
params.callback,
(ty) => {
let notification = FolderNotification[ty];
if (isFolderNotification(notification)) {
return FolderNotification[notification];
} else {
return FolderNotification.Unknown;
}
},
params.id,
params.onError
);
}
}
const isFolderNotification = (notification: string): notification is keyof typeof FolderNotification => {
return Object.values(FolderNotification).indexOf(notification) !== -1;
};

View File

@ -162,8 +162,8 @@ pub enum GridEvent {
/// [UpdateSelectOption] event is used to update a FieldTypeOptionData whose field_type is
/// FieldType::SingleSelect or FieldType::MultiSelect.
///
/// This event may trigger the GridDartNotification::DidUpdateCell event.
/// For example, GridDartNotification::DidUpdateCell will be triggered if the [SelectOptionChangesetPB]
/// This event may trigger the GridNotification::DidUpdateCell event.
/// For example, GridNotification::DidUpdateCell will be triggered if the [SelectOptionChangesetPB]
/// carries a change that updates the name of the option.
#[event(input = "SelectOptionChangesetPB")]
UpdateSelectOption = 32,

View File

@ -3,7 +3,7 @@ use flowy_notification::NotificationBuilder;
const OBSERVABLE_CATEGORY: &str = "Grid";
#[derive(ProtoBuf_Enum, Debug)]
pub enum GridDartNotification {
pub enum GridNotification {
Unknown = 0,
DidCreateBlock = 11,
DidUpdateGridViewRows = 20,
@ -22,19 +22,19 @@ pub enum GridDartNotification {
DidUpdateGridSetting = 70,
}
impl std::default::Default for GridDartNotification {
impl std::default::Default for GridNotification {
fn default() -> Self {
GridDartNotification::Unknown
GridNotification::Unknown
}
}
impl std::convert::From<GridDartNotification> for i32 {
fn from(notification: GridDartNotification) -> Self {
impl std::convert::From<GridNotification> for i32 {
fn from(notification: GridNotification) -> Self {
notification as i32
}
}
#[tracing::instrument(level = "trace")]
pub fn send_notification(id: &str, ty: GridDartNotification) -> NotificationBuilder {
pub fn send_notification(id: &str, ty: GridNotification) -> NotificationBuilder {
NotificationBuilder::new(id, ty, OBSERVABLE_CATEGORY)
}

View File

@ -1,6 +1,6 @@
use crate::entities::{CellChangesetPB, InsertedRowPB, UpdatedRowPB};
use crate::manager::GridUser;
use crate::notification::{send_notification, GridDartNotification};
use crate::notification::{send_notification, GridNotification};
use crate::services::block_editor::{GridBlockRevisionEditor, GridBlockRevisionMergeable};
use crate::services::persistence::block_index::BlockIndexCache;
use crate::services::persistence::rev_sqlite::{
@ -262,7 +262,7 @@ impl GridBlockManager {
async fn notify_did_update_cell(&self, changeset: CellChangesetPB) -> FlowyResult<()> {
let id = format!("{}:{}", changeset.row_id, changeset.field_id);
send_notification(&id, GridDartNotification::DidUpdateCell).send();
send_notification(&id, GridNotification::DidUpdateCell).send();
Ok(())
}
}

View File

@ -1,7 +1,7 @@
use crate::entities::CellPathParams;
use crate::entities::*;
use crate::manager::GridUser;
use crate::notification::{send_notification, GridDartNotification};
use crate::notification::{send_notification, GridNotification};
use crate::services::block_manager::GridBlockManager;
use crate::services::cell::{
apply_cell_data_changeset, decode_type_cell_data, stringify_cell_data, AnyTypeCache, AtomicCellDataCache,
@ -112,7 +112,7 @@ impl GridRevisionEditor {
self.view_manager.close(&self.grid_id).await;
}
/// Save the type-option data to disk and send a `GridDartNotification::DidUpdateField` notification
/// Save the type-option data to disk and send a `GridNotification::DidUpdateField` notification
/// to dart side.
///
/// It will do nothing if the passed-in type_option_data is empty
@ -852,7 +852,7 @@ impl GridRevisionEditor {
let notified_changeset = GridFieldChangesetPB::update(&self.grid_id, vec![updated_field.clone()]);
self.notify_did_update_grid(notified_changeset).await?;
send_notification(field_id, GridDartNotification::DidUpdateField)
send_notification(field_id, GridNotification::DidUpdateField)
.payload(updated_field)
.send();
}
@ -861,7 +861,7 @@ impl GridRevisionEditor {
}
async fn notify_did_update_grid(&self, changeset: GridFieldChangesetPB) -> FlowyResult<()> {
send_notification(&self.grid_id, GridDartNotification::DidUpdateGridFields)
send_notification(&self.grid_id, GridNotification::DidUpdateGridFields)
.payload(changeset)
.send();
Ok(())

View File

@ -1,5 +1,5 @@
use crate::entities::{GridRowsVisibilityChangesetPB, ReorderAllRowsPB, ReorderSingleRowPB};
use crate::notification::{send_notification, GridDartNotification};
use crate::notification::{send_notification, GridNotification};
use crate::services::filter::FilterResultNotification;
use crate::services::sort::{ReorderAllRowsResult, ReorderSingleRowResult};
use async_stream::stream;
@ -37,18 +37,15 @@ impl GridViewChangedReceiverRunner {
invisible_rows: notification.invisible_rows,
};
send_notification(
&changeset.view_id,
GridDartNotification::DidUpdateGridViewRowsVisibility,
)
.payload(changeset)
.send()
send_notification(&changeset.view_id, GridNotification::DidUpdateGridViewRowsVisibility)
.payload(changeset)
.send()
}
GridViewChanged::ReorderAllRowsNotification(notification) => {
let row_orders = ReorderAllRowsPB {
row_orders: notification.row_orders,
};
send_notification(&notification.view_id, GridDartNotification::DidReorderRows)
send_notification(&notification.view_id, GridNotification::DidReorderRows)
.payload(row_orders)
.send()
}
@ -58,7 +55,7 @@ impl GridViewChangedReceiverRunner {
old_index: notification.old_index as i32,
new_index: notification.new_index as i32,
};
send_notification(&notification.view_id, GridDartNotification::DidReorderSingleRow)
send_notification(&notification.view_id, GridNotification::DidReorderSingleRow)
.payload(reorder_row)
.send()
}

View File

@ -1,5 +1,5 @@
use crate::entities::*;
use crate::notification::{send_notification, GridDartNotification};
use crate::notification::{send_notification, GridNotification};
use crate::services::block_manager::GridBlockEvent;
use crate::services::cell::{AtomicCellDataCache, TypeCellData};
use crate::services::field::{RowSingleCellData, TypeOptionCellDataHandler};
@ -184,7 +184,7 @@ impl GridViewRevisionEditor {
}
};
send_notification(&self.view_id, GridDartNotification::DidUpdateGridViewRows)
send_notification(&self.view_id, GridNotification::DidUpdateGridViewRows)
.payload(changeset)
.send();
}
@ -616,7 +616,7 @@ impl GridViewRevisionEditor {
debug_assert!(!changeset.is_empty());
if !changeset.is_empty() {
send_notification(&changeset.view_id, GridDartNotification::DidGroupByNewField)
send_notification(&changeset.view_id, GridNotification::DidGroupByNewField)
.payload(changeset)
.send();
}
@ -630,33 +630,33 @@ impl GridViewRevisionEditor {
async fn notify_did_update_setting(&self) {
let setting = self.get_view_setting().await;
send_notification(&self.view_id, GridDartNotification::DidUpdateGridSetting)
send_notification(&self.view_id, GridNotification::DidUpdateGridSetting)
.payload(setting)
.send();
}
pub async fn notify_did_update_group_rows(&self, payload: GroupRowsNotificationPB) {
send_notification(&payload.group_id, GridDartNotification::DidUpdateGroup)
send_notification(&payload.group_id, GridNotification::DidUpdateGroup)
.payload(payload)
.send();
}
pub async fn notify_did_update_filter(&self, notification: FilterChangesetNotificationPB) {
send_notification(&notification.view_id, GridDartNotification::DidUpdateFilter)
send_notification(&notification.view_id, GridNotification::DidUpdateFilter)
.payload(notification)
.send();
}
pub async fn notify_did_update_sort(&self, notification: SortChangesetNotificationPB) {
if !notification.is_empty() {
send_notification(&notification.view_id, GridDartNotification::DidUpdateSort)
send_notification(&notification.view_id, GridNotification::DidUpdateSort)
.payload(notification)
.send();
}
}
async fn notify_did_update_view(&self, changeset: GroupViewChangesetPB) {
send_notification(&self.view_id, GridDartNotification::DidUpdateGroupView)
send_notification(&self.view_id, GridNotification::DidUpdateGroupView)
.payload(changeset)
.send();
}