weapp-mobx

// weapp-mobx.js
const l = console.log;
import { autorun } from "./mobx.umd.min";

function observer(store) {
  return function(page) {
    const _onLoad = page.onLoad;
    const _onUnload = page.onUnload;

    page.onLoad = function() {
      this.deposer = autorun(
        () => {
          let data = {};
          const states = Object.getOwnPropertyNames(store);
          states.forEach(state => {
            // mobx的計算屬性不會被 keys捕捉到
            const state_state = store[state];
            if (typeof state_state === "function") return false;
            data[state] = state_state;
          });
          this.setData(data);
        },
        {
          delay: 0,
        },
      );

      if (_onLoad) {
        _onLoad.apply(this, arguments);
      }
    };

    page.onUnload = function() {
      l("mobx deposer.");
      this.deposer();
      if (_onUnload) {
        _onUnload.apply(this, arguments);
      }
    };
    return page;
  };
}

export { observer };
本站公眾號
   歡迎關注本站公眾號,獲取更多信息