Packagecom.worlize.api.data
Classpublic dynamic class SyncedDataStore
InheritanceSyncedDataStore Inheritance flash.utils.Proxy
Implements flash.events.IEventDispatcher

Product Version : Worlize API.v1

Provides a synchronized key/value store, guaranteed to be in a consistent state across all connected clients.

Use this class when you need to synchronize the current state across all connected clients and that state does not require keeping track of historical data. For example, this would not be a good way to synchronize data about the lines drawn on a shared canvas because it would retransmit the entire canvas history with every update. Use this class to keep smaller values up to date across clients and to trigger events based on the changing of those values.

Keys must be strings and values can be anything that is serializable in AMF3 format.

Data values can be read using bracket notation such as store['penColor'], and can be used directly in a for loop or a for each loop. However, values must be set by making a call to the set() method rather than assigning a value using bracket notation.

View the examples

See also

[] (Array Access)
Action Message Format on Wikipedia


Public Methods
 MethodDefined By
  
SyncedDataStore(sourceData:Object = null)
Constructor.
SyncedDataStore
  
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
SyncedDataStore
  
dispatchEvent(event:Event):Boolean
Dispatches an event into the event flow.
SyncedDataStore
  
get(key:String):*
Get the current value of the given key.
SyncedDataStore
  
hasEventListener(type:String):Boolean
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
SyncedDataStore
  
remove(key:String):void
Requests the server to delete the given key from the data store.
SyncedDataStore
  
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
SyncedDataStore
  
set(key:String, value:*):void
Requests the server to set a new value for the given key.
SyncedDataStore
  
willTrigger(type:String):Boolean
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
SyncedDataStore
Events
 Event Summary Defined By
  Dispatched when a property is changedSyncedDataStore
  Dispatched when a property is deletedSyncedDataStore
Constructor Detail
SyncedDataStore()Constructor
public function SyncedDataStore(sourceData:Object = null)

Product Version : Worlize API.v1

Constructor.

The SyncedDataStoreinstance is created and managed by the WorlizeAPI class. You do not instantiate this object yourself.

Parameters
sourceData:Object (default = null)

See also


Example
How to get a reference to the SyncedDataStore instance
         
         var api:WorlizeAPI = WorlizeAPI.getInstance();
         var store:SyncedDataStore = api.syncedDataStore;
         
         
Method Detail
addEventListener()method
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.

Parameters

type:String
 
listener:Function
 
useCapture:Boolean (default = false)
 
priority:int (default = 0)
 
useWeakReference:Boolean (default = false)

See also

dispatchEvent()method 
public function dispatchEvent(event:Event):Boolean

Dispatches an event into the event flow. The event target is the EventDispatcher object upon which the dispatchEvent() method is called.

Parameters

event:Event

Returns
Boolean

See also

get()method 
public function get(key:String):*

Get the current value of the given key.

Note that you can also use standard bracket notation to retrieve values, though you cannot use it to set values.

Parameters

key:String — the key name of the value to retrieve

Returns
* — the value associated with the key

Example
Using bracket notation to retrieve values
         
         // Get a reference to the data store..
         var api:WorlizeAPI = WorlizeAPI.getInstance();
         var dataStore:SyncedDataStore = api.syncedDataStore;
         
         var userGuid:String = dataStore['player2'];
         var player2:User = api.thisRoom.getUserByGuid(userGuid);
         trace("Player 2 is: " + player2.name);
         
         
hasEventListener()method 
public function hasEventListener(type:String):Boolean

Checks whether the EventDispatcher object has any listeners registered for a specific type of event.

Parameters

type:String

Returns
Boolean

See also

remove()method 
public function remove(key:String):void

Product Version : Worlize APIv.1

Requests the server to delete the given key from the data store.

Be aware: this does not immediately delete the value at the specified key in the local store. Instead, it simply sends a message to the server requesting that it delete the value and broadcast the update to all users in the room. The value will be deleted locally only once the server broadcasts the new value to all the connected clients.

It is recommended that you add a listener for the propertyChanged and propertyDeleted events and take action when a value you are interested in is changed.

