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 */ 15import distributedObject from '@ohos.data.distributedDataObject' 16import Logger from './Logger' 17import Note from '../model/Note' 18 19const TAG: string = 'DistributedObjectModel' 20 21export default class DistributedObjectModel { 22 public distributedObject?: any 23 public changeCallback?: () => void 24 public statusCallback?: (sessionId: string, networkId: string, status: 'online' | 'offline') => void 25 26 constructor() { 27 this.distributedObject = distributedObject.createDistributedObject({ 28 documents: [], 29 documentSize: 0 30 }) 31 } 32 33 genSessionId() { 34 return distributedObject.genSessionId() 35 } 36 37 setChangeCallback(changeCallback: () => void) { 38 if (this.changeCallback === changeCallback) { 39 Logger.info(TAG, 'same callback') 40 return 41 } 42 Logger.info(TAG, 'start off') 43 if (this.changeCallback !== undefined) { 44 this.distributedObject.off('change', this.changeCallback) 45 } 46 this.changeCallback = changeCallback 47 Logger.info(TAG, 'start watch change') 48 this.distributedObject.on('change', this.changeCallback) 49 } 50 51 setStatusCallback(callback: (sessionId: string, networkId: string, status: 'online' | 'offline') => void) { 52 if (this.statusCallback === callback) { 53 Logger.info(TAG, 'same callback') 54 return 55 } 56 Logger.info(TAG, 'start off') 57 if (this.statusCallback !== undefined) { 58 this.distributedObject.off('status', this.statusCallback) 59 } 60 this.statusCallback = callback 61 Logger.info(TAG, 'start watch change') 62 this.distributedObject.on('status', this.statusCallback) 63 } 64 65 update(index: number, title: string, content: string, mark: number) { 66 Logger.info(TAG, `doUpdate,${title},${index}`) 67 let documents = this.distributedObject.documents 68 documents[index] = { 69 title: title, content: content, mark: mark 70 } 71 this.distributedObject.documents = documents 72 Logger.info(TAG, `update my documents,${JSON.stringify(this.distributedObject.documents)}`) 73 } 74 75 add(title: string, content: string, mark: number) { 76 Logger.info(TAG, `doAdd,${title},${content}`) 77 Logger.info(TAG, `doAdd,${JSON.stringify(this.distributedObject.documents)}`) 78 this.distributedObject.documents = [...this.distributedObject.documents, 79 { 80 title: title, content: content, mark: mark 81 }] 82 this.distributedObject.documentSize++ 83 Logger.info(TAG, `add my documents,${JSON.stringify(this.distributedObject.documents)}`) 84 } 85 86 clear() { 87 Logger.info(TAG, 'doClear') 88 this.distributedObject.documents = [] 89 this.distributedObject.documentSize = 0 90 Logger.info(TAG, 'doClear finish') 91 } 92 93 off() { 94 this.distributedObject.off('change') 95 this.changeCallback = undefined 96 this.distributedObject.off('status') 97 this.statusCallback = undefined 98 } 99} 100