source: node_modules/ts-mixer/dist/cjs/mixin-tracking.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[d24f17c]1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.hasMixin = exports.registerMixins = exports.getMixinsForClass = void 0;
4const util_1 = require("./util");
5// Keeps track of constituent classes for every mixin class created by ts-mixer.
6const mixins = new Map();
7const getMixinsForClass = (clazz) => mixins.get(clazz);
8exports.getMixinsForClass = getMixinsForClass;
9const registerMixins = (mixedClass, constituents) => mixins.set(mixedClass, constituents);
10exports.registerMixins = registerMixins;
11const 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};
40exports.hasMixin = hasMixin;
Note: See TracBrowser for help on using the repository browser.