From 2489a590b527867f9b102ffef70a89e1d5e98360 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 Oct 2021 20:58:20 +0200 Subject: [PATCH 1/3] Allow to add new things at runtime --- lib/server.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/server.ts b/lib/server.ts index 7de2139..0d68f5d 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -65,10 +65,14 @@ export class MultipleThings { * @param {String} name The mDNS server name */ constructor(things: Thing[], name: string) { - this.things = things; + this.things = things.slice(); this.name = name; } + addThing(thing: Thing): number { + return this.things.push(thing) - 1; + } + /** * Get the thing at the given index. * @@ -848,6 +852,12 @@ export class WebThingServer { this.server.on('request', this.app); } + addThing(thing: Thing) { + if (this.things instanceof SingleThing) throw new Error("Cannot add new things when Webthings was instanciated with a SingleThing."); + const i = this.things.addThing(thing); + thing.setHrefPrefix(`${this.basePath}/${i}`); + } + /** * Start listening for incoming connections. * From 761fdf1ebdf2c84808ac2a70451767cc6050775b Mon Sep 17 00:00:00 2001 From: mkr Date: Wed, 13 Oct 2021 09:44:25 +0200 Subject: [PATCH 2/3] run prettier as requested --- src/server.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index 0d68f5d..a81022b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -853,7 +853,8 @@ export class WebThingServer { } addThing(thing: Thing) { - if (this.things instanceof SingleThing) throw new Error("Cannot add new things when Webthings was instanciated with a SingleThing."); + if (this.things instanceof SingleThing) + throw new Error('Cannot add new things when Webthings was instanciated with a SingleThing.'); const i = this.things.addThing(thing); thing.setHrefPrefix(`${this.basePath}/${i}`); } From 15d68262205382f1680bb8688fa3773c9932ea81 Mon Sep 17 00:00:00 2001 From: mkr Date: Fri, 22 Oct 2021 12:41:40 +0200 Subject: [PATCH 3/3] removed linter warnings --- src/server.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/server.ts b/src/server.ts index a81022b..d911a99 100644 --- a/src/server.ts +++ b/src/server.ts @@ -852,9 +852,10 @@ export class WebThingServer { this.server.on('request', this.app); } - addThing(thing: Thing) { - if (this.things instanceof SingleThing) + addThing(thing: Thing): void { + if (this.things instanceof SingleThing) { throw new Error('Cannot add new things when Webthings was instanciated with a SingleThing.'); + } const i = this.things.addThing(thing); thing.setHrefPrefix(`${this.basePath}/${i}`); }