Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lock #185

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Examples/Example.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
"version" : "1.1.0"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
"version" : "1.3.0"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
Expand Down
21 changes: 18 additions & 3 deletions Sources/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Combine
import Foundation
import Semaphore

/**
* `DocumentOptions` are the options to create a new document.
Expand Down Expand Up @@ -99,6 +100,8 @@ public actor Document {
*/
private var presences: [ActorID: StringValueTypeDictionary]

private let workSemaphore = AsyncSemaphore(value: 1)

public init(key: String) {
self.init(key: key, opts: DocumentOptions(disableGC: false))
}
Expand All @@ -120,7 +123,13 @@ public actor Document {
/**
* `update` executes the given updater to update this document.
*/
public func update(_ updater: (_ root: JSONObject, _ presence: inout Presence) throws -> Void, _ message: String? = nil) throws {
public func update(_ updater: (_ root: JSONObject, _ presence: inout Presence) async throws -> Void, _ message: String? = nil) async throws {
await self.workSemaphore.wait()

defer {
self.workSemaphore.signal()
}

guard self.status != .removed else {
throw YorkieError.documentRemoved(message: "\(self) is removed.")
}
Expand All @@ -141,7 +150,7 @@ public actor Document {

var presence = Presence(changeContext: context, presence: self.clone?.presences[actorID] ?? [:])

try updater(proxy, &presence)
try await updater(proxy, &presence)

self.clone?.presences[actorID] = presence.presence

Expand Down Expand Up @@ -250,7 +259,13 @@ public actor Document {
*
* - Parameter pack: change pack
*/
func applyChangePack(_ pack: ChangePack) throws {
func applyChangePack(_ pack: ChangePack) async throws {
await self.workSemaphore.wait()

defer {
self.workSemaphore.signal()
}

if let snapshot = pack.getSnapshot() {
try self.applySnapshot(pack.getCheckpoint().getServerSeq(), snapshot)
} else if pack.hasChanges() {
Expand Down