From d649eb5f4eada6270da56802fc2343ad5aea50ad Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Tue, 14 Dec 2021 20:44:49 -0300 Subject: [PATCH] refactor: change the room interface --- app/definitions/IRoom.ts | 5 +++- app/views/RoomView/index.tsx | 52 +++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/app/definitions/IRoom.ts b/app/definitions/IRoom.ts index 54d70fe5f..ed1852181 100644 --- a/app/definitions/IRoom.ts +++ b/app/definitions/IRoom.ts @@ -1,3 +1,6 @@ +import { Observable } from 'rxjs'; +import Model from '@nozbe/watermelondb/Model'; + import { IRocketChatRecord } from './IRocketChatRecord'; export enum RoomType { @@ -23,7 +26,7 @@ export interface IRoom extends IRocketChatRecord { visitor?: boolean; autoTranslateLanguage?: boolean; autoTranslate?: boolean; - observe?: Function; + observe?(): Observable; usedCannedResponse?: string; bannerClosed?: boolean; lastOpen?: Date; diff --git a/app/views/RoomView/index.tsx b/app/views/RoomView/index.tsx index 968e26b64..ae261b3c3 100644 --- a/app/views/RoomView/index.tsx +++ b/app/views/RoomView/index.tsx @@ -4,7 +4,7 @@ import { connect } from 'react-redux'; import parse from 'url-parse'; import moment from 'moment'; import * as Haptics from 'expo-haptics'; -import { Model, Q } from '@nozbe/watermelondb'; +import { Q } from '@nozbe/watermelondb'; import { dequal } from 'dequal'; import { withSafeAreaInsets } from 'react-native-safe-area-context'; import { StackNavigationProp } from '@react-navigation/stack'; @@ -199,13 +199,15 @@ class RoomView extends React.Component { const name = props.route.params?.name; const fname = props.route.params?.fname; const prid = props.route.params?.prid; - const room: any = props.route.params?.room ?? { - rid: this.rid, - t: this.t, - name, - fname, - prid - }; + const room = + props.route.params?.room ?? + ({ + rid: this.rid, + t: this.t, + name, + fname, + prid + } as IRoom); this.jumpToMessageId = props.route.params?.jumpToMessageId; this.jumpToThreadId = props.route.params?.jumpToThreadId; const roomUserId = props.route.params?.roomUserId ?? RocketChat.getUidDirectMessage(room); @@ -629,22 +631,24 @@ class RoomView extends React.Component { delete this.sub; }; - observeRoom = (room: Model) => { - const observable = room.observe(); - this.subSubscription = observable.subscribe((changes: any) => { - const roomUpdate = roomAttrsUpdate.reduce((ret: any, attr: any) => { - ret[attr] = changes[attr]; - return ret; - }, {}); - if (this.mounted) { - this.internalSetState({ room: changes, roomUpdate }); - } else { - // @ts-ignore - this.state.room = changes; - // @ts-ignore - this.state.roomUpdate = roomUpdate; - } - }); + observeRoom = (room: IRoom) => { + if (room.observe) { + const observable = room.observe(); + this.subSubscription = observable.subscribe((changes: any) => { + const roomUpdate = roomAttrsUpdate.reduce((ret: any, attr: any) => { + ret[attr] = changes[attr]; + return ret; + }, {}); + if (this.mounted) { + this.internalSetState({ room: changes, roomUpdate }); + } else { + // @ts-ignore + this.state.room = changes; + // @ts-ignore + this.state.roomUpdate = roomUpdate; + } + }); + } }; errorActionsShow = (message: string) => {