Add config to StructuredCloneMap
| @@ -6,6 +6,18 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | export 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 | 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 | 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 | 27 | * @returns {this} The updated map |
| 16 | 28 | */ |
| 17 | 29 | set(key, value) { |
| 30 | + if (!this.cloneOnSet) { | |
| 31 | + return super.set(key, value); | |
| 32 | + } | |
| 33 | + | |
| 18 | 34 | const clonedValue = structuredClone(value); |
| 19 | 35 | super.set(key, clonedValue); |
| 20 | 36 | return this; |
| @@ -30,6 +46,10 @@ export class StructuredCloneMap extends Map { | ||
| 30 | 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 | 48 | get(key) { |
| 49 | + if (!this.cloneOnGet) { | |
| 50 | + return super.get(key); | |
| 51 | + } | |
| 52 | + | |
| 33 | 53 | const value = super.get(key); |
| 34 | 54 | return structuredClone(value); |
| 35 | 55 | } |