Skip to content

Commit 91f64d2

Browse files
authored
refactor: replace deepClone with structuredClone (@fehmer) (monkeytypegame#6882)
1 parent a070911 commit 91f64d2

File tree

13 files changed

+15
-99
lines changed

13 files changed

+15
-99
lines changed

frontend/__tests__/controllers/preset-controller.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as UpdateConfig from "../../src/ts/config";
66
import * as Notifications from "../../src/ts/elements/notifications";
77
import * as TestLogic from "../../src/ts/test/test-logic";
88
import * as TagController from "../../src/ts/controllers/tag-controller";
9-
import { deepClone } from "../../src/ts/utils/misc";
109

1110
describe("PresetController", () => {
1211
describe("apply", () => {
@@ -107,7 +106,7 @@ describe("PresetController", () => {
107106
});
108107

109108
UpdateConfig.setNumbers(true);
110-
const oldConfig = deepClone(UpdateConfig.default);
109+
const oldConfig = structuredClone(UpdateConfig.default);
111110

112111
//WHEN
113112
await PresetController.apply(preset._id);

frontend/__tests__/utils/misc.spec.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { deepClone, getErrorMessage, isObject } from "../../src/ts/utils/misc";
2+
import { getErrorMessage, isObject } from "../../src/ts/utils/misc";
33
import {
44
getLanguageDisplayString,
55
removeLanguageSize,
@@ -122,57 +122,7 @@ describe("misc.ts", () => {
122122
});
123123
});
124124
});
125-
describe("deepClone", () => {
126-
it("should correctly clone objects", () => {
127-
const tests = [
128-
{
129-
input: {},
130-
expected: {},
131-
},
132-
{
133-
input: { a: 1 },
134-
expected: { a: 1 },
135-
},
136-
{
137-
input: { a: { b: 2 } },
138-
expected: { a: { b: 2 } },
139-
},
140-
{
141-
input: { a: { b: 2 }, c: [1, 2, 3] },
142-
expected: { a: { b: 2 }, c: [1, 2, 3] },
143-
},
144-
{
145-
input: [],
146-
expected: [],
147-
},
148-
{
149-
input: [1, 2, 3],
150-
expected: [1, 2, 3],
151-
},
152-
{
153-
input: "string",
154-
expected: "string",
155-
},
156-
{
157-
input: 1,
158-
expected: 1,
159-
},
160-
{
161-
input: null,
162-
expected: null,
163-
},
164-
{
165-
input: undefined,
166-
expected: undefined,
167-
},
168-
];
169125

170-
tests.forEach((test) => {
171-
const result = deepClone(test.input);
172-
expect(result).toStrictEqual(test.expected);
173-
});
174-
});
175-
});
176126
describe("getErrorMesssage", () => {
177127
it("should correctly get the error message", () => {
178128
const tests = [

frontend/src/ts/constants/default-config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Config, CustomThemeColors } from "@monkeytype/schemas/configs";
2-
import { deepClone } from "../utils/misc";
32

43
const obj = {
54
theme: "serika_dark",
@@ -105,5 +104,5 @@ const obj = {
105104
} as Config;
106105

107106
export function getDefaultConfig(): Config {
108-
return deepClone(obj);
107+
return structuredClone(obj);
109108
}

frontend/src/ts/constants/default-result-filters.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ResultFilters } from "@monkeytype/schemas/users";
2-
import { deepClone } from "../utils/misc";
32
import { LanguageList } from "./languages";
43
import { getFunboxNames } from "@monkeytype/funbox";
54

@@ -67,4 +66,4 @@ const object: ResultFilters = {
6766
},
6867
};
6968

70-
export default deepClone(object);
69+
export default structuredClone(object);

frontend/src/ts/constants/default-snapshot.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
UserProfileDetails,
55
UserTag,
66
} from "@monkeytype/schemas/users";
7-
import { deepClone } from "../utils/misc";
87
import { getDefaultConfig } from "./default-config";
98
import { Mode } from "@monkeytype/schemas/shared";
109
import { Result } from "@monkeytype/schemas/results";
@@ -135,5 +134,5 @@ const defaultSnap = {
135134
} as Snapshot;
136135

137136
export function getDefaultSnapshot(): Snapshot {
138-
return deepClone(defaultSnap);
137+
return structuredClone(defaultSnap);
139138
}

frontend/src/ts/controllers/preset-controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as Notifications from "../elements/notifications";
55
import * as TestLogic from "../test/test-logic";
66
import * as TagController from "./tag-controller";
77
import { SnapshotPreset } from "../constants/default-snapshot";
8-
import { deepClone } from "../utils/misc";
98

109
export async function apply(_id: string): Promise<void> {
1110
const snapshot = DB.getSnapshot();
@@ -18,7 +17,7 @@ export async function apply(_id: string): Promise<void> {
1817

1918
if (isPartialPreset(presetToApply)) {
2019
await UpdateConfig.apply({
21-
...deepClone(Config),
20+
...structuredClone(Config),
2221
...presetToApply.config,
2322
});
2423
} else {

frontend/src/ts/elements/account/result-filters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function addFilterPresetToSnapshot(filter: ResultFilters): void {
173173
if (!snapshot) return;
174174
DB.setSnapshot({
175175
...snapshot,
176-
filterPresets: [...snapshot.filterPresets, Misc.deepClone(filter)],
176+
filterPresets: [...snapshot.filterPresets, structuredClone(filter)],
177177
});
178178
}
179179

@@ -928,7 +928,7 @@ $(".group.presetFilterButtons .filterBtns").on(
928928

929929
function verifyResultFiltersStructure(filterIn: ResultFilters): ResultFilters {
930930
const filter = mergeWithDefaultFilters(
931-
sanitize(ResultFiltersSchema.partial().strip(), Misc.deepClone(filterIn))
931+
sanitize(ResultFiltersSchema.partial().strip(), structuredClone(filterIn))
932932
);
933933

934934
return filter;

frontend/src/ts/test/custom-text.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { CustomTextLimitMode, CustomTextMode } from "@monkeytype/schemas/util";
22
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";
33
import { z } from "zod";
44
import { CompletedEventCustomTextSchema } from "@monkeytype/schemas/results";
5-
import { deepClone } from "../utils/misc";
65

76
const CustomTextObjectSchema = z.record(z.string(), z.string());
87
type CustomTextObject = z.infer<typeof CustomTextObjectSchema>;
@@ -47,7 +46,7 @@ const customTextSettings = new LocalStorageWithSchema({
4746
schema: CustomTextSettingsSchema,
4847
fallback: defaultCustomTextSettings,
4948
migrate: (oldData, _zodIssues) => {
50-
const fallback = deepClone(defaultCustomTextSettings);
49+
const fallback = structuredClone(defaultCustomTextSettings);
5150

5251
if (typeof oldData !== "object" || oldData === null) {
5352
return fallback;

frontend/src/ts/test/result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ export async function update(
850850
dontSave: boolean
851851
): Promise<void> {
852852
resultAnnotation = [];
853-
result = Misc.deepClone(res);
853+
result = structuredClone(res);
854854
hideCrown();
855855
$("#resultWordsHistory .words").empty();
856856
$("#result #resultWordsHistory").addClass("hidden");

frontend/src/ts/test/test-logic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ export async function finish(difficultyFailed = false): Promise<void> {
971971
dontSave = true;
972972
}
973973

974-
const completedEvent = Misc.deepClone(ce) as CompletedEvent;
974+
const completedEvent = structuredClone(ce) as CompletedEvent;
975975

976976
///////// completed event ready
977977

@@ -1127,7 +1127,7 @@ export async function finish(difficultyFailed = false): Promise<void> {
11271127

11281128
$("#result .stats .dailyLeaderboard").addClass("hidden");
11291129

1130-
TestStats.setLastResult(Misc.deepClone(completedEvent));
1130+
TestStats.setLastResult(structuredClone(completedEvent));
11311131

11321132
if (!ConnectionState.get()) {
11331133
ConnectionState.showOfflineBanner();
@@ -1297,7 +1297,7 @@ async function saveResult(
12971297
//TODO - this type cast was not needed before because we were using JSON cloning
12981298
// but now with the stronger types it shows that we are forcing completed event
12991299
// into a snapshot result - might not cuase issues but worth investigating
1300-
const result = Misc.deepClone(
1300+
const result = structuredClone(
13011301
completedEvent
13021302
) as unknown as SnapshotResult<Mode>;
13031303
result._id = data.insertedId;

0 commit comments

Comments
 (0)