Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce generic warnOnce function for warning messages #22109

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Libraries/Utilities/warnOnce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';

const warning = require('fbjs/lib/warning');

const warnedKeys: {[string]: boolean} = {};

/**
* A simple function that prints a warning message once per session.
*
* @param {string} key - The key used to ensure the message is printed once.
* This should be unique to the callsite.
* @param {string} message - The message to print
*/
function warnOnce(key: string, message: string) {
if (warnedKeys[key]) {
return;
}

warning(false, message);

warnedKeys[key] = true;
}

module.exports = warnOnce;
46 changes: 17 additions & 29 deletions Libraries/react-native/react-native-implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
'use strict';

const invariant = require('invariant');

let showedListViewDeprecation = false;
let showedSwipeableListViewDeprecation = false;
let showedWebWiewDeprecation = false;
const warnOnce = require('warnOnce');

// Export React, plus some native additions.
module.exports = {
Expand Down Expand Up @@ -62,14 +59,11 @@ module.exports = {
return require('KeyboardAvoidingView');
},
get ListView() {
if (!showedListViewDeprecation) {
console.warn(
'ListView is deprecated and will be removed in a future release. ' +
'See https://fb.me/nolistview for more information',
);

showedListViewDeprecation = true;
}
warnOnce(
'listview-deprecation',
'ListView is deprecated and will be removed in a future release. ' +
'See https://fb.me/nolistview for more information',
);
return require('ListView');
},
get MaskedViewIOS() {
Expand Down Expand Up @@ -121,14 +115,11 @@ module.exports = {
return require('SwipeableFlatList');
},
get SwipeableListView() {
if (!showedSwipeableListViewDeprecation) {
console.warn(
'ListView and SwipeableListView are deprecated and will be removed in a future release. ' +
'See https://fb.me/nolistview for more information',
);

showedSwipeableListViewDeprecation = true;
}
warnOnce(
'swipablelistview-deprecation',
'ListView and SwipeableListView are deprecated and will be removed in a future release. ' +
'See https://fb.me/nolistview for more information',
);
return require('SwipeableListView');
},
get Text() {
Expand Down Expand Up @@ -165,15 +156,12 @@ module.exports = {
return require('VirtualizedList');
},
get WebView() {
if (!showedWebWiewDeprecation) {
console.warn(
'WebView has been extracted from react-native core and will be removed in a future release. ' +
"It can now be installed and imported from 'react-native-webview' instead of 'react-native'. " +
'See https://github.com/react-native-community/react-native-webview for more informations.',
);

showedWebWiewDeprecation = true;
}
warnOnce(
'webview-moved',
'WebView has been extracted from react-native core and will be removed in a future release. ' +
"It can now be installed and imported from 'react-native-webview' instead of 'react-native'. " +
'See https://github.com/react-native-community/react-native-webview for more informations.',
);
return require('WebView');
},

Expand Down