私はreduxのソースコードを読んでいます。createStoreの観測可能な関数を理解する方法
誰でも私がここのcreateStoreの機能を理解するのを助けることができますか?
これは、オブザーバパターンの別のバージョンを実装したようです。
質問:
コードがここに表示された理由は、この機能と呼ばれる任意のコードを、私は見つかりませんでした。目的は何ですか?我々が取得する
var store$= store[$$observable]()
ようなものが必要
:どのように偽のコード
function observable() {
var _ref;
var outerSubscribe = subscribe;
return _ref = {
/**
* The minimal observable subscription method.
* @param {Object} observer Any object that can be used as an observer.
* The observer object should have a `next` method.
* @returns {subscription} An object with an `unsubscribe` method that can
* be used to unsubscribe the observable from the store, and prevent further
* emission of values from the observable.
*/
subscribe: function subscribe(observer) {
if (typeof observer !== 'object') {
throw new TypeError('Expected the observer to be an object.');
}
function observeState() {
if (observer.next) {
observer.next(getState());
}
}
observeState();
var unsubscribe = outerSubscribe(observeState);
return { unsubscribe: unsubscribe };
}
}, _ref[$$observable] = function() {
return this;
}, _ref;
}
// When a store is created, an "INIT" action is dispatched so that every
// reducer returns their initial state. This effectively populates
// the initial state tree.
dispatch({ type: ActionTypes.INIT });
return _ref2 = {
dispatch: dispatch,
subscribe: subscribe,
getState: getState,
replaceReducer: replaceReducer
}, _ref2[$$observable] = observable, _ref2;
}
(キーがSymbole($$観測可能)によって生成された)ストアによって、この関数を呼び出す 2観察可能な と状態が変更されたときに通知を受けたい人は、
store $ .subscribe(observer);
しかし、ランタイム値を参照するには、ブレークポイントを作成していない
var x = window.Symbol.for('observable'); var y = store[x]; var z = y(); console.log(x); console.log(store[x]); console.log("y", y); console.log("z", z);
:私はいくつかのテストの後に[$$観測可能]