Use of cached managed config in components (#2758)

This commit is contained in:
Elias Nahum 2019-05-01 18:34:09 -04:00 committed by GitHub
parent 0708a53d09
commit 6eeb830662
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 34 additions and 163 deletions

View file

@ -97,9 +97,9 @@ export default class AtMention extends React.PureComponent {
handleLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'false') {
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});
const actionText = formatMessage({id: 'mobile.mention.copy_mention', defaultMessage: 'Copy Mention'});

View file

@ -82,7 +82,7 @@ export default class MarkdownCodeBlock extends React.PureComponent {
handleLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});

View file

@ -144,7 +144,7 @@ export default class MarkdownImage extends React.Component {
handleLinkLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});

View file

@ -99,7 +99,7 @@ export default class MarkdownLink extends PureComponent {
handleLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});

View file

@ -58,15 +58,6 @@ export default class PostListBase extends PureComponent {
siteURL: '',
};
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.mounted = true;
this.setManagedConfig();
}
componentDidUpdate() {
if (this.props.deepLinkURL) {
this.handleDeepLink(this.props.deepLinkURL);
@ -74,11 +65,6 @@ export default class PostListBase extends PureComponent {
}
}
componentWillUnmount() {
this.mounted = false;
mattermostManaged.removeEventListener(this.listenerId);
}
handleClosePermalink = () => {
const {actions} = this.props;
actions.selectFocusedPostId('');
@ -163,7 +149,7 @@ export default class PostListBase extends PureComponent {
highlightPinnedOrFlagged: this.props.highlightPinnedOrFlagged,
isSearchResult: this.props.isSearchResult,
location: this.props.location,
managedConfig: this.state.managedConfig,
managedConfig: mattermostManaged.getCachedConfig(),
navigator: this.props.navigator,
onHashtagPress: this.props.onHashtagPress,
onPermalinkPress: this.handlePermalinkPress,
@ -192,19 +178,6 @@ export default class PostListBase extends PureComponent {
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
if (this.mounted) {
this.setState({
managedConfig: nextConfig,
});
}
};
showPermalinkView = (postId) => {
const {actions, navigator} = this.props;

View file

@ -8,12 +8,12 @@ import JailMonkey from 'jail-monkey';
const {MattermostManaged} = NativeModules;
const listeners = [];
let localConfig;
let cachedConfig = {};
export default {
addEventListener: (name, callback) => {
const listener = DeviceEventEmitter.addListener(name, (config) => {
localConfig = config;
cachedConfig = config;
if (callback && typeof callback === 'function') {
callback(config);
}
@ -36,17 +36,17 @@ export default {
},
authenticate: LocalAuth.auth,
blurAppScreen: MattermostManaged.blurAppScreen,
getConfig: MattermostManaged.getConfig,
getLocalConfig: async () => {
if (!localConfig) {
try {
localConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
getConfig: async () => {
try {
cachedConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
return localConfig || {};
return cachedConfig;
},
getCachedConfig: () => {
return cachedConfig;
},
isDeviceSecure: async () => {
try {

View file

@ -8,12 +8,12 @@ const {BlurAppScreen, MattermostManaged} = NativeModules;
const mattermostManagedEmitter = new NativeEventEmitter(MattermostManaged);
const listeners = [];
let localConfig;
let cachedConfig = {};
export default {
addEventListener: (name, callback) => {
const listener = mattermostManagedEmitter.addListener(name, (config) => {
localConfig = config;
cachedConfig = config;
if (callback && typeof callback === 'function') {
callback(config);
}
@ -36,17 +36,17 @@ export default {
},
authenticate: LocalAuth.authenticate,
blurAppScreen: BlurAppScreen.enabled,
getConfig: MattermostManaged.getConfig,
getLocalConfig: async () => {
if (!localConfig) {
try {
localConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
getConfig: async () => {
try {
cachedConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
return localConfig || {};
return cachedConfig;
},
getCachedConfig: () => {
return cachedConfig;
},
isDeviceSecure: async () => {
try {

View file

@ -57,22 +57,6 @@ export default class FlaggedPosts extends PureComponent {
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
props.actions.clearSearch();
props.actions.getFlaggedPosts();
this.state = {
managedConfig: {},
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.setManagedConfig();
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
goToThread = (post) => {
@ -159,7 +143,7 @@ export default class FlaggedPosts extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
if (isDateLine(item)) {
return (
<DateHeader
@ -186,7 +170,7 @@ export default class FlaggedPosts extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
showFullDate={false}
skipFlaggedHeader={true}
skipPinnedHeader={true}
@ -196,17 +180,6 @@ export default class FlaggedPosts extends PureComponent {
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;

View file

@ -56,27 +56,14 @@ export default class PinnedPosts extends PureComponent {
super(props);
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
this.state = {
managedConfig: {},
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
const {actions, currentChannelId} = this.props;
this.setManagedConfig();
actions.clearSearch();
actions.getPinnedPosts(currentChannelId);
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
goToThread = (post) => {
const {actions, navigator, theme} = this.props;
const channelId = post.channel_id;
@ -161,7 +148,6 @@ export default class PinnedPosts extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
if (isDateLine(item)) {
return (
<DateHeader
@ -187,7 +173,7 @@ export default class PinnedPosts extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
showFullDate={false}
skipFlaggedHeader={false}
skipPinnedHeader={true}
@ -197,17 +183,6 @@ export default class PinnedPosts extends PureComponent {
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;

View file

@ -57,22 +57,6 @@ export default class RecentMentions extends PureComponent {
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
props.actions.clearSearch();
props.actions.getRecentMentions();
this.state = {
managedConfig: {},
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.setManagedConfig();
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
goToThread = (post) => {
@ -159,7 +143,7 @@ export default class RecentMentions extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
if (isDateLine(item)) {
return (
<DateHeader
@ -185,7 +169,7 @@ export default class RecentMentions extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
showFullDate={false}
skipPinnedHeader={true}
/>
@ -194,17 +178,6 @@ export default class RecentMentions extends PureComponent {
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;

View file

@ -92,18 +92,11 @@ export default class Search extends PureComponent {
channelName: '',
cursorPosition: 0,
value: props.initialValue,
managedConfig: {},
recent: props.recent,
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.setManagedConfig();
if (this.props.initialValue) {
this.search(this.props.initialValue);
}
@ -147,10 +140,6 @@ export default class Search extends PureComponent {
}
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
archivedIndicator = (postID, style) => {
const channelIsArchived = this.props.archivedPostIds.includes(postID);
let archivedIndicator = null;
@ -365,7 +354,6 @@ export default class Search extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
const style = getStyleFromTheme(theme);
if (item.id) {
@ -402,7 +390,7 @@ export default class Search extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
skipPinnedHeader={true}
/>
{separator}
@ -458,17 +446,6 @@ export default class Search extends PureComponent {
this.search(this.state.value.trim());
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;