All files / src/accessor accessor-manager.ts

100% Statements 80/80
100% Branches 34/34
100% Functions 18/18
100% Lines 74/74

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 18222x 22x   22x     22x 155x   155x     1880x       916x 3x                 265x   265x               18x 18x 18x 17x 17x                 17x 17x 17x 17x   17x   17x     17x 18x 18x     17x 13x   13x 13x       13x 60x   13x 18x       13x   27x 3x   24x 24x     24x 6x 5x   1x       24x     27x 27x   13x 13x 13x 13x   13x 13x   13x   13x             13x   4x 7x   1x                           13x     13x 44x   13x 14x     13x   13x         13x     13x 42x   42x     13x 13x 12x     13x       26x       57x      
import get from 'lodash.get';
import { Loader } from '../loader/loader';
import { IDictionary } from '../utils/interfaces';
import { Accessor } from './accessors';
import { IAccessorManager } from './interfaces';
 
export class AccessorManager implements IAccessorManager {
	private accessors: { [key: string]: { instance: any; accessor: string } } = {};
 
	private props: IDictionary = {};
 
	hasAccessor(name: string): boolean {
		return !!this.accessors[name];
	}
 
	addProp(name: string, value: any) {
		if (Accessor.isAccessorDefinition(value)) {
			this.props[name] = value;
		}
	}
 
	/**
	 * Creates all accessors based on props value.
	 */
	createAccessors(obj: any, props: IDictionary = this.props): void {
		// TODO: re-factory this forEach for better performance
		Object.keys(props).forEach((name) => this.createAccessor(name, obj, props));
 
		this.props = {};
	}
 
	/**
	 * Creates a new accessor.
	 * @param name Accessor name
	 */
	private createAccessor(name: string, obj: any, props: IDictionary) {
		const [controller, accessor] = Accessor.getAccessor(props[name]);
		const instance = Loader.getInstance(controller);
		if (instance) {
			this.accessors[name] = { accessor, instance };
			this.defineAccessor(name, obj);
		}
	}
 
	/**
	 * Replaces instance property to accessor reference.
	 * @param name Accessor name. It can also be the path to a property, separated by `.`
	 */
	private defineAccessor(name: string, mainObj: any) {
		const split = name.split('.');
		const propName = split.pop() as string;
		const path = split.join('.');
		const { instance, accessor } = this.accessors[name];
 
		const obj = path ? get(mainObj, path) : mainObj;
 
		let objProto = obj;
		let objDescriptor;
 
		do {
			objDescriptor = this.getDescriptor(objProto, name);
			objProto = Object.getPrototypeOf(objProto);
		} while (objProto && !objDescriptor);
 
		if (objDescriptor?.set && objDescriptor.get) {
			const ctrlProtoDescriptor = this.getProtoDescriptor(instance, accessor);
 
			const objGetFunction = objDescriptor.get.bind(obj);
			const objSetFunction = objDescriptor.set.bind(obj);
 
			let ctrlGetFunction: any;
 
			if (!ctrlProtoDescriptor || ctrlProtoDescriptor.get) {
				ctrlGetFunction = () => instance[accessor];
			}
			const ctrlSetFunction: any = (value: any) => {
				instance[accessor] = value;
			};
 
			let prevGet: any;
			const getFunction = () => {
				let value: any;
				if (!ctrlGetFunction) {
					value = objGetFunction();
				} else {
					const ctrlVal = ctrlGetFunction();
					const objVal = objGetFunction();
 
					// if the values are different, returns the value that is different from the previously returned value
					if (ctrlVal !== objVal) {
						if (ctrlSetFunction && ctrlVal === prevGet) {
							ctrlSetFunction(objVal);
						} else {
							objSetFunction(ctrlVal);
						}
					}
 
					value = ctrlGetFunction();
				}
 
				prevGet = value;
				return value;
			};
			const setFunction = (value: any) => {
				let newValue = value;
				if (ctrlSetFunction) {
					ctrlSetFunction(value);
				}
				if (ctrlGetFunction) newValue = ctrlGetFunction();
				objSetFunction(newValue);
			};
			objSetFunction.call(obj, instance[accessor]);
 
			Object.defineProperty(obj, propName, {
				get: getFunction,
				set: setFunction,
				enumerable: true,
				configurable: true,
			});
 
			this.defineNewCtrlDescriptor(instance, accessor, objSetFunction, objGetFunction);
		} else {
			Object.defineProperty(obj, propName, {
				get: () => instance[accessor],
				set: (value: any) => {
					instance[accessor] = value;
				},
				enumerable: true,
				configurable: true,
			});
		}
	}
 
	private defineNewCtrlDescriptor(
		instance: any,
		accessor: string,
		objSetFunction: (value: any) => void,
		objGetFunction: () => any,
	) {
		const ctrlProtoDescriptor = this.getProtoDescriptor(instance, accessor);
 
		// basic getter/setter for the controller property (in case it doesnt have getter/setter)
		let accessorValue = instance[accessor];
		const basicCtrlGetFunction = () => accessorValue;
 
		const basicCtrlSetFunction = (newValue: any) => {
			accessorValue = newValue;
		};
 
		const ctrlDescriptor = this.getDescriptor(instance, accessor) || ctrlProtoDescriptor;
 
		const newCtrlDescriptor: PropertyDescriptor = {
			enumerable: true,
			configurable: true,
		};
 
		const prevCtrlSet = ctrlDescriptor?.set?.bind(instance) ?? basicCtrlSetFunction.bind(instance);
 
		// this setter will call the setter of each object that uses this accessor
		const newCtrlSetFunction = (value: any) => {
			if (value !== objGetFunction()) objSetFunction(value);
 
			if (prevCtrlSet) prevCtrlSet(value);
		};
 
		newCtrlDescriptor.set = newCtrlSetFunction.bind(instance);
		if (!ctrlProtoDescriptor || ctrlProtoDescriptor.get) {
			newCtrlDescriptor.get = ctrlDescriptor?.get?.bind(instance) ?? basicCtrlGetFunction.bind(instance);
		}
 
		Object.defineProperty(instance, accessor, newCtrlDescriptor);
	}
 
	private getProtoDescriptor(instance: any, accessor: string) {
		return this.getDescriptor(Object.getPrototypeOf(instance), accessor);
	}
 
	private getDescriptor(instance: any, accessor: string) {
		return Object.getOwnPropertyDescriptor(instance, accessor);
	}
}