This repository has been archived by the owner on Jul 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
feature-manager.ts
86 lines (76 loc) · 3.52 KB
/
feature-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {privateState, StateProvider} from "../core/private-state";
export interface FeatureMetadata<D, T extends object> {
feature: FeatureOrFactory<T>;
decoration: D;
forceAround: Array<FeatureOrFactory<T>>;
symbols: any[];
}
export type Feature<T extends object> = <T1 extends T>(subj: T1) => T1
export type FeatureFactory<T extends object, C> = (config: C) => <T1 extends T>(subj: T1) => T1
export type FeatureOrFactory<T extends object> = Feature<T> | FeatureFactory<T, any>
/** @internal */
export class FeatureManager {
static readonly instance = new FeatureManager();
readonly featureMetadataProvider: StateProvider<FeatureMetadata<any, any>, FeatureOrFactory<any>>;
featureSymbolsReducer = <T extends object>(symbols: Array<any>, feature: Feature<T>) => symbols.concat(this.featureMetadataProvider(feature).symbols);
// singleton
private constructor() {
if (FeatureManager.instance) {
return FeatureManager.instance;
}
this.featureMetadataProvider = privateState<FeatureMetadata<any, any>, FeatureOrFactory<any>>('feature-metadata', (feature: FeatureOrFactory<any>) => ({
feature: feature,
decoration: null as any,
forceAround: [],
symbols: [feature],
}));
}
/**
* return the metadata of the supplied features, by the order they should apply
* (reverse the argument's order, and apply constraints)
*/
getOrderedMetadata<T extends object>(features: Array<Feature<T>>): Array<FeatureMetadata<any, T>> {
// bubble sort
// you have reached the drakest part of the code. as far as dark code go, I think this one is pretty harmless
const result: Array<FeatureMetadata<any, T>> = [];
// go over each feature ans add them to the result
for (let i = features.length - 1; i >= 0; i--) {
const featureToAdd = this.featureMetadataProvider(features[i]);
// most features have no special constraints
if (featureToAdd.forceAround.length) {
let destinationIdx = 0;
let searching = true;
while (searching && (destinationIdx < result.length)) {
const otherFeature = result[destinationIdx];
let shouldAddBeforeOther = false;
for (let k = 0; !shouldAddBeforeOther && k < featureToAdd.forceAround.length; k++) {
const forceBefore = featureToAdd.forceAround[k];
shouldAddBeforeOther = otherFeature.symbols.indexOf(forceBefore) >= 0;
}
if (shouldAddBeforeOther) {
searching = false;
} else {
destinationIdx++;
}
}
// insert featureToAdd before destinationIdx;
result.splice(destinationIdx, 0, featureToAdd);
} else {
result.push(featureToAdd);
}
}
return result;
}
isConstrained<T extends object>(features: Array<Feature<T>>, symbols: Array<any>) {
for (let i = 0; i < features.length; i++) {
const featureMetadata = this.featureMetadataProvider(features[i]);
for (let j = 0; j < featureMetadata.forceAround.length; j++) {
const forceBefore = featureMetadata.forceAround[j];
if (symbols.indexOf(forceBefore) >= 0) {
return true;
}
}
}
return false;
}
}