{"id":7011,"date":"2019-05-10T21:25:51","date_gmt":"2019-05-10T21:25:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7011"},"modified":"2019-05-11T21:55:06","modified_gmt":"2019-05-11T21:55:06","slug":"reactive-extensions-rx-in-javascript-rxjs","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/reactive-extensions-rx-in-javascript-rxjs\/","title":{"rendered":"Reactive Extensions (Rx) in JavaScript (rxjs)"},"content":{"rendered":"<p>Reactive Extensions are everywhere &#8211; I wanted to try the JavaScript version of the library, so below is a sample React component demonstrating a &#8220;fake&#8221; service call that might occur within the <em>fetch<\/em> function. The setTimeout is used simply to simulate some latency in the call.<\/p>\n<p>To add rxjs simple use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn add rxjs\r\n<\/pre>\n<p>Now here&#8217;s the sample component code&#8230;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport {Observable, Subscriber, Subscription} from 'rxjs';\r\n\r\ninterface ServiceState {\r\n  data: string;\r\n}\r\n\r\nclass ServiceComponent extends Component&lt;{}, ServiceState&gt; {\r\n\r\n  _subscription!: Subscription;\r\n\r\n  constructor(prop: any) {\r\n    super(prop);\r\n    this.state = {\r\n      data : &quot;Busy&quot;\r\n    };\r\n  }\r\n\r\n  fetch() : Observable&lt;string&gt; {\r\n    return Observable.create((o: Observer&lt;string&gt;) =&gt; {\r\n      setTimeout(() =&gt; {\r\n        o.next('Some data');\r\n        o.complete();\r\n      }, 3000);\r\n    });\r\n  }\r\n\r\n  componentDidMount() {\r\n    this._subscription = this.fetch().subscribe(s =&gt; {\r\n      this.setState({data: s})\r\n    });\r\n  }\r\n\r\n  componentWillUnmount() {\r\n    if(this._subscription) {\r\n      this._subscription.unsubscribe();\r\n    }\r\n  }  \r\n\r\n  render() {\r\n    return (\r\n      &lt;div&gt;{this.state.data}&lt;\/div&gt;\r\n    );\r\n  }\r\n}\r\n<\/pre>\n<p>In this example we have the <em>fetch<\/em> function returns an instance of an Observable of type string. We then subscribe to this within the <em>componentDidMount<\/em> function, which is called when the component is inserted into the DOM and then we subscribe to the Observable, updates will be applied to the component&#8217;s state. <\/p>\n<p>The <em>componentWillUnmount<\/em> is called when the component is removed from the DOM and hence we unsubscribe from the Observable. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reactive Extensions are everywhere &#8211; I wanted to try the JavaScript version of the library, so below is a sample React component demonstrating a &#8220;fake&#8221; service call that might occur within the fetch function. The setTimeout is used simply to simulate some latency in the call. To add rxjs simple use yarn add rxjs Now [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45],"tags":[],"class_list":["post-7011","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7011"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7011\/revisions"}],"predecessor-version":[{"id":7047,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7011\/revisions\/7047"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}