Skip to content

Commit c3da507

Browse files
committed
inline cachingMixin into deduplicatingMixin
1 parent f15e4ee commit c3da507

File tree

2 files changed

+33
-66
lines changed

2 files changed

+33
-66
lines changed

closure.log

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,11 @@ Found : undefined
55
return window.Polymer._polymerFn(info);
66
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77

8-
/polymer.html_script_1.js:44: WARNING - The right side in the assignment is not a subtype of the left side.
9-
Expected : T
10-
Found : Function|null
11-
More details:
12-
The found type is a union that includes an unexpected type: null
13-
mixin = cachingMixin(mixin);
14-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
16-
/polymer.html_script_1.js:44: WARNING - Invalid type for parameter 1 of function cachingMixin.
17-
Expected : Function|null
18-
Found : T
19-
20-
mixin = cachingMixin(mixin);
21-
^^^^^
22-
23-
/polymer.html_script_1.js:46: WARNING - Cannot create property __dedupeId on non-object type T.
24-
mixin.__dedupeId = ++dedupeId;
25-
^^^^^
26-
27-
/polymer.html_script_1.js:47: WARNING - Returned type does not match declared return type.
28-
Expected : T
29-
Found : function({__mixinSet:?} (loose)):{__mixinSet:?} (loose)
30-
31-
return function(base) {
32-
^^^^^^^^^^^^^^^^^^^^^^^
33-
34-
/polymer.html_script_1.js:49: WARNING - Cannot access property __dedupeId of non-object type T.
35-
if (baseSet && baseSet[mixin.__dedupeId]) {
36-
^^^^^
37-
38-
/polymer.html_script_1.js:52: WARNING - Cannot call non-function type T
39-
let extended = mixin(base);
40-
^^^^^^^^^^^
41-
42-
/polymer.html_script_1.js:58: WARNING - Cannot access property __dedupeId of non-object type T.
43-
extended.__mixinSet[mixin.__dedupeId] = true;
44-
^^^^^
8+
/polymer.html_script_1.js:59: WARNING - invalid cast - the types do not have a common subtype
9+
from: function(?=):?
10+
to : T
11+
return /** @type {T} */(dedupingMixin);
12+
^^^^^^^^^^^^^^^
4513

4614
/polymer.html_script_10.js:144: WARNING - Property name never defined on trigger of type Object
4715
let triggerPath = trigger.name;
@@ -1378,4 +1346,4 @@ externs/closure-types.js:762: WARNING - property toggle on interface Polymer_Arr
13781346
Polymer_ArraySelectorMixin.prototype.toggle;
13791347
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13801348

1381-
0 error(s), 299 warning(s), 72.7% typed
1349+
0 error(s), 293 warning(s), 72.8% typed

lib/utils/mixin.html

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,14 @@
2020
let dedupeId = 0;
2121

2222
/**
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.
23+
* @constructor
24+
* @extends {Function}
2825
*/
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-
}
26+
function MixinFunction(){}
27+
/** @type {(WeakMap | undefined)} */
28+
MixinFunction.prototype.__mixinApplications;
29+
/** @type {(Object | undefined)} */
30+
MixinFunction.prototype.__mixinSet;
4331

4432
/**
4533
* Wraps an ES6 class expression mixin such that the mixin is only applied
@@ -53,23 +41,34 @@
5341
* mixin applications to base
5442
*/
5543
Polymer.dedupingMixin = function(mixin) {
56-
mixin = cachingMixin(mixin);
44+
let mixinApplications = /** @type {!MixinFunction} */(mixin).__mixinApplications;
45+
if (!mixinApplications) {
46+
mixinApplications = new WeakMap();
47+
/** @type {!MixinFunction} */(mixin).__mixinApplications = mixinApplications;
48+
}
5749
// maintain a unique id for each mixin
58-
mixin.__dedupeId = ++dedupeId;
59-
return function(base) {
60-
let baseSet = base.__mixinSet;
61-
if (baseSet && baseSet[mixin.__dedupeId]) {
50+
let mixinDedupeId = dedupeId++;
51+
function dedupingMixin(base) {
52+
let baseSet = /** @type {!MixinFunction} */(base).__mixinSet;
53+
if (baseSet && baseSet[mixinDedupeId]) {
6254
return base;
6355
}
64-
let extended = mixin(base);
56+
let map = mixinApplications;
57+
let extended = map.get(base);
58+
if (!extended) {
59+
extended = /** @type {!Function} */(mixin)(base);
60+
map.set(base, extended);
61+
}
6562
// copy inherited mixin set from the extended class, or the base class
6663
// NOTE: we avoid use of Set here because some browser (IE11)
6764
// cannot extend a base Set via the constructor.
68-
extended.__mixinSet =
69-
Object.create(extended.__mixinSet || baseSet || null);
70-
extended.__mixinSet[mixin.__dedupeId] = true;
65+
let mixinSet = Object.create(/** @type {!MixinFunction} */(extended).__mixinSet || baseSet || null);
66+
mixinSet[mixinDedupeId] = true;
67+
/** @type {!MixinFunction} */(extended).__mixinSet = mixinSet;
7168
return extended;
7269
}
70+
71+
return /** @type {T} */(dedupingMixin);
7372
};
7473

7574
})();

0 commit comments

Comments
 (0)