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 3 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
17 changes: 14 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,7 @@ 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 {
guard self.status != .removed else {
throw YorkieError.documentRemoved(message: "\(self) is removed.")
}
Expand All @@ -129,6 +132,8 @@ public actor Document {
throw YorkieError.unexpected(message: "actor ID is null.")
}

await self.workSemaphore.wait()

// 01. Update the clone object and create a change.
let clone = self.cloned
let context = ChangeContext(id: self.changeID.next(), root: clone.root, message: message)
Expand All @@ -141,7 +146,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 @@ -172,6 +177,8 @@ public actor Document {

Logger.trace("after update a local change: \(self.toJSON())")
}

self.workSemaphore.signal()
}

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

if let snapshot = pack.getSnapshot() {
try self.applySnapshot(pack.getCheckpoint().getServerSeq(), snapshot)
} else if pack.hasChanges() {
Expand All @@ -276,6 +285,8 @@ public actor Document {
}

Logger.trace("\(self.root.toJSON())")

self.workSemaphore.signal()
}

/**
Expand Down