Description
Currently, the exposing process is composed of two phases: produce and expose. The two phases are handled by the produce
function and the expose
method of the ExposedThing
interface. The problem that my colleagues and I saw is in the mixed nature of the ExposedThing
interface.
First of all, semantically, it sounds strange that a produce
method creates an ExposedThing object that is actually not exposed yet until one calls the expose
method on it. Secondly, technically, in an ExposedThing
the handlers can be changed after the exposition, causing possible problems and side effects if the object is passed in different parts of the program. (what if one library function adds its own handler, or it expects a no-exposed thing).
Discussion
Define a strong split between the two phases and let be ExposedThing an immutable object. In concrete something like the following (typescript notation):
function produce(init: ExposedThingInit): Promise<ProducedThing>;
export interface ProducedThing {
setPropertyReadHandler(name: string, handler: PropertyReadHandler): ProducedThing;
setPropertyWriteHandler(name: string, handler: PropertyWriteHandler): ProducedThing;
setPropertyObserveHandler(name: string, handler: PropertyReadHandler): ProducedThing;
setPropertyUnobserveHandler(name: string, handler: PropertyReadHandler): ProducedThing;
setActionHandler(name: string, handler: ActionHandler): ProducedThing;
setEventSubscribeHandler(name: string, handler: EventSubscriptionHandler): ProducedThing;
setEventUnsubscribeHandler(name: string, handler: EventSubscriptionHandler): ProducedThing;
setEventHandler(name: string, handler: EventListenerHandler): ProducedThing;
getPartialThingDescription(): Partial<ThingDescription>;
async expose(): Promise<ExposedThing>
}
export interface ExposedThing {
destroy(): Promise<void>;
getThingDescription(): ThingDescription;
emitEvent(name: string, data: InteractionInput): void;
emitPropertyChange(name: string): void;
}
This way we can detect silly errors like emitEvent before exposing or setting a handler in an exposed thing at compile time.