1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.hasMixin = exports.registerMixins = exports.getMixinsForClass = void 0;
|
---|
4 | const util_1 = require("./util");
|
---|
5 | // Keeps track of constituent classes for every mixin class created by ts-mixer.
|
---|
6 | const mixins = new Map();
|
---|
7 | const getMixinsForClass = (clazz) => mixins.get(clazz);
|
---|
8 | exports.getMixinsForClass = getMixinsForClass;
|
---|
9 | const registerMixins = (mixedClass, constituents) => mixins.set(mixedClass, constituents);
|
---|
10 | exports.registerMixins = registerMixins;
|
---|
11 | const hasMixin = (instance, mixin) => {
|
---|
12 | if (instance instanceof mixin)
|
---|
13 | return true;
|
---|
14 | const constructor = instance.constructor;
|
---|
15 | const visited = new Set();
|
---|
16 | let frontier = new Set();
|
---|
17 | frontier.add(constructor);
|
---|
18 | while (frontier.size > 0) {
|
---|
19 | // check if the frontier has the mixin we're looking for. if not, we can say we visited every item in the frontier
|
---|
20 | if (frontier.has(mixin))
|
---|
21 | return true;
|
---|
22 | frontier.forEach(item => visited.add(item));
|
---|
23 | // build a new frontier based on the associated mixin classes and prototype chains of each frontier item
|
---|
24 | const newFrontier = new Set();
|
---|
25 | frontier.forEach(item => {
|
---|
26 | var _a;
|
---|
27 | const itemConstituents = (_a = mixins.get(item)) !== null && _a !== void 0 ? _a : (0, util_1.protoChain)(item.prototype).map(proto => proto.constructor).filter(item => item !== null);
|
---|
28 | if (itemConstituents)
|
---|
29 | itemConstituents.forEach(constituent => {
|
---|
30 | if (!visited.has(constituent) && !frontier.has(constituent))
|
---|
31 | newFrontier.add(constituent);
|
---|
32 | });
|
---|
33 | });
|
---|
34 | // we have a new frontier, now search again
|
---|
35 | frontier = newFrontier;
|
---|
36 | }
|
---|
37 | // if we get here, we couldn't find the mixin anywhere in the prototype chain or associated mixin classes
|
---|
38 | return false;
|
---|
39 | };
|
---|
40 | exports.hasMixin = hasMixin;
|
---|