5 lines
14 KiB
JSON
5 lines
14 KiB
JSON
|
{
|
||
|
"id": "guide/rx-library",
|
||
|
"title": "The RxJS library",
|
||
|
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/rx-library.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"the-rxjs-library\">The RxJS library<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/rx-library#the-rxjs-library\"><i class=\"material-icons\">link</i></a></h1>\n<p>Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change (<a href=\"https://en.wikipedia.org/wiki/Reactive_programming\">Wikipedia</a>). RxJS (Reactive Extensions for JavaScript) is a library for reactive programming using observables that makes it easier to compose asynchronous or callback-based code. See (<a href=\"https://rxjs.dev/guide/overview\">RxJS Docs</a>).</p>\n<p>RxJS provides an implementation of the <code>Observable</code> type, which is needed until the type becomes part of the language and until browsers support it. The library also provides utility functions for creating and working with observables. These utility functions can be used for:</p>\n<ul>\n<li>Converting existing code for async operations into observables</li>\n<li>Iterating through the values in a stream</li>\n<li>Mapping values to different types</li>\n<li>Filtering streams</li>\n<li>Composing multiple streams</li>\n</ul>\n<h2 id=\"observable-creation-functions\">Observable creation functions<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/rx-library#observable-creation-functions\"><i class=\"material-icons\">link</i></a></h2>\n<p>RxJS offers a number of functions that can be used to create new observables. These functions can simplify the process of creating observables from things such as events, timers, promises, and so on. For example:</p>\n<code-example path=\"rx-library/src/simple-creation.1.ts\" region=\"promise\" header=\"Create an observable from a promise\">\nimport { from } from 'rxjs';\n\n// Create an Observable out of a promise\nconst data = from(fetch('/api/endpoint'));\n// Subscribe to begin listening for async result\ndata.subscribe({\n next(response) { console.log(response); },\n error(err) { console.error('Error: ' + err); },\n complete() { console.log('Completed'); }\n});\n\n\n</code-example>\n<code-example path=\"rx-library/src/simple-creation.2.ts\" region=\"interval\" header=\"Create an observable from a counter\">\nimport { interval } from 'rxjs';\n\n// Create an Observable that will publish a value on an interval\nconst secondsCounter = interval(1000);\n// Subscribe to begin publishing values\nconst subscription = secondsCounter.subscribe(n =>\n console.log(`It's been ${n + 1} seconds since subscribing!`));\n\n\n</code-example>\n<code-example path=\"rx-library/src/simple-creation.3.ts\" region=\"event\" header=\"Create an observable from an event\">\nimport { fromEvent } from 'rxjs';\n\nconst el = document.getElementById('my-element');\n\n// Create an Observable that will publish mouse movements\nconst mouseMoves = fromEvent(el, 'mousemove');\n\n// Subscribe to start listening for mouse-move events\nconst subscription = mouseMoves.subscribe((evt: MouseEvent) => {\n // Log coords of mouse movements\n console.log(`Coords: ${evt.clientX} X ${evt.clientY}`);\n\n // When the mouse is over the upper-left of the screen,\n // unsubscribe to stop listening for mouse movements\n if (evt.clientX < 40 && evt.clientY < 40) {\n subscription.unsubscribe();\n }\n});\n\n\n</code-example>\n<code-example path=\"rx-library/src/simple-creation.ts\" region=\"ajax\" header=\"Create an observable that creates an AJAX request\">\n import { ajax } from 'rxjs/ajax';\n\n// Create an Observable that will create an AJAX request\n const apiData = ajax('/api/data');\n // Subscribe to create the request\n apiData.subscribe(res => console.log(res.
|
||
|
}
|