Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Jul 5, 2024
1 parent 644bec3 commit 050cd45
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
6 changes: 4 additions & 2 deletions Sources/Verge/Derived/Derived.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ public class Derived<Value: Equatable>: Store<Value, Never>, DerivedType, @unche
retainsUpstream: Any?
) where Pipeline.Input == UpstreamState, Value == Pipeline.Output {

let pipelineStorage: Pipeline.Storage = pipeline.makeStorage()

weak var indirectSelf: Derived<Value>?

let s = subscribeUpstreamState { value in
let update = pipeline.yieldContinuously(value)
let update = pipeline.yieldContinuously(value, storage: pipelineStorage)
switch update {
case .noUpdates:
break
Expand All @@ -135,7 +137,7 @@ public class Derived<Value: Equatable>: Store<Value, Never>, DerivedType, @unche
self._set = set
super.init(
name: name,
initialState: pipeline.yield(initialUpstreamState),
initialState: pipeline.yield(initialUpstreamState, storage: pipelineStorage),
logger: nil,
sanitizer: nil
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Store/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public protocol PipelineType<Input, Output> {

associatedtype Input
associatedtype Output
associatedtype Storage = Void
associatedtype Storage: Sendable = Void

func makeStorage() -> Storage

Expand Down
28 changes: 14 additions & 14 deletions Sources/VergeNormalizationDerived/DispatcherType+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ where _StorageSelector.Storage == _TableSelector.Storage {

typealias Entity = _TableSelector.Table.Entity
typealias Input = Changes<_StorageSelector.Source>
typealias Storage = _StorageSelector.Storage
typealias EntityStorage = _StorageSelector.Storage
typealias Output = EntityWrapper<Entity>

private let selector: AbsoluteTableSelector<_StorageSelector, _TableSelector>
Expand All @@ -342,7 +342,7 @@ where _StorageSelector.Storage == _TableSelector.Storage {
self.selector = selector
}

func yield(_ input: consuming Input) -> Output {
func yield(_ input: consuming Input, storage: Void) -> Output {

let result = selector.table(source: input.primitive)
.find(by: entityID)
Expand All @@ -351,21 +351,21 @@ where _StorageSelector.Storage == _TableSelector.Storage {

}

func yieldContinuously(_ input: Input) -> Verge.ContinuousResult<Output> {
func yieldContinuously(_ input: Input, storage: Void) -> Verge.ContinuousResult<Output> {

guard let previous = input.previous else {
return .new(yield(input))
return .new(yield(input, storage: storage))
}

if NormalizedStorageComparisons<Storage>.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
if NormalizedStorageComparisons<EntityStorage>.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
return .noUpdates
}

if NormalizedStorageComparisons<Storage>.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
if NormalizedStorageComparisons<EntityStorage>.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
return .noUpdates
}

return .new(yield(input))
return .new(yield(input, storage: storage))

}

Expand All @@ -379,7 +379,7 @@ where _StorageSelector.Storage == _TableSelector.Storage {

typealias Entity = _TableSelector.Table.Entity
typealias Input = Changes<_StorageSelector.Source>
typealias Storage = _StorageSelector.Storage
typealias EntityStorage = _StorageSelector.Storage
typealias Output = NonNullEntityWrapper<Entity>

private let selector: AbsoluteTableSelector<_StorageSelector, _TableSelector>
Expand All @@ -399,7 +399,7 @@ where _StorageSelector.Storage == _TableSelector.Storage {

}

func yield(_ input: consuming Input) -> Output {
func yield(_ input: consuming Input, storage: Void) -> Output {

let result = selector.table(source: input.primitive)
.find(by: entityID)
Expand All @@ -412,21 +412,21 @@ where _StorageSelector.Storage == _TableSelector.Storage {

}

func yieldContinuously(_ input: Input) -> Verge.ContinuousResult<Output> {
func yieldContinuously(_ input: Input, storage: Void) -> Verge.ContinuousResult<Output> {

guard let previous = input.previous else {
return .new(yield(input))
return .new(yield(input, storage: storage))
}

if NormalizedStorageComparisons<Storage>.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
if NormalizedStorageComparisons<EntityStorage>.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) {
return .noUpdates
}

if NormalizedStorageComparisons<Storage>.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
if NormalizedStorageComparisons<EntityStorage>.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) {
return .noUpdates
}

return .new(yield(input))
return .new(yield(input, storage: storage))

}

Expand Down
20 changes: 12 additions & 8 deletions Sources/VergeNormalizationDerived/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ struct QueryPipeline<
>: PipelineType, Sendable {

typealias Input = Changes<_StorageSelector.Source>
typealias Storage = _StorageSelector.Storage
typealias EntityStorage = _StorageSelector.Storage

private let storageSelector: _StorageSelector
private let query: @Sendable (Storage) -> Output
private let query: @Sendable (EntityStorage) -> Output

init(
storageSelector: consuming _StorageSelector,
query: @escaping @Sendable (Storage) -> Output
query: @escaping @Sendable (EntityStorage) -> Output
) {
self.storageSelector = storageSelector
self.query = query
}

func yield(_ input: consuming Input) -> Output {
func makeStorage() -> Void {
()
}

func yield(_ input: consuming Input, storage: Void) -> Output {

let storage = storageSelector.select(source: input.primitive)
let output = query(storage)
Expand All @@ -27,18 +31,18 @@ struct QueryPipeline<

}

func yieldContinuously(_ input: Input) -> Verge.ContinuousResult<Output> {
func yieldContinuously(_ input: Input, storage: Void) -> Verge.ContinuousResult<Output> {

guard let previous = input.previous else {
return .new(yield(input))
return .new(yield(input, storage: storage))
}

// check if the storage has been updated
if NormalizedStorageComparisons<Storage>.StorageComparison()(storageSelector.select(source: input.primitive), storageSelector.select(source: previous.primitive)) {
if NormalizedStorageComparisons<EntityStorage>.StorageComparison()(storageSelector.select(source: input.primitive), storageSelector.select(source: previous.primitive)) {
return .noUpdates
}

return .new(yield(input))
return .new(yield(input, storage: storage))

}

Expand Down

0 comments on commit 050cd45

Please sign in to comment.