|
| 1 | +`Biddle.MemoizationBehavior` allows you to define a computed property |
| 2 | +as memoizable (http://en.wikipedia.org/wiki/Memoization) with `_memoizeComputed` |
| 3 | +or more generally memoize any given function with `_memoize` |
| 4 | + |
| 5 | +To implement a computed property as memoizable in the simplest fashion (although |
| 6 | +this is a trivial example, you would likely only want to memoize expensive functions) |
| 7 | + |
| 8 | + behaviors: [ |
| 9 | + Biddle.MemoizationBehavior, |
| 10 | + ], |
| 11 | + |
| 12 | + properties: { |
| 13 | + firstName: String, |
| 14 | + lastName: String, |
| 15 | + fullName: { |
| 16 | + type: String, |
| 17 | + computed: '_memoizeComputed("fullName", firstName, lastName)' |
| 18 | + method: '_getFullName' |
| 19 | + } |
| 20 | + }, |
| 21 | + |
| 22 | + _getFullName: function (first, last) { |
| 23 | + return first + ' ' + last; |
| 24 | + } |
| 25 | + |
| 26 | +Given that the default hash function is `JSON.stringify` which may be inappropriate |
| 27 | +for object/array parameters you may wish to override the default hashing method |
| 28 | +for a specific property. |
| 29 | + |
| 30 | + properties: { |
| 31 | + foo: Object, |
| 32 | + bar: Object, |
| 33 | + foobar: { |
| 34 | + type: String, |
| 35 | + computed: '_memoizeComputed("fullName", foo, bar)' |
| 36 | + method: '_computeFoobar', |
| 37 | + hashFn: (foo, bar) => foo.id + ':' + bar.id |
| 38 | + } |
| 39 | + }, |
| 40 | + |
| 41 | +To generally memoize any function you may do the following |
| 42 | + |
| 43 | + var someValue = this._memoize(this.someExpensiveFunction)(arg2, arg2, arg3); |
| 44 | + |
| 45 | +Cache invalidation happens manually with the `_invalidateMemoizeCache` method |
| 46 | + |
| 47 | + var foo = this._memoize(this.bar)('Hello'); |
| 48 | + // ... |
| 49 | + this._invalidateMemoizeCache(this.bar); |
| 50 | + |
| 51 | + |
0 commit comments