r106077 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106076‎ | r106077 | r106078 >
Date:20:13, 13 December 2011
Author:gwicke
Status:deferred
Tags:
Comment:
Comment TokenTransformDispatcher.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformDispatcher.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformDispatcher.js
@@ -17,6 +17,14 @@
1818 * insert tokens in front of other ongoing expansion tasks.
1919 * */
2020
 21+/**
 22+ * Central dispatcher for potentially asynchronous token transformations.
 23+ *
 24+ * @class
 25+ * @constructor
 26+ * @param {Function} callback, a callback function accepting a token list as
 27+ * its only argument.
 28+ */
2129 function TokenTransformDispatcher( callback ) {
2230 this.cb = callback; // Called with transformed token list when done
2331 this.transformers = {
@@ -29,9 +37,14 @@
3038 any: [] // all tokens, before more specific handlers are run
3139 };
3240 this.reset();
33 - return this;
3441 }
3542
 43+/**
 44+ * Reset the internal token and callback state of the
 45+ * TokenTransformDispatcher, but keep registrations untouched.
 46+ *
 47+ * @method
 48+ */
3649 TokenTransformDispatcher.prototype.reset = function () {
3750 this.accum = new TokenAccumulator(null);
3851 this.firstaccum = this.accum;
@@ -39,6 +52,17 @@
4053 // (e.g., async template fetches/expansions)
4154 };
4255
 56+/**
 57+ * Append a listener registration. The new listener will be executed after
 58+ * other listeners for the same token have been called.
 59+ *
 60+ * @method
 61+ * @param {Function} listener, a function accepting a TokenContext and
 62+ * returning a TokenContext.
 63+ * @param {String} type, one of 'tag', 'text', 'newline', 'comment', 'end',
 64+ * 'martian' (unknown token), 'any' (any token, matched before other matches).
 65+ * @param {String} tag name for tags, omitted for non-tags
 66+ */
4367 TokenTransformDispatcher.prototype.appendListener = function ( listener, type, name ) {
4468 if ( type === 'tag' ) {
4569 name = name.toLowerCase();
@@ -52,6 +76,17 @@
5377 }
5478 };
5579
 80+/**
 81+ * Prepend a listener registration. The new listener will be called before
 82+ * other listeners for the same token have been called.
 83+ *
 84+ * @method
 85+ * @param {Function} listener, a function accepting a TokenContext and
 86+ * returning a TokenContext.
 87+ * @param {String} type, one of 'tag', 'text', 'newline', 'comment', 'end',
 88+ * 'martian' (unknown token), 'any' (any token, matched before other matches).
 89+ * @param {String} tag name for tags, omitted for non-tags
 90+ */
5691 TokenTransformDispatcher.prototype.prependListener = function ( listener, type, name ) {
5792 if ( type === 'tag' ) {
5893 name = name.toLowerCase();
@@ -65,6 +100,19 @@
66101 }
67102 };
68103
 104+/**
 105+ * Remove a listener registration
 106+ *
 107+ * XXX: matching the function for equality is not ideal. Use a string key
 108+ * instead?
 109+ *
 110+ * @method
 111+ * @param {Function} listener, a function accepting a TokenContext and
 112+ * returning a TokenContext.
 113+ * @param {String} type, one of 'tag', 'text', 'newline', 'comment', 'end',
 114+ * 'martian' (unknown token), 'any' (any token, matched before other matches).
 115+ * @param {String} tag name for tags, omitted for non-tags
 116+ */
