|
1 | 1 | import { OperatorDoc } from '../operator.model'; |
2 | 2 |
|
3 | 3 | export const from: OperatorDoc = { |
4 | | - 'name': 'from', |
5 | | - 'operatorType': 'creation' |
| 4 | + name: 'from', |
| 5 | + operatorType: 'creation', |
| 6 | + signature: `from(ish: ArrayLike | ObservableInput, scheduler: Scheduler): Observable`, |
| 7 | + parameters: [ |
| 8 | + { |
| 9 | + name: 'ish', |
| 10 | + type: 'ArrayLike | ObservableInput', |
| 11 | + attribute: '', |
| 12 | + description: `A subscribable object, a Promise, an Observable-like, an Array, an |
| 13 | + iterable or an array-like object to be converted.` |
| 14 | + }, |
| 15 | + { |
| 16 | + name: 'scheduler', |
| 17 | + type: 'Scheduler', |
| 18 | + attribute: 'optional', |
| 19 | + description: `The scheduler on which to schedule the emissions of values.` |
| 20 | + } |
| 21 | + ], |
| 22 | + marbleUrl: 'http://reactivex.io/rxjs/img/from.png', |
| 23 | + shortDescription: { |
| 24 | + description: `Creates an Observable from an Array, an array-like object, a Promise, an |
| 25 | + iterable object, or an Observable-like object. |
| 26 | + <span class="informal">Converts almost anything to an Observable.</span>` |
| 27 | + }, |
| 28 | + walkthrough: { |
| 29 | + description: `Converts various other objects and data types into Observables. <span class="markdown-code">from</span> |
| 30 | + converts a Promise or an array-like or an |
| 31 | + <a href ='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable' target='_blank'>iterable</a> |
| 32 | + object into an Observable that emits the items in that promise or array or |
| 33 | + iterable. A String, in this context, is treated as an array of characters. |
| 34 | + Observable-like objects (contains a function named with the ES2015 Symbol |
| 35 | + for Observable) can also be converted through this operator. |
| 36 | + ` |
| 37 | + }, |
| 38 | + examples: [ |
| 39 | + { |
| 40 | + name: 'Converts an array to an Observable', |
| 41 | + code: ` |
| 42 | + const array = [10, 20, 30]; |
| 43 | + const result = Rx.Observable.from(array); |
| 44 | + result.subscribe(x => console.log(x)); |
| 45 | +
|
| 46 | + // Results in the following: |
| 47 | + // 10 20 30`, |
| 48 | + externalLink: { |
| 49 | + platform: 'JSBin', |
| 50 | + url: 'http://jsbin.com/qodocay/embed?js,console' |
| 51 | + } |
| 52 | + }, |
| 53 | + { |
| 54 | + name: 'Convert an infinite iterable (from a generator) to an Observable', |
| 55 | + code: ` |
| 56 | + function* generateDoubles(seed) { |
| 57 | + let i = seed; |
| 58 | + while (true) { |
| 59 | + yield i; |
| 60 | + i = 2 * i; // double it |
| 61 | + } |
| 62 | + } |
| 63 | + const iterator = generateDoubles(3); |
| 64 | + const result = Rx.Observable.from(iterator).take(10); |
| 65 | + result.subscribe(x => console.log(x)); |
| 66 | + // Results in the following: |
| 67 | + // 3 6 12 24 48 96 192 384 768 1536`, |
| 68 | + externalLink: { |
| 69 | + platform: 'JSBin', |
| 70 | + url: 'http://jsbin.com/kidevan/embed?js,console' |
| 71 | + } |
| 72 | + }, |
| 73 | + { |
| 74 | + name: |
| 75 | + 'Using <span class="markdown-code">from</span> with async scheduler', |
| 76 | + code: ` |
| 77 | + console.log('start'); |
| 78 | + const array = [10, 20, 30]; |
| 79 | + const result = Rx.Observable.from(array, Rx.Scheduler.async); |
| 80 | + result.subscribe(x => console.log(x)); |
| 81 | + console.log('end'); |
| 82 | + // Results in the following: |
| 83 | + // start end 10 20 30`, |
| 84 | + externalLink: { |
| 85 | + platform: 'JSBin', |
| 86 | + url: 'http://jsbin.com/xunesam/embed?js,console' |
| 87 | + } |
| 88 | + } |
| 89 | + ], |
| 90 | + relatedOperators: ['create', 'fromEvent', 'fromEventPattern', 'fromPromise'] |
6 | 91 | }; |
0 commit comments