{"id":318,"date":"2016-11-11T16:24:15","date_gmt":"2016-11-11T07:24:15","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/11\/11\/sequencial-response-with-rxjs"},"modified":"2016-11-11T16:24:15","modified_gmt":"2016-11-11T07:24:15","slug":"sequencial-response-with-rxjs","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/11\/11\/sequencial-response-with-rxjs\/","title":{"rendered":"RxJS\u3092\u4f7f\u3063\u3066\u975e\u540c\u671f\u306a\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u305f\u9806\u306b\u7d50\u5408\u3059\u308b\u8a71"},"content":{"rendered":"

RxJS<\/code>\u306f\u975e\u540c\u671f\u51e6\u7406\u3092\u9ad8\u5ea6\u306b\u62bd\u8c61\u5316\u3059\u308b\u306e\u3067\u3001\u7169\u96d1\u306a\u624b\u7d9a\u304d\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u66f8\u3051\u308b\u5834\u9762\u304c\u591a\u304f\u3001\u305d\u306e\u8fba\u306bRxJS<\/code>\u306e\u30e6\u30fc\u30b6\u304c\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306e\u3081\u308a\u8fbc\u3093\u3067\u3044\u304f\u8981\u56e0\u304c\u3042\u308b\u3088\u3046\u306b\u601d\u3044\u307e\u3059\u3002
\n\u4e00\u65b9\u3001\u5b9f\u5730\u3067\u4f7f\u3063\u3066\u3044\u306a\u3044\u3068\u3053\u3046\u3044\u3063\u305f\u30e1\u30ea\u30c3\u30c8\u306f\u3001\u8aac\u660e\u3055\u308c\u3066\u3082\u30d4\u30f3\u3068\u3053\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u304c\u3042\u308b\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u3002<\/p>\n

\u305d\u3053\u3067\u672c\u7a3f\u3067\u306f\u3001\u5b9f\u969b\u306b\u906d\u9047\u3057\u305f\u300eRxJS<\/code>\u3092\u4f7f\u3063\u3066\u3044\u305f\u304b\u3089\u697d\u3060\u3063\u305f\u300f\u30b3\u30fc\u30c9\u3092\u30d9\u30fc\u30b9\u306b\u3001RxJS<\/code>\u306e\u30e1\u30ea\u30c3\u30c8\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u6700\u8fd1\u5f0a\u793e\u3067\u306f\u3001\u3068\u3042\u308b\u691c\u7d22\u6a5f\u80fd\u3092\u6301\u3063\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u3063\u3066\u3044\u307e\u3057\u305f\u3002
\n(SUUMO<\/code>\u3084Home'S<\/code>\u306e\u7269\u4ef6\u691c\u7d22\u306e\u3088\u3046\u306a\u3082\u306e\u3092\u30a4\u30e1\u30fc\u30b8\u3057\u3066\u3082\u3089\u3046\u3068\u5b9f\u50cf\u306b\u8fd1\u3044\u3068\u601d\u3044\u307e\u3059)<\/p>\n

\u30e6\u30fc\u30b6\u304c\u4efb\u610f\u306b\u5165\u529b\u3057\u305f\u691c\u7d22\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u5fdc\u3058\u3066\u3001\u90fd\u5ea6API\u30b5\u30fc\u30d0\u3092\u53e9\u3044\u3066\u691c\u7d22\u7d50\u679c\u3092\u8868\u793a\u3059\u308b\u3088\u3046\u306a\u6a5f\u80fd\u3067\u3059\u3002
\n\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u975e\u540c\u671f\u306b\u767a\u884c\u3055\u308c\u308b\u691c\u7d22\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u3001\u8907\u6570\u53f0\u3042\u308bAPI\u30b5\u30fc\u30d0\u306e\u3044\u305a\u308c\u304b\u306b\u6295\u3052\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u3082\u3089\u3046\u3001\u3068\u3044\u3046\u69cb\u6210\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002
\n\u3053\u3053\u3067\u554f\u984c\u306b\u306a\u3063\u305f\u306e\u304c\u3001\u5404\u691c\u7d22\u30ea\u30af\u30a8\u30b9\u30c8\u3068\u5404\u30ec\u30b9\u30dd\u30f3\u30b9\u306f\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u30b5\u30fc\u30d0\u30de\u30b7\u30f3\u304b\u3089\u898b\u3066\u5404\u691c\u7d22\u30ea\u30af\u30a8\u30b9\u30c8\u540c\u58eb\u306f\u95a2\u9023\u6027\u3092\u6301\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u5fc5\u305a\u3057\u3082\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306b\u306a\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n

\u8aac\u660e\u304c\u5206\u304b\u308a\u3065\u3089\u3044\u6c17\u304c\u3059\u308b\u306e\u3067\u3001\u5177\u4f53\u4f8b\u3092\u4e0a\u3052\u3066\u307f\u307e\u3059<\/p>\n

    \n
  1. \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u691c\u7d22\u30ea\u30af\u30a8\u30b9\u30c8-A\u304c\u767a\u884c<\/li>\n
  2. API\u30b5\u30fc\u30d0-1\u3067\u51e6\u7406\u3092\u958b\u59cb<\/li>\n
  3. \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u691c\u7d22\u30ea\u30af\u30a8\u30b9\u30c8-B\u304c\u767a\u884c<\/li>\n
  4. API\u30b5\u30fc\u30d0-2\u3067\u51e6\u7406\u3092\u958b\u59cb<\/li>\n
  5. API\u30b5\u30fc\u30d0-2\u304b\u3089\u30ec\u30b9\u30dd\u30f3\u30b9-B'\u304c\u8fd4\u5374<\/li>\n
  6. API\u30b5\u30fc\u30d0-1\u304b\u3089\u30ec\u30b9\u30dd\u30f3\u30b9-A'\u304c\u8fd4\u5374<\/li>\n<\/ol>\n

    API\u30b5\u30fc\u30d0\u3067\u306e\u51e6\u7406\u6642\u9593\u3068\u30bf\u30a4\u30df\u30f3\u30b0\u5982\u4f55\u3067\u306f\u3001\u3053\u306e\u3088\u3046\u306b\u300e\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u305f\u9806\u306b\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u3063\u3066\u3053\u306a\u3044\u300f\u3088\u3046\u306a\u3053\u3068\u304c\u767a\u751f\u3057\u5f97\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002
    \n\uff08\u9006\u306b\u30ec\u30b9\u30dd\u30f3\u30b9-A'<\/code>->\u30ec\u30b9\u30dd\u30f3\u30b9-B'<\/code>\u306e\u9806\u756a\u3067\u8fd4\u3063\u3066\u304f\u308b\u3053\u3068\u3082\u3042\u308a\u5f97\u307e\u3059)<\/p>\n

    \u3053\u308c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u3001\u307e\u305aRxJS<\/code>\u3092\u4f7f\u308f\u305a\u306b\u66f8\u3044\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002
    \n\u4f8b\u3048\u3070\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30ad\u30fc\u306b\u3057\u305f\u30ad\u30e5\u30fc\u69cb\u9020(\u306e\u3088\u3046\u306a\u3082\u306e)\u3092\u7528\u610f\u3057\u3066\u3001\u5148\u51fa\u3057\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u6e80\u305f\u3055\u308c\u305f\u3089pop\u3059\u308b\u3088\u3046\u306a\u5b9f\u88c5\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n

    let queue = []; \/\/ \u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u30ad\u30fc\u306b\u63a2\u7d22\u3067\u304d\u308b\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u306a\u30ad\u30e5\u30fc\nconst PENDING = "PENDING";\n\nconst request = (parameter)=> {\n  const query = encode(parameter); \/\/ \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30af\u30a8\u30ea\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u95a2\u6570\u3060\u3068\u601d\u3063\u3066\u304f\u3060\u3055\u3044\n  queue.push({ [query]: PENDING });\n\n  return fetch(`\/endpoint?${query}`)\n    .then(res => res.json())\n    .then(response => {\n      \/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u308b\u6bce\u306b\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u30ad\u30fc\u306bqueue\u306b\u683c\u7d0d\u3059\u308b\n      queue.forEach(q => {\n        if (q[query] === PENDING) {\n          q[query] = response\n        }\n      })\n\n      \/\/ queue\u3092\u8210\u3081\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u6e80\u305f\u3055\u308c\u3066\u3044\u308b\u3082\u306e\u3060\u3051\u53d6\u308a\u51fa\u3059\n      const results = [];\n      for (let i = 0; i < queue.length; i++) {\n        const element = queue[i];\n        if (element === PENDING) {\n          break;\n        }\n        results.push(element);\n      }\n      results.forEach(() => queue.shift());\n      return results;\n    });\n};\n\nrequest({ parameterA: "foo" })\n  .then(console.log);<\/code><\/pre>\n

    \u4f55\u3092\u3084\u3063\u3066\u3044\u308b\u304b\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u3067\u3059\u306d...
    \n\u30b3\u30fc\u30c9\u304c\u60aa\u3044\u3060\u3051\u304b\u3082\u77e5\u308c\u306a\u3044\u3067\u3059\u304c\u3001\u30ad\u30e5\u30fc\u306e\u4e2d\u8eab\u3092\u90fd\u5ea6\u66f8\u304d\u63db\u3048\u3066\u3044\u305f\u308a\u3057\u3066\u3001\u4e0d\u5b89\u306e\u6b8b\u308b\u30b3\u30fc\u30c9\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002
    \n\u30ad\u30e5\u30fc\u304b\u3089\u53d6\u308a\u51fa\u3057\u305f\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306a\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u3001\u9577\u3055\u306e\u4e0d\u5b9a\u306a\u914d\u5217\u306b\u683c\u7d0d\u3057\u3066\u8fd4\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3068\u3057\u3066\u3082\u76f4\u611f\u7684\u3067\u306f\u306a\u3044\u6c17\u304c\u3057\u307e\u3059\u3002<\/p>\n

    \u3067\u306f\u6b21\u306b\u3001\u540c\u3058\u4ed5\u69d8\u3067RxJS<\/code>\u3092\u4f7f\u3046\u524d\u63d0\u3067\u66f8\u304d\u76f4\u3057\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

    const request_Rx = (parameter: { [key: string]: any }) => {\n  const query = encode(parameter);\n  const fetched = fetch(`\/endpoint?${query}`)\n    .then(res => res.json());\n  return Rx.Observable.fromPromise(fetched).first();\n};\n\nrequest_Rx({ parameterA: "foo" })\n  .concatAll() \/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306b\u7d50\u5408\u3059\u308b\u30aa\u30da\u30ec\u30fc\u30bf\n  .map(res => [res]) \/\/ \u6700\u521d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3068\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5408\u308f\u305b\u308b\u305f\u3081\u306b\u914d\u5217\u306b\u5165\u308c\u3066\u3044\u308b\n  .subscribe(console.log)<\/code><\/pre>\n

    \u3054\u89a7\u306e\u901a\u308a\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306b\u66f8\u3051\u307e\u3057\u305f\u3002
    \n\u6700\u521d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u82e6\u52b4\u3057\u305f\u3001\u975e\u540c\u671f\u306b\u767a\u751f\u3059\u308b\u30a4\u30d9\u30f3\u30c8\u3092\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306b\u7d50\u5408\u3059\u308b<\/code>\u90e8\u5206\u3092RxJS<\/code>\u304c\u62bd\u8c61\u5316\u3057\u3066\u304f\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u307b\u3068\u3093\u3069\u30b3\u30fc\u30c9\u3092\u66f8\u304b\u306a\u3044\u3067\u6e08\u3093\u3067\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

    \u4ee5\u4e0a\u3001\u5b9f\u969b\u306b\u958b\u767a\u4e2d\u306b\u906d\u9047\u3057\u305f\u5834\u9762\u3092\u5143\u306b\u3001RxJS<\/code>\u306e\u4fbf\u5229\u3055\u3092\u7d39\u4ecb\u3057\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":"

    RxJS\u306f\u975e\u540c\u671f\u51e6\u7406\u3092\u9ad8\u5ea6\u306b\u62bd\u8c61\u5316\u3059\u308b\u306e\u3067\u3001\u7169\u96d1\u306a\u624b\u7d9a\u304d\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u66f8\u3051\u308b\u5834\u9762\u304c\u591a\u304f\u3001\u305d\u306e\u8fba\u306bRxJS\u306e\u30e6\u30fc\u30b6\u304c\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306e\u3081\u308a\u8fbc\u3093\u3067\u3044\u304f\u8981\u56e0\u304c\u3042\u308b\u3088\u3046\u306b\u601d\u3044\u307e\u3059\u3002 \u4e00\u65b9\u3001\u5b9f\u5730\u3067\u4f7f\u3063\u3066\u3044\u306a\u3044\u3068\u3053\u3046\u3044\u3063\u305f\u30e1\u30ea\u30c3\u30c8\u306f\u3001\u8aac\u660e\u3055\u308c\u3066\u3082\u30d4\u30f3\u3068\u3053\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u304c\u3042\u308b\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u3002 \u305d\u3053\u3067\u672c\u7a3f\u3067\u306f\u3001\u5b9f\u969b\u306b\u906d\u9047\u3057\u305f\u300eRxJS\u3092\u4f7f\u3063\u3066\u3044\u305f\u304b\u3089\u697d\u3060\u3063\u305f\u300f\u30b3\u30fc\u30c9\u3092\u30d9\u30fc\u30b9\u306b\u3001RxJS\u306e\u30e1\u30ea\u30c3\u30c8\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e […]<\/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\/318"}],"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=318"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/318\/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=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}