Skip to content

Commit

Permalink
refactor(binding-coap): refactor server's destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed May 10, 2023
1 parent 09dfd6a commit 3046655
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,21 @@ export default class CoapServer implements ProtocolServer {
});
}

public destroy(thingId: string): Promise<boolean> {
public async destroy(thingId: string): Promise<boolean> {
debug(`CoapServer on port ${this.getPort()} destroying thingId '${thingId}'`);
return new Promise<boolean>((resolve, reject) => {
let removedThing: ExposedThing;
for (const name of Array.from(this.things.keys())) {
const expThing = this.things.get(name);
if (expThing?.id === thingId) {
this.things.delete(name);
this.coreResources.delete(name);
removedThing = expThing;
}
}
if (removedThing) {
info(`CoapServer succesfully destroyed '${removedThing.title}'`);
} else {
info(`CoapServer failed to destroy thing with thingId '${thingId}'`);
for (const name of this.things.keys()) {
const exposedThing = this.things.get(name);
if (exposedThing?.id === thingId) {
this.things.delete(name);
this.coreResources.delete(name);

info(`CoapServer succesfully destroyed '${exposedThing.title}'`);
return true;
}
resolve(removedThing !== undefined);
});
}

info(`CoapServer failed to destroy thing with thingId '${thingId}'`);
return false;
}

private formatCoreLinkFormatResources() {
Expand Down

0 comments on commit 3046655

Please sign in to comment.