All files / src/feature feature.ts

67.19% Statements 43/64
34.62% Branches 9/26
52.63% Functions 10/19
67.19% Lines 43/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157  2x 2x   2x   2x 2x           2x 2x   2x           2x   2x         2x 2x 1x       2x           3x       6x       1x       2x 2x       2x   2x     2x       3x 2x                     1x     3x     2x       2x       2x       2x 1x 1x           2x 2x             2x                         2x                   1x 1x 1x         5x 5x                 8x 8x    
import { XEvent, XGroup, XGroupSpec, XInit } from '../typing';
import { defineGetters } from '../utils';
import { isXGroup } from '../xgroup/xgroup';
import { Feature, ExperimentDescription, ExperimentsObserver, FeatureDescription, FeatureChangeObserver } from './feature.typings';
import { createConsoleReporter } from '../../reporter/console';
 
const featuresRegistry = {} as {[id: string]: Feature<any, any>};
const experimentsRegistry = {} as {
	[id: string]: {
		feature: Feature<string, XGroup<string, XGroupSpec, XInit>>;
		description: ExperimentDescription;
	};
};
const experimentsObservers = [] as ExperimentsObserver[];
const featureChangeObservers = [] as FeatureChangeObserver[];
 
export function createFeature<
	ID extends string,
	XG extends XGroup<string, XGroupSpec, XInit>,
>(
	descr: FeatureDescription<ID, XG>,
): Feature<ID, XG> {
	const existsEeature = featuresRegistry[descr.id];
 
	Iif (existsEeature !== void 0) {
		Object.assign(existsEeature, descr);
		return existsEeature;
	}
 
	Eif (isXGroup(descr.events)) {
		descr.events.$on((xevt) => {
			notifyExperimentsObservers(feature, xevt);
		});
	}
 
	const feature = defineGetters({...descr}, {
		split() {
			return getFeatureState(feature, 'split');
		},
 
		active() {
			return this.enabled && !!getFeatureState(feature, 'active');
		},
 
		enabled() {
			return !!getFeatureState(feature, 'enabled');
		},
 
		released() {
			return !!getFeatureState(feature, 'released');
		},
	});
 
	featuresRegistry[feature.id] = feature;
	Iif (experimentsRegistry[feature.id] !== void 0) {
		experimentsRegistry[feature.id].feature = feature;
	}
 
	notifyFeatureChangeObservers(feature);
 
	return feature;
}
 
export function setupExperiment(
	feature: Feature<string, any>,
	description: Partial<ExperimentDescription>,
) {
	if (experimentsRegistry[feature.id] === void 0) {
		experimentsRegistry[feature.id] = {
			feature,
			description: {
				split: null,
				active: false,
				enabled: false,
				released: false,
				...description,
			},
		};
	} else {
		Object.assign(experimentsRegistry[feature.id].description, description);
	}
 
	notifyFeatureChangeObservers(feature);
}
 
export function getFeature<ID extends string>(id: ID): Feature<ID, any> | null {
	return featuresRegistry[id] === void 0 ? null : featuresRegistry[id];
}
 
export function getAllFeatures() {
	return Object.values(featuresRegistry);
}
 
export function getAllExperiment() {
	return Object.values(experimentsRegistry);
}
 
export function addExperimentsObserver(fn: ExperimentsObserver) {
	experimentsObservers.push(fn);
	return () => {
		const idx = experimentsObservers.indexOf(fn);
		(idx !== -1) && experimentsObservers.splice(idx, 1);
	};
}
 
const _verboseReporter = createConsoleReporter();
let _verboseExperimentsUnobserve = null as (null | (() => void))
 
export type VerboseFilter = (
	feature: Feature<string, any>,
	xevt: XEvent<string, any, any>,
) => boolean;
 
export function verboseExperiments(state?: boolean | VerboseFilter) {
	_verboseExperimentsUnobserve && _verboseExperimentsUnobserve();
	_verboseExperimentsUnobserve = null;
	
	if (state === true) {
		_verboseExperimentsUnobserve = addExperimentsObserver(_verboseReporter);
	} else if (typeof state === 'function') {
		_verboseExperimentsUnobserve = addExperimentsObserver((f, e) => {
			state(f, e) && _verboseReporter(f, e);
		});
	}
}
 
export function addFeatureChangeObserver(fn: FeatureChangeObserver) {
	featureChangeObservers.push(fn);
 
	return () => {
		const idx = featureChangeObservers.indexOf(fn);
		(idx !== -1) && featureChangeObservers.splice(idx, 1);
	};
}
 
function notifyExperimentsObservers(feature: Feature<string, any>, xevt: XEvent<string, any, any>) {
	let idx = experimentsObservers.length;
	while (idx--) {
		experimentsObservers[idx](feature, xevt);
	}
}
 
function notifyFeatureChangeObservers(feature: Feature<string, any>) {
	let idx = featureChangeObservers.length;
	while (idx--) {
		featureChangeObservers[idx](feature);
	}
}
 
function getFeatureState<K extends keyof ExperimentDescription>(
	descr: FeatureDescription<string, any>,
	key: K,
): ExperimentDescription[K] | null {
	const item = experimentsRegistry[descr.id];
	return item === void 0 ? null : item.description[key];
}