1 /* 2 * Copyright (c) 2022 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 "sync_observer.h" 17 18 namespace OHOS::DistributedKv { SyncObserver(const std::vector<std::shared_ptr<KvStoreSyncCallback>> & callbacks)19SyncObserver::SyncObserver(const std::vector<std::shared_ptr<KvStoreSyncCallback>> &callbacks) 20 :callbacks_(callbacks) 21 {}; 22 SyncObserver()23SyncObserver::SyncObserver() 24 { 25 std::lock_guard<decltype(mutex_)> lock(mutex_); 26 callbacks_.clear(); 27 } 28 Add(const std::shared_ptr<KvStoreSyncCallback> callback)29bool SyncObserver::Add(const std::shared_ptr<KvStoreSyncCallback> callback) 30 { 31 std::lock_guard<decltype(mutex_)> lock(mutex_); 32 callbacks_.push_back(callback); 33 return true; 34 } 35 Clean()36bool SyncObserver::Clean() 37 { 38 std::lock_guard<decltype(mutex_)> lock(mutex_); 39 callbacks_.clear(); 40 return true; 41 } 42 SyncCompleted(const std::map<std::string,DistributedKv::Status> & results)43void SyncObserver::SyncCompleted(const std::map<std::string, DistributedKv::Status> &results) 44 { 45 std::lock_guard<decltype(mutex_)> lock(mutex_); 46 for (auto &callback : callbacks_) { 47 callback->SyncCompleted(results); 48 } 49 } 50 } // namespace OHOS::DistributedKv 51