• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_sync_service.h"
17 
18 namespace OHOS::CollaborationEdit {
GetInstance()19 std::shared_ptr<SyncService> SyncService::GetInstance()
20 {
21     static std::shared_ptr<SyncService> serviceObj_ = std::make_shared<SyncService>();
22     return serviceObj_;
23 }
24 
GetSyncId()25 uint64_t SyncService::GetSyncId()
26 {
27     uint64_t value = ++syncId_;
28     if (value == 0) {
29         value = ++syncId_;
30     }
31     return value;
32 }
33 
GetSyncContext(uint64_t syncId)34 std::shared_ptr<SyncContext> SyncService::GetSyncContext(uint64_t syncId)
35 {
36     std::lock_guard<std::mutex> lock(contextMutex_);
37     auto iter = syncContextMap_.find(syncId);
38     if (iter == syncContextMap_.end()) {
39         return nullptr;
40     }
41     return iter->second;
42 }
43 
AddSyncContext(uint64_t syncId,std::shared_ptr<SyncContext> syncContext)44 void SyncService::AddSyncContext(uint64_t syncId, std::shared_ptr<SyncContext> syncContext)
45 {
46     std::lock_guard<std::mutex> lock(contextMutex_);
47     syncContextMap_[syncId] = syncContext;
48 }
49 
RemoveSyncContext(uint64_t syncId)50 void SyncService::RemoveSyncContext(uint64_t syncId)
51 {
52     std::lock_guard<std::mutex> lock(contextMutex_);
53     auto iter = syncContextMap_.find(syncId);
54     if (iter != syncContextMap_.end()) {
55         syncContextMap_.erase(iter);
56     }
57 }
58 }
59