OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Provides a system for memoizing computations applied to | 6 * @fileoverview Provides a system for memoizing computations applied to |
7 * DOM nodes within the same call stack. | 7 * DOM nodes within the same call stack. |
8 * | 8 * |
9 * To make a function memoizable - suppose you have a function | 9 * To make a function memoizable - suppose you have a function |
10 * isAccessible that takes a node and returns a boolean: | 10 * isAccessible that takes a node and returns a boolean: |
(...skipping 25 matching lines...) Expand all Loading... |
36 */ | 36 */ |
37 | 37 |
38 | 38 |
39 goog.provide('cvox.Memoize'); | 39 goog.provide('cvox.Memoize'); |
40 | 40 |
41 | 41 |
42 /** | 42 /** |
43 * Create the namespace. | 43 * Create the namespace. |
44 * @constructor | 44 * @constructor |
45 */ | 45 */ |
46 cvox.Memoize = function() { | 46 cvox.Memoize = function() {}; |
47 }; | |
48 | 47 |
49 /** | 48 /** |
50 * The cache: a map from string function name to a WeakMap from DOM node | 49 * The cache: a map from string function name to a WeakMap from DOM node |
51 * to function result. This variable is null when we're out of scope, and it's | 50 * to function result. This variable is null when we're out of scope, and it's |
52 * a map from string to WeakMap to result when we're in scope. | 51 * a map from string to WeakMap to result when we're in scope. |
53 * | 52 * |
54 * @type {?Object<WeakMap<Node, *> >} | 53 * @type {?Object<WeakMap<Node, *> >} |
55 * @private | 54 * @private |
56 */ | 55 */ |
57 cvox.Memoize.nodeMap_ = null; | 56 cvox.Memoize.nodeMap_ = null; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 if (result === undefined) { | 123 if (result === undefined) { |
125 result = functionClosure(node); | 124 result = functionClosure(node); |
126 if (result === undefined) { | 125 if (result === undefined) { |
127 throw 'A memoized function cannot return undefined.'; | 126 throw 'A memoized function cannot return undefined.'; |
128 } | 127 } |
129 cvox.Memoize.nodeMap_[functionName].set(node, result); | 128 cvox.Memoize.nodeMap_[functionName].set(node, result); |
130 } | 129 } |
131 | 130 |
132 return result; | 131 return result; |
133 }; | 132 }; |
OLD | NEW |