{"id":298,"date":"2016-09-08T11:24:47","date_gmt":"2016-09-08T02:24:47","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/09\/08\/implementation-pattern-with-reactive-programming-library-head"},"modified":"2016-09-08T11:24:47","modified_gmt":"2016-09-08T02:24:47","slug":"implementation-pattern-with-reactive-programming-library-head","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/09\/08\/implementation-pattern-with-reactive-programming-library-head\/","title":{"rendered":"RxJS\u3092\u7528\u3044\u305f\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u306e\u5b9f\u4f8b\u307e\u3068\u3081(\u524d\u7de8)"},"content":{"rendered":"
\u5c0f\u98fc\u3067\u3059\u3002 \u307e\u305a\u57fa\u672c\u3068\u306a\u308b\u3001\u30e6\u30fc\u30b6\u306eUI\u64cd\u4f5c\u304b\u3089\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002 \u3053\u306e\u624b\u6cd5\u3067\u306f\u3001\u30b9\u30c8\u30ea\u30fc\u30e0\u306b\u30a4\u30d9\u30f3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u8d77\u70b9\u3068\u306a\u308bDOM\u8981\u7d20\u3054\u3068\u306bSubject\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002 \u524d\u9805\u3067\u898b\u305f\u3088\u3046\u306b\u3001\u30a4\u30d9\u30f3\u30c8\u3092\u767a\u884c\u3059\u308bDOM\u8981\u7d20\u3054\u3068\u306bSubject\u3092\u751f\u6210\u3059\u308b\u306e\u306f\u3084\u3084\u5197\u9577\u306a\u304d\u3089\u3044\u304c\u3042\u308a\u307e\u3059\u3002 \u3053\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u5c0e\u5165\u3059\u308b\u3068\u3059\u308b\u3068\u3001\u524d\u9805\u306e\u65b9\u6cd5\u3068\u306f\u300e\u30a4\u30d9\u30f3\u30c8\u6bce\u306b\u5225\u3005\u306eSubject\u3092\u7528\u3044\u308b\u306e\u304b\uff1f\u300f\u300e\u5358\u4e00\u306eSubject\u3092\u8377\u7269\u306e\u7a2e\u985e\u306b\u3088\u3063\u3066\u5206\u5c90\u3059\u308b\u306e\u304b\u300f\u304c\u9055\u3044\u307e\u3059\u304c\u3001\u300e\u30e6\u30fc\u30b6\u304c\u4f55\u3092\u3057\u305f\u304b\u300f\u300e\u30e6\u30fc\u30b6\u304c\u3057\u305f\u3053\u3068\u306b\u3088\u3063\u3066\u751f\u3058\u308b\u30c7\u30fc\u30bf\u3092\u89b3\u6e2c\u3067\u304d\u308b\u304b\u300f\u306f\u6e80\u305f\u3055\u308c\u305f\u307e\u307e\u3067\u3059\u306e\u3067\u3001\u5927\u304d\u304f\u69cb\u9020\u3092\u5909\u3048\u305a\u306b\u524d\u9805\u3068\u540c\u69d8\u306e\u5b9f\u88c5\u3092\u4f5c\u6210\u3067\u304d\u305d\u3046\u3067\u3059\u3002<\/p>\n
\n\u4eca\u590f\u5f0a\u793e\u3067\u306f\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b5\u30a4\u30c9\u3067\u306e\u72b6\u614b\u9077\u79fb\u30fb\u4fdd\u6301\u306bReactiveProgramming<\/code>\u306e\u8003\u3048\u65b9\u3092\u7528\u3044\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5236\u4f5c\u3057\u3066\u3044\u307e\u3057\u305f\u3002
\n\u305d\u306e\u904e\u7a0b\u3067\u898b\u3064\u3051\u305f\u3001\u3044\u304f\u3064\u304b\u306e\u5b9f\u969b\u7684\u306a\u30b9\u30c8\u30ea\u30fc\u30e0\u4f5c\u308a\u306e\u30d1\u30bf\u30fc\u30f3\u3092\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002
\nReactiveProgramming<\/code>\u306e(\u8003\u3048\u65b9\u3092\u542b\u3081\u305f)\u5165\u9580\u8a18\u4e8b\u3084\u3001\u30d1\u30e9\u30c0\u30a4\u30e0\u306e\u9aa8\u5b50\u3092\u8a73\u7d30\u306b\u89e3\u8aac\u3057\u305f\u8a18\u4e8b\u306f\u6ca2\u5c71\u3042\u308b\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u3001
\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u3069\u3046\u4f7f\u3046\u306e\u304b?<\/code>\u3042\u308b\u3044\u306f
\u3069\u3046\u4f7f\u3063\u305f\u306e\u304b?<\/code>\u3068\u3044\u3046\u8fba\u308a\u306e\u60c5\u5831\u306f\u307e\u3060\u5c11\u306a\u3044\u3088\u3046\u306b\u3082\u601d\u3046\u306e\u3067\u3001\u305d\u306e\u8fba\u308a\u306e\u53c2\u8003\u306b\u306a\u308c\u3070\u3046\u308c\u3057\u3044\u3067\u3059\u3002<\/p>\n
DOM\u8981\u7d20\u304b\u3089\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n
\n\u3053\u308c\u306f\u5165\u9580\u7cfb\u306e\u8a18\u4e8b\u3067\u3082\u6271\u308f\u308c\u3066\u3044\u308b\u4e8b\u304c\u591a\u3044\u3067\u3059\u304c\u3001\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u751f\u6210\u6642\u306bSubject\u3092\u4f5c\u6210\u3057\u3066\u3001\u76f4\u63a5DOM\u8981\u7d20\u306b\u6e21\u3059\u624b\u6cd5\u3092\u5c0e\u5165\u3068\u3057\u3066\u66f8\u3044\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\nimport * as React from "react";\nimport { Observable, Subject } from "rxjs";\n\nconst { Component } = React;\n\ninterface RootState {\n greeting: string;\n onClick: (event: MouseEvent) => void;\n}\n\n\/\/ \u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u751f\u6210\u3059\u308b\u6642\u306b\u3001Subject\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u3066\u3001\n\/\/ \u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc(onClick\u95a2\u6570)\u3054\u3068State\u306b\u6d41\u3057\u3066\u3057\u307e\u3046\n\/\/ Subject\u306f\u751f\u6210\u3057\u305f\u3082\u306e\u3092\u6ce8\u5165\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3068\u3001\u30c6\u30b9\u30c8\u3057\u3084\u3059\u3044\nconst root$ = (subject = new Subject<string>()): Observable<RootState> => {\n const onClick = (event: React.MouseEvent<HTMLDivElement>): void => subject.next("Hello, world");\n const greeting$ = subject.map(payload => `${payload}!!!`);\n const initialState = {\n greeting: "",\n onClick,\n };\n\n return subject\n .map(payload => payload + "!!!")\n .map(greeting => ({ greeting, onClick }))\n .startWith(initialState)\n ;\n};\n\n\nexport class SampleApp extends Component<void, RootState> {\n componentWillMount() {\n root$().subscribe(root => this.setState(root));\n }\n\n render() {\n const { greeting, onClick } = this.state;\n return (\n <div onClick={ onClick }>{ greeting }<\/div>\n );\n }\n}<\/code><\/pre>\n
\nDOM\u8981\u7d20\u3054\u3068\u306b\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u6210\u3059\u308b\u306e\u3067\u69cb\u9020\u3068\u3057\u3066\u306f\u308f\u304b\u308a\u3084\u3059\u3044\u3067\u3059\u304c\u3001\u8d77\u70b9\u3068\u306a\u308bDOM\u8981\u7d20\u3054\u3068(=\u6bce\u30e6\u30fc\u30b6\u304c\u30af\u30ea\u30c3\u30af\u3067\u304d\u308b\u8981\u7d20\u3068\u540c\u3058\u3060\u3051)Subject\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001\u3084\u3084\u5197\u9577\u306a\u6c17\u3082\u3057\u3066\u304d\u307e\u3059\u3002
\n\u307e\u305f\u3001\u30eb\u30fc\u30c8\u3068\u306a\u308b\u72b6\u614b\u306e\u30b9\u30c8\u30ea\u30fc\u30e0Observable<RootState><\/code>\u306e\u4e2d\u306b\u3001DOM\u306b\u675f\u7e1b\u3055\u308c\u308b\u95a2\u6570(
RootState.onClick<\/code>)\u3082\u540c\u68b1\u3057\u3066\u6d41\u3057\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u5b9f\u88c5\u304c\u5927\u304d\u304f\u306a\u3063\u3066\u3044\u304f\u306b\u5f93\u3063\u3066\u4f59\u8a08\u306a\u8377\u7269\u304c\u4e00\u7dd2\u306b\u6d41\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3082\u601d\u3048\u3066\u304d\u307e\u3059\u3002
\n\u305d\u3053\u3067\u3001\u6b21\u9805\u3067\u306f\u5358\u4e00\u306eSubject\u3092\u4f7f\u3044\u56de\u3059\u624b\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\u5358\u4e00\u306eSubject\u3092\u4f7f\u3044\u56de\u3059<\/h2>\n
\n\u4f8b\u3048\u3070Redux<\/code>\u3067\u306f\u3001\u5358\u4e00\u306e
dispatch<\/code>\u95a2\u6570\u3092\u5168\u3066\u306eDOM\u8981\u7d20\u306b\u7d10\u4ed8\u3051\u3066\u3001dispatch\u95a2\u6570\u306e\u5f15\u6570\u306b\u300e\u3069\u3046\u3044\u3063\u305f\u64cd\u4f5c\u304c\u884c\u308f\u308c\u305f\u306e\u304b\u300f\u3068\u3044\u3046\u60c5\u5831(=ActionType)\u3092\u8f09\u305b\u308b\u3053\u3068\u3067\u3001\u64cd\u4f5c\u3055\u308c\u305f\u8981\u7d20\u3092\u5224\u5225\u3059\u308b\u3001\u3068\u3044\u3046\u30a2\u30d7\u30ed\u30fc\u30c1\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
import * as React from "react";\nimport { Observable, Subject } from "rxjs";\n\nconst { Component } = React;\nconst ACTION_ON_CLICK = "ACTION_ON_CLICK";\n\ninterface RootState {\n greeting: string;\n}\n\ninterface Action<T> {\n type: string;\n payload: T;\n}\n\nconst subject = new Subject<Action<any>>();\n\nconst onClick = (event: React.MouseEvent<HTMLDivElement>): void => subject.next({\n type: ACTION_ON_CLICK,\n payload: "Hello, world",\n});\n\nconst root$ = (): Observable<RootState> => {\n const initialState = { greeting: "" };\n\n return subject\n .filter(({ type }) => type === ACTION_ON_CLICK)\n .map(({ payload }) => `${payload}!!!`)\n .map(greeting => ({ greeting }))\n .startWith(initialState)\n ;\n};\n\n\nexport class SampleApp extends Component<void, RootState> {\n componentWillMount() {\n root$().subscribe(root => this.setState(root));\n }\n\n render() {\n const { greeting } = this.state;\n return (\n <div onClick={ onClick }>{ greeting }<\/div>\n );\n }\n}<\/code><\/pre>\n
subject<\/code>\u304c
dispatcher<\/code>\u3092\u4ee3\u66ff\u3059\u308b\u3088\u3046\u306a\u30a2\u30d7\u30ed\u30fc\u30c1\u3067\u3059\u3002
\nRxJS<\/code>\u3092\u306f\u3058\u3081\u3068\u3059\u308bRP\u7cfb\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30e9\u30c3\u30d7\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea(\u4f8b\u3048\u3070
redux-observable<\/code>)\u3067\u306f\u3001\u3053\u308c\u306b\u8fd1\u3044\u30a2\u30d7\u30ed\u30fc\u30c1\u304c\u53d6\u3089\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n