Skip to content

Commit

Permalink
Replace serialQueue with actor to avoid concurrent access
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Sep 24, 2024
1 parent ffda8d4 commit 4228ceb
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions Sources/Arrow/Arrow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,19 @@ For more fine grained control, use `dateFormat` on a per field basis :
createdAt <-- json["created_at"]?.dateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ")
*/
public class Arrow {
public actor Arrow {

internal static let arrowSerialQueue = DispatchQueue(label: "com.arrow.arrow")
nonisolated(unsafe) internal static var dateFormatter: DateFormatter? = DateFormatter()
nonisolated(unsafe) internal static var useReferenceDate = false
internal static var dateFormatter: DateFormatter? = DateFormatter()
internal static var useReferenceDate = false

/// Sets the defaut dateFormat for parsing NSDates.
public class func setDateFormat(_ format: String) {
arrowSerialQueue.sync {
dateFormatter?.dateFormat = format
}
public static func setDateFormat(_ format: String) {
dateFormatter?.dateFormat = format
}

/// Sets the defaut dateFormatter for parsing NSDates.
public class func setDateFormatter(_ formatter: DateFormatter?) {
arrowSerialQueue.sync {
dateFormatter = formatter
}
public static func setDateFormatter(_ formatter: DateFormatter?) {
dateFormatter = formatter
}

/**
Expand All @@ -86,10 +81,8 @@ public class Arrow {
For more information see `NSDate(timeIntervalSinceReferenceDate`
documentation
*/
public class func setUseTimeIntervalSinceReferenceDate(_ ref: Bool) {
arrowSerialQueue.sync {
useReferenceDate = ref
}
public static func setUseTimeIntervalSinceReferenceDate(_ ref: Bool) {
useReferenceDate = ref
}
}

Expand Down Expand Up @@ -211,7 +204,7 @@ public func <-- (left: inout Date?, right: JSON?) {
df.dateFormat = customFormat
left = df.date(from: s)
} else if let s = right?.data as? String {
if let date = Arrow.arrowSerialQueue.sync(execute: { Arrow.dateFormatter?.date(from: s) }) {
if let date = Arrow.dateFormatter?.date(from: s) {
left = date
} else if let t = TimeInterval(s) {
left = timeIntervalToDate(t)
Expand Down Expand Up @@ -301,7 +294,7 @@ func parseString<T>(_ left: inout T?, string: String) {
}

func timeIntervalToDate(_ timeInterval: TimeInterval) -> Date {
return Arrow.arrowSerialQueue.sync(execute: { Arrow.useReferenceDate })
return Arrow.useReferenceDate
? Date(timeIntervalSinceReferenceDate: timeInterval)
: Date(timeIntervalSince1970: timeInterval)
}
Expand Down

0 comments on commit 4228ceb

Please sign in to comment.