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 emitter from '@ohos.events.emitter' 17import { EmitterConst } from '../../common/EmitterConst' 18 19export class CustomEmitter { 20 shoppingCartGoodsList: { data: { id: number } }[] = [] 21 22 // 单次订阅回调 23 public showEventsDialog(callback) { 24 let dialogShowEventsId = { 25 eventId: EmitterConst.DIALOG_EVENT_ID 26 } 27 let dialogShowEventsData = { 28 data: { 29 id: EmitterConst.DIALOG_EVENT_ID 30 } 31 } 32 // 单次订阅事件--广告 33 emitter.once(dialogShowEventsId, callback) 34 // 事件发布 35 emitter.emit(dialogShowEventsId, dialogShowEventsData) 36 } 37 38 // 持久化订阅的事件回调 39 public setShoppingCartGoodsList(callback: (eventData) => void) { 40 let addGoodDataId = { 41 eventId: EmitterConst.ADD_EVENT_ID 42 } 43 // 以持久化方式订阅购物车添加事件并接收事件回调 44 emitter.off(EmitterConst.ADD_EVENT_ID) 45 emitter.on(addGoodDataId, (eventData) => { 46 callback(eventData) 47 }) 48 } 49 50 public addGoods(goodId) { 51 let addToShoppingCartId = { 52 eventId: EmitterConst.ADD_EVENT_ID 53 } 54 let shoppingCartData = { 55 data: { 56 id: goodId 57 } 58 } 59 // 持续订阅发布事件 60 emitter.emit(addToShoppingCartId, shoppingCartData) 61 } 62}