From 050cd45a9f0561ac1de487012af1b08b3f7cc2bf Mon Sep 17 00:00:00 2001 From: Muukii Date: Fri, 5 Jul 2024 14:48:34 +0900 Subject: [PATCH] Update --- Sources/Verge/Derived/Derived.swift | 6 ++-- Sources/Verge/Store/Pipeline.swift | 2 +- .../DispatcherType+.swift | 28 +++++++++---------- Sources/VergeNormalizationDerived/Query.swift | 20 +++++++------ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Sources/Verge/Derived/Derived.swift b/Sources/Verge/Derived/Derived.swift index b3fcd4a15b..d5a80998cc 100644 --- a/Sources/Verge/Derived/Derived.swift +++ b/Sources/Verge/Derived/Derived.swift @@ -108,10 +108,12 @@ public class Derived: Store, DerivedType, @unche retainsUpstream: Any? ) where Pipeline.Input == UpstreamState, Value == Pipeline.Output { + let pipelineStorage: Pipeline.Storage = pipeline.makeStorage() + weak var indirectSelf: Derived? let s = subscribeUpstreamState { value in - let update = pipeline.yieldContinuously(value) + let update = pipeline.yieldContinuously(value, storage: pipelineStorage) switch update { case .noUpdates: break @@ -135,7 +137,7 @@ public class Derived: Store, DerivedType, @unche self._set = set super.init( name: name, - initialState: pipeline.yield(initialUpstreamState), + initialState: pipeline.yield(initialUpstreamState, storage: pipelineStorage), logger: nil, sanitizer: nil ) diff --git a/Sources/Verge/Store/Pipeline.swift b/Sources/Verge/Store/Pipeline.swift index 46608bfd2d..c5a2b39a5a 100644 --- a/Sources/Verge/Store/Pipeline.swift +++ b/Sources/Verge/Store/Pipeline.swift @@ -37,7 +37,7 @@ public protocol PipelineType { associatedtype Input associatedtype Output - associatedtype Storage = Void + associatedtype Storage: Sendable = Void func makeStorage() -> Storage diff --git a/Sources/VergeNormalizationDerived/DispatcherType+.swift b/Sources/VergeNormalizationDerived/DispatcherType+.swift index d19da2eab3..8174d08db7 100644 --- a/Sources/VergeNormalizationDerived/DispatcherType+.swift +++ b/Sources/VergeNormalizationDerived/DispatcherType+.swift @@ -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 private let selector: AbsoluteTableSelector<_StorageSelector, _TableSelector> @@ -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) @@ -351,21 +351,21 @@ where _StorageSelector.Storage == _TableSelector.Storage { } - func yieldContinuously(_ input: Input) -> Verge.ContinuousResult { + func yieldContinuously(_ input: Input, storage: Void) -> Verge.ContinuousResult { guard let previous = input.previous else { - return .new(yield(input)) + return .new(yield(input, storage: storage)) } - if NormalizedStorageComparisons.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) { + if NormalizedStorageComparisons.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) { return .noUpdates } - if NormalizedStorageComparisons.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) { + if NormalizedStorageComparisons.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)) } @@ -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 private let selector: AbsoluteTableSelector<_StorageSelector, _TableSelector> @@ -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) @@ -412,21 +412,21 @@ where _StorageSelector.Storage == _TableSelector.Storage { } - func yieldContinuously(_ input: Input) -> Verge.ContinuousResult { + func yieldContinuously(_ input: Input, storage: Void) -> Verge.ContinuousResult { guard let previous = input.previous else { - return .new(yield(input)) + return .new(yield(input, storage: storage)) } - if NormalizedStorageComparisons.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) { + if NormalizedStorageComparisons.StorageComparison()(selector.storage(source: input.primitive), selector.storage(source: previous.primitive)) { return .noUpdates } - if NormalizedStorageComparisons.TableComparison<_TableSelector.Table>()(selector.table(source: input.primitive), selector.table(source: previous.primitive)) { + if NormalizedStorageComparisons.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)) } diff --git a/Sources/VergeNormalizationDerived/Query.swift b/Sources/VergeNormalizationDerived/Query.swift index 80fe7a0357..09a9790806 100644 --- a/Sources/VergeNormalizationDerived/Query.swift +++ b/Sources/VergeNormalizationDerived/Query.swift @@ -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) @@ -27,18 +31,18 @@ struct QueryPipeline< } - func yieldContinuously(_ input: Input) -> Verge.ContinuousResult { + func yieldContinuously(_ input: Input, storage: Void) -> Verge.ContinuousResult { 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.StorageComparison()(storageSelector.select(source: input.primitive), storageSelector.select(source: previous.primitive)) { + if NormalizedStorageComparisons.StorageComparison()(storageSelector.select(source: input.primitive), storageSelector.select(source: previous.primitive)) { return .noUpdates } - return .new(yield(input)) + return .new(yield(input, storage: storage)) }