iT邦幫忙

2021 iThome 鐵人賽

DAY 11
0
Modern Web

telegram開發筆記系列 第 17

[17] 建立統一放資料的地方

建立一個放資料的地方,統一讀取和寫入這樣比較方便

dataCenter.js

const GetInstanceClass = require("./component/GetInstanceClass");
/**
 * @description 資料都存在這邊
 *
 * @class DataCenter
 */
class DataCenter extends GetInstanceClass {
  constructor() {
    super();

    this.state = {};
  }

  /**
   * @description 獲取對應資料
   *
   * @param {string} key
   * @return {any} state data
   * @memberof DataCenter
   */
  getData(key) {
    switch (key) {
      case "example":
        return this.state[key];
      default:
        break;
    }
  }

  /**
   * @description 設定 state 資料
   *
   * @param {string} key
   * @param {any} data state data
   * @memberof DataCenter
   */
  setData(key, data) {
    switch (key) {
      case "example":
        this.state[key] = data;
        break;
      default:
        break;
    }
  }

  /**
   * @description 訂閱資料變更後要執行的程式,先預留到時候再寫
   *
   * @param {string} key state data
   * @param {function} callBack
   * @memberof DataCenter
   */
  subscription(key, callBack) {

  }
}

let dataCenter = DataCenter.getInstance();

module.exports = dataCenter;

獲取資料、設定資料和訂閱資料變更的資料中心

day17


上一篇
[16] 建立登入 telegram 功能
下一篇
[18] 登入後將用戶資料紀錄
系列文
telegram開發筆記18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言