1  /**
     2   * com.sekati.data.SO
     3   * @version 1.1.1
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.core.CoreObject;
    10  import com.sekati.events.Dispatcher;
    11  import com.sekati.events.Event;
    12  import com.sekati.utils.Delegate;
    13  
    14  /**
    15   * SharedObject wrapper class.
    16   */
    17  class com.sekati.data.SO extends CoreObject {
    18  
    19  	private var _this:SO;
    20  	private var _so:SharedObject;
    21  	private var _name:String;
    22  	private static var onSOStatusEVENT:String = "onSOStatus";
    23  	private static var onSOSyncEVENT:String = "onSOSync";
    24  
    25  	/**
    26  	 * SO Constructor
    27  	 * @param so_name (String) shared object name
    28  	 * @return Void
    29  	 * @throws Error if no so_name was passed.
    30  	 */
    31  	public function SO(so_name:String) {
    32  		super( );
    33  		if(!so_name) {
    34  			throw new Error( "@@@ " + this.toString( ) + " Error: instance Constructor expects so_name param." );
    35  			return;	
    36  		}
    37  		_name = so_name;
    38  		_this = this;
    39  		_so = SharedObject.getLocal( _name );
    40  		_so.onStatus = Delegate.create( _this, so_onStatus );
    41  		_so.onSync = Delegate.create( _this, so_onSync );
    42  	}
    43  
    44  	/**
    45  	 * onStatus event handler dispatches event
    46  	 * @param info (Object)
    47  	 * @return Void
    48  	 */
    49  	public function so_onStatus(info:Object):Void {
    50  		trace( "status info: " + info );
    51  		var e:Event = new Event( onSOStatusEVENT, this, {info:info} );
    52  		Dispatcher.$.dispatchEvent( e );
    53  	}
    54  
    55  	/**
    56  	 * onSync event handler dispatches event
    57  	 */
    58  	public function so_onSync(obj:Object):Void {
    59  		trace( "sync obj: " + obj );
    60  		var e:Event = new Event( onSOSyncEVENT, this, {obj:obj} );
    61  		Dispatcher.$.dispatchEvent( e );
    62  	}
    63  
    64  	/**
    65  	 * write a property value to the shared object
    66  	 * @param prop (String) property name
    67  	 * @param val (Object) value to wrote to property
    68  	 * @return Void
    69  	 */
    70  	public function write(prop:String, val:Object):Void {
    71  		_so.data[prop] = val;
    72  		_so.flush( );
    73  	}
    74  
    75  	/**
    76  	 * read a property value from the shared object
    77  	 * @param prop (String) property to read
    78  	 * @return Object - property value
    79  	 */
    80  	public function read(prop:String):Object {
    81  		return _so.data[prop];
    82  	}
    83  
    84  	/**
    85  	 * destroy the shared object
    86  	 * @return Void
    87  	 */
    88  	public function clear():Void {
    89  		_so.clear( );
    90  	}
    91  
    92  	/**
    93  	 * get the size of the shared object in bytes.
    94  	 * @return Number
    95  	 */
    96  	public function getSize():Number {
    97  		return _so.getSize( );
    98  	}
    99  
   100  	/**
   101  	 * return recursively formatted data of shared object
   102  	 * @return String
   103  	 */
   104  	public function getData():String {
   105  		var str:String = _name + "={\n";
   106  		for (var prop in _so.data) {
   107  			str += prop + ": " + _so.data[prop] + "\n";
   108  		}
   109  		str += "};";
   110  		return str;
   111  	}
   112  
   113  	/**
   114  	 * Show FlashPlayer storage System settings.
   115  	 * @return Void
   116  	 */
   117  	public function showSettings():Void {
   118  		System.showSettings( 1 );	
   119  	}
   120  
   121  	/**
   122  	 * Destroy sharedObject data and instance
   123  	 * @return Void
   124  	 */
   125  	public function destroy():Void {
   126  		_so.data = null;
   127  		_so.flush( );
   128  		delete this;
   129  		super.destroy( );
   130  	}		
   131  }