Add config to StructuredCloneMap

dabcf6e9943a3d13f5ce22858ee555ba970217e0

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +20 -0Ignore whitespace
public/scripts/util/StructuredCloneMap.js+20 -0
@@ -6,6 +6,18 @@
66 */
77export class StructuredCloneMap extends Map {
88 /**
9+ * Constructs a new StructuredCloneMap.
10+ * @param {object} options - Options for the map
11+ * @param {boolean} options.cloneOnGet - Whether to clone the value when getting it from the map
12+ * @param {boolean} options.cloneOnSet - Whether to clone the value when setting it in the map
13+ */
14+ constructor({ cloneOnGet, cloneOnSet } = { cloneOnGet: true, cloneOnSet: true }) {
15+ super();
16+ this.cloneOnGet = cloneOnGet;
17+ this.cloneOnSet = cloneOnSet;
18+ }
19+
20+ /**
921 * Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
1022 *
1123 * The set value will always be a deep clone of the provided value to provide consistent data storage.
@@ -15,6 +27,10 @@ export class StructuredCloneMap extends Map {
1527 * @returns {this} The updated map
1628 */
1729 set(key, value) {
30+ if (!this.cloneOnSet) {
31+ return super.set(key, value);
32+ }
33+
1834 const clonedValue = structuredClone(value);
1935 super.set(key, clonedValue);
2036 return this;
@@ -30,6 +46,10 @@ export class StructuredCloneMap extends Map {
3046 * @returns {V | undefined} Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
3147 */
3248 get(key) {
49+ if (!this.cloneOnGet) {
50+ return super.get(key);
51+ }
52+
3353 const value = super.get(key);
3454 return structuredClone(value);
3555 }