非常に単純なサイクルjsサンプルアプリケーションで、ブラウザ履歴をonionify状態ストア内の状態と同期させようとしています。私は状態ストアから歴史へのマッピングに問題はなく、問題はhistory.stateを状態ストアに戻すことです。基本的に、私は、互いに流れ込む状態ストリームの無限ループに巻き込まれ、ブラウザがクラッシュします。サイクルJs - 履歴と状態を同期させる方法
import { run } from "@cycle/run";
import { div, button, p, makeDOMDriver } from "@cycle/dom";
import onionify from "cycle-onionify";
import { makeHistoryDriver } from "@cycle/history";
import xs from "xstream";
const main = (sources) => {
const popHistory$ = sources.history; // This is the problem...
const action$ = xs.merge(
sources.DOM.select(".decrement").events("click").map(() => -1),
sources.DOM.select(".increment").events("click").map(() => +1),
);
const state$ = sources.onion.state$;
const vdom$ = state$.map(state =>
div([
button(".decrement", "Decrement"),
button(".increment", "Increment"),
p(`Count: ${state.count}`)
])
);
const initReducer$ = xs.of(function initReducer() {
return {count: 0};
});
const updateReducer$ = action$.map(x => function reducer(prevState) {
return {count: prevState.count + x};
});
// this is where the inifinity loop starts
const historyReducer$ = popHistory$.map(history => function(prevState) {
return {count: history.state.count};
});
// You can't merge historyReducer$ here
const reducer$ = xs.merge(initReducer$, updateReducer$, historyReducer$);
const pushHistory$ = state$.map(state => ({
type: "push",
pathname: `/?count=${state.count}`,
state: state
}));
return {
DOM: vdom$,
onion: reducer$,
history: pushHistory$,
debug: popHistory$
}
};
const onionMain = onionify(main);
run(onionMain, {
DOM: makeDOMDriver("#main"),
history: makeHistoryDriver(),
debug: $ => $.subscribe({next: console.log})
});
私は疑問に思っています。これは簡単な方法ですか?ここで役立つ演算子はありますか?私がやろうとしていることは基本的に不可能だと思う。有用なリソースへの回答やリンクは高く評価されます。
のエキストラでいくつかの興味深いの演算子があります。これはストリームやCycle.jsではなく、ループ内でお互いを変えるだけのものです。だから私はより明確にする必要があります:あなたは何をフィーチャーとして達成しようとしていますか? –
あなたは正しいです。それは単なる無限ループの問題でした。私は、歴史源を州と州に接続して歴史のシンクに戻す方法について混乱しました。 – MFave
ヒストリシンクにヒストリソースを接続しないことでこの問題を解決し、すべてが期待通りに機能します。私の目標は、私が見つけることができないので、非常に単純なルーティングの例を作ることでした。私は自分の答えを投稿します。初めてクライアントルーティングを学習する人には便利です。 – MFave