From 95f2287cc6e30a8273ba73f266289f41c81cb09e Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 4 Feb 2022 17:12:51 -0300 Subject: [PATCH 1/5] create redux store poc --- app/ReactotronConfig.js | 8 +++++++- app/containers/MessageBox/EmojiKeyboard.tsx | 2 +- app/ee/omnichannel/lib/subscriptions/inquiry.js | 2 +- app/index.tsx | 2 ++ app/lib/auxStore.ts | 9 +++++++++ app/lib/encryption/encryption.js | 2 +- app/lib/methods/callJitsi.js | 2 +- app/lib/methods/canOpenRoom.js | 2 +- app/lib/methods/enterpriseModules.js | 2 +- app/lib/methods/getCustomEmojis.js | 2 +- app/lib/methods/getPermissions.js | 2 +- app/lib/methods/getRoles.js | 2 +- app/lib/methods/getSettings.js | 2 +- app/lib/methods/getUsersPresence.js | 2 +- app/lib/methods/helpers/mergeSubscriptionsRooms.js | 2 +- app/lib/methods/subscriptions/rooms.js | 2 +- app/lib/rocketchat.js | 2 +- app/notifications/push/index.ts | 2 +- app/notifications/push/push.ios.ts | 2 +- app/reducers/share.ts | 8 ++++---- app/share.tsx | 2 +- app/utils/isReadOnly.ts | 2 +- app/utils/localAuthentication.ts | 2 +- 23 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 app/lib/auxStore.ts diff --git a/app/ReactotronConfig.js b/app/ReactotronConfig.js index 80cb90beb8..6559233635 100644 --- a/app/ReactotronConfig.js +++ b/app/ReactotronConfig.js @@ -1,4 +1,5 @@ /* eslint-disable */ +import AsyncStorage from '@react-native-community/async-storage'; import { NativeModules } from 'react-native'; import Reactotron from 'reactotron-react-native'; import { reactotronRedux } from 'reactotron-redux'; @@ -7,7 +8,12 @@ import sagaPlugin from 'reactotron-redux-saga'; if (__DEV__) { const scriptURL = NativeModules.SourceCode.scriptURL; const scriptHostname = scriptURL.split('://')[1].split(':')[0]; - Reactotron.configure({ host: scriptHostname }).useReactNative().use(reactotronRedux()).use(sagaPlugin()).connect(); + Reactotron.setAsyncStorageHandler(AsyncStorage) + .configure({ host: scriptHostname }) + .useReactNative() + .use(reactotronRedux()) + .use(sagaPlugin()) + .connect(); // Running on android device // $ adb reverse tcp:9090 tcp:9090 Reactotron.clear(); diff --git a/app/containers/MessageBox/EmojiKeyboard.tsx b/app/containers/MessageBox/EmojiKeyboard.tsx index 91acc45d10..e5bc65a6eb 100644 --- a/app/containers/MessageBox/EmojiKeyboard.tsx +++ b/app/containers/MessageBox/EmojiKeyboard.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { View } from 'react-native'; import { KeyboardRegistry } from 'react-native-ui-lib/keyboard'; -import store from '../../lib/createStore'; +import { store } from '../../lib/auxStore'; import EmojiPicker from '../EmojiPicker'; import styles from './styles'; import { themes } from '../../constants/colors'; diff --git a/app/ee/omnichannel/lib/subscriptions/inquiry.js b/app/ee/omnichannel/lib/subscriptions/inquiry.js index d10d5c8921..4e6cda6821 100644 --- a/app/ee/omnichannel/lib/subscriptions/inquiry.js +++ b/app/ee/omnichannel/lib/subscriptions/inquiry.js @@ -1,5 +1,5 @@ import log from '../../../../utils/log'; -import store from '../../../../lib/createStore'; +import { store } from '../../../../lib/auxStore'; import RocketChat from '../../../../lib/rocketchat'; import { inquiryQueueAdd, inquiryQueueRemove, inquiryQueueUpdate, inquiryRequest } from '../../actions/inquiry'; diff --git a/app/index.tsx b/app/index.tsx index cc0a06a265..f86827c090 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -32,6 +32,7 @@ import debounce from './utils/debounce'; import { isFDroidBuild } from './constants/environment'; import { IThemePreference } from './definitions/ITheme'; import { ICommand } from './definitions/ICommand'; +import { initStore } from './lib/auxStore'; RNScreens.enableScreens(); @@ -100,6 +101,7 @@ export default class Root extends React.Component<{}, IState> { if (isTablet) { this.initTablet(); } + initStore(store); } componentDidMount() { diff --git a/app/lib/auxStore.ts b/app/lib/auxStore.ts new file mode 100644 index 0000000000..1dd70c9948 --- /dev/null +++ b/app/lib/auxStore.ts @@ -0,0 +1,9 @@ +import { Store } from 'redux'; + +import { IApplicationState } from '../definitions'; + +export let store: Store = null as any; + +export const initStore = (_store: Store): void => { + store = _store; +}; diff --git a/app/lib/encryption/encryption.js b/app/lib/encryption/encryption.js index 99a12bd8ca..2d7f808fda 100644 --- a/app/lib/encryption/encryption.js +++ b/app/lib/encryption/encryption.js @@ -9,7 +9,7 @@ import database from '../database'; import protectedFunction from '../methods/helpers/protectedFunction'; import Deferred from '../../utils/deferred'; import log from '../../utils/log'; -import store from '../createStore'; +import { store } from '../auxStore'; import { E2E_BANNER_TYPE, E2E_MESSAGE_TYPE, diff --git a/app/lib/methods/callJitsi.js b/app/lib/methods/callJitsi.js index 8d2fa7db4b..b554660ed2 100644 --- a/app/lib/methods/callJitsi.js +++ b/app/lib/methods/callJitsi.js @@ -1,4 +1,4 @@ -import reduxStore from '../createStore'; +import { store as reduxStore } from '../auxStore'; import Navigation from '../Navigation'; import { events, logEvent } from '../../utils/log'; diff --git a/app/lib/methods/canOpenRoom.js b/app/lib/methods/canOpenRoom.js index ca8835b9c5..183b32a369 100644 --- a/app/lib/methods/canOpenRoom.js +++ b/app/lib/methods/canOpenRoom.js @@ -1,5 +1,5 @@ import database from '../database'; -import store from '../createStore'; +import { store } from '../auxStore'; const restTypes = { channel: 'channels', diff --git a/app/lib/methods/enterpriseModules.js b/app/lib/methods/enterpriseModules.js index 312a992525..b4cecb55c7 100644 --- a/app/lib/methods/enterpriseModules.js +++ b/app/lib/methods/enterpriseModules.js @@ -1,5 +1,5 @@ import { compareServerVersion, methods } from '../utils'; -import reduxStore from '../createStore'; +import { store as reduxStore } from '../auxStore'; import database from '../database'; import log from '../../utils/log'; import { clearEnterpriseModules, setEnterpriseModules as setEnterpriseModulesAction } from '../../actions/enterpriseModules'; diff --git a/app/lib/methods/getCustomEmojis.js b/app/lib/methods/getCustomEmojis.js index 5e8a3e7457..8f0a9e3bb5 100644 --- a/app/lib/methods/getCustomEmojis.js +++ b/app/lib/methods/getCustomEmojis.js @@ -2,7 +2,7 @@ import orderBy from 'lodash/orderBy'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { compareServerVersion, methods } from '../utils'; -import reduxStore from '../createStore'; +import { store as reduxStore } from '../auxStore'; import database from '../database'; import log from '../../utils/log'; import { setCustomEmojis as setCustomEmojisAction } from '../../actions/customEmojis'; diff --git a/app/lib/methods/getPermissions.js b/app/lib/methods/getPermissions.js index 889aeaf163..f869fcfd7b 100644 --- a/app/lib/methods/getPermissions.js +++ b/app/lib/methods/getPermissions.js @@ -5,7 +5,7 @@ import orderBy from 'lodash/orderBy'; import { compareServerVersion, methods } from '../utils'; import database from '../database'; import log from '../../utils/log'; -import reduxStore from '../createStore'; +import { store as reduxStore } from '../auxStore'; import RocketChat from '../rocketchat'; import { setPermissions as setPermissionsAction } from '../../actions/permissions'; import protectedFunction from './helpers/protectedFunction'; diff --git a/app/lib/methods/getRoles.js b/app/lib/methods/getRoles.js index 77bc5cf832..d43ca00ef8 100644 --- a/app/lib/methods/getRoles.js +++ b/app/lib/methods/getRoles.js @@ -2,7 +2,7 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import database from '../database'; import log from '../../utils/log'; -import reduxStore from '../createStore'; +import { store as reduxStore } from '../auxStore'; import { removeRoles, setRoles as setRolesAction, updateRoles } from '../../actions/roles'; import protectedFunction from './helpers/protectedFunction'; diff --git a/app/lib/methods/getSettings.js b/app/lib/methods/getSettings.js index 9e10829389..bd5cdfbc31 100644 --- a/app/lib/methods/getSettings.js +++ b/app/lib/methods/getSettings.js @@ -3,7 +3,7 @@ import { Q } from '@nozbe/watermelondb'; import { addSettings, clearSettings } from '../../actions/settings'; import RocketChat from '../rocketchat'; -import reduxStore from '../createStore'; +import { store as reduxStore } from '../auxStore'; import settings from '../../constants/settings'; import log from '../../utils/log'; import database from '../database'; diff --git a/app/lib/methods/getUsersPresence.js b/app/lib/methods/getUsersPresence.js index 07a7b985e1..27f11552c5 100644 --- a/app/lib/methods/getUsersPresence.js +++ b/app/lib/methods/getUsersPresence.js @@ -2,7 +2,7 @@ import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { compareServerVersion, methods } from '../utils'; -import reduxStore from '../createStore'; +import { store as reduxStore } from '../auxStore'; import { setActiveUsers } from '../../actions/activeUsers'; import { setUser } from '../../actions/login'; import database from '../database'; diff --git a/app/lib/methods/helpers/mergeSubscriptionsRooms.js b/app/lib/methods/helpers/mergeSubscriptionsRooms.js index 95a81001cc..1838cb493f 100644 --- a/app/lib/methods/helpers/mergeSubscriptionsRooms.js +++ b/app/lib/methods/helpers/mergeSubscriptionsRooms.js @@ -1,7 +1,7 @@ import EJSON from 'ejson'; import { Encryption } from '../../encryption'; -import reduxStore from '../../createStore'; +import { store as reduxStore } from '../../auxStore'; import { compareServerVersion, methods } from '../../utils'; import findSubscriptionsRooms from './findSubscriptionsRooms'; import normalizeMessage from './normalizeMessage'; diff --git a/app/lib/methods/subscriptions/rooms.js b/app/lib/methods/subscriptions/rooms.js index c2fc9fcdf4..ee986908a7 100644 --- a/app/lib/methods/subscriptions/rooms.js +++ b/app/lib/methods/subscriptions/rooms.js @@ -7,7 +7,7 @@ import protectedFunction from '../helpers/protectedFunction'; import messagesStatus from '../../../constants/messagesStatus'; import log from '../../../utils/log'; import random from '../../../utils/random'; -import store from '../../createStore'; +import { store } from '../../auxStore'; import { handlePayloadUserInteraction } from '../actions'; import buildMessage from '../helpers/buildMessage'; import RocketChat from '../../rocketchat'; diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 1cb627a412..1db2019f5d 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -27,7 +27,7 @@ import { updatePermission } from '../actions/permissions'; import { TEAM_TYPE } from '../definitions/ITeam'; import { updateSettings } from '../actions/settings'; import { compareServerVersion, methods } from './utils'; -import reduxStore from './createStore'; +import { store as reduxStore } from './auxStore'; import database from './database'; import subscribeRooms from './methods/subscriptions/rooms'; import getUsersPresence, { getUserPresence, subscribeUsersPresence } from './methods/getUsersPresence'; diff --git a/app/notifications/push/index.ts b/app/notifications/push/index.ts index 09fa860275..749517642f 100644 --- a/app/notifications/push/index.ts +++ b/app/notifications/push/index.ts @@ -1,6 +1,6 @@ import EJSON from 'ejson'; -import store from '../../lib/createStore'; +import { store } from '../../lib/auxStore'; import { deepLinkingOpen } from '../../actions/deepLinking'; import { isFDroidBuild } from '../../constants/environment'; import PushNotification from './push'; diff --git a/app/notifications/push/push.ios.ts b/app/notifications/push/push.ios.ts index a6b1118c56..b9713af2a6 100644 --- a/app/notifications/push/push.ios.ts +++ b/app/notifications/push/push.ios.ts @@ -2,7 +2,7 @@ // TODO BUMP LIB VERSION import NotificationsIOS, { NotificationAction, NotificationCategory, Notification } from 'react-native-notifications'; -import reduxStore from '../../lib/createStore'; +import { store as reduxStore } from '../../lib/auxStore'; import I18n from '../../i18n'; import { INotification } from '../../definitions/INotification'; diff --git a/app/reducers/share.ts b/app/reducers/share.ts index b5737eac69..03904ec46d 100644 --- a/app/reducers/share.ts +++ b/app/reducers/share.ts @@ -16,14 +16,14 @@ export interface IShareUser { } export interface IShare { - user: IShareUser | {}; - server: IShareServer | {}; + user: IShareUser; + server: IShareServer; settings: TShareSettings; } export const initialState: IShare = { - user: {}, - server: {}, + user: {} as IShareUser, + server: {} as IShareServer, settings: {} }; diff --git a/app/share.tsx b/app/share.tsx index 4af2d60260..be91227527 100644 --- a/app/share.tsx +++ b/app/share.tsx @@ -8,7 +8,7 @@ import { Provider } from 'react-redux'; import { defaultTheme, newThemeState, subscribeTheme, unsubscribeTheme } from './utils/theme'; import UserPreferences from './lib/userPreferences'; import Navigation from './lib/ShareNavigation'; -import store from './lib/createStore'; +import { store } from './lib/auxStore'; import { supportSystemTheme } from './utils/deviceInfo'; import { defaultHeader, getActiveRouteName, navigationTheme, themedHeader } from './utils/navigation'; import RocketChat, { THEME_PREFERENCES_KEY } from './lib/rocketchat'; diff --git a/app/utils/isReadOnly.ts b/app/utils/isReadOnly.ts index d94b73c491..6afc2d962c 100644 --- a/app/utils/isReadOnly.ts +++ b/app/utils/isReadOnly.ts @@ -1,5 +1,5 @@ import RocketChat from '../lib/rocketchat'; -import reduxStore from '../lib/createStore'; +import { store as reduxStore } from '../lib/auxStore'; import { ISubscription } from '../definitions/ISubscription'; const canPostReadOnly = async ({ rid }: { rid: string }) => { diff --git a/app/utils/localAuthentication.ts b/app/utils/localAuthentication.ts index f435996333..fcde7382e9 100644 --- a/app/utils/localAuthentication.ts +++ b/app/utils/localAuthentication.ts @@ -5,7 +5,7 @@ import AsyncStorage from '@react-native-community/async-storage'; import { sha256 } from 'js-sha256'; import UserPreferences from '../lib/userPreferences'; -import store from '../lib/createStore'; +import { store } from '../lib/auxStore'; import database from '../lib/database'; import { ATTEMPTS_KEY, From 0ae61650c2bd31a43951080a2b0b91491e20cbf1 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 4 Feb 2022 17:17:37 -0300 Subject: [PATCH 2/5] move to app init instead of constructor --- app/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/index.tsx b/app/index.tsx index f86827c090..89e6c5270c 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -35,6 +35,7 @@ import { ICommand } from './definitions/ICommand'; import { initStore } from './lib/auxStore'; RNScreens.enableScreens(); +initStore(store); interface IDimensions { width: number; @@ -101,7 +102,6 @@ export default class Root extends React.Component<{}, IState> { if (isTablet) { this.initTablet(); } - initStore(store); } componentDidMount() { From 586c883f49a7fcc73aa01db191ab7aa6b71a4e64 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 4 Feb 2022 17:21:56 -0300 Subject: [PATCH 3/5] add doc link --- app/lib/auxStore.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/app/lib/auxStore.ts b/app/lib/auxStore.ts index 1dd70c9948..2de3f72aa3 100644 --- a/app/lib/auxStore.ts +++ b/app/lib/auxStore.ts @@ -2,6 +2,7 @@ import { Store } from 'redux'; import { IApplicationState } from '../definitions'; +// https://redux.js.org/faq/code-structure#how-can-i-use-the-redux-store-in-non-component-files export let store: Store = null as any; export const initStore = (_store: Store): void => { From f393b1e2d3fa4af75e9d1d3ece1a23611f358d02 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 4 Feb 2022 17:23:05 -0300 Subject: [PATCH 4/5] back reactotron --- app/ReactotronConfig.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/app/ReactotronConfig.js b/app/ReactotronConfig.js index 6559233635..80cb90beb8 100644 --- a/app/ReactotronConfig.js +++ b/app/ReactotronConfig.js @@ -1,5 +1,4 @@ /* eslint-disable */ -import AsyncStorage from '@react-native-community/async-storage'; import { NativeModules } from 'react-native'; import Reactotron from 'reactotron-react-native'; import { reactotronRedux } from 'reactotron-redux'; @@ -8,12 +7,7 @@ import sagaPlugin from 'reactotron-redux-saga'; if (__DEV__) { const scriptURL = NativeModules.SourceCode.scriptURL; const scriptHostname = scriptURL.split('://')[1].split(':')[0]; - Reactotron.setAsyncStorageHandler(AsyncStorage) - .configure({ host: scriptHostname }) - .useReactNative() - .use(reactotronRedux()) - .use(sagaPlugin()) - .connect(); + Reactotron.configure({ host: scriptHostname }).useReactNative().use(reactotronRedux()).use(sagaPlugin()).connect(); // Running on android device // $ adb reverse tcp:9090 tcp:9090 Reactotron.clear(); From 3106dbeda48f8e93de0be4085d77ae4e1df9cc32 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Tue, 8 Feb 2022 13:23:19 -0300 Subject: [PATCH 5/5] change app/share store logic --- app/share.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/share.tsx b/app/share.tsx index be91227527..8a756d91a6 100644 --- a/app/share.tsx +++ b/app/share.tsx @@ -8,7 +8,8 @@ import { Provider } from 'react-redux'; import { defaultTheme, newThemeState, subscribeTheme, unsubscribeTheme } from './utils/theme'; import UserPreferences from './lib/userPreferences'; import Navigation from './lib/ShareNavigation'; -import { store } from './lib/auxStore'; +import store from './lib/createStore'; +import { initStore } from './lib/auxStore'; import { supportSystemTheme } from './utils/deviceInfo'; import { defaultHeader, getActiveRouteName, navigationTheme, themedHeader } from './utils/navigation'; import RocketChat, { THEME_PREFERENCES_KEY } from './lib/rocketchat'; @@ -28,6 +29,8 @@ import { DimensionsContext } from './dimensions'; import debounce from './utils/debounce'; import { ShareInsideStackParamList, ShareOutsideStackParamList, ShareAppStackParamList } from './definitions/navigationTypes'; +initStore(store); + interface IDimensions { width: number; height: number;