69117 TokenTransformDispatcher.prototype.removeListener = function ( listener, type, name ) {
70118 var i = -1;
71119 var ts;
@@ -146,20 +194,23 @@
147195 return tokenCTX;
148196 };
149197
150 -/* Transform and expand tokens.
 198+/**
 199+ * Transform and expand tokens.
151200 *
152201 * Normally called with undefined accum. Asynchronous expansions will call
153202 * this with their known accum, which allows expanded tokens to be spliced in
154203 * at the appropriate location in the token list, which is always at the tail
155 - * end of the current accumulator.
 204+ * end of the current accumulator. Calls back registered callback if there are
 205+ * no more outstanding asynchronous expansions.
156206 *
157 - * @param tokens {List of tokens} Tokens to process.
158 - * @param accum {TokenAccumulator} object. Undefined for first call, set to
159 - * accumulator with expanded token at tail for asynchronous
160 - * expansions.
161 - * @returns nothing: Calls back registered callback if there are no more
162 - * outstanding asynchronous expansions.
163 - * */
 207+ * @param {Array} Tokens to process.
 208+ * @param {Object} TokenAccumulator object. Undefined for first call, set to
 209+ * accumulator with expanded token at tail for asynchronous expansions.
 210+ * @param {Int} delta, default 1. Decrement the outstanding async callback
 211+ * count by this much to determine when all outstanding actions are done.
 212+ * Main use of this argument is to avoid counting some extra callbacks from
 213+ * actions before they are done.
 214+ */
164215 TokenTransformDispatcher.prototype.transformTokens = function ( tokens, accum, delta ) {
165216 if ( accum === undefined ) {
166217 this.reset();
@@ -219,6 +270,14 @@
220271 this.finish( delta );
221272 };
222273
 274+/**
 275+ * Decrement the number of outstanding async actions by delta and call the
 276+ * callback with a list of tokens if none are remaining.
 277+ *
 278+ * @method
 279+ * @param {Int} delta, how much to decrement the number of outstanding async
 280+ * actions.
 281+ */
223282 TokenTransformDispatcher.prototype.finish = function ( delta ) {
224283 this.outstanding -= delta;
225284 if ( this.outstanding === 0 ) {
@@ -235,7 +294,14 @@
236295 }
237296 };
238297
239 -/* Start a new accumulator for asynchronous work. */
 298+/**
 299+ * Start a new accumulator for asynchronous work.
 300+ *
 301+ * @param {Object} TokenAccumulator object after which to insert a new
 302+ * accumulator
 303+ * @count {Int} (optional, default 1) The number of callbacks to expect before
 304+ * considering the asynch work on the new accumulator done.
 305+ * */
240306 TokenTransformDispatcher.prototype.newAccumulator = function ( accum, count ) {
241307 if ( count !== undefined ) {
242308 this.outstanding += count;
@@ -248,8 +314,15 @@
249315 return accum.insertAccumulator( );
250316 };
251317
252 -// Token accumulators in a linked list. Using a linked list simplifies async
253 -// callbacks for template expansions.
 318+/**
 319+ * Token accumulators in a linked list. Using a linked list simplifies async
 320+ * callbacks for template expansions as it avoids stable references to chunks.
 321+ *
 322+ * @class
 323+ * @constructor
 324+ * @param {Object} next TokenAccumulator to link to
 325+ * @param {Array} (optional) tokens, init accumulator with tokens or []
 326+ */
254327 function TokenAccumulator ( next, tokens ) {
255328 this.next = next;
256329 if ( tokens ) {
@@ -260,14 +333,32 @@
261334 return this;
262335 }
263336
 337+/**
 338+ * Push a token into the accumulator
 339+ *
 340+ * @method
 341+ * @param {Object} token
 342+ */
264343 TokenAccumulator.prototype.push = function ( token ) {
265344 return this.accum.push(token);
266345 };
267346
 347+/**
 348+ * Pop a token from the accumulator
 349+ *
 350+ * @method
 351+ * @returns {Object} token
 352+ */
268353 TokenAccumulator.prototype.pop = function ( ) {
269354 return this.accum.pop();
270355 };
271356
 357+/**
 358+ * Insert an accumulator after this one.
 359+ *
 360+ * @method
 361+ * @returns {Object} created TokenAccumulator
 362+ */
272363 TokenAccumulator.prototype.insertAccumulator = function ( ) {
273364 this.next = new TokenAccumulator(this.next);
274365 return this.next;

Status & tagging log