Skip to content

Commit

Permalink
fix: updated immutability patterns to avoid using the older `JSON.par…
Browse files Browse the repository at this point in the history
…se(JSON.stringify())` approach in favor of `structureClone()` and standard shallow copying to better handle circular references
  • Loading branch information
DRiFTy17 committed Nov 9, 2023
1 parent acc7562 commit a2ee413
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/lib/autocomplete/autocomplete-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,11 @@ export class AutocompleteFoundation extends ListDropdownAwareFoundation implemen
if (value == null) {
values = [];
} else if (Array.isArray(value)) {
values = JSON.parse(JSON.stringify(value));
values = structuredClone(value);
} else if (isString(value)) {
values = [value];
} else {
values = [JSON.parse(JSON.stringify(value))];
values = [structuredClone(value)];
}

const hasNewValue = values.length !== this._values.length || values.some(v => !this._values.includes(v));
Expand Down
4 changes: 0 additions & 4 deletions src/lib/menu/menu-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,6 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation<IMenuOp
} else {
this.optionsFactory = undefined;
// Intentional shallow copy of member properties. These member objects have properties that are references to functions.
// The typical JSON.parse(JSON.stringify(object)) will not work here. If this becomes an issue we'll add a deepClone
// function to the core library.
this._options = options.map(o => ({ ...o }));

if (this._open) {
Expand All @@ -523,8 +521,6 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation<IMenuOp
}

// Intentional shallow copy of member properties. These member objects have properties that are references to functions.
// The typical JSON.parse(JSON.stringify(object)) will not work here. If this becomes an issue we'll add a deepClone
// function to the core library.
return this._flatOptions.map(o => ({ ...o }));
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/stepper/stepper/stepper-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class StepperFoundation implements IStepperFoundation {

/** The step configurations. */
public get steps(): IStepConfiguration[] {
return JSON.parse(JSON.stringify(this._steps));
return this._steps.map(s => ({ ...s })); // Shallow clone
}
public set steps(value: IStepConfiguration[]) {
if (Array.isArray(value) && value.length > 0) {
Expand Down
6 changes: 1 addition & 5 deletions src/lib/table/table-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,12 @@ export class TableFoundation implements ITableFoundation {
this._renderBody();
}
public get data(): any[] {
return JSON.parse(JSON.stringify(this._data));
return this._data.map(o => ({ ...o })); // Shallow clone
}

/** The column configuration options. */
public set columnConfigurations(value: IColumnConfiguration[]) {
// Intentional shallow copy of member properties. These member objects have properties that are references to functions.
// The typical JSON.parse(JSON.stringify(object)) will not work here. If this becomes an issue we'll add a deepClone
// function to the core library.
this._columnConfigurations = value.map(cc => ({ ...cc }));

// Update hidden column manager
Expand All @@ -206,8 +204,6 @@ export class TableFoundation implements ITableFoundation {
}
public get columnConfigurations(): IColumnConfiguration[] {
// Intentional shallow copy of member properties. These member objects have properties that are references to functions.
// The typical JSON.parse(JSON.stringify(object)) will not work here. If this becomes an issue we'll add a deepClone
// function to the core library.
return this._columnConfigurations.map(cc => ({ ...cc }));
}

Expand Down

0 comments on commit a2ee413

Please sign in to comment.