|
1 | 1 | import { OperatorDoc } from '../operator.model';
|
2 | 2 |
|
3 | 3 | export const bufferWhen: OperatorDoc = {
|
4 |
| - 'name': 'bufferWhen', |
5 |
| - 'operatorType': 'transformation' |
| 4 | + name: 'bufferWhen', |
| 5 | + operatorType: 'transformation', |
| 6 | + signature: `bufferWhen(closingSelector: () => Observable): Observable`, |
| 7 | + parameters: [ |
| 8 | + { |
| 9 | + name: 'closingSelector', |
| 10 | + type: '() => Observable', |
| 11 | + attribute: '', |
| 12 | + description: `A function that takes no arguments and returns an Observable that signals buffer closure.` |
| 13 | + } |
| 14 | + ], |
| 15 | + marbleUrl: 'http://reactivex.io/rxjs/img/bufferWhen.png', |
| 16 | + shortDescription: { |
| 17 | + description: `Buffers the source Observable values, using a factory function of closing Observables |
| 18 | + to determine when to close, emit, and reset the buffer. |
| 19 | + <span class="informal">Collects values from the past as an array. |
| 20 | + When it starts collecting values, it calls a function that returns an Observable that tells |
| 21 | + when to close the buffer and restart collecting.</span>` |
| 22 | + }, |
| 23 | + walkthrough: { |
| 24 | + description: ` |
| 25 | + Opens a buffer immediately, then closes the buffer when the observable returned by calling |
| 26 | + <span class="markdown-code">closingSelector</span> function emits a value. |
| 27 | + When it closes the buffer, it immediately opens a new buffer and repeats the process.` |
| 28 | + }, |
| 29 | + examples: [ |
| 30 | + { |
| 31 | + name: 'Emit an array of the last clicks every [1-5] random seconds', |
| 32 | + code: ` |
| 33 | + import { fromEvent } from 'rxjs/observable/fromEvent'; |
| 34 | + import { interval } from 'rxjs/observable/interval'; |
| 35 | + import { bufferWhen } from 'rxjs/operators'; |
| 36 | +
|
| 37 | + const clicks = fromEvent(document, 'click', e => ({x: e.clientX, y: e.clientY})); |
| 38 | + const buffered = clicks.pipe( |
| 39 | + bufferWhen(() => interval(1000 + Math.random() * 4000)) |
| 40 | + ) |
| 41 | + buffered.subscribe(x => console.log(x)); |
| 42 | +
|
| 43 | + /* |
| 44 | + Example console output: |
| 45 | +
|
| 46 | + [] |
| 47 | +
|
| 48 | + [] |
| 49 | +
|
| 50 | + [[object Object] { |
| 51 | + x: 87, |
| 52 | + y: 222 |
| 53 | + }, [object Object] { |
| 54 | + x: 87, |
| 55 | + y: 222 |
| 56 | + }, [object Object] { |
| 57 | + x: 100, |
| 58 | + y: 228 |
| 59 | + }, [object Object] { |
| 60 | + x: 151, |
| 61 | + y: 296 |
| 62 | + }] |
| 63 | +
|
| 64 | + [[object Object] { |
| 65 | + x: 130, |
| 66 | + y: 368 |
| 67 | + }, [object Object] { |
| 68 | + x: 132, |
| 69 | + y: 368 |
| 70 | + }, [object Object] { |
| 71 | + x: 227, |
| 72 | + y: 212 |
| 73 | + }, [object Object] { |
| 74 | + x: 189, |
| 75 | + y: 321 |
| 76 | + }, [object Object] { |
| 77 | + x: 160, |
| 78 | + y: 411 |
| 79 | + }, [object Object] { |
| 80 | + x: 160, |
| 81 | + y: 411 |
| 82 | + }, [object Object] { |
| 83 | + x: 155, |
| 84 | + y: 366 |
| 85 | + }] |
| 86 | +
|
| 87 | + [] |
| 88 | + */ |
| 89 | + `, |
| 90 | + externalLink: { |
| 91 | + platform: 'JSBin', |
| 92 | + url: 'http://jsbin.com/jemeron/9/embed?js,console,output' |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + name: 'Buffer all the click events until you press the Enter key', |
| 97 | + code: ` |
| 98 | + import { fromEvent } from 'rxjs/observable/fromEvent'; |
| 99 | + import { filter, bufferWhen } from 'rxjs/operators'; |
| 100 | +
|
| 101 | + const enterKeys = fromEvent(document,'keyup') |
| 102 | + .pipe(filter(e => e.key === "Enter")); |
| 103 | + const clicks = fromEvent(document, 'click', e => ({x: e.clientX, y: e.clientY})); |
| 104 | + const buffered = clicks.pipe( |
| 105 | + bufferWhen(() => enterKeys) |
| 106 | + ); |
| 107 | + buffered.subscribe(x => console.log(x)); |
| 108 | +
|
| 109 | + /* |
| 110 | + Example console output: |
| 111 | +
|
| 112 | + [[object Object] { |
| 113 | + x: 186, |
| 114 | + y: 136 |
| 115 | + }, [object Object] { |
| 116 | + x: 188, |
| 117 | + y: 136 |
| 118 | + }, [object Object] { |
| 119 | + x: 189, |
| 120 | + y: 136 |
| 121 | + }, [object Object] { |
| 122 | + x: 189, |
| 123 | + y: 136 |
| 124 | + }, [object Object] { |
| 125 | + x: 192, |
| 126 | + y: 136 |
| 127 | + }] |
| 128 | +
|
| 129 | + [[object Object] { |
| 130 | + x: 196, |
| 131 | + y: 135 |
| 132 | + }, [object Object] { |
| 133 | + x: 196, |
| 134 | + y: 135 |
| 135 | + }] |
| 136 | +
|
| 137 | + [[object Object] { |
| 138 | + x: 198, |
| 139 | + y: 125 |
| 140 | + }] |
| 141 | +
|
| 142 | + [[object Object] { |
| 143 | + x: 196, |
| 144 | + y: 135 |
| 145 | + }, [object Object] { |
| 146 | + x: 196, |
| 147 | + y: 135 |
| 148 | + }] |
| 149 | +
|
| 150 | + [] |
| 151 | + */ |
| 152 | +`, |
| 153 | + externalLink: { |
| 154 | + platform: 'JSBin', |
| 155 | + url: 'http://jsbin.com/tuvesok/5/embed?js,console,output' |
| 156 | + } |
| 157 | + } |
| 158 | + ], |
| 159 | + relatedOperators: [ |
| 160 | + 'buffer', |
| 161 | + 'bufferCount', |
| 162 | + 'bufferTime', |
| 163 | + 'bufferToggle', |
| 164 | + 'windowWhen' |
| 165 | + ] |
6 | 166 | };
|
0 commit comments