zhouzhongyuan / qa

Questions recods
MIT License
5 stars 1 forks source link

What is Flux? #81

Open zhouzhongyuan opened 6 years ago

zhouzhongyuan commented 6 years ago
zhouzhongyuan commented 6 years ago

Contents

zhouzhongyuan commented 6 years ago

What is store?

There is one sentence in constructor of FluxStore

this._dispatchToken = dispatcher.register((payload) => {
      this.__invokeOnDispatch(payload);
    });

invoke:

  __invokeOnDispatch(payload: Object): void {
    this.__changed = false;
    this.__onDispatch(payload);
    if (this.__changed) {
      this.__emitter.emit(this.__changeEvent);
    }
  }

invoke:

this.setState(
          (prevState, currentProps) => getState(
            prevState,
            currentProps,
            context,
          ),
        );

* this setState was added by this._fluxContainerSubscriptions.addListener in FluxContainer

invoke:

Component update
zhouzhongyuan commented 6 years ago

...

zhouzhongyuan commented 6 years ago

What dose Container.create do?

Container

Container.create

zhouzhongyuan commented 6 years ago

What is Dispatcher?

根据这个An async version of Facebook's flux dispatcher, 推断,要求是同步函数。

实例method

一言以蔽之:更好的输出错误信息

A way to provide descriptive errors in development but generic errors in production.

Dispatcher的原理

register

  register(callback) {
    var id = _prefix + _lastID++;
    this._callbacks[id] = callback;
    return id;
  }

dispatch

  /**
   * Dispatches a payload to all registered callbacks.
   *
   * @param {object} payload
   */
  dispatch(payload) {
    invariant(
      !this._isDispatching,
      'Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch.'
    );
    this._startDispatching(payload);
    try {
      for (var id in this._callbacks) {
        if (this._isPending[id]) {
          continue;
        }
        this._invokeCallback(id);
      }
    } finally {
      this._stopDispatching();
    }
  }

_startDispatching

  /**
   * Set up bookkeeping needed when dispatching.
   *
   * @param {object} payload
   * @internal
   */
  _startDispatching(payload) {
    for (var id in this._callbacks) {
      this._isPending[id] = false;
      this._isHandled[id] = false;
    }
    this._pendingPayload = payload;
    this._isDispatching = true;
  }

_invokeCallback

  /**
   * Call the calback stored with the given id. Also do some internal
   * bookkeeping.
   *
   * @param {string} id
   * @internal
   */
  _invokeCallback(id) {
    this._isPending[id] = true;
    this._callbacks[id](this._pendingPayload);
    this._isHandled[id] = true;
  }

waitFor的原理

  /**
   * Waits for the callbacks specified to be invoked before continuing execution
   * of the current callback. This method should only be used by a callback in
   * response to a dispatched payload.
   *
   * @param {array<string>} ids
   */
  waitFor(ids) {
    invariant(
      this._isDispatching,
      'Dispatcher.waitFor(...): Must be invoked while dispatching.'
    );
    for (var ii = 0; ii < ids.length; ii++) {
      var id = ids[ii];
      if (this._isPending[id]) {
        invariant(
          this._isHandled[id],
          'Dispatcher.waitFor(...): Circular dependency detected while ' +
          'waiting for `%s`.',
          id
        );
        continue;
      }
      invariant(
        this._callbacks[id],
        'Dispatcher.waitFor(...): `%s` does not map to a registered callback.',
        id
      );
      this._invokeCallback(id);
    }
  }