|
| 1 | +ReadableStreamSource : Object |
| 2 | + |
| 3 | +Object that provides the sequence of values for a %%/ReadableStream|ReadableStream%%. |
| 4 | +Passed to the %%/ReadableStream|ReadableStream%% constructor. Any JavaScript object |
| 5 | +can be used as a readable stream source. Any of the properties/method can be specified. |
| 6 | +If a property/method is not provided, the ReadableStream will use a default behavior |
| 7 | +for the property/method. |
| 8 | + |
| 9 | +Spec: |
| 10 | +https://streams.spec.whatwg.org/#dictdef-underlyingsource |
| 11 | + |
| 12 | +---- |
| 13 | +instance.start(controller : ReadableStreamController) : Object |
| 14 | + |
| 15 | +<example> |
| 16 | +// Create a stream of strings. |
| 17 | +const stringStream = new ReadableStream({ |
| 18 | + start(controller) { |
| 19 | + ['a', 'b', 'c'].forEach(s => controller.enqueue(s)); |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +const reader = stringStream.getReader(); |
| 24 | +let result; |
| 25 | +while (!(result = await reader.read()).done) { |
| 26 | + console.log(result.value); |
| 27 | +} |
| 28 | +</example> |
| 29 | + |
| 30 | +Spec: |
| 31 | +https://streams.spec.whatwg.org/#dom-underlyingsource-start |
| 32 | + |
| 33 | +---- |
| 34 | +instance.pull(controller : ReadableStreamController) : Promise<undefined> |
| 35 | + |
| 36 | +Called each time the stream's internal queue drops below the |
| 37 | +%%/QueuingStrategy#highWaterMark|high water mark%%, indicating **this** should |
| 38 | +enqueue more chunks. |
| 39 | + |
| 40 | +<example> |
| 41 | +const stringStream = new ReadableStream({ |
| 42 | + i: 1, |
| 43 | + pull(controller) { |
| 44 | + console.log('pull', controller.desiredSize); |
| 45 | + while (controller.desiredSize > 0) { |
| 46 | + controller.enqueue(`chunk_${this.i++}`); |
| 47 | + } |
| 48 | + if (this.i > 5) controller.close(); |
| 49 | + } |
| 50 | +}, new CountQueuingStrategy({ highWaterMark: 2 })); |
| 51 | + |
| 52 | +const reader = stringStream.getReader(); |
| 53 | +let result; |
| 54 | +while (!(result = await reader.read()).done) { |
| 55 | + console.log('received:', result.value); |
| 56 | +} |
| 57 | +</example> |
| 58 | + |
| 59 | +Spec: |
| 60 | +https://streams.spec.whatwg.org/#dom-underlyingsource-pull |
| 61 | + |
| 62 | +---- |
| 63 | +instance.cancel(reason : Object) : Promise<undefined> |
| 64 | + |
| 65 | +Called when the stream has been canceled, either by %%/ReadableStream#cancel|ReadableStream.cancel()%% or |
| 66 | +%%ReadableStreamDefaultReader#cancel|Reader.cancel()%%, with **reason** as the value |
| 67 | +passed to that function. |
| 68 | + |
| 69 | +<example> |
| 70 | +const stringStream = new ReadableStream({ |
| 71 | + start(controller) { |
| 72 | + controller.enqueue('a'); |
| 73 | + controller.enqueue('b'); |
| 74 | + controller.close(); |
| 75 | + }, |
| 76 | + cancel(reason) { |
| 77 | + console.log(`cancel reason: ${reason}`); |
| 78 | + } |
| 79 | +}); |
| 80 | + |
| 81 | +const reader = stringStream.getReader(); |
| 82 | + |
| 83 | +let result; |
| 84 | +while (!(result = await reader.read()).done) { |
| 85 | + console.log(result.value); |
| 86 | + reader.cancel('xyz'); |
| 87 | +} |
| 88 | +</example> |
| 89 | + |
| 90 | +Spec: |
| 91 | +https://streams.spec.whatwg.org/#dom-underlyingsource-pull |
| 92 | + |
| 93 | +---- |
| 94 | +instance.type : String |
| 95 | + |
| 96 | +If set to **'bytes'**, the ReadableStream will be a readable byte stream. |
| 97 | + |
| 98 | +Spec: |
| 99 | +https://streams.spec.whatwg.org/#dom-underlyingsource-type |
0 commit comments