package com.actiontad.utils
{
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
/**
* Updated
* Shared Object Manager Class, can instantiate it and persist the Object,
* or just call upon the static methods with specified params.
*/
public class SOManager
{
public static var theSO:SharedObject = null;
public static var thePlace:String = "/";
public static var soSize:int = 25000;
/**
*
* If you instantiate this class, passing the name of the SharedObject you want to keep a reference to,
* then calls to the static methods of this class will use that reference no matter what SO name you pass them.
* And then also the sharedObjectData property will return the data object of that SO, instead of null.
*
*
* @param SOName The string name of the SharedObject you want to establish a reference to.
* The SharedObject is created if not already present.
* @param location The string url location for the SharedObject, default is same domain '/'.
* @param size The max byte size for the SharedObject, default is 25000
*/
public function SOManager(SOName:String, location:String = "/", size:int = 25000) {
thePlace = location;
soSize = size;
try {
theSO = SharedObject.getLocal(SOName, thePlace);
} catch(e:Error) {
theSO = null;
}
}
/**
*
* @return Returns null unless you have instantiated the SOManager class,
* otherwise it returns the data object of the SharedObject referenced during instantiation.
*/
public static function get sharedObjectData():Object {
return (theSO)?theSO.data:null;
}
/**
* If the local SharedObject of the given name does not exsist, it is created.
* This method returns the data object of the established local SharedObejct.
*
* @param SOName The name of the local SharedObject.
* @param location The location or url of the SharedObject, normally "/".
* @return The data object of the established local SharedObject.
*/
public static function establishLocalSO(SOName:String = "so", location:String = ""):Object {
var result:Object;
var atPlace:String = (location != "")?location:thePlace;
try {
if (theSO) { result = theSO.data; }
else {
theSO = SharedObject.getLocal(SOName, atPlace);
result = theSO.data;
}
} catch (e:Error) {
result = null;
}
return result;
}
/**
* Sets data on the SharedObject. With an option to also flush the data right away.
* The default behavior is to not flush.
* @param property The name of the property to set.
* @param value The value to set on the property.
* @param SOName The name of the local SharedObject. Optional if the class was instatiated.
* @param location The location or url of the SharedObject, normally "/". Optional if the class was instantiated.
* @param doFlush Boolean to flush after setting data, default is false.
* @param netStatusHandlerFunction An optional Function to handle the NetStatusEvent.
* @return True if data set was successful, false otherwise.
* @example Flushing with a persistent SharedObject that was declared during instantiation of the SoManager class:
* setDataOnLocalSO("propToSet", 3, "", "", true);
* @example Flushing with non persistant SharedObject, that was not declared during instantion:
* setDataOnLocalSO("propToSet", 3, "SOName", "/", true);
*/
public static function setDataOnLocalSO(property:String = "", value:Object = null, SOName:String = "so", location:String = "", doFlush:Boolean = false, netStatusHandlerFunction:Function = null):Boolean {
var result:Boolean;
var atPlace:String = (location != "")?location:thePlace;
var so:SharedObject;
try {
so = (theSO)?theSO:SharedObject.getLocal(SOName, atPlace);
if (property != "") {
so.data[property] = value;
result = true;
}
} catch (e:Error) {
//this function returns false on failure
}
if (result == true && doFlush == true) {
if (netStatusHandlerFunction == null) { netStatusHandlerFunction = SOManager.netStatusHandler; }
if (!so.hasEventListener(NetStatusEvent.NET_STATUS)) {
so.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandlerFunction );
}
try {
so.flush(soSize);
} catch(e:Error) {
result = false;
}
}
return result;
}
/**
*
* @param property The name of the property to get.
* @param SOName The name of the local SharedObject.
* @param location The location or url of the local SharedObject, normally "/".
* @return The value of the property you wanted, or null if there was no value.
*/
public static function getDataFromLocalSO(property:String = "", SOName:String = "so", location:String = ""):Object {
var so:SharedObject;
var atPlace:String = (location != "")?location:thePlace;
var result:Object = null;
try {
so = (theSO)?theSO:SharedObject.getLocal(SOName, atPlace);
result = (so.data[property])?so.data[property]:null;
} catch (e:Error) {
//disregard errors, one would test for null from this function if an error needs to be thrown
}
return result;
}
/**
*
* @param SOName The name of the local Shared Object.
* @param location The location or url, normally just "/"
* @param size The amount of data size for flush.
* @param netStatusHandlerFunction A Function to handle the NetStatusEvent.
* @return True if the flus was successful.
*/
public static function flushLocalSO(SOName:String = "so", location:String = "", size:int = 0, netStatusHandlerFunction:Function = null):Boolean {
var result:Boolean;
var atPlace:String = (location != "")?location:thePlace;
var flushSize:int = (size != 0)?size:soSize;
var so:SharedObject;
try {
so = (theSO)?theSO:SharedObject.getLocal(SOName, atPlace);
result = true;
} catch (e:Error) {
//this function returns false on failure
}
if (result == true) {
if (netStatusHandlerFunction == null) { netStatusHandlerFunction = SOManager.netStatusHandler; }
if (!so.hasEventListener(NetStatusEvent.NET_STATUS)) {
so.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandlerFunction );
}
try {
so.flush(flushSize);
} catch(e:Error) {
result = false;
}
}
return result;
}
public static function netStatusHandler(e:NetStatusEvent = null):void {
//catch Net Status Fail
}
public static function deleteLocalSO(SOName:String = "so", location:String = ""):void {
var atPlace:String = (location != "")?location:thePlace;
var so:SharedObject = (theSO)?theSO:SharedObject.getLocal(SOName, atPlace);
so.clear();
so = null;
}
}
}