Skip to content

Commit

Permalink
Introduce generic warnOnce function for warning messages (#22109)
Browse files Browse the repository at this point in the history
Summary:
This pull request adds a function called `warnOnce` that prints a warning message to the console once per session.

It uses a unique key per callsite to help ensure that the message is not printed multiple times.

[General] [Added] - Added new `warnOnce` function for printing a message to the developer console once per session
[General] [Changed] - Changed the warnings in `react-native-implementation` to use `warnOnce`
Pull Request resolved: #22109

Differential Revision: D13955887

Pulled By: cpojer

fbshipit-source-id: aa51ac427a80cc0554a6bcc915715821d0bd5439
  • Loading branch information
empyrical authored and facebook-github-bot committed Feb 5, 2019
1 parent 52bdf34 commit 9a7fff9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
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');

This comment has been minimized.

Copy link
@jogboms

jogboms May 15, 2019

This was done wrong #23943 #24065

This comment has been minimized.

Copy link
@jogboms

jogboms May 15, 2019

@empyrical 👀

This comment has been minimized.

Copy link
@empyrical

empyrical May 15, 2019

Author Contributor

Thanks for the heads up, I'll see if I can figure out what's going on there and reproduce the bug

This comment has been minimized.

Copy link
@jogboms

jogboms May 22, 2019

Sure. Thanks @empyrical


// 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

0 comments on commit 9a7fff9

Please sign in to comment.