Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publisher as an associated object for StorageStream #1

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"originHash" : "25ff82c37f645f2c42df15aa9a59679d03518e9f2ec3b0858edf16504f8b18a5",
"originHash" : "c363ee6ef8d42aa78193eb96c78dad95089cdedc561bcd9ec34e35706ff74e78",
"pins" : [
{
"identity" : "cleeviocore",
"kind" : "remoteSourceControl",
"location" : "git@gitlab.cleevio.cz:cleevio-dev-ios/CleevioCore.git",
"location" : "https://github.com/cleevio/CleevioCore.git",
"state" : {
"revision" : "593b5d1011ad0aa6963ea2d8bf2ddc5e59caa917",
"version" : "2.1.7"
Expand Down
19 changes: 18 additions & 1 deletion Sources/CleevioStorage/StorageStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
import Combine
import Foundation
import Observation
import CleevioCore

@available(macOS 10.15, *)
open class StorageStream<Value>: @unchecked Sendable {
var onChange: ((Value?) -> Void)?

public private(set) lazy var id = ObjectIdentifier(self)
private let currentValueSubject: CurrentValueSubject<Value?, Never>

public var publisher: AnyPublisher<Value?, Never> {
currentValueSubject.eraseToAnyPublisher()
let publisher = currentValueSubject.eraseToAnyPublisher()
setAssociatedObject(base: self, key: &id, value: self)
return publisher
}

public var value: Value? {
Expand All @@ -31,6 +35,19 @@ open class StorageStream<Value>: @unchecked Sendable {
}
}

extension StorageStream: Identifiable { }
extension StorageStream: Equatable {
public static func == (lhs: StorageStream<Value>, rhs: StorageStream<Value>) -> Bool {
lhs.id == rhs.id
}
}

extension StorageStream: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}

@available(watchOS 10.0, *)
@available(iOS 17.0, *)
@available(macOS 14.0, *)
Expand Down