UNPKG

este-library-oldschool

Version:

Library for github.com/steida/este.git

95 lines (88 loc) 2.59 kB
// Generated by github.com/steida/coffee2closure 900.1.18 /** @fileoverview Chunked JSONP implementation. Payload is divided into max. 2kb chunks. */ goog.provide('este.net.ChunkedJsonp'); goog.provide('este.net.ChunkedJsonp.create'); goog.require('goog.net.Jsonp'); goog.require('este.string'); goog.require('este.json'); /** @param {Function} jsonpFactory @param {Function} randomStringFactory @constructor */ este.net.ChunkedJsonp = function(jsonpFactory1, randomStringFactory) { this.jsonpFactory = jsonpFactory1; this.randomStringFactory = randomStringFactory; } /** @param {goog.Uri|string} uri The Uri of the server side code that receives data posted through this channel (e.g., "http://maps.google.com/maps/geo"). @param {string=} opt_callbackParamName The parameter name that is used to specify the callback. Defaults to "callback". */ este.net.ChunkedJsonp.create = function(uri, opt_callbackParamName) { var jsonpFactory; jsonpFactory = function() { return new goog.net.Jsonp(uri, opt_callbackParamName); }; return new este.net.ChunkedJsonp(jsonpFactory, goog.string.getRandomString); }; /** http://support.microsoft.com/kb/208427 @type {number} */ este.net.ChunkedJsonp.MAX_CHUNK_SIZE = 1900; /** @type {Function} @protected */ este.net.ChunkedJsonp.prototype.jsonpFactory = null; /** @type {Function} @protected */ este.net.ChunkedJsonp.prototype.randomStringFactory = null; /** @param {Object} payload @param {Function=} opt_replyCallback */ este.net.ChunkedJsonp.prototype.send = function(payload, opt_replyCallback) { var callCount, chunk, chunks, i, jsonp, jsonpPayload, jsonpReplyCallback, len, randomString; chunks = this.getChunks(payload); randomString = this.randomStringFactory(); callCount = 0; jsonpReplyCallback = function() { callCount++; if (callCount === chunks.length) { return opt_replyCallback.apply(null, arguments); } }; for (i = 0, len = chunks.length; i < len; i++) { chunk = chunks[i]; jsonp = this.jsonpFactory(); jsonpPayload = { 'u': randomString, 'd': chunk.text, 'i': chunk.index, 't': chunk.total }; if (opt_replyCallback) { jsonp.send(jsonpPayload, jsonpReplyCallback); } else { jsonp.send(jsonpPayload); } } }; /** @param {Object} payload @return {Array.<Object>} @protected */ este.net.ChunkedJsonp.prototype.getChunks = function(payload) { var str; str = este.json.stringify(payload); return este.string.chunkToObject(str, este.net.ChunkedJsonp.MAX_CHUNK_SIZE); };