The component function
Description
The heart of your component logic is the component function. It receives an object which provides access to various streams and a few utilities, and returns an Observable or an object with Observable properties.
Example
The return value
Shallow Equal check
Whenever the component function emits a new value Melody will perform a shallow equal check on the state object to determine whether it needs to render the component again. "Shallow Equal" means that all properties are compared against each other (for identity). Unless at least one property has changed, the value would be discarded.
Returning an Observable
If the function returns an Observable (should be compatible with Rx.js) then the values emitted by it will be used for rendering the UI of the component.
Returning an object
The state object can also be provided by returning an object. Melody will wrap it into an observable automatically so that all observable properties of the object are merged into a single stream. This is provided for convenience but also to improve readability.
In this example, the component will update whenever either id
, selected
or item
emit new values. Since meaningOfLife
is not an observable, its value will be available in the template, but changes to it won't be tracked and will not end up in the UI.
Delaying a rendering
Since the return value of the function is an Observable it is possible that it won't immediately emit a value. In these cases, Melody will hold off with rendering the component until a state has been emitted. When returning an object, Melody will wait until all property streams have emitted a value before rendering.
This allows implementing UIs which avoid showing loading animations until needed and similar features.
In this case, the Item
component will only render after the details of the item have been retrieved from the server. Until then, nothing will be shown.
It is possible to extend this example slightly to show a loading animation after a certain amount of time has passed to provide an even better user experience.
Buffered rendering
Melody will automatically delay the rendering to the next frame. Thus, two or more consecutive state changes will result in only the last state being rendered.
The input object
The component function receives an object which provides access to some special parts of the component. These properties are described in detail here.
props: Observable<any>
An observable of all properties that are being passed into the component through mount
:
Whenever the parent is re-rendered, the props
stream will emit an object containing the current properties. In the above example, it would contain a field named images
and a field named itemId
. These can then be used in the Gallery
template:
Melody does not do any check to verify that the properties have actually changed. If that is required, the component should take care of it on its own.
A common pattern to select a part of the stream would look like this:
If you need the original props
in addition to some Observable you create in the component function, you can use combine
:
combine
takes any number of Observables or objects of Observables, and turns them into an "umbrella Observable" that takes each value from the individual Observables and delivers these values in one single object. This object then becomes the state of the component which is available for rendering.
updates: Observable<undefined>
This observable emits after every rendering of a Melody component. It does not emit an actual value. A common use case is to do some additional initialization on the first mount of the component.
Example
subscribe
subscribe
takes an Observable, calls its subscribe
function, and stores the returned unsubscribe handler. When the component is unmounted, Melody will go through all these unsubscribe handlers and call them. Therefore, you have automatic unsubscription, which prevents memory leaks.
dispatchCustomEvent
Last updated
Was this helpful?