Created
November 2, 2009 16:26
-
-
Save aherrman/224258 to your computer and use it in GitHub Desktop.
LocalConnection size limit workaround helpers attempt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculates the size of an object. This is based on SharedObject.getSize(). | |
* @param o The object to get the size of | |
* @return The size of the object. | |
*/ | |
public static function getObjectSize(o:Object):Number { | |
var so:SharedObject = SharedObject.getLocal("__getObjectSizeHelper"); | |
so.data.o = o; | |
var size:Number = so.getSize(); | |
so.clear(); | |
return size; | |
} | |
/** | |
* Checks to see if an object is too large to be sent through a | |
* LocalConnection. This is more of an estimate than a difinitive answer, as | |
* it is based on SharedObject.getSize(), which may not use the same object | |
* encoding as LocalConnection. | |
* @param o The object to check | |
* @return True if the object is too big, false if it's the right size. | |
*/ | |
public static function isTooBigForLC(o:Object):Boolean { | |
/* We're using a cutoff significantly smaller than the documented limit of | |
* LocalConnection. While this could cause us to skip some objects we don't | |
* want, generally we shouldn't get individual objects this big anyway. | |
* Also, the objects generally will be sent with some extra metadata (like | |
* the event name), which shouldn't take up too much space but we want to | |
* make sure there's enough room. */ | |
return getObjectSize(o) > 35000; | |
} | |
/** | |
* Recursively splits an array into separate arrays are all individually small | |
* enough to be sent over a LocalConnection. | |
* <h3>Example</h3> | |
* <p> | |
* Say you have the array: | |
* </p> | |
* <code>[1, 2, 3, 4, 5]</code> | |
* <p> | |
* and (for the sake of the example) the LocalConnection could only handle | |
* arrays containing up to 2 numbers. If you call: | |
* </p> | |
* <code>splitArrayForLC([1, 2, 3, 4, 5])</code> | |
* <p> | |
* it would return: | |
* </p> | |
* <code>[[1, 2], [3], [4, 5]]</code> | |
* @param a The array to split | |
* @return Array containing the split arrays. | |
*/ | |
public static function splitArrayForLC(a:Array):Array { | |
if (!isTooBigForLC(a)) { return [a]; } | |
if (a.length <= 1) { | |
LOG.warn("individual object is too big for LocalConnection! Skipping"); | |
return []; | |
} | |
var mid:Number = Math.floor(a.length / 2); | |
var left:Array = splitArrayForLC(a.slice(0, mid)); | |
var right:Array = splitArrayForLC(a.slice(mid)); | |
return left.concat(right); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment