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 16import distributedObject from '@ohos.data.distributedDataObject' 17import Logger from '../data/Logger' 18import DataModel from '../data/DataModel' 19 20const TAG: string = 'DistributedDataModel' 21 22class DistributedDataModel { 23 public documents: Array<DataModel> = [] 24 public distributedObject?: any 25 public changeCallback?: () => void = null 26 public statusCallback?: () => void = null 27 28 constructor() { 29 this.distributedObject = distributedObject.createDistributedObject({ 30 documents: this.documents 31 }) 32 this.share() 33 } 34 35 share() { 36 Logger.info(TAG, 'share start') 37 this.distributedObject.setSessionId('123456789') 38 Logger.info(TAG, `share setSessionId`) 39 } 40 41 clearCallback() { 42 this.distributedObject.off('change') 43 this.changeCallback = undefined 44 this.distributedObject.off('status') 45 this.statusCallback = undefined 46 } 47 48 onChangeCallback(callback) { 49 if (this.changeCallback === callback) { 50 Logger.info(TAG, 'setCallback same callback') 51 return 52 } 53 if (this.changeCallback !== undefined) { 54 Logger.info(TAG, 'setCallback start off') 55 this.distributedObject.off('change', this.changeCallback) 56 } 57 this.changeCallback = callback 58 Logger.info(TAG, 'setCallback start watch change') 59 this.distributedObject.on('change', this.changeCallback) 60 } 61 62 onStatusCallback(callback) { 63 if (this.statusCallback === callback) { 64 Logger.info(TAG, 'setStatusCallback same callback') 65 return 66 } 67 if (this.statusCallback !== undefined) { 68 Logger.info(TAG, 'setStatusCallback start off') 69 this.distributedObject.off('status', this.statusCallback) 70 } 71 this.statusCallback = callback 72 Logger.info(TAG, 'setStatusCallback start watch change') 73 this.distributedObject.on('status', this.statusCallback) 74 } 75 76 add(deviceId: string, screenSize: string, content: string) { 77 Logger.info(TAG, `add start ${deviceId} ${screenSize} ${content} ${JSON.stringify(this.distributedObject.documents)}`) 78 this.documents = this.distributedObject.documents 79 let dataModel = new DataModel 80 dataModel.deviceId = deviceId 81 dataModel.screenSize = screenSize 82 dataModel.content = content 83 this.documents.push(dataModel) 84 this.distributedObject.documents = this.documents 85 Logger.info(TAG, `this.distributedObject.documents = ${JSON.stringify(this.distributedObject.documents)}`) 86 } 87} 88 89export let distributedDataModel = new DistributedDataModel() 90