Skip to content

Commit

Permalink
fix http client and server property "subscription" (#922)
Browse files Browse the repository at this point in the history
* fix: http client and server property subscription

Signed-off-by: valentin <[email protected]>

* refactor(package-lock): revert

* fixup!(core): getClientFor do not use cached clients

* fix(core): re-use the same client for observe and listening to events

---------

Signed-off-by: valentin <[email protected]>
Co-authored-by: valentin <[email protected]>
Co-authored-by: reluc <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2023
1 parent a6256f2 commit a5f99de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
8 changes: 8 additions & 0 deletions packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@ export default class HttpServer implements ProtocolServer {
res.writeHead(500);
res.end(err.message);
}
} else if (req.method === "HEAD") {
res.writeHead(202);
res.end();
} else {
respondUnallowedMethod(res, "GET");
}
Expand Down Expand Up @@ -855,6 +858,11 @@ export default class HttpServer implements ProtocolServer {
}
// resource found and response sent
return;
} else if (req.method === "HEAD") {
// HEAD support for long polling subscription
res.writeHead(202);
res.end();
return;
} else {
respondUnallowedMethod(res, "GET, PUT");
return;
Expand Down
37 changes: 19 additions & 18 deletions packages/core/src/consumed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ class ConsumedThingEvent extends TD.ThingEvent {
abstract class InternalSubscription implements Subscription {
active: boolean;

constructor(protected readonly thing: ConsumedThing, protected readonly name: string) {
constructor(
protected readonly thing: ConsumedThing,
protected readonly name: string,
protected readonly client: ProtocolClient
) {
this.active = true;
}

Expand All @@ -120,8 +124,13 @@ abstract class InternalSubscription implements Subscription {
class InternalPropertySubscription extends InternalSubscription {
active = false;
private formIndex: number;
constructor(thing: ConsumedThing, name: string, private readonly form: FormElementProperty) {
super(thing, name);
constructor(
thing: ConsumedThing,
name: string,
client: ProtocolClient,
private readonly form: FormElementProperty
) {
super(thing, name, client);
const index = this.thing.properties?.[name].forms.indexOf(form as TD.Form);
if (index === undefined || index < 0) {
throw new Error(`Could not find form ${form.href} in property ${name}`);
Expand All @@ -143,20 +152,13 @@ class InternalPropertySubscription extends InternalSubscription {
if (!options.formIndex) {
options.formIndex = this.matchingUnsubscribeForm();
}
const { client, form } = this.thing.getClientFor(
tp.forms,
"unobserveproperty",
Affordance.PropertyAffordance,
options
);
const { form } = this.thing.getClientFor(tp.forms, "unobserveproperty", Affordance.PropertyAffordance, options);
if (!form) {
throw new Error(`ConsumedThing '${this.thing.title}' did not get suitable form`);
}
if (!client) {
throw new Error(`ConsumedThing '${this.thing.title}' did not get suitable client for ${form.href}`);
}

debug(`ConsumedThing '${this.thing.title}' unobserving to ${form.href}`);
await client.unlinkResource(form);
await this.client.unlinkResource(form);
this.active = false;
}

Expand Down Expand Up @@ -255,8 +257,8 @@ function findFormIndexWithScoring(

class InternalEventSubscription extends InternalSubscription {
private formIndex: number;
constructor(thing: ConsumedThing, name: string, private readonly form: FormElementEvent) {
super(thing, name);
constructor(thing: ConsumedThing, name: string, client: ProtocolClient, private readonly form: FormElementEvent) {
super(thing, name, client);
const index = this.thing.events?.[name].forms.indexOf(form as TD.Form);
if (index === undefined || index < 0) {
throw new Error(`Could not find form ${form.href} in event ${name}`);
Expand Down Expand Up @@ -485,7 +487,6 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
if (options.formIndex >= 0 && options.formIndex < forms.length) {
form = forms[options.formIndex];
const scheme = Helpers.extractScheme(form.href);

if (this.getServient().hasClientFor(scheme)) {
debug(`ConsumedThing '${this.title}' got client for '${scheme}'`);
client = this.getServient().getClientFor(scheme);
Expand Down Expand Up @@ -740,7 +741,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
/* TODO: current scripting api cannot handle this */
}
);
const subscription = new InternalPropertySubscription(this, name, form as FormElementProperty);
const subscription = new InternalPropertySubscription(this, name, client, form as FormElementProperty);
this.observedProperties.set(name, subscription);
return subscription;
}
Expand Down Expand Up @@ -798,7 +799,7 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
}
);

const subscription = new InternalEventSubscription(this, name, form as FormElementEvent);
const subscription = new InternalEventSubscription(this, name, client, form as FormElementEvent);
this.subscribedEvents.set(name, subscription);
return subscription;
}
Expand Down

0 comments on commit a5f99de

Please sign in to comment.