-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…le is unloaded
- Loading branch information
Showing
7 changed files
with
163 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
Sources/Spezi/Capabilities/Communication/CollectedModuleValue.swift
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
Sources/Spezi/Capabilities/Communication/CollectedModuleValues.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SpeziFoundation | ||
|
||
protocol AnyCollectModuleValue { | ||
associatedtype Value | ||
|
||
var moduleReference: ModuleReference { get } | ||
} | ||
|
||
protocol AnyCollectModuleValues { | ||
associatedtype Value | ||
|
||
var values: [any AnyCollectModuleValue] { get } | ||
|
||
mutating func removeValues(from module: any Module) -> Bool | ||
|
||
func store(into storage: inout SpeziStorage) | ||
} | ||
|
||
|
||
struct CollectModuleValue<Value>: AnyCollectModuleValue { | ||
let value: Value | ||
let moduleReference: ModuleReference | ||
|
||
init(_ value: Value) { | ||
self.value = value | ||
|
||
guard let module = Spezi.moduleInitContext else { | ||
preconditionFailure("Tried to initialize CollectModuleValue with unknown module init context.") | ||
} | ||
self.moduleReference = ModuleReference(module) | ||
} | ||
} | ||
|
||
/// Provides the ``KnowledgeSource`` for any value we store in the ``SpeziStorage`` that is | ||
/// provided or request from am ``Module``. | ||
/// | ||
/// For more information, look at the ``Module/Provide`` or ``Module/Collect`` property wrappers. | ||
struct CollectedModuleValues<ModuleValue>: DefaultProvidingKnowledgeSource { | ||
typealias Anchor = SpeziAnchor | ||
|
||
typealias Value = [CollectModuleValue<ModuleValue>] | ||
|
||
|
||
static var defaultValue: Value { | ||
[] | ||
} | ||
} | ||
|
||
|
||
extension Array: AnyCollectModuleValues where Element: AnyCollectModuleValue { | ||
typealias Value = Element.Value | ||
|
||
var values: [any AnyCollectModuleValue] { | ||
self | ||
} | ||
|
||
mutating func removeValues(from module: any Module) -> Bool { | ||
let previousCount = count | ||
removeAll { entry in | ||
entry.moduleReference == ModuleReference(module) | ||
} | ||
return previousCount != count | ||
} | ||
|
||
func store(into storage: inout SpeziStorage) { | ||
guard let values = self as? [CollectModuleValue<Value>] else { | ||
preconditionFailure("Unexpected array type: \(type(of: self))") | ||
} | ||
storage[CollectedModuleValues<Value>.self] = values | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters