{"id":301,"date":"2016-09-14T15:41:20","date_gmt":"2016-09-14T06:41:20","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/09\/14\/implementation-pattern-with-reactive-programming-library-tail"},"modified":"2016-09-14T15:41:20","modified_gmt":"2016-09-14T06:41:20","slug":"implementation-pattern-with-reactive-programming-library-tail","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/09\/14\/implementation-pattern-with-reactive-programming-library-tail\/","title":{"rendered":"RxJS\u3092\u7528\u3044\u305f\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u306e\u5b9f\u4f8b\u307e\u3068\u3081(\u5f8c\u7de8)"},"content":{"rendered":"

\u5c0f\u98fc\u3067\u3059\u3002
\n\u524d\u56de<\/a>\u306b\u5f15\u304d\u7d9a\u304d\u3001RxJS\u306e\u30b3\u30fc\u30c9\u5b9f\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n

\u30c9\u30e9\u30c3\u30b0\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u4f5c\u6210<\/h2>\n

onmousedown<\/code>\u30fbonmouseup<\/code>\u30fbonmousemove<\/code>\u30a4\u30d9\u30f3\u30c8\u304b\u3089\u4f5c\u3063\u305f\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u3088\u308a\u5408\u308f\u305b\u3066\u3001 \u300e\u30de\u30a6\u30b9\u30c9\u30e9\u30c3\u30b0\u300f\u3068\u3044\u3046\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
\n\u3053\u3046\u3044\u3063\u305f\u300e\u65e2\u5b58\u306eDOM\u30a4\u30d9\u30f3\u30c8\u3092\u6df7\u305c\u5408\u308f\u305b\u3066\u65b0\u3057\u3044\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u308b\u300f\u3088\u3046\u306a\u3053\u3068\u306f\u3001Rx\u306e\u6700\u3082\u5f97\u610f\u3068\u3059\u308b\u9818\u57df\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

const { merge, combineLatest } = Observable;\n\nconst ACTION_ON_MOUSE_DOWN = "ACTION_ON_MOUSE_DOWN";\nconst ACTION_ON_MOUSE_UP = "ACTION_ON_MOUSE_UP";\nconst ACTION_ON_MOUSE_MOVE = "ACTION_ON_MOUSE_MOVE";\n\ninterface Point {\n  x: number;\n  y: number;\n}\n\ninterface RootState {\n  point: Point;\n}\n\nconst subject = new Subject<Action<any>>();\n\nconst onMouseDown = (event: React.MouseEvent<HTMLDivElement>): void => subject.next({\n  type: ACTION_ON_MOUSE_DOWN,\n});\n\nconst onMouseUp = (event: React.MouseEvent<HTMLDivElement>): void => subject.next({\n  type: ACTION_ON_MOUSE_UP,\n});\n\nconst onMouseMove = (event: React.MouseEvent<HTMLDivElement>): void => {\n  const nativeEvent = event.nativeEvent as MouseEvent;\n  subject.next({\n    type: ACTION_ON_MOUSE_MOVE,\n    payload: {\n      x: nativeEvent.offsetX,\n      y: nativeEvent.offsetY,\n    },\n  });\n};\n\nconst root$ = (): Observable<RootState> => {\n  const onMouseDown$ = subject.ofType<void>(ACTION_ON_MOUSE_DOWN);\n  const onMouseUp$ = subject.ofType<void>(ACTION_ON_MOUSE_UP);\n  const onMouseMove$ = subject.ofType<Point>(ACTION_ON_MOUSE_MOVE);\n\n  const isDragging$ = merge(onMouseDown$, onMouseUp$)\n    .scan<boolean>(isDragging => !isDragging, false);\n\n  const drag$ = combineLatest(\n      isDragging$, onMouseMove$,\n      (isDragging, nextPoint) => ({ isDragging, nextPoint })\n    )\n    .filter(({ isDragging }) => isDragging)\n    .map(({ nextPoint }) => nextPoint);\n\n  const initialState = { point: { x: 0, y: 0 } };\n\n  return drag$\n    .map(point => ({ point }))\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 { point } = this.state;\n    return (\n      <div>\n        <div\n          onMouseDown={ onMouseDown }\n          onMouseMove={ onMouseMove }\n          onMouseUp={ onMouseUp }\n        >\u30c9\u30e9\u30c3\u30b0\u3067\u304d\u308b\u8981\u7d20<\/div>\n        <span>X: { point.x }<\/span>\n        <span>Y: { point.y }<\/span>\n      <\/div>\n    );\n  }\n}<\/code><\/pre>\n

\u540c\u671f\u7684\u306a\u30a4\u30d9\u30f3\u30c8\u306e\u30d5\u30a3\u30eb\u30bf<\/h2>\n

\u8907\u6570\u306e\u30b9\u30c8\u30ea\u30fc\u30e0\u3092combineLatest<\/code>\u306a\u3069\u3067\u5408\u6210\u3057\u305f\u6642\u3001\u540c\u671f\u7684\u306b(\u540c\u3058\u30d5\u30ec\u30fc\u30e0\u306b)\u30a4\u30d9\u30f3\u30c8\u304c\u767a\u884c\u3055\u308c\u3066\u304f\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308a\u307e\u3059\u3002
\n\u4f8b\u3048\u3070\u3053\u306e\u3088\u3046\u306a\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u5834\u5408--(abc)--(def)--|<\/code>\u3001c<\/code>\u3068f<\/code>\u3092\u89b3\u6e2c\u3067\u304d\u308c\u3070\u5145\u5206\u306a\u3053\u3068\u3082\u591a\u3044\u3067\u3059\u3002<\/p>\n

\u7279\u306b\u30de\u30fc\u30d6\u30eb\u8a18\u6cd5\u3067\u30c6\u30b9\u30c8\u3092\u66f8\u3044\u3066\u3044\u308b\u3068\u3001\u540c\u671f\u7684\u306a\u30a4\u30d9\u30f3\u30c8\u5168\u3066\u3092\u30c6\u30b9\u30c8\u3057\u3066\u3044\u308b\u3068\u30c6\u30b9\u30c8\u304c\u5197\u9577\u306b\u306a\u3063\u3066\u53ef\u8aad\u6027\u304c\u4e0b\u304c\u3063\u3066\u304d\u307e\u3059\u306e\u3067\u3001\u3067\u304d\u308c\u3070\u5fc5\u8981\u306a\u30a4\u30d9\u30f3\u30c8\u3060\u3051\u3092\u767a\u884c\u3059\u308b\u3088\u3046\u306b\u30d5\u30a3\u30eb\u30bf\u3067\u304d\u308b\u3068\u3046\u308c\u3057\u3044\u3067\u3059\u3002
\n\u305d\u3093\u306a\u6642\u306f\u3001auditTime\u30aa\u30da\u30ec\u30fc\u30bf\u306b\u6975\u5c0f\u306e\u6642\u9593\u3092\u6e21\u3057\u3066\u3042\u3052\u308b\u3053\u3068\u3067\u3001\u540c\u671f\u7684\u306a\u30a4\u30d9\u30f3\u30c8\u3060\u3051\u3092\u30d5\u30a3\u30eb\u30bf\u3057\u305f\u30b9\u30c8\u30ea\u30fc\u30e0\u304c\u4f5c\u308c\u307e\u3059\u3002<\/p>\n

const { of, merge } = Observable;\n\nconst foo$ = of("foo");\nconst bar$ = of("bar");\n\nconst combined$ = merge(foo$, bar$)\n  .auditTime(5);<\/code><\/pre>\n

\u3053\u306e\u6642\u3001\u30c6\u30b9\u30c8\u306e\u305f\u3081\u306b\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u3092\u6ce8\u5165\u3059\u308b\u305f\u3081\u306b<\/p>\n

import { TestCheduler } from "rxjs"\nconst createCombined$ = (shceduler: TestScheduler = null) => merge(foo$, bar$)\n  .auditTime(5, shceduler);<\/code><\/pre>\n

\u306e\u3088\u3046\u306b\u3001null<\/code>\u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306b\u6e21\u3057\u3066\u3044\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u898b\u304b\u3051\u308b\u6642\u304c\u3042\u308a\u307e\u3059\u3002(4.x<\/code>\u3092\u4f7f\u3063\u305f\u6642\u306e\u30b3\u30fc\u30c9)<\/p>\n

Reactive Programming with RxJS<\/a>\u306e\u30c6\u30b9\u30c8\u306e\u7ae0\u306b\u3082\u540c\u69d8\u306e\u8a18\u8ff0\u304c\u8f09\u3063\u3066\u3057\u307e\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u672c\u6765\u300e\u30c7\u30d5\u30a9\u30eb\u30c8\u5f15\u6570\u304c\u4e0e\u3048\u3089\u308c\u3066\u3044\u306a\u3044\u300f\u3053\u3068\u3092\u8868\u73fe\u3059\u308b\u305f\u3081\u306b\u306fnull\u3067\u306a\u304fundefined\u3092\u4f7f\u3046\u3053\u3068<\/a>\u304c\u671f\u5f85\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n

