Skip to content
typescript
// oxlint-disable no-unused-vars
import React from 'react'

type STATE = React.$any
const UUID = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c: React.$any) {
    const r: React.$any = Math.random() * 16 | 0
    let v: React.$any = ''
    v = c === 'x' ? r : (r && 0x3 | 0x8);
    return v.toString(16);
  });
}

class AntSuperset_Cache {
  //全局 data
  state: STATE;
  //被绑定的方法
  fun: STATE;
  //开启监听
  listen: STATE;
  AntList: STATE;

//初始化私有数据
  constructor() {
    this.state = {};
    this.fun = [];
    this.listen = {};
    this.listen['AntSuperset_Cache_React'] = `${UUID()}`;

  }

  //外部调用初始化 state
  initState(res: STATE) {
    if (JSON.stringify(res)[0] === '{') {
      this.state = {...res};
      this.listen.AntSuperset_Cache_React = `${UUID()}`
    } else {
      alert('请以对象形式初始化数据')
      console.warn('请以对象形式初始化数据')
    }

  }

  //外部调用 初始化 监听方法
  ReactX(fun: (AfterUpdate: STATE, BeforeUpdating?: STATE) => void, Uid: string, keyStr?: string) {
    if (!this.AntList) {
      this.AntList = []
    }
    this.AntList.push(Uid)
    if (this.fun.indexOf(fun) === -1) {
      this.fun.push({
        fun: fun,
        id: Uid,
        keyStr: keyStr,
        keyObj: keyStr ? this.state[keyStr] : '',
      });

    }
    Object.defineProperty(this.listen, 'AntSuperset_Cache_React', {
      set: () => {
        try {
          this.fun.forEach((item: {
            fun: (AfterUpdate: STATE, BeforeUpdating?: STATE) => void,
            keyStr: string,
            keyObj: string
          }) => {
            if (item.keyStr) {
              // eslint-disable-next-line eqeqeq
              if (this.state[item.keyStr] != item.keyObj) {
                item.keyObj = this.state[item.keyStr];
                item.fun(this.state[item.keyStr], item.keyObj);
              }
            }
            if (item.keyStr === 'true') {
              item.fun(this.state);
            }
          });

        } catch (e) {
          console.log('报错');
        }
      },
    });
  }

  UnloadWindow() {
    window.addEventListener('beforeunload', () => {
      if (typeof (this.AntList) === 'object') {
        this.AntList.forEach((item: string) => {
          console.log('我已经卸载了UUID为:', item, '的值')
          this.Unload(item)
        })
      }
    });
  }

  //外部调用 初始化 删除监听方法
  Unload(AntSuperset_Cache_Id: string) {
    let max = 0;
    try {
      this.fun.forEach((item: { id: string }, index: number) => {
        if (item.id === AntSuperset_Cache_Id) {
          max = index;
          throw new Error('');
        }
      });
    } catch (e) {
      if (max !== -1) {
        this.fun.splice(max, 1);
      }
      console.warn('');
    }
  }

  //外部调用 启用新增state值
  setObj(key: string, value: STATE) {
    this.state[key] = value;
    this.listen.AntSuperset_Cache_React = `${UUID()}`

  }

  //外部调用查询 getObj
  getObj(key: string) {
    return {
      res: this.state,
      value: this.state[key],
      valueFun: () => {
        return this.state
      }
    }
  }
}

const Pineapple = new AntSuperset_Cache();
export default Pineapple;

不知道说啥了很无语了