From dabcf6e9943a3d13f5ce22858ee555ba970217e0 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Mon, 22 Jul 2024 22:34:03 +0300 Subject: [PATCH] Add config to StructuredCloneMap --- public/scripts/util/StructuredCloneMap.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/public/scripts/util/StructuredCloneMap.js b/public/scripts/util/StructuredCloneMap.js index 51173e630..42dea5faa 100644 --- a/public/scripts/util/StructuredCloneMap.js +++ b/public/scripts/util/StructuredCloneMap.js @@ -5,6 +5,18 @@ * @extends Map */ export class StructuredCloneMap extends Map { + /** + * Constructs a new StructuredCloneMap. + * @param {object} options - Options for the map + * @param {boolean} options.cloneOnGet - Whether to clone the value when getting it from the map + * @param {boolean} options.cloneOnSet - Whether to clone the value when setting it in the map + */ + constructor({ cloneOnGet, cloneOnSet } = { cloneOnGet: true, cloneOnSet: true }) { + super(); + this.cloneOnGet = cloneOnGet; + this.cloneOnSet = cloneOnSet; + } + /** * 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. * @@ -15,6 +27,10 @@ export class StructuredCloneMap extends Map { * @returns {this} The updated map */ set(key, value) { + if (!this.cloneOnSet) { + return super.set(key, value); + } + const clonedValue = structuredClone(value); super.set(key, clonedValue); return this; @@ -30,6 +46,10 @@ export class StructuredCloneMap extends Map { * @returns {V | undefined} Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned. */ get(key) { + if (!this.cloneOnGet) { + return super.get(key); + } + const value = super.get(key); return structuredClone(value); }