| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2017 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 |
| 11 <link rel="import" href="boot.html"> |
| 12 |
| 13 <script> |
| 14 |
| 15 (function() { |
| 16 |
| 17 'use strict'; |
| 18 |
| 19 // unique global id for deduping mixins. |
| 20 let dedupeId = 0; |
| 21 |
| 22 /** |
| 23 * Given a mixin producing function, memoize applications of mixin to base |
| 24 * @private |
| 25 * @param {Function} mixin Mixin for which to create a caching mixin. |
| 26 * @return {Function} Returns a mixin which when applied multiple times to the |
| 27 * same base will always return the same extended class. |
| 28 */ |
| 29 function cachingMixin(mixin) { |
| 30 return function(base) { |
| 31 if (!mixin.__mixinApplications) { |
| 32 mixin.__mixinApplications = new WeakMap(); |
| 33 } |
| 34 let map = mixin.__mixinApplications; |
| 35 let application = map.get(base); |
| 36 if (!application) { |
| 37 application = mixin(base); |
| 38 map.set(base, application); |
| 39 } |
| 40 return application; |
| 41 }; |
| 42 } |
| 43 |
| 44 /** |
| 45 * Wraps an ES6 class expression mixin such that the mixin is only applied |
| 46 * if it has not already been applied its base argument. Also memoizes mixin |
| 47 * applications. |
| 48 * |
| 49 * @memberof Polymer |
| 50 * @param {Function} mixin ES6 class expression mixin to wrap |
| 51 * @return {Function} Wrapped mixin that deduplicates and memoizes |
| 52 * mixin applications to base |
| 53 */ |
| 54 Polymer.dedupingMixin = function(mixin) { |
| 55 mixin = cachingMixin(mixin); |
| 56 // maintain a unique id for each mixin |
| 57 mixin.__dedupeId = ++dedupeId; |
| 58 return function(base) { |
| 59 let baseSet = base.__mixinSet; |
| 60 if (baseSet && baseSet[mixin.__dedupeId]) { |
| 61 return base; |
| 62 } |
| 63 let extended = mixin(base); |
| 64 // copy inherited mixin set from the extended class, or the base class |
| 65 // NOTE: we avoid use of Set here because some browser (IE11) |
| 66 // cannot extend a base Set via the constructor. |
| 67 extended.__mixinSet = |
| 68 Object.create(extended.__mixinSet || baseSet || null); |
| 69 extended.__mixinSet[mixin.__dedupeId] = true; |
| 70 return extended; |
| 71 } |
| 72 }; |
| 73 |
| 74 })(); |
| 75 |
| 76 </script> |
| OLD | NEW |