• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2/*
3 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.propDeepCopy = void 0;
18const observable_1 = require("./observable");
19/*
20    When decorating variables of complex types,
21    @Prop makes a deep copy, during which all types,
22    except primitive types, Map, Set, Date, and Array, will be lost.
23 */
24function propDeepCopy(sourceObject) {
25    if (!sourceObject || typeof sourceObject !== 'object') {
26        return sourceObject;
27    }
28    const copiedObjects = new Map();
29    return recursiveDeepCopy(sourceObject);
30    function recursiveDeepCopy(sourceObject) {
31        if (!sourceObject || typeof sourceObject !== 'object') {
32            return sourceObject;
33        }
34        const storedObject = copiedObjects.get(sourceObject);
35        if (storedObject !== undefined) {
36            return storedObject;
37        }
38        const copy = copyDeepTrackable(sourceObject);
39        const objectToCopyFrom = (0, observable_1.getObservableTarget)(sourceObject);
40        Object.keys(objectToCopyFrom)
41            .forEach((key) => {
42            const property = objectToCopyFrom[key];
43            if (typeof property === "function") {
44                Reflect.set(copy, key, property);
45                copy[key] = copy[key].bind(copy);
46                return;
47            }
48            Reflect.set(copy, key, recursiveDeepCopy(property));
49        });
50        return copy;
51    }
52    function copyDeepTrackable(sourceObject) {
53        if (sourceObject instanceof Set) {
54            const copy = new Set();
55            Object.setPrototypeOf(copy, Object.getPrototypeOf(sourceObject));
56            copiedObjects.set(sourceObject, copy);
57            for (const setKey of sourceObject.keys()) {
58                copy.add(recursiveDeepCopy(setKey));
59            }
60            return copy;
61        }
62        if (sourceObject instanceof Map) {
63            const copy = new Map();
64            Object.setPrototypeOf(copy, Object.getPrototypeOf(sourceObject));
65            copiedObjects.set(sourceObject, copy);
66            for (const mapKey of sourceObject.keys()) {
67                copy.set(mapKey, recursiveDeepCopy(sourceObject.get(mapKey)));
68            }
69            return copy;
70        }
71        if (sourceObject instanceof Date) {
72            const copy = new Date();
73            copy.setTime(sourceObject.getTime());
74            Object.setPrototypeOf(copy, Object.getPrototypeOf(sourceObject));
75            copiedObjects.set(sourceObject, copy);
76            return copy;
77        }
78        if (sourceObject instanceof Object) {
79            const copy = Array.isArray(sourceObject) ? [] : {};
80            Object.setPrototypeOf(copy, Object.getPrototypeOf(sourceObject));
81            copiedObjects.set(sourceObject, copy);
82            return copy;
83        }
84        return sourceObject;
85    }
86}
87exports.propDeepCopy = propDeepCopy;
88//# sourceMappingURL=prop-deep-copy.js.map