CombineWrapper
public final class CombineWrapper : WebcomWrapper, Hashable, CustomStringConvertible
The type that wraps DatasyncNode in order to enrich it with a Combine-based API.
See also the documentation section of WebcomWrapper for the corresponding wrapped type.
To use this type, import the following dependency in your code:
import WebcomCombine
-
Declaration
Swift
public let wrapped: DatasyncNode
-
A reference to the parent of the node referred by this instance.
The root node does not have a parent, so this property returns
nilfor the root node. For all other nodes, this property never returnsnil.Note: With a reference to the root node,
relativeNode(for: "..")returns a reference to the root node.Declaration
Swift
public var parent: CombineWrapper? { get } -
Returns a reference to another node, relatively to the node referred by this instance.
This method only returns
nilwhen thepathis ill-formed, that is when it contains forbidden characters. All nodes always exists: this method always returns a plain node reference when thepathis syntactically valid.The path consists of segments separated by
/(slash) characters. Each segment corresponds to a move in the database tree:- The starting point is the node corresponding to this instance.
- The
.segment is a no-operation. - The
..segment indicates to go one level up in the tree. When already at the root node, this is a no-operation. - The empty segment, when using a
/at the beginning or at the end of thepath, or when using several consecutive/, is a no-operation. - Any other valid segment indicates to go one level down towards the so-named child.
If the path referred by this node is
/foo/bar:- and
pathis equal togeek,/geek,geek/or/geek/, the path of the resulting node is/foo/bar/geek, - and
pathis equal to../geek/noobor/../geek//noob/.or../geek/./noob/., the path of the resulting node is/foo/geek/noob, - and
pathis equal to../geek/../..//../.., the resulting node is the root node.
Note: With a reference to the root node,
parentreturnsnil.Declaration
Swift
public func relativeNode(for path: String) -> CombineWrapper?Parameters
pathThe relative path of the node to refer. It can start or not with a
/(slash) character. It is always relative.Return Value
A reference to the node corresponding to the relative
pathwhen it is valid,nilotherwise. -
Returns a reference to a database node, relatively to a base node.
This operator returns:
node.relativeNode(for: path)Declaration
Swift
public static func / (node: CombineWrapper, path: String) -> CombineWrapper?Parameters
nodeA reference to the base node.
pathThe relative path of the node to refer. It can start or not with a
/(slash) character. It is always relative.Return Value
A node reference when the
pathis valid,nilotherwise. -
Creates a reference to a new child of the node referred by this instance.
The key of the new child is a unique identifier based on a timestamp. Such a key is also called a push identifier. The keys of several children created with this method are ordered chronologically. With this method, several users can add children to the same data node of a database at the same time without any write conflict.
Note: More exactly, since from a theoretical point of view all nodes always exist, this method actually returns a reference to a node whose current value is JSON
null.Declaration
Swift
public func createChild() -> CombineWrapperReturn Value
A reference to a new child of this node.
-
Gets the value of the node referred by this instance.
The
decodedValue()method can be used to get decoded values.Declaration
Swift
public func value() -> WebcomFuture<DatasyncValue>Return Value
A future with the value of the referred node
-
value()AsynchronousDeclaration
Swift
public func value() async throws -> DatasyncValue -
Gets the value of the node referred by this instance.
This method behaves like the
value()method but it returns a future that gets a decoded value in case of success. When the value cannot be decoded, the future is rejected with an error having theWebcomError.SDKCode.decodingFailedcode. If this behavior does not fit your needs, use thevalue()method and process the parameter as expected.The decoding is done using the
WebcomApplication.jsonDecoderof theWebcomApplicationfrom which this node derives.Declaration
Swift
public func decodedValue<T>() -> WebcomFuture<T> where T : DecodableReturn Value
A future with the decoded value of the referred node
-
decodedValue()AsynchronousDeclaration
Swift
public func decodedValue<T>() async throws -> T where T : Decodable -
Gets the value of the node referred by this instance.
This method behaves like the
value()method but it returns a future that gets a decoded value in case of success. When the value cannot be decoded, the future is rejected with an error having theWebcomError.SDKCode.decodingFailedcode. If this behavior does not fit your needs, use thevalue()method and process the parameter as expected.The decoding is done using the
WebcomApplication.jsonDecoderof theWebcomApplicationfrom which this node derives.Declaration
Swift
public func decodedValue<T>(as type: T.Type) -> WebcomFuture<T> where T : DecodableParameters
typeThe target type for the decoding.
Return Value
A future with the decoded value of the referred node
-
decodedValue(as:Asynchronous) Declaration
Swift
public func decodedValue<T>(as type: T.Type) async throws -> T where T : Decodable -
Clears the value of the node referred by this instance.
Clearing the value is equivalent to setting a JSON
nullvalue. Therefore, calling this method is equivalent to call:set(nil, at: time, queue: queue, then: completionCallback)Declaration
Swift
@discardableResult public func clear(at time: DatasyncTime = .now) -> WebcomFuture<Void>Parameters
timeThe time at which to perform the operation.
Return Value
A future with a void value.
-
clear(at:Asynchronous) Declaration
Swift
public func clear(at time: DatasyncTime = .now) async throws -
Sets the value of the node referred by this instance.
Note: In most cases, you should rather use the
set(_:at:)overload that takes anEncodableparameter.All children of the referred node cleared before setting the new value. The
merge(_:with:at:)method may be used to merge existing children with the ones of thevaluewhen it is a JSON object or array.Declaration
Swift
@discardableResult public func set(_ raw: WebcomMarkers.Raw, _ json: Any?, at time: DatasyncTime = .now) -> WebcomFuture<Void>Parameters
rawUse the
.rawvalue to disambiguate with theset(_:at:)overload that takes anEncodableparameter.jsonThe value to set. It must be valid JSON (object, array, string, number, boolean or
null). Thenullvalue can be specified withnilorNSNull().timeThe time at which to perform the operation.
Return Value
A future with a void value.
-
set(_:Asynchronous_: at: ) Declaration
Swift
public func set(_ raw: WebcomMarkers.Raw, _ json: Any?, at time: DatasyncTime = .now) async throws -
Sets the value of the node referred by this instance.
All children of the referred node cleared before setting the new value. The
merge(with:at:)method may be used to merge existing children with the ones of thevaluewhen it is a JSON object or array.Declaration
Swift
@discardableResult public func set(_ value: (some Encodable)?, at time: DatasyncTime = .now) -> WebcomFuture<Void>Parameters
valueThe value to set. It will be encoded to JSON before being sent to the back-end, using the
WebcomApplication.jsonEncoderof theWebcomApplicationfrom which this node derives. Thenullvalue can be specified withnilorNSNull().timeThe time at which to perform the operation.
Return Value
A future with a void value.
-
set(_:Asynchronousat: ) Declaration
Swift
public func set(_ value: (some Encodable)?, at time: DatasyncTime = .now) async throws -
Sets the value of a node.
This operator is equivalent to:
_ = node.set(value)Declaration
Swift
public static func <- (node: CombineWrapper, value: (some Encodable)?)Parameters
nodeA reference to the node whose value must be set.
valueThe value to set.
-
<-(_:Asynchronous_: ) Declaration
Swift
public static func <- (node: CombineWrapper, value: (some Encodable)?) async throws -
Merges a value with the current value of the node referred by this instance.
Note: In most cases, you should rather use the
merge(with:at:)overload that takes anEncodableparameter.Unlike what happens with the
set(_:_:at:):jsonmust be a valid JSON object,existing direct children (i.e. the descendants at the first level of depth) that exist in the current value are preserved when
jsondoes not specify them.
Declaration
Swift
@discardableResult public func merge(_ raw: WebcomMarkers.Raw, with json: [String : Any], at time: DatasyncTime = .now) -> WebcomFuture<Void>Parameters
rawUse the
.rawvalue to disambiguate with themerge(with:at:)overload that takes anEncodableparameter.jsonThe value to merge with. It must be a valid JSON object.
timeThe time at which to perform the operation.
Return Value
A future with a void value.
-
merge(_:Asynchronouswith: at: ) Declaration
Swift
public func merge(_ raw: WebcomMarkers.Raw, with json: [String : Any], at time: DatasyncTime = .now) async throws -
Merges a value with the current value of the node referred by this instance.
Unlike what happens with the
set(_:at:):valuemay not benil,- the encoding of
valueto JSON must give a JSON object (or a JSON array but this is discouraged), or the method will fail, existing direct children (i.e. the descendants at the first level of depth) that exist in the current value are preserved when
valuedoes not specify them.
Declaration
Swift
@discardableResult public func merge(with value: some Encodable, at time: DatasyncTime = .now) -> WebcomFuture<Void>Parameters
valueThe value to merge with. It will be encoded to JSON before being sent to the back-end, using the
WebcomApplication.jsonEncoderof theWebcomApplicationfrom which this node derives.timeThe time at which to perform the operation.
Return Value
A future with a void value.
-
merge(with:Asynchronousat: ) Declaration
Swift
public func merge(with value: some Encodable, at time: DatasyncTime = .now) async throws -
Adds a new child with the given value to the node referred by this instance.
Note: In most cases, you should rather use the
push(_:)overload that takes anEncodableparameter.This method creates a new child to the referred node and then it sets its value.
When this method returns
nil,completionCallbackis called in failure with aWebcomErrorvalue indicating the reason of the failure. It may well happen that this method returns a non-nilnode reference and thecompletionCallbackis called in failure, for example if a security rule prevents writing the child.Declaration
Swift
@discardableResult public func push(_ raw: WebcomMarkers.Raw, _ json: Any?) -> WebcomFuture<CombineWrapper>Parameters
rawUse the
.rawvalue to disambiguate with thepush(_:)overload that takes anEncodableparameter.jsonThe value to set for the created child. It must be valid JSON (object, array, string, number, boolean or
null). Thenullvalue can be specified withnilorNSNull().Return Value
A future with a reference to the added child.
-
push(_:Asynchronous_: ) Declaration
Swift
public func push(_ raw: WebcomMarkers.Raw, _ json: Any?) async throws -> DatasyncNode -
Adds a new child with the given value to the node referred by this instance.
This method creates a new child to the referred node and then it sets its value.
When this method returns
nil,completionCallbackis called in failure with aWebcomErrorvalue indicating the reason of the failure. It may well happen that this method returns a non-nilnode reference and thecompletionCallbackis called in failure, for example if a security rule prevents writing the child.Declaration
Swift
@discardableResult public func push(_ value: (some Encodable)?) -> WebcomFuture<CombineWrapper>Parameters
valueThe value to set for the created child. It will be encoded to JSON before being sent to the back-end, using the
WebcomApplication.jsonEncoderof theWebcomApplicationfrom which this node derives. Thenullvalue can be specified withnilorNSNull().Return Value
A future with a reference to the added child.
-
push(_:Asynchronous) Declaration
Swift
public func push(_ value: (some Encodable)?) async throws -> DatasyncNode -
Adds a new child to a node with the given value.
This operator is equivalent to:
node.push(value)Declaration
Swift
public static func += (node: CombineWrapper, value: (some Encodable)?)Parameters
nodeA reference to the node to which a new child must be pushed.
valueThe value to set for the created child.
-
Atomically increments (or decrements) the value of the node referred by this instance.
When the node does not exist (in other words, when its initial value is
null), the node is implicitly assumed having thestartValuevalue and the incrementstepis added to that implicit value.When the node exists and its initial value is not a number, the operation fails.
This operation is robust to concurrent increment operations. It is guaranteed that if n increment operations are performed at the same time, then the value of the node is incremented by n
steps(in other words, none of the concurrent operations can overwrite another one).However, note that the returned value may not be synchronized with the actual value of the node, especially if other write operations are performed at the same time. You should rather use the
subscribe(to:childrenConstraint:stores:file:line:queue:onEvent:onCompletion:)method to watch the actual value of the node.Declaration
Swift
public func increment(from startValue: NSNumber = 0, by step: NSNumber = 1) -> WebcomFuture<NSNumber>Parameters
startValueThe implicit initial value of the node when it does not exist.
stepThe increment to add to the initial value to value the final value. To decrement, use a negative value.
Return Value
A future with the resulting value of the increment operation.
-
increment(from:Asynchronousby: ) Declaration
Swift
public func increment(from startValue: NSNumber = 0, by step: NSNumber = 1) async throws -> NSNumber -
Runs a transaction on the node referred by this instance.
A transaction is a process that atomically sets a new value to the node depending on its initial value. In other words, the new value is computed from the initial value and it is sets only if the value of the node has not been modified in the meantime. If the value of the node just before setting the new value is no longer the initial value, then the transaction is run again.
The transaction is specified with a function, which receives as its single parameter the initial value and returns an operation to execute. The operation may consist of setting a new value or clearing the value (which is equivalent to setting the
nullvalue). The operation may also indicate to abort the transaction.If the transaction consists in incrementing (or decrementing) the value of the referred node, you should rather use the
increment(from:by:)method which is more efficient in this case.Declaration
Swift
@discardableResult public func runTransaction(_ function: @escaping DatasyncTransaction.Function) -> WebcomFuture<DatasyncTransaction.Result>Parameters
functionThe function that computes the operation to execute from the initial value of the node.
Return Value
A future with the result of the transaction.
-
runTransaction(_:Asynchronous) Declaration
Swift
public func runTransaction(_ function: @escaping DatasyncTransaction.Function) async throws -> DatasyncTransaction.Result -
Creates a publisher that produces a
DatasyncEventvalue each time the value of the referred node is modified.The
decodedValuePublisher(for:childrenConstraint:file:line:)method can be used to directly get decoded values.Subscriptions made with the returned publisher must be stored explicitly to remain active, e.g. using the
AnyCancellable.store(in:)method. Unlike callback-based subscriptions made with theDatasyncNode.subscribe(to:childrenConstraint:stores:file:line:queue:onEvent:onCompletion:)method, these subscriptions are not stored implicitly.Declaration
Swift
public func eventPublisher(for eventType: DatasyncEventType = .valueChange, childrenConstraint: DatasyncChildrenConstraint = .all, file: StaticString = #fileID, line: UInt = #line) -> AnyPublisher<DatasyncEvent, WebcomError>Parameters
eventTypeThe type of events to subscribe.
childrenConstraintA constraint to filter the children of the referred node that are affected by the subscription
fileThe file from which the method is called. This is used for debugging purposes.
lineThe line from which the method is called. This is used for debugging purposes.
Return Value
A type-erased publisher that produces
DatasyncEventvalues and aWebcomErrorin case of revocation by the back-end. -
Creates a publisher that produces a decoded value each time the value of the referred node is modified.
This method behaves like the
eventPublisher(for:childrenConstraint:file:line:)method but it returns a publisher that decodes the value. When the value of the referred node cannot be decoded, the returned publisher does not produce any value and it does not complete. Such a non-decodable value is simply ignored. If this behavior does not fit your needs, use theeventPublisher(for:childrenConstraint:file:line:)method and process the produced events as expected.Subscriptions made with the returned publisher must be stored explicitly to remain active, e.g. using the
AnyCancellable.store(in:)method. Unlike callback-based subscriptions made with theDatasyncNode.subscribe(to:childrenConstraint:stores:file:line:queue:onEvent:onCompletion:)method, these subscriptions are not stored implicitly.Declaration
Swift
public func decodedValuePublisher<T>(for eventType: DatasyncEventType = .valueChange, childrenConstraint: DatasyncChildrenConstraint = .all, file: StaticString = #fileID, line: UInt = #line) -> AnyPublisher<T, WebcomError> where T : DecodableParameters
eventTypeThe type of events to subscribe.
childrenConstraintA constraint to filter the children of the referred node that are affected by the subscription.
fileThe file from which the method is called. This is used for debugging purposes.
lineThe line from which the method is called. This is used for debugging purposes.
Return Value
A type-erased publisher that produces decoded values and a
WebcomErrorin case of revocation by the back-end. -
Copies the value of the node referred by this instance to another node.
All children of the destination node are cleared before setting its value.
Declaration
Swift
public func copyValue(to destinationNode: DatasyncNode.CombineWrapper) -> WebcomFuture<DatasyncValue>Parameters
destinationNodeA reference to the node to set with the value of this node.
Return Value
A future with the copied value.
-
copyValue(to:Asynchronous) Declaration
Swift
public func copyValue(to destinationNode: DatasyncNode.CombineWrapper) async throws -> DatasyncValue -
Copies the value of the node referred by this instance to another node.
All children of the destination node are cleared before setting its value.
Declaration
Swift
public func copyValue(to destinationPath: String) -> WebcomFuture<DatasyncValue>Parameters
destinationPathThe path of the node to set with the value of this node.
Return Value
A future with the copied value.
-
copyValue(to:Asynchronous) Declaration
Swift
public func copyValue(to destinationNode: String) async throws -> DatasyncValue