Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use structuredClone for deep copying #1105

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export default class HttpServer implements ProtocolServer {
for (const inUri in this.urlRewrite) {
const toUri = this.urlRewrite[inUri];
if (form.href.endsWith(toUri)) {
const form2: TD.Form = JSON.parse(JSON.stringify(form)); // deep copy
const form2 = structuredClone(form);
form2.href = form2.href.substring(0, form.href.lastIndexOf(toUri)) + inUri;
forms.push(form2);
debug(`HttpServer on port ${this.getPort()} assigns urlRewrite '${form2.href}' for '${form.href}'`);
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/consumed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,9 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
this.properties = {};
this.actions = {};
this.events = {};
// Deep clone the Thing Model
// without functions or methods
const clonedModel = JSON.parse(JSON.stringify(thingModel));
Object.assign(this, clonedModel);

const deepClonedModel = structuredClone(thingModel);
Object.assign(this, deepClonedModel);
this.extendInteractions();
}

Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
this.properties = {};
this.actions = {};
this.events = {};
// Deep clone the Thing Model
// without functions or methods
const clonedModel = JSON.parse(JSON.stringify(thingModel));
Object.assign(this, clonedModel);

const deepClonedModel = structuredClone(thingModel);
Object.assign(this, deepClonedModel);

// unset "@type":"tm:ThingModel" ?
// see https://github.com/eclipse-thingweb/node-wot/issues/426
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export default class Helpers implements Resolver {
* Helper function to remove reserved keywords in required property of TD JSON Schema
*/
static createExposeThingInitSchema(tdSchema: unknown): SomeJSONSchema {
const tdSchemaCopy = JSON.parse(JSON.stringify(tdSchema));
const tdSchemaCopy = structuredClone(tdSchema) as SomeJSONSchema;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as another instance suitable for using the structuredClone function – the typing is not that nice here, but I think casting should be fine here since we are performing type checks down below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is acceptable, we could even put it as the argument type given the fact that is just an internal helper function


if (tdSchemaCopy.required !== undefined) {
const reservedKeywords: Array<string> = [
Expand Down
Loading