All files / mixins StateMetadataInitializerMixin.ts

94.12% Statements 16/17
70% Branches 7/10
100% Functions 5/5
94.12% Lines 16/17

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    3x   9x                     13x   13x   12x     1x   1x   1x           2x       1x               1x           1x   1x   1x   1x               3x
import { CustomElementMetadata, CustomElementStateMetadata } from "../interfaces";
 
const StateMetadataInitializerMixin = Base =>
 
    class StateMetadataInitializer extends Base {
 
        /**
         * The state to track in the class
         */
        static state: () => Record<string, CustomElementStateMetadata>;
 
        protected static initializeState(metadata: CustomElementMetadata): void {
 
            const {
                state
            } = this;
 
            if (state === undefined) {
 
                return;
            }
 
            Object.entries(state).forEach(([name, stateMetadata]) => {
 
                stateMetadata.name = name; // Set the name of the state property
 
                Object.defineProperty(
                    this.prototype,
                    name,
                    {
                        get(): any {
 
                            return this._state[name];
                        },
                        set(this: any, value: unknown) {
 
                            this.setState(name, value);
                        },
                        configurable: true,
                        enumerable: true,
                    }
                );
 
                // Add it to the metadata properties so the properties of the instances can be validated and initialized
                metadata.state.set(name, stateMetadata);
 
            });
 
            // Add the properties of the state base class if any so we can validate and initialize
            // the values of the properties of the state of the base class in the instance
            const baseClass = Object.getPrototypeOf(this.prototype)?.constructor;
 
            Eif (baseClass !== undefined) {
 
                const baseClassMetadata = baseClass.metadata;
 
                Iif (baseClassMetadata !== undefined) {
 
                    metadata.state = new Map([...metadata.state, ...baseClassMetadata.state]);
                }
            }
        }
    }
 
export default StateMetadataInitializerMixin;