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 @@
6 */6 */
7export class StructuredCloneMap extends Map {7export class StructuredCloneMap extends Map {
8 /**8 /**
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 /**
9 * 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.21 * 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.
10 *22 *
11 * The set value will always be a deep clone of the provided value to provide consistent data storage.23 * 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 {
15 * @returns {this} The updated map27 * @returns {this} The updated map
16 */28 */
17 set(key, value) {29 set(key, value) {
30 if (!this.cloneOnSet) {
31 return super.set(key, value);
32 }
33
18 const clonedValue = structuredClone(value);34 const clonedValue = structuredClone(value);
19 super.set(key, clonedValue);35 super.set(key, clonedValue);
20 return this;36 return this;
@@ -30,6 +46,10 @@ export class StructuredCloneMap extends Map {
30 * @returns {V | undefined} Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.46 * @returns {V | undefined} Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
31 */47 */
32 get(key) {48 get(key) {
49 if (!this.cloneOnGet) {
50 return super.get(key);
51 }
52
33 const value = super.get(key);53 const value = super.get(key);
34 return structuredClone(value);54 return structuredClone(value);
35 }55 }