Skip to content

Commit

Permalink
nonisolated(unsafe) property
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Jun 22, 2024
1 parent 5d3f865 commit 5284eed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
32 changes: 17 additions & 15 deletions Sources/RTMP/AMFFoundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,32 @@ public struct ASUndefined: Sendable, CustomStringConvertible {
public struct ASTypedObject {
public typealias TypedObjectDecoder = (_ type: String, _ data: ASObject) throws -> Any

static var decoders: [String: TypedObjectDecoder] = [:]

static func decode(typeName: String, data: ASObject) throws -> Any {
let decoder = decoders[typeName] ?? { ASTypedObject(typeName: $0, data: $1) }
return try decoder(typeName, data)
}

var typeName: String
var data: ASObject

public static func register(typeNamed name: String, decoder: @escaping TypedObjectDecoder) {
decoders[name] = decoder
decoders.mutate { $0[name] = decoder }
}

public static func register<T: Decodable>(type: T.Type, named name: String) {
decoders[name] = {
let jsonData = try JSONSerialization.data(withJSONObject: $1, options: [])
return try JSONDecoder().decode(type, from: jsonData)
decoders.mutate {
$0[name] = {
let jsonData = try JSONSerialization.data(withJSONObject: $1, options: [])
return try JSONDecoder().decode(type, from: jsonData)
}
}
}

public static func unregister(typeNamed name: String) {
decoders.removeValue(forKey: name)
decoders.mutate { $0.removeValue(forKey: name) }
}

static nonisolated(unsafe) var decoders: Atomic<[String: TypedObjectDecoder]> = .init([:])

static func decode(typeName: String, data: ASObject) throws -> Any {
let decoder = decoders.value[typeName] ?? { ASTypedObject(typeName: $0, data: $1) }
return try decoder(typeName, data)
}

var typeName: String
var data: ASObject
}

// MARK: -
Expand Down
26 changes: 13 additions & 13 deletions Sources/RTMP/RTMPSharedObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,19 @@ extension RTMPSharedObjectEvent: CustomDebugStringConvertible {
// MARK: -
/// The RTMPSharedObject class is used to read and write data on a server.
public final class RTMPSharedObject: EventDispatcher {
private static var remoteSharedObjects: [String: RTMPSharedObject] = [:]
private static nonisolated(unsafe) var remoteSharedObjects: Atomic<[String: RTMPSharedObject]> = .init([:])

/// Returns a reference to a shared object on a server.
public static func getRemote(withName: String, remotePath: String, persistence: Bool) -> RTMPSharedObject {
let key: String = remotePath + "/" + withName + "?persistence=" + persistence.description
objc_sync_enter(remoteSharedObjects)
if remoteSharedObjects[key] == nil {
remoteSharedObjects[key] = RTMPSharedObject(name: withName, path: remotePath, persistence: persistence)
let key = remotePath + "/" + withName + "?persistence=" + persistence.description
guard let sharedObject = remoteSharedObjects.value[key] else {
let sharedObject = RTMPSharedObject(name: withName, path: remotePath, persistence: persistence)
remoteSharedObjects.mutate { $0[key] = sharedObject }
return sharedObject
}
objc_sync_exit(remoteSharedObjects)
return remoteSharedObjects[key]!
return sharedObject
}

var name: String
var path: String
var timestamp: TimeInterval = 0
var persistence: Bool
var currentVersion: UInt32 = 0

/// The AMF object encoding type.
public let objectEncoding: RTMPObjectEncoding = RTMPConnection.defaultObjectEncoding
/// The current data storage.
Expand All @@ -115,6 +109,12 @@ public final class RTMPSharedObject: EventDispatcher {
}
}

let name: String
let path: String
var timestamp: TimeInterval = 0
let persistence: Bool
var currentVersion: UInt32 = 0

private var connection: RTMPConnection?

init(name: String, path: String, persistence: Bool) {
Expand Down

0 comments on commit 5284eed

Please sign in to comment.