Skip to content

Commit

Permalink
Merge branch 'main' into reduce_changes_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
humdrum committed Jul 11, 2023
2 parents 14fcd54 + b6424dc commit 981eb24
Show file tree
Hide file tree
Showing 15 changed files with 590 additions and 66 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/swift-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ on:

jobs:
build:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- uses: docker-practice/actions-setup-docker@master
- name: Setup Docker on macOS
uses: douglascamata/setup-docker-macos-action@v1-alpha
- run: docker-compose -f docker/docker-compose-ci.yml up --build -d
- name: Run tests
run: swift test --enable-code-coverage -v --filter YorkieIntegrationTests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
build:
runs-on: macos-12
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- name: SwiftLint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ class TextViewModel {
}

// subscribe document event.
let clientID = await self.client.id

await self.document.subscribe { [weak self] event in
if event.type == .snapshot {
switch event.type {
case .snapshot, .remoteChange:
Task { [weak self] in
await self?.syncText()
}
default:
break
}
}

Expand All @@ -73,15 +74,9 @@ class TextViewModel {

var textChanges = [TextOperation]()

event.value.filter { $0.actorID != clientID }.forEach { changeInfo in
event.value.forEach { changeInfo in
changeInfo.operations.forEach {
if let op = $0 as? EditOpInfo {
let range = NSRange(location: op.from, length: op.to - op.from)
let content = op.content ?? ""

textChanges.append(.edit(range: range, content: content))
} else if let _ = $0 as? StyleOpInfo {
} else if let op = $0 as? SelectOpInfo {
if let op = $0 as? SelectOpInfo {
let range: NSRange

if op.from <= op.to {
Expand Down Expand Up @@ -146,10 +141,10 @@ class TextViewModel {
}

func pause() async {
try! await self.client.pause(self.document)
try? await self.client.pauseRemoteChanges(doc: self.document)
}

func resume() async {
try! await self.client.resume(self.document)
try? await self.client.resumeRemoteChanges(doc: self.document)
}
}
8 changes: 8 additions & 0 deletions Sources/API/V1/yorkie/v1/yorkie.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ struct Yorkie_V1_PushPullChangesRequest {
/// Clears the value of `changePack`. Subsequent reads from it will return its default value.
mutating func clearChangePack() {self._changePack = nil}

var pushOnly: Bool = false

var unknownFields = SwiftProtobuf.UnknownStorage()

init() {}
Expand Down Expand Up @@ -976,6 +978,7 @@ extension Yorkie_V1_PushPullChangesRequest: SwiftProtobuf.Message, SwiftProtobuf
1: .standard(proto: "client_id"),
2: .standard(proto: "document_id"),
3: .standard(proto: "change_pack"),
4: .standard(proto: "push_only"),
]

mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
Expand All @@ -987,6 +990,7 @@ extension Yorkie_V1_PushPullChangesRequest: SwiftProtobuf.Message, SwiftProtobuf
case 1: try { try decoder.decodeSingularBytesField(value: &self.clientID) }()
case 2: try { try decoder.decodeSingularStringField(value: &self.documentID) }()
case 3: try { try decoder.decodeSingularMessageField(value: &self._changePack) }()
case 4: try { try decoder.decodeSingularBoolField(value: &self.pushOnly) }()
default: break
}
}
Expand All @@ -1006,13 +1010,17 @@ extension Yorkie_V1_PushPullChangesRequest: SwiftProtobuf.Message, SwiftProtobuf
try { if let v = self._changePack {
try visitor.visitSingularMessageField(value: v, fieldNumber: 3)
} }()
if self.pushOnly != false {
try visitor.visitSingularBoolField(value: self.pushOnly, fieldNumber: 4)
}
try unknownFields.traverse(visitor: &visitor)
}

static func ==(lhs: Yorkie_V1_PushPullChangesRequest, rhs: Yorkie_V1_PushPullChangesRequest) -> Bool {
if lhs.clientID != rhs.clientID {return false}
if lhs.documentID != rhs.documentID {return false}
if lhs._changePack != rhs._changePack {return false}
if lhs.pushOnly != rhs.pushOnly {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}
Expand Down
1 change: 1 addition & 0 deletions Sources/API/V1/yorkie/v1/yorkie.proto
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ message PushPullChangesRequest {
bytes client_id = 1;
string document_id = 2;
ChangePack change_pack = 3;
bool push_only = 4;
}

message PushPullChangesResponse {
Expand Down
32 changes: 24 additions & 8 deletions Sources/Core/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import NIOCore
class AuthClientInterceptor<Request, Response>: ClientInterceptor<Request, Response> {
let apiKey: String?
let token: String?
let docKey: String?

init(apiKey: String? = nil, token: String? = nil) {
init(apiKey: String? = nil, token: String? = nil, docKey: String? = nil) {
self.apiKey = apiKey
self.token = token
self.docKey = docKey
}

override func send(_ part: GRPCClientRequestPart<Request>, promise: EventLoopPromise<Void>?, context: ClientInterceptorContext<Request, Response>) {
Expand All @@ -34,6 +36,14 @@ class AuthClientInterceptor<Request, Response>: ClientInterceptor<Request, Respo
case .metadata(var header):
if let apiKey {
header.add(name: "x-api-key", value: apiKey)

var shardKey = "\(apiKey)"

if let docKey = self.docKey, docKey.isEmpty == false {
shardKey += "/\(docKey)"
}

header.add(name: "x-shard-key", value: shardKey)
}

if let token {
Expand All @@ -53,10 +63,16 @@ class AuthClientInterceptor<Request, Response>: ClientInterceptor<Request, Respo
final class AuthClientInterceptors: YorkieServiceClientInterceptorFactoryProtocol {
let apiKey: String?
let token: String?
let docKey: String?

init(apiKey: String? = nil, token: String? = nil) {
init(apiKey: String? = nil, token: String? = nil, docKey: String? = nil) {
self.apiKey = apiKey
self.token = token
self.docKey = docKey
}

func docKeyChangedInterceptors(_ docKey: String?) -> AuthClientInterceptors {
AuthClientInterceptors(apiKey: self.apiKey, token: self.token, docKey: docKey)
}

func makeActivateClientInterceptors() -> [GRPC.ClientInterceptor<ActivateClientRequest, ActivateClientResponse>] {
Expand All @@ -68,26 +84,26 @@ final class AuthClientInterceptors: YorkieServiceClientInterceptorFactoryProtoco
}

func makeUpdatePresenceInterceptors() -> [GRPC.ClientInterceptor<UpdatePresenceRequest, UpdatePresenceResponse>] {
[AuthClientInterceptor<UpdatePresenceRequest, UpdatePresenceResponse>(apiKey: self.apiKey, token: self.token)]
[AuthClientInterceptor<UpdatePresenceRequest, UpdatePresenceResponse>(apiKey: self.apiKey, token: self.token, docKey: self.docKey)]
}

func makeAttachDocumentInterceptors() -> [GRPC.ClientInterceptor<AttachDocumentRequest, AttachDocumentResponse>] {
[AuthClientInterceptor<AttachDocumentRequest, AttachDocumentResponse>(apiKey: self.apiKey, token: self.token)]
[AuthClientInterceptor<AttachDocumentRequest, AttachDocumentResponse>(apiKey: self.apiKey, token: self.token, docKey: self.docKey)]
}

func makeDetachDocumentInterceptors() -> [GRPC.ClientInterceptor<DetachDocumentRequest, DetachDocumentResponse>] {
[AuthClientInterceptor<DetachDocumentRequest, DetachDocumentResponse>(apiKey: self.apiKey, token: self.token)]
[AuthClientInterceptor<DetachDocumentRequest, DetachDocumentResponse>(apiKey: self.apiKey, token: self.token, docKey: self.docKey)]
}

func makeWatchDocumentInterceptors() -> [GRPC.ClientInterceptor<WatchDocumentRequest, WatchDocumentResponse>] {
[AuthClientInterceptor<WatchDocumentRequest, WatchDocumentResponse>(apiKey: self.apiKey, token: self.token)]
[AuthClientInterceptor<WatchDocumentRequest, WatchDocumentResponse>(apiKey: self.apiKey, token: self.token, docKey: self.docKey)]
}

func makeRemoveDocumentInterceptors() -> [GRPC.ClientInterceptor<Yorkie_V1_RemoveDocumentRequest, Yorkie_V1_RemoveDocumentResponse>] {
[AuthClientInterceptor<RemoveDocumentRequest, RemoveDocumentResponse>(apiKey: self.apiKey, token: self.token)]
[AuthClientInterceptor<RemoveDocumentRequest, RemoveDocumentResponse>(apiKey: self.apiKey, token: self.token, docKey: self.docKey)]
}

func makePushPullChangesInterceptors() -> [GRPC.ClientInterceptor<Yorkie_V1_PushPullChangesRequest, Yorkie_V1_PushPullChangesResponse>] {
[AuthClientInterceptor<PushPullChangeRequest, PushPullChangeResponse>(apiKey: self.apiKey, token: self.token)]
[AuthClientInterceptor<PushPullChangeRequest, PushPullChangeResponse>(apiKey: self.apiKey, token: self.token, docKey: self.docKey)]
}
}
Loading

0 comments on commit 981eb24

Please sign in to comment.