Parameters

key:String — the name of the key to delete

See also

removeEventListener()method 
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void

Removes a listener from the EventDispatcher object. If there is no matching listener registered with the EventDispatcher object, a call to this method has no effect.

Parameters

type:String
 
listener:Function
 
useCapture:Boolean (default = false)

See also

set()method 
public function set(key:String, value:*):void

Product Version : Worlize APIv.1

Requests the server to set a new value for the given key.

The value can be any object that can be serialized to the AMF3 format.

Be aware: this does not immediately change the value at the specified key in the local store. Instead, it simply sends a message to the server requesting that it update the value and broadcast the update to all users in the room. The value will be updated locally only once the server broadcasts the new value to all the connected clients.

Also note that if a value contains an Object or Array, changes to child properties will not trigger an update through the server and will not cause a propertyChanged event to be dispatched.

It is recommended that you add a listener for the propertyChanged and propertyDeleted events and take action when a value you are interested in is changed.

Also be aware that while you can use bracket notation to read values from the data store, you cannot use bracket notation to set values.

Parameters

key:String — the name of the key whose value you wish to set
 
value:* — an AMF3 serializable value to associate with the key

See also


Example
Update a value
         
         var store:SyncedDataStore = WorlizeAPI.getInstance().syncedDataStore;
         
         function onPropertyChanged(event:ChangeEvent):void {
             if (event.name === 'color') {
                 trace("The color changed from " + event.oldValue +
                       " to " + event.newValue);
             }
         }
         store.addEventListener(ChangeEvent.PROPERTY_CHANGED, onPropertyChanged);
         
         store.set('color', 'red');
         store.set('color', 'blue');
         
         // Trace output will be:
         // The color changed from undefined to red
         // The color changed from red to blue
         
         
willTrigger()method 
public function willTrigger(type:String):Boolean

Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.

Parameters

type:String

Returns
Boolean

See also

Event Detail
propertyChanged Event
Event Object Type: com.worlize.api.event.ChangeEvent
ChangeEvent.type property = com.worlize.api.event.ChangeEvent.PROPERTY_CHANGED

Product Version : Worlize API.v1

Dispatched when a property is changed

The ChangeEvent.PROPERTY_CHANGED constant defines the value of the type property of the event object for a propertyChanged event.

The properties of the event object have the following values:

PropertyValue
bubblesfalse
cancelablefalse
currentTargetThe Object that defines the event listener that handles the event. For example, if you use myButton.addEventListener() to register an event listener, myButton is the value of the currentTarget.
nameThe name of the property that changed on the target object.
changedByA User instance that represents the Worlize user who changed the property, if available
newValueThe new value of the property
oldValueThe old value of the property, as it was before it was changed.
targetThe Object that dispatched the event; it is not always the Object listening for the event. Use the currentTarget property to always access the Object listening for the event.

See also

propertyDeleted Event  
Event Object Type: com.worlize.api.event.ChangeEvent
ChangeEvent.type property = com.worlize.api.event.ChangeEvent.PROPERTY_DELETED

Product Version : Worlize API.v1

Dispatched when a property is deleted

The ChangeEvent.PROPERTY_DELETED constant defines the value of the type property of the event object for a propertyDeleted event.

The properties of the event object have the following values:

PropertyValue
bubblesfalse
cancelablefalse
currentTargetThe Object that defines the event listener that handles the event. For example, if you use myButton.addEventListener() to register an event listener, myButton is the value of the currentTarget.
nameThe name of the property that changed on the target object.
changedByA User instance that represents the Worlize user who deleted the property, if available
newValueundefined
oldValueThe old value of the property, as it was before it was deleted.
targetThe Object that dispatched the event; it is not always the Object listening for the event. Use the currentTarget property to always access the Object listening for the event.

See also

Examples
Trace all keys and values currently stored
     
     var store:SyncedDataStore = WorlizeAPI.getInstance().syncedDataStore;
     for (var key:String in store) {
         var value:= store[key];
         trace(key + ": " + value);
     }