TypeScript\u306b\u79fb\u884c\u3057\u305f(\u3053\u3068\u3067\u30c7\u30d5\u30a9\u30eb\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u6271\u3044\u304cES\u6a19\u6e96\u306b\u6e96\u62e0\u3057\u305f)RxJS 5.x\u3067\u306f\u3001\u3053\u306e\u3088\u3046\u306a\u5834\u5408undefined<\/code>\u3092\u6e21\u3055\u306a\u3044\u3068\u4f8b\u5916\u3092\u6295\u3052\u3089\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u306e\u3067\u5c11\u3057\u3060\u3051\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n

import { TestCheduler } from "rxjs"\nconst createCombined$ = (shceduler: TestScheduler = undefined) => merge(foo$, bar$)\n  .auditTime(5, shceduler);<\/code><\/pre>\n

\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u914d\u5217\u3092\u914d\u5217\u306e\u30b9\u30c8\u30ea\u30fc\u30e0\u306b\u5909\u63db\u3059\u308b<\/h2>\n

\u3061\u3087\u3063\u3068\u3084\u3084\u3053\u3057\u3044\u3067\u3059\u304c\u3001Observable<SomeType>[]<\/code>\u3092Observable<SomeType[]><\/code>\u306b\u3059\u308b\u3088\u3046\u306a\u64cd\u4f5c\u306e\u3053\u3068\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u3002
\n\u4f8b\u3048\u3070\u30e9\u30b8\u30aa\u30dc\u30bf\u30f3\u306e\u3088\u3046\u306a\u3001\u300e\u8907\u6570\u306e\u30bd\u30fc\u30b9\u30b9\u30c8\u30ea\u30fc\u30e0\u304c\u3042\u308b\u3051\u3069\u51fa\u529b\u3057\u305f\u3044\u30a4\u30d9\u30f3\u30c8\u306f\u4e00\u3064\u300f\u307f\u305f\u3044\u306a\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u308b\u6642\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n

const input1$ = of(1);\nconst input2$ = of(2);\nconst input3$ = of(3);\nconst input4$ = of(4);\nconst input5$ = of(5);\n\nconst input$List: Observable<number>[] = [input1$, input2$, input3$, input4$, input5$];\nconst inputs$ = combineLatest(...input$List);\n\n\/\/ input$List\u306b\u683c\u7d0d\u305b\u305a\u306b\n\/\/ const inputs$ = combineLatest(input1$, input2$, input3$, input4$, input5$);\n\/\/ \u3068\u3082\u66f8\u3051\u308b<\/code><\/pre>\n

\u307e\u3068\u3081<\/h2>\n

\u4ee5\u4e0a\u3001RxJS\u3092\u72b6\u614b\u7ba1\u7406\u306b\u7528\u3044\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u5b9f\u969b\u306b\u906d\u9047\u3057\u305f\u30b9\u30c8\u30ea\u30fc\u30e0\u64cd\u4f5c\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002
\n\u53c2\u8003\u306b\u306a\u308c\u3070\u3046\u308c\u3057\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5c0f\u98fc\u3067\u3059\u3002 \u524d\u56de\u306b\u5f15\u304d\u7d9a\u304d\u3001RxJS\u306e\u30b3\u30fc\u30c9\u5b9f\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002 \u30c9\u30e9\u30c3\u30b0\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u4f5c\u6210 onmousedown\u30fbonmouseup\u30fbonmousemove\u30a4\u30d9\u30f3\u30c8\u304b\u3089\u4f5c\u3063\u305f\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u3088\u308a\u5408\u308f\u305b\u3066\u3001 \u300e\u30de\u30a6\u30b9\u30c9\u30e9\u30c3\u30b0\u300f\u3068\u3044\u3046\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 \u3053\u3046\u3044\u3063\u305f\u300e\u65e2\u5b58\u306eDOM\u30a4\u30d9\u30f3\u30c8\u3092\u6df7\u305c\u5408\u308f\u305b\u3066\u65b0\u3057\u3044\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u308b\u300f\u3088\u3046\u306a\u3053\u3068\u306f\u3001Rx\u306e\u6700\u3082\u5f97\u610f\u3068\u3059\u308b\u9818\u57df\u3060\u3068\u601d\u3044\u307e\u3059\u3002 const { merg […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[77,174],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/301"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}