More.
Made ObserverList work with a passed-in function, actionscript style. I might reconsider some earlier decisions to go away from ObserverList. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3952 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -5,6 +5,19 @@ package com.threerings.util {
|
||||
*/
|
||||
public class ArrayUtil
|
||||
{
|
||||
/**
|
||||
* Return the first index of the specified element, or -1 if not present.
|
||||
*/
|
||||
public static function indexOf (arr :Array, element :Object) :int
|
||||
{
|
||||
for (var ii :int = 0; ii < arr.length; ii++) {
|
||||
if (arr[ii] === element) {
|
||||
return ii;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the first instance of the specified element from the array.
|
||||
*/
|
||||
|
||||
@@ -384,3 +384,8 @@ public class MessageBundle
|
||||
protected static const QUAL_SEP :String = ":";
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: figure out how this is all actually going to even work
|
||||
class ResourceBundle
|
||||
{
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.threerings.util {
|
||||
|
||||
public class ObserverList
|
||||
{
|
||||
/** A notification ordering policy indicating that the observers
|
||||
* should be notified in the order they were added and that the
|
||||
* notification should be done on a snapshot of the array. */
|
||||
public static const SAFE_IN_ORDER_NOTIFY :int = 1;
|
||||
|
||||
/** A notification ordering policy wherein the observers are notified
|
||||
* last to first so that they can be removed during the notification
|
||||
* process and new observers added will not inadvertently be notified
|
||||
* as well, but no copy of the observer list need be made. This will
|
||||
* not work if observers are added or removed from arbitrary positions
|
||||
* in the list during a notification call. */
|
||||
public static const FAST_UNSAFE_NOTIFY :int = 2;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function ObserverList (notifyPolicy :int = 2)
|
||||
{
|
||||
_notifyPolicy = notifyPolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an observer to this list.
|
||||
*/
|
||||
public function add (observer :Object) :void
|
||||
{
|
||||
if (ArrayUtil.indexOf(_list, observer) == -1) {
|
||||
_list.push(observer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an observer from this list.
|
||||
*/
|
||||
public function remove (observer :Object) :void
|
||||
{
|
||||
ArrayUtil.removeFirst(_list, observer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply some operation to all observers.
|
||||
* The function to be passed in should expect one argument and return a
|
||||
* Boolean.
|
||||
* function (observer :Object) :Boolean.
|
||||
* The function should return false if the observer should be removed
|
||||
* from the list.
|
||||
*/
|
||||
public function apply (func :Function) :void
|
||||
{
|
||||
var list :Array = _list;
|
||||
if (_notifyPolicy == SAFE_IN_ORDER_NOTIFY) {
|
||||
list = list.concat(); // make a duplicate
|
||||
list.reverse(); // reverse it so that we start with earlier obs
|
||||
}
|
||||
for (var ii :int = list.length-1; ii >= 0; ii--) {
|
||||
try {
|
||||
var result :Boolean = func(list[ii]);
|
||||
if (!result) {
|
||||
// remove it if directed to do so
|
||||
remove(list[ii]);
|
||||
}
|
||||
} catch (err :Error) {
|
||||
Log.warning("ObserverOp choked during notification.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Our notification policy. */
|
||||
protected var _notifyPolicy :int;
|
||||
|
||||
/** The actual list of observers. */
|
||||
protected var _list :Array = new Array();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.threerings.util {
|
||||
|
||||
public interface ResultListener
|
||||
{
|
||||
function requestCompleted (obj :Object) :void;
|
||||
|
||||
function requestFailed (cause :Error) :void;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user