@@ -58,16 +58,35 @@ define(function (require, exports, module) {
5858 * Show deprecation message with the call stack if it
5959 * has never been displayed before.
6060 * @param {!string } message The deprecation message to be displayed.
61+ * @param {boolean= } oncePerCaller If true, displays the message once for each unique call location.
62+ * If false (the default), only displays the message once no matter where it's called from.
63+ * Note that setting this to true can cause a slight performance hit (because it has to generate
64+ * a stack trace), so don't set this for functions that you expect to be called from performance-
65+ * sensitive code (e.g. tight loops).
6166 */
62- function deprecationWarning ( message ) {
63- // If we have displayed this message before, then don't
64- // show it again.
65- if ( ! message || displayedWarnings [ message ] ) {
67+ function deprecationWarning ( message , oncePerCaller ) {
68+ // If oncePerCaller isn't set, then only show the message once no matter who calls it.
69+ if ( ! message || ( ! oncePerCaller && displayedWarnings [ message ] ) ) {
6670 return ;
6771 }
6872
69- console . warn ( message + "\n" + _trimStack ( new Error ( ) . stack ) ) ;
70- displayedWarnings [ message ] = true ;
73+ // Don't show the warning again if we've already gotten it from the current caller.
74+ // The true caller location is the fourth line in the stack trace:
75+ // * 0 is the word "Error"
76+ // * 1 is this function
77+ // * 2 is the caller of this function (the one throwing the deprecation warning)
78+ // * 3 is the actual caller of the deprecated function.
79+ var stack = new Error ( ) . stack ,
80+ callerLocation = stack . split ( "\n" ) [ 3 ] ;
81+ if ( oncePerCaller && displayedWarnings [ message ] && displayedWarnings [ message ] [ callerLocation ] ) {
82+ return ;
83+ }
84+
85+ console . warn ( message + "\n" + _trimStack ( stack ) ) ;
86+ if ( ! displayedWarnings [ message ] ) {
87+ displayedWarnings [ message ] = { } ;
88+ }
89+ displayedWarnings [ message ] [ callerLocation ] = true ;
7190 }
7291
7392 // Define public API
0 commit comments