From 8a611f4109ace88b957edc0645578b14d4fadce8 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 11 Nov 2024 21:46:08 -0800 Subject: [PATCH] Make ArticleStatus Sendable. --- Articles/Sources/Articles/ArticleStatus.swift | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/Articles/Sources/Articles/ArticleStatus.swift b/Articles/Sources/Articles/ArticleStatus.swift index 4ccc6b95c..a958e3d5e 100644 --- a/Articles/Sources/Articles/ArticleStatus.swift +++ b/Articles/Sources/Articles/ArticleStatus.swift @@ -7,15 +7,10 @@ // import Foundation +import os -// Threading rules: -// * Main-thread only -// * Except: may be created on background thread by StatusesTable. -// Which is safe, because at creation time it’t not yet shared, -// and it won’t be mutated ever on a background thread. +public final class ArticleStatus: Sendable, Hashable { -public final class ArticleStatus: Hashable { - public enum Key: String { case read = "read" case starred = "starred" @@ -24,13 +19,31 @@ public final class ArticleStatus: Hashable { public let articleID: String public let dateArrived: Date - public var read = false - public var starred = false + private let _read: OSAllocatedUnfairLock + private let _starred: OSAllocatedUnfairLock + + public var read: Bool { + get { + _read.withLock { $0 } + } + set { + _read.withLock { $0 = newValue } + } + } + + public var starred: Bool { + get { + _starred.withLock { $0 } + } + set { + _starred.withLock { $0 = newValue } + } + } public init(articleID: String, read: Bool, starred: Bool, dateArrived: Date) { self.articleID = articleID - self.read = read - self.starred = starred + self._read = OSAllocatedUnfairLock(initialState: read) + self._starred = OSAllocatedUnfairLock(initialState: starred) self.dateArrived = dateArrived } @@ -65,20 +78,20 @@ public final class ArticleStatus: Hashable { // MARK: - Equatable public static func ==(lhs: ArticleStatus, rhs: ArticleStatus) -> Bool { - return lhs.articleID == rhs.articleID && lhs.dateArrived == rhs.dateArrived && lhs.read == rhs.read && lhs.starred == rhs.starred + lhs.articleID == rhs.articleID && lhs.dateArrived == rhs.dateArrived && lhs.read == rhs.read && lhs.starred == rhs.starred } } public extension Set where Element == ArticleStatus { - + func articleIDs() -> Set { - return Set(map { $0.articleID }) + Set(map { $0.articleID }) } } public extension Array where Element == ArticleStatus { - - func articleIDs() -> [String] { - return map { $0.articleID } + + func articleIDs() -> [String] { + map { $0.articleID } } }