ClientSyncEngine
The ClientSyncEngine is responsible for driving client side of the differential synchronization algorithm.
During construction the engine gets injected with an instance of ClientSynchronizer
which takes care of diff/patching operations, and an instance of ClientDataStore for
storing data.
A synchronizer in AeroGear is a module that serves two purposes which are closely related. One, is to provide
storage for the data type, and the second is to provide the patching algorithm to be used on that data type.
The name synchronizer is because they take care of the synchronization part of the Differential Synchronization
algorithm. For example, one synchronizer, such as DiffMatchPatchSynchronizer, might support plain text while another, such as JsonPatchSynchronizer supports JSON Objects as the
content of documents being stored. But a patching algorithm used for plain text might not be appropriate for JSON
Objects.
To construct a client that uses the JSON Patch you would use the following code:
var engine: ClientSyncEngine<JsonPatchSynchronizer, InMemoryDataStore<JsonNode, JsonPatchEdit>>
engine = ClientSyncEngine(synchronizer: JsonPatchSynchronizer(), dataStore: InMemoryDataStore())
The ClientSynchronizer generic type is the type that this implementation can handle.
The DataStore generic type is the type that this implementation can handle.
The ClientSynchronizer and DataStore should have compatible document type.
-
Default init.
Declaration
Swift
public init(synchronizer: CS, dataStore: D)
Parameters
synchronizer
that this ClientSyncEngine will use.
dataStore
that this ClientSyncEngine will use.
-
Adds a new document to this sync engine.
Declaration
Swift
public func addDocument(clientDocument: ClientDocument<T>, callback: ClientDocument<T> -> ())
Parameters
document
the document to add.
-
Returns an PatchMessage of the type compatible with ClientSynchronizer (ie: eith DiffMatchPatchMessage or JsonPatchMessage) which contains a diff against the engine’s stored shadow document and the passed-in document.
There might be pending edits that represent edits that have not made it to the server for some reasons (for example packet drop). If a pending edit exits, the contents (ie: the diffs) of the pending edit will be included in the returned Edits from this method.
The returned PatchMessage instance is indended to be sent to the server engine for processing.Declaration
Swift
public func diff(clientDocument: ClientDocument<T>) -> P?
Parameters
document
the updated document.
Return Value
PatchMessage containing the edits for the changes in the document.
-
Patches the client side shadow with updates (PatchMessage) from the server.
When updates happen on the server, the server will create an PatchMessage instance by calling the server engines diff method. This PatchMessage instance will then be sent to the client for processing which is done by this method.Declaration
Swift
public func patch(patchMessage: P)
Parameters
patchMessage
the updates from the server.
-
Delegate to Synchronizer.patchMessageFronJson. Creates a PatchMessage by parsing the passed-in json.
Declaration
Swift
public func patchMessageFromJson(json: String) -> P?
Parameters
json
string representation.
Return Value
PatchMessage created fron jsons string.
-
Delegate to Synchronizer.addContent.
Declaration
Swift
public func documentToJson(clientDocument:ClientDocument<T>) -> String
Parameters
clientDocument
the content itself.
Return Value
String with all ClientDocument information.