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 RealtimeSyncOff and refactor interface of SyncMode #160

Merged
merged 1 commit into from
Apr 16, 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
3 changes: 1 addition & 2 deletions Examples/KanbanApp/KanbanApp/Kanban/KanbanViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class KanbanViewModel: ObservableObject {
private let document: Document

init() {
self.client = Client(rpcAddress: RPCAddress(host: "localhost", port: 8080),
options: ClientOptions())
self.client = Client(RPCAddress(host: "localhost", port: 8080))

let docKey = "KanbanViewModel-8"
self.document = Document(key: docKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class TextViewModel {
self.operationSubject = operationSubject

// create client with RPCAddress.
self.client = Client(rpcAddress: RPCAddress(host: "localhost", port: 8080),
options: ClientOptions())
self.client = Client(RPCAddress(host: "localhost", port: 8080))

// create a document
self.document = Document(key: "codemirror")
Expand Down Expand Up @@ -141,12 +140,12 @@ class TextViewModel {
}
}

func pause() async {
try? await self.client.pauseRemoteChanges(self.document)
func pause() async throws {
try await self.client.changeSyncMode(self.document, .realtimePushOnly)
}

func resume() async {
try? await self.client.resumeRemoteChanges(self.document)
func resume() async throws {
try await self.client.changeSyncMode(self.document, .realtime)
}

private func decodePresence<T: Decodable>(_ dictionary: Any?) -> T? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ class TextEditorViewController: UIViewController {
didSet {
if oldValue != self.isCompositioning {
Task {
if self.isCompositioning {
await self.model?.pause()
} else {
await self.model?.resume()
do {
if self.isCompositioning {
try await self.model?.pause()
} else {
try await self.model?.resume()
}
} catch {
assertionFailure()
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions Sources/Core/Attachment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation
import GRPC

class Attachment {
var doc: Document
var docID: String
var syncMode: SyncMode
var remoteChangeEventReceived: Bool
var remoteWatchStream: GRPCAsyncServerStreamingCall<WatchDocumentRequest, WatchDocumentResponse>?
var watchLoopReconnectTimer: Timer?

init(doc: Document, docID: String, syncMode: SyncMode, remoteChangeEventReceived: Bool, remoteWatchStream: GRPCAsyncServerStreamingCall<WatchDocumentRequest, WatchDocumentResponse>? = nil, watchLoopReconnectTimer: Timer? = nil) {
self.doc = doc
self.docID = docID
self.syncMode = syncMode
self.remoteChangeEventReceived = remoteChangeEventReceived
self.remoteWatchStream = remoteWatchStream
self.watchLoopReconnectTimer = watchLoopReconnectTimer
}

/**
* `needRealtimeSync` returns whether the document needs to be synced in real time.
*/
func needRealtimeSync() async -> Bool {
if self.syncMode == .realtimeSyncOff {
return false
}

let hasLocalChanges = await doc.hasLocalChanges()

return self.syncMode != .manual && (hasLocalChanges || self.remoteChangeEventReceived)
}
}
Loading
Loading