RemoteModel API

fluid.remoteModelComponent builds on top of fluid.modelComponent with the purpose of providing a buffer between a local and remote model that are attempting to stay in sync. For example, a local model is being updated by user interaction, this is sent back to a remote server, which in turn attempts to update the local model. If additional user actions occur during this roundtrip, an infinite loop of updates may occur. fluid.remoteModelComponent solves this by restricting reading and writing to a single request at a time, waiting for one request to complete before operating the next. Additionally, it will rebase local and remote changes, with the local changes taking priority. (See: Fetch Workflow for more details on the rebasing.)

Supported Events

In addition to the standard fluid.modelComponent events, fluid.remoteModelComponent adds the following:

  • onFetch,
  • onFetchError,
  • afterFetch,
  • onWrite,
  • onWriteError,
  • afterWrite

For information on how events work and how to configure listeners to them, see Infusion Event System.

Note: The onFetch, afterFetch, onWrite and afterWrite are synthetic events that fire as a sequence. As implemented in the fluid.remoteModelComponent, the listeners for the event will all fire in the specified order and be passed the same arguments. However, the next listener will not trigger till the previous one has completed. That is, either returned a value or resolved/rejected a returned promise. This is useful to prevent further actions from happening until all of the listeners have completed, but unlike other event sequences, it cannot be used to pass the payload through a series of transformations.

onFetch

Description Fires an event sequence that executes before fetchImpl is called.
Type default
Arguments Undefined

onFetchError

Description Fired if the onFetch sequence or fetchImpl promises are rejected. By default the afterFetch.unblock (unblocks the request queue) handler will execute.
Type default
Arguments Any parameters passed along to the rejected promise. This may be an Error object, an error message, and/or some other means of indicating the rejection reason.

afterFetch

Description Fires an event sequence that executes after fetchImpl is called. By default the afterFetch.updateModel and afterFetch.unblock (unblocks the request queue) handlers will execute as part of this event sequence.
Type default
Arguments
data
The payload returned from the resolved fetchImpl promise.

onWrite

Description Fires an event sequence that executes before writeImpl is called.
Type default

onWriteError

Description Fired if the onWrite sequence or writeImpl promises are rejected. By default the afterWrite.unblock (unblocks the request queue) handler will execute.
Type default
Arguments Any parameters passed along to the rejected promise. This may be an Error object, an error message, and/or some other means of indicating the rejection reason.

afterWrite

Description Fires an event sequence that executes after writeImpl is called. By default the afterWrite.unblock (unblocks the request queue) handler will execute as part of this event sequence.
Type default
Arguments
data
The payload returned from the resolved writeImpl promise.

Model

Model Path Description Default
local Provides the buffered mapping of the portions of the components's model to the remote data that should be kept in sync. An implementor must setup the model relay(s) between the provider of the model data and this component's local path. {}
remote Stores the data fetched from the remote source.
Note: This is managed by the component and should not be modified directly.
{}
requestInFlight The state of the requests. When a request is in flight, being processed, the state will be true and false otherwise. This is used to prevent more than one request from executing at a time.
Note: This is managed by the component and should not be modified directly.
false

Invokers

fetch

Description Initiates a fetch request. See the Fetch Workflow for more details.
Default "fluid.remoteModelComponent.fetch"
Arguments
that
The component.

fetchImpl

Description Must be supplied by an implementor to provide the concrete implementation for fetching data from the remote source. It must return a promise yielding the fetched data.
Default "fluid.notImplemented"

write

Description Initiates a write request. See the Write Workflow for more details.
Default "fluid.remoteModelComponent.write"
Arguments
that
The component.

writeImpl

Description Must be supplied by an implementor to provide the concrete implementation for writing data to the remote source. It must return a promise that is resolved/rejected after the write operation has been handled by the remote end point.
Default "fluid.notImplemented"
Arguments
payload
The data, from that.model.local, to send to the remote source.

Fetch Workflow

Executing the fetch invoker will add a fetch request and returns a promise for the result. Only one request can be in flight (processing) at a time. If a write request is in flight, the fetch will be queued. If a fetch request is already in queue/flight, the requests will be coalesced into a single fetch request sent to the server, and all representatives in the queue will receive the same response payload. When a fetch request is in flight , it will trigger the fetchImpl invoker to perform the actual request.

Two synthetic events, onFetch and afterFetch, are fired during the processing of a fetch. onFetch can be used to perform any necessary actions before running fetchImpl. afterFetch can be used to perform any necessary actions after running fetchImpl (e.g. updating the model, unblocking the queue). If promises returned from onFetch, afterFetch, or fetchImpl are rejected, the onFetchError event will be fired.

After a fetch is performed, the local representation is rebased with the results. The rebasing is performed by re-applying the local model changes on top of the values returned from the fetch. In this way the local changes are not lost and take precedence over changes pulled down from the remote.

A flow diagram depicting the Fetch workflow

Write Workflow

Executing the write invoker adds a write request and returns a promise for the result. Only one request can be in flight (processing) at a time. If a fetch or write request is in flight, the write will be queued. If a write request is already in queue, the requests will be coalesced into a single write request sent to the server, and all representatives in the queue will receive the same response payload. When a write request is in flight, it will trigger the writeImpl invoker to perform the actual request.

Two synthetic events, onWrite and afterWrite, are fired during the processing of a write. onWrite can be used to perform any necessary actions before running writeImpl (e.g. performing a fetch). afterWrite can be used to perform any necessary actions after running writeImpl (e.g. updating the remote model, unblocking the queue, performing a fetch). If promises returned from onWrite, afterWrite, or writeImpl are rejected, the onWriteError event will be fired.

By default, upon a successful write, the afterWrite event will trigger the remote model path to be updated with the values from the local model path. Because of this, the writeImpl must resolve/reject its promise after the write implementation has been fully handled by the remote endpoint, not simply after the I/O for the request itself has concluded.

A flow diagram depicting the Write workflow