Skip to content

Commit

Permalink
refactor: add private access control to lock isolated values (#328)
Browse files Browse the repository at this point in the history
* refactor: add private access control to lock isolated values

* chore: allow refactor prexi on commit

* chore: add readonly access to CallbackManager properties
  • Loading branch information
grdsdev authored Apr 8, 2024
1 parent 8063610 commit cc1b3af
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/conventional-commits-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const ALLOWED_CONVENTIONAL_COMMIT_PREFIXES = [
"chore",
"style",
"test",
"refactor",
];

const object = process.argv[2];
Expand Down Expand Up @@ -102,4 +103,4 @@ if (failed) {
process.exit(1);
}

process.exit(0);
process.exit(0);
2 changes: 1 addition & 1 deletion Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class PostgrestClient: Sendable {
}
}

let _configuration: LockIsolated<Configuration>
private let _configuration: LockIsolated<Configuration>
public var configuration: Configuration { _configuration.value }

/// Creates a PostgREST client with the specified configuration.
Expand Down
10 changes: 9 additions & 1 deletion Sources/Realtime/V2/CallbackManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ final class CallbackManager: @unchecked Sendable {
var callbacks: [RealtimeCallback] = []
}

let mutableState = LockIsolated(MutableState())
private let mutableState = LockIsolated(MutableState())

var serverChanges: [PostgresJoinConfig] {
mutableState.serverChanges
}

var callbacks: [RealtimeCallback] {
mutableState.callbacks
}

@discardableResult
func addBroadcastCallback(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Realtime/V2/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class WebSocket: NSObject, URLSessionWebSocketDelegate, WebSocketClient, @
var stream: SocketStream?
}

let mutableState = LockIsolated(MutableState())
private let mutableState = LockIsolated(MutableState())

init(config: RealtimeClientV2.Configuration) {
realtimeURL = config.realtimeWebSocketURL
Expand Down
2 changes: 1 addition & 1 deletion Sources/_Helpers/EventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class ObservationToken: Sendable {
package final class EventEmitter<Event: Sendable>: Sendable {
public typealias Listener = @Sendable (Event) -> Void

let listeners = LockIsolated<[ObjectIdentifier: Listener]>([:])
private let listeners = LockIsolated<[ObjectIdentifier: Listener]>([:])
public let lastEvent: LockIsolated<Event>

let emitsLastEventWhenAttaching: Bool
Expand Down
8 changes: 4 additions & 4 deletions Tests/RealtimeTests/CallbackManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ final class CallbackManagerTests: XCTestCase {

XCTAssertEqual(callbackManager.addPresenceCallback { _ in }, 3)

XCTAssertEqual(callbackManager.mutableState.value.callbacks.count, 3)
XCTAssertEqual(callbackManager.callbacks.count, 3)

callbackManager.removeCallback(id: 2)
callbackManager.removeCallback(id: 3)

XCTAssertEqual(callbackManager.mutableState.value.callbacks.count, 1)
XCTAssertEqual(callbackManager.callbacks.count, 1)
XCTAssertFalse(
callbackManager.mutableState.value.callbacks
callbackManager.callbacks
.contains(where: { $0.id == 2 || $0.id == 3 })
)
}
Expand All @@ -62,7 +62,7 @@ final class CallbackManagerTests: XCTestCase {

callbackManager.setServerChanges(changes: changes)

XCTAssertEqual(callbackManager.mutableState.value.serverChanges, changes)
XCTAssertEqual(callbackManager.serverChanges, changes)
}

func testTriggerPostgresChanges() {
Expand Down

0 comments on commit cc1b3af

Please sign in to comment.