Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beyond Promise - stop & regain #3

Open
pengVc opened this issue Jul 25, 2019 · 0 comments
Open

Beyond Promise - stop & regain #3

pengVc opened this issue Jul 25, 2019 · 0 comments
Labels

Comments

@pengVc
Copy link
Owner

pengVc commented Jul 25, 2019

受到 Promise.stop 灵感启发, 既然能 "停止" 为何不来试试 "恢复", 以及 "恢复" 时可拿到停止的返回值.
作为技术实现上探讨, 在业务应用上, 可各自脑洞.

停止/恢复 方法:

  • Promise.stop()
  • Promise.regain()

停止示例:

  .then(() => Promise.stop())

恢复示例:

  // 用法较为特别, Promise.regain 包装上 onResolved 回调
  .then(Promise.regain((res) => { 
    // here promise is regained
  }));

如下为实现:

(() => {

  const then = Promise.prototype.then;
  const STOP_SIG = {};
  const REGAIN_SIG = "regain";

  Promise.stop = () => Object.create(STOP_SIG);

  Promise.regain = (onResolved) => {
    return function regain(res) {
      return "function" === typeof onResolved ? onResolved(res) : res;
    }
  };

  Promise.prototype.then = function (onResolved, onRejected) {

    return then.call(this, (val) => {

      const willResume = _isResumeSignal(onResolved);
      const shouldStop = _isStopValue(val);

      if (shouldStop && !willResume) {
        return val;
      }

      // retrive latest stop value
      if (willResume && _isStopValue(val)) {
        // prepare gc
        let newValue = val.stopedValue;
        val.stopedValue = null;
        val = newValue;
      }

      const nextValue = "function" === typeof onResolved ? onResolved(val) : val;
      // First Stop
      if (!shouldStop && _isStopValue(nextValue)) {
        // cache First stop value
        nextValue.stopedValue = val;
      }
      return nextValue;

    }, onRejected);

  };

  function _isStopValue(val) {
    return Object.getPrototypeOf(val || "") === STOP_SIG;
  }

  function _isResumeSignal(onResulved) {
    return "function" === typeof onResulved && onResulved.name === REGAIN_SIG;
  }

})();

完整示例:

var iPr = Promise

  .resolve("hello")

  .then((res) => {

    let val = "wow !";

    console.log(`then I receive [${ res }], and return [${ val }]`);

    return val;
  })

  // stop promise:
  .then((res) => Promise.stop())

  // never get call:
  .then((res) => console.log("then II: ", res))

;

// sometime later...
setTimeout(() => {
  iPr
    .then(Promise.regain((res) => {
      
      // give me stopped values
      console.log(`promise is recovered, \nand get stopped values: [${ res }]`);

    }));
}, 1e3);

further reading: #xieranmaya/blog#5

@pengVc pengVc added the draft label Jul 25, 2019
@pengVc pengVc changed the title Beyond Promise Beyond Promise - stop & regain Jul 29, 2019
@pengVc pengVc added idea and removed draft labels Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant