-
Notifications
You must be signed in to change notification settings - Fork 22
Description
When trying to implement the current discovery API I got the impression that its current design could be simplified by using a structure similar to the subscription/observation of events/properties. In particular, I had problems with implementing step two of the next()
method:
If the active property is true, wait until the discovery results internal slot is not empty.
Correct me if I am wrong, but it seemed to me as if this would only be possible by continuously checking the results, using a function like setTimeout
or within a while loop (are there any implementation examples?). As this didn't seem that optimal to me, I tried out a callback based approach instead (similar to the Subscription
interface and the observeProperty
/subscribeEvent
methods) which worked quite well so far.
I therefore wanted to suggest changing the signature of the discover
method (or its potential successors which are discussed in #222) to something like
ThingDiscovery discover(DiscoveryListener listener, optional ThingFilter thingFilter = null);
where the DiscoveryListener
function signature could look like the following:
type DiscoveryListener = (thingDescription: ThingDescription) => void;
A minimal example could look like this:
async function handleDiscovery(thingDescription: ThingDescription) {
const consumedThing = await wot.consume(thingDescription);
await consumedThing.invokeAction("foo");
}
wot.discover(handleDiscovery, { method: "direct", url: "coap://example.org/td" );
// Calling stop() can probably be omitted when the "direct" method is being used.
Here, the discovery process is initialized using the direct
method. When a Thing Description has been retrieved successfully, the handleDiscovery
function is called which consumes the TD and interacts with the Thing. If I am not mistaken, a similar approach (using a class called Observable
) was already part of the specification some time ago but I still wanted to open this issue to discuss it as an alternative to the current discover
method.
I am looking forward to your feedback!