33 lines
788 B
Swift
33 lines
788 B
Swift
//
|
|
// SnapshotPublisher.swift
|
|
// punchnet
|
|
//
|
|
// Created by 安礼成 on 2026/2/5.
|
|
//
|
|
import Atomics
|
|
|
|
final class SnapshotPublisher<IdentitySnapshot: AnyObject> {
|
|
private let atomic: ManagedAtomic<Unmanaged<IdentitySnapshot>>
|
|
|
|
init(initial snapshot: IdentitySnapshot) {
|
|
self.atomic = ManagedAtomic(.passRetained(snapshot))
|
|
}
|
|
|
|
func publish(_ snapshot: IdentitySnapshot) {
|
|
let newRef = Unmanaged.passRetained(snapshot)
|
|
let oldRef = atomic.exchange(newRef, ordering: .acquiring)
|
|
oldRef.release()
|
|
}
|
|
|
|
@inline(__always)
|
|
func current() -> IdentitySnapshot {
|
|
atomic.load(ordering: .relaxed).takeUnretainedValue()
|
|
}
|
|
|
|
deinit {
|
|
let ref = atomic.load(ordering: .relaxed)
|
|
ref.release()
|
|
}
|
|
|
|
}
|