{"id":2573,"date":"2019-05-23T14:42:54","date_gmt":"2019-05-23T12:42:54","guid":{"rendered":"http:\/\/www.openmediavault.org\/?p=2573"},"modified":"2021-01-08T11:11:29","modified_gmt":"2021-01-08T09:11:29","slug":"concatjoin-rxjs-operator","status":"publish","type":"post","link":"https:\/\/www.openmediavault.org\/?p=2573","title":{"rendered":"concatJoin RxJS operator"},"content":{"rendered":"\n<p>Are you searching for a RxJS concat operator that works like forkJoin? Then have a look at the following simple code that will do that for you.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { concat, EMPTY, Observable } from 'rxjs';\n\n\/**\n * Joins last values emitted by passed Observables.\n *\n * `concatJoin` is an operator that takes any number of Observables which\n * can be passed either as an array or directly as arguments. If no input\n * Observables are provided, resulting stream will complete immediately.\n *\n * `concatJoin` will wait for all passed Observables to complete one by\n * one and then it will emit an array with last values from corresponding\n * Observables. So if you pass `n` Observables to the operator, resulting\n * array will have `n` values, where first value is the last thing emitted\n * by the first Observable, second value is the last thing emitted by the\n * second Observable and so on. That means `concatJoin` will not emit more\n * than once and it will complete after that.\n *\n * @param {...Observable} sources Any number of Observables provided either\n * as an array or as an arguments passed directly to the operator.\n * @return {Observable} Observable emitting either an array of last values\n * emitted by passed Observables.\n *\/\nexport function concatJoin&lt;T>(...sources: Array&lt;Observable&lt;T> | Observable&lt;T>&#91;]>): Observable&lt;T&#91;]> {\n  \/\/ If the first and only other argument is an array assume it's been\n  \/\/ called with `concatJoin(&#91;obs1, obs2, obs3])`\n  if (sources.length === 1 &amp;&amp; Array.isArray(sources&#91;0])) {\n    sources = sources&#91;0] as Array&lt;Observable&lt;T>>;\n  }\n\n  if (sources.length === 0) {\n    return EMPTY;\n  }\n\n  return new Observable((subscriber) => {\n    const values: Array&lt;T> = &#91;];\n    concat(...sources).subscribe(\n      (value: T) => {\n        values.push(value);\n      },\n      (err) => {\n        subscriber.error(err);\n      },\n      () => {\n        subscriber.next(values);\n        subscriber.complete();\n      }\n    );\n  });\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you searching for a RxJS concat operator that works like forkJoin? Then have a look at the following simple code that will do that for you.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-2573","post","type-post","status-publish","format-standard","hentry","category-coding"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=\/wp\/v2\/posts\/2573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2573"}],"version-history":[{"count":3,"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=\/wp\/v2\/posts\/2573\/revisions"}],"predecessor-version":[{"id":2876,"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=\/wp\/v2\/posts\/2573\/revisions\/2876"}],"wp:attachment":[{"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.openmediavault.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}