|
1 | 1 | import { OperatorDoc } from '../operator.model';
|
2 | 2 |
|
3 | 3 | export const bufferToggle: OperatorDoc = {
|
4 |
| - 'name': 'bufferToggle', |
5 |
| - 'operatorType': 'transformation' |
| 4 | + name: 'bufferToggle', |
| 5 | + operatorType: 'transformation', |
| 6 | + signature: `bufferToggle( |
| 7 | + openings: SubscribableOrPromise, |
| 8 | + closingSelector: (value) => SubscribableOrPromise): Observable`, |
| 9 | + parameters: [ |
| 10 | + { |
| 11 | + name: 'openings', |
| 12 | + type: 'SubscribableOrPromise', |
| 13 | + attribute: '', |
| 14 | + description: `A Subscribable or Promise of notifications to start new buffers.` |
| 15 | + }, |
| 16 | + { |
| 17 | + name: 'closingSelector', |
| 18 | + type: '(value) => SubscribableOrPromise', |
| 19 | + attribute: '', |
| 20 | + description: `A function that takes the value emitted by the openings observable |
| 21 | + and returns a Subscribable or Promise, which, when it emits, signals that the associated buffer should be emitted and cleared.` |
| 22 | + } |
| 23 | + ], |
| 24 | + marbleUrl: 'http://reactivex.io/rxjs/img/bufferToggle.png', |
| 25 | + shortDescription: { |
| 26 | + description: ` |
| 27 | + Buffers the source Observable values starting from an emission from <span class="markdown-code">openings</span> |
| 28 | + and ending when the output of <span class="markdown-code">closingSelector</span> emits. |
| 29 | + <span class="informal"> |
| 30 | + Collects values from the past as an array. Starts collecting only when <span class="markdown-code">openings</span> emits, |
| 31 | + and calls the <span class="markdown-code">closingSelector</span> function |
| 32 | + to get an Observable that tells when to close the buffer.</span>` |
| 33 | + }, |
| 34 | + walkthrough: { |
| 35 | + description: ` |
| 36 | + Buffers values from the source by opening the buffer via signals from an Observable |
| 37 | + provided to <span class="markdown-code">openings</span>, |
| 38 | + and closing and sending the buffers when a Subscribable or Promise |
| 39 | + returned by the <span class="markdown-code">closingSelector</span> function emits. |
| 40 | + ` |
| 41 | + }, |
| 42 | + examples: [ |
| 43 | + { |
| 44 | + name: 'Every other second, emit the click events from the next 500ms', |
| 45 | + code: ` |
| 46 | + import { fromEvent } from 'rxjs/observable/fromEvent'; |
| 47 | + import { interval } from 'rxjs/observable/interval'; |
| 48 | + import { empty } from 'rxjs/observable/empty'; |
| 49 | + import { bufferToggle } from 'rxjs/operators'; |
| 50 | +
|
| 51 | + const clicks = fromEvent(document, 'click', e => ({x: e.clientX, y: e.clientY})); |
| 52 | + const openings = interval(1000); |
| 53 | + const buffered = clicks.pipe( |
| 54 | + bufferToggle(openings, i => i % 2 ? interval(500) : empty()) |
| 55 | + ); |
| 56 | + buffered.subscribe(x => console.log(x)); |
| 57 | + /* |
| 58 | + Expected console output: |
| 59 | +
|
| 60 | + [] |
| 61 | +
|
| 62 | + [[object Object] { |
| 63 | + x: 156, |
| 64 | + y: 165 |
| 65 | + }, [object Object] { |
| 66 | + x: 156, |
| 67 | + y: 165 |
| 68 | + }, [object Object] { |
| 69 | + x: 156, |
| 70 | + y: 165 |
| 71 | + }] |
| 72 | +
|
| 73 | + [] |
| 74 | +
|
| 75 | + [] |
| 76 | + */ |
| 77 | + `, |
| 78 | + externalLink: { |
| 79 | + platform: 'JSBin', |
| 80 | + url: 'http://jsbin.com/nuriyod/6/embed?js,console,output' |
| 81 | + } |
| 82 | + }, |
| 83 | + { |
| 84 | + name: |
| 85 | + 'Start buffering all the click events when you press the "S" key and close the buffer when you press the "E" key', |
| 86 | + code: ` |
| 87 | + import { fromEvent } from 'rxjs/observable/fromEvent'; |
| 88 | + import { filter, bufferToggle } from 'rxjs/operators'; |
| 89 | +
|
| 90 | + const clicks = fromEvent(document, 'click', e => ({x: e.clientX, y: e.clientY})); |
| 91 | + const keyUp = fromEvent(document,'keyup'); |
| 92 | + const openings = keyUp.pipe(filter(e => e.key === 's')); |
| 93 | + const closing = keyUp.pipe(filter(e => e.key === 'e')); |
| 94 | + const buffered = clicks.pipe( |
| 95 | + bufferToggle(openings, _ => closing) |
| 96 | + ); |
| 97 | + buffered.subscribe(x => console.log(x)); |
| 98 | +
|
| 99 | + /* |
| 100 | + Expected console output: |
| 101 | +
|
| 102 | + [[object Object] { |
| 103 | + x: 147, |
| 104 | + y: 135 |
| 105 | + }, [object Object] { |
| 106 | + x: 147, |
| 107 | + y: 135 |
| 108 | + }, [object Object] { |
| 109 | + x: 144, |
| 110 | + y: 135 |
| 111 | + }, [object Object] { |
| 112 | + x: 144, |
| 113 | + y: 135 |
| 114 | + }, [object Object] { |
| 115 | + x: 144, |
| 116 | + y: 135 |
| 117 | + }] |
| 118 | +
|
| 119 | + [[object Object] { |
| 120 | + x: 144, |
| 121 | + y: 135 |
| 122 | + }, [object Object] { |
| 123 | + x: 144, |
| 124 | + y: 135 |
| 125 | + }] |
| 126 | +
|
| 127 | + [[object Object] { |
| 128 | + x: 143, |
| 129 | + y: 136 |
| 130 | + }] |
| 131 | + */ |
| 132 | +`, |
| 133 | + externalLink: { |
| 134 | + platform: 'JSBin', |
| 135 | + url: 'http://jsbin.com/vurobel/12/embed?js,console,output' |
| 136 | + } |
| 137 | + } |
| 138 | + ], |
| 139 | + relatedOperators: [ |
| 140 | + 'buffer', |
| 141 | + 'bufferCount', |
| 142 | + 'bufferTime', |
| 143 | + 'bufferWhen', |
| 144 | + 'windowToggle' |
| 145 | + ] |
6 | 146 | };
|
0 commit comments