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