EntityStore

public class EntityStore

Manages entities lifecycle and synchronisation

  • Declaration

    Swift

    public typealias Update<T> = (inout T) -> Void
  • Create a new EntityStore instance optionally with a queue and a logger

    Declaration

    Swift

    public convenience init(queue: DispatchQueue? = nil, logger: Logger? = nil)

    Parameters

    queue

    the queue on which to receive updates. If nil identitymap will create its own.

    logger

    a logger to follow/debug identity internal state

  • Store an entity in the storage. Entity will be stored only if stamp (modifiedAt) is higher than in previous insertion.

    Declaration

    Swift

    public func store<T: Identifiable>(
        entity: T,
        named: AliasKey<T>? = nil,
        modifiedAt: Stamp? = nil,
        ifPresent update: Update<T>? = nil
    ) -> EntityObserver<T>

    Parameters

    entity

    the element to store in the identity map

    named

    an alias to reference the entity and retrieve it using it

    modifiedAt

    if entity was already stored it will be used to determine if the update should be applied or discarded

    ifPresent

    applies the closure before storing it if it’s already been stored. In this case this is similar as calling update

    Return Value

    an object to observe changes on the entity

  • Store an aggregate in the storage. Each aggregate entities will be stored only if stamp (modifiedAt) is higher than in previous insertion. Finally aggregate will be stored accordingly to each of its entities.

    Declaration

    Swift

    public func store<T: Aggregate>(
        entity: T,
        named: AliasKey<T>? = nil,
        modifiedAt: Stamp? = nil,
        ifPresent update: Update<T>? = nil
    ) -> EntityObserver<T>

    Parameters

    entity

    the aggregate to store in the identity map

    named

    an alias to reference the aggregate and retrieve it using it

    modifiedAt

    if aggregate was already stored it will be used to determine if the update should be applied or discarded

    ifPresent

    applies the closure before storing it if it’s already been stored. In this case this is similar as calling update

    Return Value

    an object to observe changes on the entity

  • Store multiple entities at once

    Declaration

    Swift

    public func store<C: Collection>(entities: C, named: AliasKey<C>? = nil, modifiedAt: Stamp? = nil)
    -> EntityObserver<[C.Element]> where C.Element: Identifiable
  • store multiple aggregates at once

    Declaration

    Swift

    public func store<C: Collection>(entities: C, named: AliasKey<C>? = nil, modifiedAt: Stamp? = nil)
    -> EntityObserver<[C.Element]> where C.Element: Aggregate
  • Try to find an entity/aggregate in the storage.

    Declaration

    Swift

    public func find<T>(_ type: T.Type, id: T.ID) -> EntityObserver<T>? where T : Identifiable

    Parameters

    type

    the entity type

    id

    the entity id

    Return Value

    nil if not found, an EntityObserver` otherwise

  • Try to find an entity/aggregate registered under named alias

    Declaration

    Swift

    public func find<T>(named: AliasKey<T>) -> EntityObserver<T?> where T : Identifiable

    Parameters

    named

    the alias to look for

  • Try to find a collected registered under named alias

    Declaration

    Swift

    public func find<C>(named: AliasKey<C>) -> EntityObserver<C?> where C : Collection

    Return Value

    an observer returning the alias value. Note that the value will be an Array

Update

  • Updates an already stored entity using a closure. Useful to update a few properties or when you assume the entity should already be stored. Note: the closure is evaluated before checking modifiedAt. As such the closure execution does not mean the change was applied

    Declaration

    Swift

    @discardableResult
    public func update<T>(_ type: T.Type, id: T.ID, modifiedAt: Stamp? = nil, update: Update<T>) -> Bool where T : Identifiable

    Return Value

    true if entity exists and might be updated, false otherwise. The update might not be applied if modifiedAt is too old

  • Updates an already stored alias using a closure. This is useful if you don’t have a full entity for update but just a few attributes/modifications. Note: the closure is evaluated before checking modifiedAt. As such the closure execution does not mean the change was applied

    Declaration

    Swift

    @discardableResult
    public func update<T>(_ type: T.Type, id: T.ID, modifiedAt: Stamp? = nil, _ update: Update<T>) -> Bool where T : Aggregate

    Return Value

    true if entity exists and might be updated, false otherwise. The update might not be applied if modifiedAt is too old

  • Updates an already stored alias using a closure. Note: the closure is evaluated before checking modifiedAt. As such the closure execution does not mean the change was applied

    Declaration

    Swift

    @discardableResult
    public func update<T>(named: AliasKey<T>, modifiedAt: Stamp? = nil, update: Update<T>) -> Bool where T : Identifiable

    Return Value

    true if entity exists and might be updated, false otherwise. The update might not be applied if modifiedAt is too old

  • Updates an already stored alias using a closure. Note: the closure is evaluated before checking modifiedAt. As such the closure execution does not mean the change was applied

    Declaration

    Swift

    @discardableResult
    public func update<T>(named: AliasKey<T>, modifiedAt: Stamp? = nil, update: Update<T>) -> Bool where T : Aggregate

    Return Value

    true if entity exists and might be updated, false otherwise. The update might not be applied if modifiedAt is too old

  • Updates an already existing collection alias content Note: the closure is evaluated before checking modifiedAt. As such the closure execution does not mean the change was applied

    Declaration

    Swift

    @discardableResult
    public func update<C: Collection>(named: AliasKey<C>, modifiedAt: Stamp? = nil, update: Update<C>)
    -> Bool where C.Element: Identifiable

    Return Value

    true if entity exists and might be updated, false otherwise. The update might not be applied if modifiedAt is too old

  • Updates an already existing collection alias content Note: the closure is evaluated before checking modifiedAt. As such the closure execution does not mean the change was applied

    Declaration

    Swift

    @discardableResult
    public func update<C: Collection>(named: AliasKey<C>, modifiedAt: Stamp? = nil, update: Update<C>)
    -> Bool where C.Element: Aggregate

    Return Value

    true if entity exists and might be updated, false otherwise. The update might not be applied if modifiedAt is too old

Delete

  • Removes an alias from the storage

    Declaration

    Swift

    public func removeAlias<T>(named: AliasKey<T>)
  • Removes an alias from the storage

    Declaration

    Swift

    public func removeAlias<C>(named: AliasKey<C>) where C : Collection
  • Removes all alias from identity map

    Declaration

    Swift

    public func removeAllAlias()
  • Removes all alias AND all objects stored weakly. You should not need this method and rather use removeAlias. But this can be useful if you fear retain cycles

    Declaration

    Swift

    public func removeAll()