All files / src/utils/data/record DataRecord.ts

87.69% Statements 57/65
75% Branches 21/28
78.57% Functions 11/14
87.3% Lines 55/63

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234  4x 4x   4x         4x   63x   63x                       63x       63x   277x                                                                 60x   60x   270x   270x   270x                   60x   60x   60x               122x   122x   49x     73x   73x   330x   330x   330x   327x         73x   73x         5x             59x   59x   150x   59x     59x         18x         12x   53x   53x   53x   53x                 12x         3x   1x         2x   2x   8x   8x                 61x   25x         36x         36x   36x     27x         9x       36x                            
import Subscriber from "../../observer/Subscriber";
import DataField from "./DataField";
import DataRecordDescriptor from "./DataRecordDescriptor";
import { DataFieldDescriptor, DataProvider, DataSetter } from "./interfaces";
import areEqual from "../../areEqual";
 
/**
 * The collection of data fields
 */
export default class DataRecord implements DataProvider, DataSetter, Subscriber {
 
    private _fields: Record<string, DataField> = {};
 
    private _modifiedFields: Record<string, DataField> = {};
 
    private _recordDescriptor: DataRecordDescriptor;
 
    /**
     * The cached id of the record
     */
    private _id: any;
 
    /**
     * The cached data of the record
     */
    private _data?: any = undefined;
 
    constructor(recordDescriptor?: DataRecordDescriptor) {
 
        this._recordDescriptor = recordDescriptor ?? new DataRecordDescriptor();
 
        this._recordDescriptor.fieldDescriptors.forEach(fd => this._fields[fd.name] = new DataField(fd, this));
    }
 
    addField(fd: DataFieldDescriptor): DataField {
 
        this._recordDescriptor.addFieldDescriptor(fd);
 
        this._fields[fd.name] = new DataField(fd, this);
 
        return this._fields[fd.name];
    }
 
    getField(name: string) {
 
        return this._fields[name];
    }
 
    removeField(fd: DataFieldDescriptor) {
 
        this._recordDescriptor.removeFieldDescriptor(fd);
 
        delete this._fields[fd.name];
    }
 
    /**
     * Initializes the data record with the data passed in that matches the field descriptors
     * If the property of the data is not described in the field descriptor, it will be ignored
     * @param data The data to initialize the data record with
     */
    initialize(data: any) {
 
        const {
            _fields
        } = this;
 
        for (const key in data) {
 
            Eif (data.hasOwnProperty(key)) {
 
                Eif (_fields.hasOwnProperty(key)) { // The field exists, initialize it
 
                    _fields[key].initialize(data[key]);
                }
                else {
 
                    console.warn(`There is no field for property: '${key}' that was passed as data`);
                }
 
            }
        }
 
        this._id = undefined;
 
        this._data = undefined;
 
        this._modifiedFields = {};
    }
 
    getData(): any {
 
        const {
            _data,
            _fields
        } = this;
 
        if (_data !== undefined) {
 
            return _data;
        }
 
        const data: any = {};
 
        for (const key in _fields) {
 
            Eif (_fields.hasOwnProperty(key)) {
 
                const value = _fields[key].value;
 
                if (value != undefined && value != null) {
 
                    data[key] = value;
                }
            }
        }
 
        this._data = data;
 
        return data;
    }
 
    set id(id: any) {
 
        this._id = id;
    }
 
    get id() {
 
        const {
            _fields
        } = this;
 
        Eif (this._id === undefined) {
 
            const idInfo = this._recordDescriptor.getId(_fields, (f: { value: any; }) => f.value);
 
            this._id = idInfo.value;
        }
 
        return this._id;
    }
 
    get isModified() {
 
        return Object.keys(this._modifiedFields).length > 0;
    }
 
    setData(data: Record<string, any>): void {
 
        for (const key in data) {
 
            Eif (data.hasOwnProperty(key)) {
 
                const field = this._fields[key];
 
                Eif (field !== undefined) {
 
                    field.value = data[key];
                }
                else { // The field does not need to exist for the given data member but let the programmer know it is missing
 
                    console.warn(`Field of name: '${key}' was not found for data memeber with same name`);
                }
            }
        }
 
        this._id = undefined;
    }
 
    reset() {
 
        if (!this.isModified) {
 
            return;
        }
 
        const {
            _fields
        } = this;
 
        for (const key in _fields) {
 
            Eif (_fields.hasOwnProperty(key)) {
 
                _fields[key].reset();
            }
        }
 
        //this._modifiedFields = {}; It should be empty
    }
 
    onValueSet(descriptor: DataFieldDescriptor, value: any, oldValue: any, initialValue: any) {
 
        if (areEqual(value, oldValue)) {
 
            return; // Nothing to change
        }
 
        const {
            name
        } = descriptor;
 
        const {
            _fields,
            _modifiedFields
        } = this;
 
        if (!areEqual(value, initialValue)) {
 
            // Reference the field in the modified set
            _modifiedFields[name] = _fields[name];
        }
        else {
 
            // Remove it from the modified fields
            delete _modifiedFields[name];
        }
 
        // Clear the data cache to get new data
        this._data = undefined;
    }
 
    // commit(callback: (record: any) => void) {
 
    //     if (!this.isModified) {
 
    //         return;
    //     }
 
    //     callback(this._data);
 
    //     this.initialize(this._data);
    // }
}