Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/streams/resources/rs-utils.js

Issue 1404523005: Implement author-constructible ReadableStream using V8 extras (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 'use strict';
2 (function () {
3
4 function RandomPushSource(toPush) {
5 this.pushed = 0;
6 this.toPush = toPush;
7 this.started = false;
8 this.paused = false;
9 this.closed = false;
10
11 this._intervalHandle = null;
12 }
13
14 RandomPushSource.prototype = {
15
16 readStart: function() {
17 if (this.closed) {
18 return;
19 }
20
21 if (!this.started) {
22 this._intervalHandle = setInterval(writeChunk, 23);
23 this.started = true;
24 }
25
26 if (this.paused) {
27 this._intervalHandle = setInterval(writeChunk, 23);
28 this.paused = false;
29 }
30
31 var source = this;
32 function writeChunk() {
33 if (source.paused) {
34 return;
35 }
36
37 source.pushed++;
38
39 if (source.toPush > 0 && source.pushed > source.toPush) {
40 if (source._intervalHandle) {
41 clearInterval(source._intervalHandle);
42 source._intervalHandle = undefined;
43 }
44 source.closed = true;
45 source.onend();
46 } else {
47 source.ondata(randomChunk(128));
48 }
49 }
50 },
51
52 readStop: function() {
53 if (this.paused) {
54 return;
55 }
56
57 if (this.started) {
58 this.paused = true;
59 clearInterval(this._intervalHandle);
60 this._intervalHandle = undefined;
61 } else {
62 throw new Error('Can\'t pause reading an unstarted source.');
63 }
64 }
65 };
66
67 function randomChunk(size) {
68 var chunk = '';
69
70 for (var i = 0; i < size; i++) {
71 // Add a random character from the basic printable ASCII set.
72 chunk += String.fromCharCode(Math.round(Math.random() * 84) + 32)
73 }
74
75 return chunk;
76 }
77
78 function readableStreamToArray(readable, reader) {
79 var chunks = [];
80
81 if (reader == undefined) {
82 reader = readable.getReader();
83 }
84
85 return pump();
86
87 function pump() {
88 return reader.read().then(function(result) {
89 if (result.done) {
90 return chunks;
91 }
92
93 chunks.push(result.value);
94 return pump();
95 });
96 }
97 }
98
99 function SequentialPullSource(limit, async) {
100 this.current = 0;
101 this.limit = limit;
102 this.opened = false;
103 this.closed = false;
104
105 this._exec = function(f) {
106 f();
107 };
108 if (async)
109 this._exec = function(f) {
110 setTimeout(f, 0);
111 };
112 }
113
114 SequentialPullSource.prototype = {
115
116 open: function(cb) {
117 var myFunction = function() {
118 this.opened = true
119 cb();
120 };
121 this._exec(myFunction.bind(this));
122 },
123
124 read: function(cb) {
125 var myFunction = function() {
126 if (++this.current <= this.limit) {
127 cb(null, false, this.current);
128 } else {
129 cb(null, true, null);
130 }
131 };
132 this._exec(myFunction.bind(this));
133 },
134
135 close: function(cb) {
136 var myFunction = function() {
137 this.closed = true;
138 cb();
139 };
140 this._exec(myFunction.bind(this));
141 },
142 }
143
144 function sequentialReadableStream(limit, options) {
145 var sequentialSource = new SequentialPullSource(limit, options);
146
147 var stream = new ReadableStream({
148 start: function() {
149 return new Promise(function(resolve, reject) {
150 sequentialSource.open(function(err) {
151 if (err) {
152 reject(err);
153 }
154 resolve();
155 });
156 });
157 },
158
159 pull: function(c) {
160 return new Promise(function(resolve, reject) {
161 sequentialSource.read(function(err, done, chunk) {
162 if (err) {
163 reject(err);
164 } else if (done) {
165 sequentialSource.close(function(err) {
166 if (err) {
167 reject(err);
168 }
169 c.close();
170 resolve();
171 });
172 } else {
173 c.enqueue(chunk);
174 resolve();
175 }
176 });
177 });
178 },
179 });
180
181 stream.source = sequentialSource;
182
183 return stream;
184 }
185
186
187 self.RandomPushSource = RandomPushSource;
188 self.readableStreamToArray = readableStreamToArray;
189 self.sequentialReadableStream = sequentialReadableStream;
190
191 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698