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