Add data source model section to be used for substream() method.
authorTakeshi Yoshino <tyoshino@google.com>
Fri, 08 Nov 2013 01:30:19 +0900
changeset 47 b0e6c718b1ff
parent 46 d9d23bed8517
child 48 f31fd34af780
Add data source model section to be used for substream() method.
preview.html
--- a/preview.html	Thu Nov 07 03:53:25 2013 +0900
+++ b/preview.html	Fri Nov 08 01:30:19 2013 +0900
@@ -294,6 +294,48 @@
 writeData();</pre>
 	</section>
 
+	<section class="section" id="model">
+		<h2>Data source model</h2>
+
+		<p>
+			Interfaces introduced in the later section provides simple and convenient way to consume data.
+			They connect a raw sequence of bytes to with JavaScript world using Promises and method invocations.
+			Before describing them, here, we'll describe the model of data source which provides to be consumed via the interfaces.
+			To allow multiple consumers, we should do some reference counting on the data source.
+			The algorithm below allows for it.
+		</p>
+
+		<pre>
+function DataSource() {
+  this.data = new Deque([]);
+  // SortedList keeps elements sorted in accending order based
+  // on value.
+  this.sortedReaderPositions = new SortedMap([{key: 0, value: 0}]);
+  // Holds the number of bytes consumed and freed from data.
+  this.bytesConsumed = 0;
+}
+
+DataSource.prototype.read = function(long readerId, unsigned long long amount) {
+  // Calculate offset in data.
+  var startOffset = this.sortedReaderPositions.getAndRemove(readerId) - bytesConsumed;
+
+  var result = this.data.slice(startOffset, amount);
+
+  this.sortedReaderPosisions.put(readerId, end);
+
+  // Free bytes consumed by all readers.
+  var newBytesConsumed = this.sortedReaderPosisions.lastValue();
+  this.data.pop(newBytesConsumed - this.bytesConsumed);
+  this.bytesConsumed = newBytesConsumed;
+
+  return result;
+};</pre>
+
+    <p>
+			When substream() is called, a new reader is registered to the original <a>ReadableByteStream</a>'s data source and the new <a>ReadableByteStream</a> uses read() method to fetch data from it.
+    </p>
+	</section>
+
 	<section class="section" id="writableByteStream">
 		<h2>WritableByteStream interface</h2>
 
@@ -1237,6 +1279,7 @@
 			Jonas Sicking,
 			Kenneth Russell,
 			Yusuke Suzuki,
+			Yutaka Hirano,
 			Adrian Bateman
 			for their contributions to this specification.
 		</p>