|
| 1 | +<!-- |
| 2 | +@license |
| 3 | +Copyright (c) 2016 Shawn Biddle |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in all |
| 13 | +copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +SOFTWARE. |
| 22 | +
|
| 23 | +--> |
| 24 | +<script> |
| 25 | + window.Biddle = window.Biddle || {}; |
| 26 | +/** |
| 27 | +`Biddle.MemoizationBehavior` allows you to define a computed property |
| 28 | +as memoizable (http://en.wikipedia.org/wiki/Memoization) with `_memoizeComputed` |
| 29 | +or more generally memoize any given function with `_memoize` |
| 30 | +
|
| 31 | +To implement a computed property as memoizable in the simplest fashion (although |
| 32 | +this is a trivial example, you would likely only want to memoize expensive functions) |
| 33 | +
|
| 34 | + behaviors: [ |
| 35 | + Biddle.MemoizationBehavior, |
| 36 | + ], |
| 37 | +
|
| 38 | + properties: { |
| 39 | + firstName: String, |
| 40 | + lastName: String, |
| 41 | + fullName: { |
| 42 | + type: String, |
| 43 | + computed: '_memoizeComputed("fullName", firstName, lastName)' |
| 44 | + method: '_getFullName' |
| 45 | + } |
| 46 | + }, |
| 47 | +
|
| 48 | + _getFullName: function (first, last) { |
| 49 | + return first + ' ' + last; |
| 50 | + } |
| 51 | +
|
| 52 | +Given that the default hash function is `JSON.stringify` which may be inappropriate |
| 53 | +for object/array parameters you may wish to override the default hashing method |
| 54 | +for a specific property. |
| 55 | +
|
| 56 | + properties: { |
| 57 | + foo: Object, |
| 58 | + bar: Object, |
| 59 | + foobar: { |
| 60 | + type: String, |
| 61 | + computed: '_memoizeComputed("fullName", foo, bar)' |
| 62 | + method: '_computeFoobar', |
| 63 | + hashFn: (foo, bar) => foo.id + ':' + bar.id |
| 64 | + } |
| 65 | + }, |
| 66 | +
|
| 67 | +To generally memoize any function you may do the following |
| 68 | +
|
| 69 | + var someValue = this._memoize(this.someExpensiveFunction)(arg2, arg2, arg3); |
| 70 | +
|
| 71 | +Cache invalidation happens manually with the `_invalidateMemoizeCache` method |
| 72 | +
|
| 73 | + var foo = this._memoize(this.bar)('Hello'); |
| 74 | + // ... |
| 75 | + this._invalidateMemoizeCache(this.bar); |
| 76 | +
|
| 77 | +@demo demo/demo.html |
| 78 | +@polymerBehavior |
| 79 | +*/ |
| 80 | + Biddle.MemoizationBehavior = { |
| 81 | + |
| 82 | + /** |
| 83 | + * Default method to create the hash to key a value in the function's cache |
| 84 | + * @type {!Function} |
| 85 | + */ |
| 86 | + _defaultMemoizeHash: (...args) => JSON.stringify(args), |
| 87 | + |
| 88 | + /** |
| 89 | + * Memoize a given function |
| 90 | + * @param {!Function} func Function to memoize |
| 91 | + * @param {Function} hashFn Method to create the hash key, defaults to `_defaultMemoizeHash` |
| 92 | + * @return {Function} |
| 93 | + */ |
| 94 | + _memoize: function (func, hashFn) { |
| 95 | + hashFn = hashFn || this._defaultMemoizeHash; |
| 96 | + return function (...args) { |
| 97 | + var key = '' + hashFn.apply(this, args); |
| 98 | + func.cache = func.cache || {}; |
| 99 | + if (!(key in func.cache)) { |
| 100 | + func.cache[key] = func.apply(this, args); |
| 101 | + } |
| 102 | + return func.cache[key]; |
| 103 | + }; |
| 104 | + }, |
| 105 | + |
| 106 | + /** |
| 107 | + * Memoize a given computed function |
| 108 | + * @param {!String} property Name of property being computed (camelCase) |
| 109 | + * @param {...*} args Any args to pass to compute function as normal |
| 110 | + * @return {*} |
| 111 | + */ |
| 112 | + _memoizeComputed: function (property, ...args) { |
| 113 | + var propertyDef = this.properties[property]; |
| 114 | + var hashFn = propertyDef.hashFn || this._defaultMemoizeHash; |
| 115 | + return this._memoize(this[propertyDef.method], hashFn).apply(this, args); |
| 116 | + }, |
| 117 | + |
| 118 | + /** |
| 119 | + * Invalidate the cache for a given method |
| 120 | + * @param {String} method |
| 121 | + */ |
| 122 | + _invalidateMemoizeCache: function (method) { |
| 123 | + method.cache = {}; |
| 124 | + } |
| 125 | + }; |
| 126 | +</script> |
0 commit comments