/flight/utils/Registry.as

https://code.google.com/ · ActionScript · 171 lines · 78 code · 19 blank · 74 comment · 28 complexity · 5e779f7a768628298241b9cbe4394380 MD5 · raw file

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2009 Tyler Wright, Robert Taylor, Jacob Wright
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////
  24. package flight.utils
  25. {
  26. import flash.utils.Dictionary;
  27. /**
  28. * The Registry is a global store for system-wide values and objects.
  29. * Because Registry represents a static class it provides a single point of
  30. * access everywhere.
  31. */
  32. public class Registry
  33. {
  34. // where system-wide values are stored by scope and index
  35. private static var scopeIndex:Dictionary = new Dictionary(true);
  36. private static var globalIndex:Dictionary = scopeIndex[null] = new Dictionary(true);
  37. private static var watcherByTarget:Dictionary = new Dictionary(true);
  38. private static var watcherByIndex:Dictionary = new Dictionary(true);
  39. /**
  40. * Register data with some global identifier for system-wide lookup.
  41. *
  42. * @param index String or object identifier with which to
  43. * register and lookup data.
  44. * @param value Data to be registered.
  45. * @param scope Optionally register data to a specific scope
  46. * identifier, creating a localized scope
  47. * within the global space.
  48. *
  49. * @see #lookup
  50. */
  51. public static function register(index:Object, value:Object, scope:Object = null):void
  52. {
  53. if (scopeIndex[scope] == null) {
  54. scopeIndex[scope] = new Dictionary(true);
  55. }
  56. scopeIndex[scope][index] = value;
  57. // update any "watching" for this particular 'index', on any scope
  58. for each (var syncDetail:Array in watcherByIndex[index]) {
  59. syncDetail[0][ syncDetail[1] ] = lookup(index, syncDetail[3]);
  60. }
  61. }
  62. /**
  63. * Remove any data registered at the specified index and scope.
  64. *
  65. * @param index String or object identifier with which to
  66. * locate and remove data.
  67. * @param scope Optionally remove data by a specific scope
  68. * identifier, a localized scope within the
  69. * global space.
  70. *
  71. * @see #register
  72. */
  73. public static function unregister(index:Object, scope:Object = null):void
  74. {
  75. if (scopeIndex[scope] == null) {
  76. scopeIndex[scope] = new Dictionary(true);
  77. }
  78. delete scopeIndex[scope][index];
  79. }
  80. /**
  81. * Retrieve data registered at the specified index and scope.
  82. *
  83. * @param index String or object identifier with which to
  84. * lookup registered data.
  85. * @param scope Optionally lookup data by a specific scope
  86. * identifier, a localized scope within the
  87. * global space.
  88. *
  89. * @return Registered data.
  90. *
  91. * @see #register
  92. */
  93. public static function lookup(index:Object, scope:Object = null):*
  94. {
  95. if (scope == null) {
  96. return scopeIndex[scope][index];
  97. }
  98. while (scope != null) {
  99. if (scopeIndex[scope] != null && index in scopeIndex[scope]) {
  100. return scopeIndex[scope][index];
  101. }
  102. if ("owner" in scope && scope["owner"] != null) {
  103. scope = scope["owner"];
  104. } else if ("parent" in scope) {
  105. if (scope["parent"] is Function) {
  106. scope = scope["parent"]();
  107. } else {
  108. scope = scope["parent"];
  109. }
  110. } else {
  111. return;
  112. }
  113. }
  114. }
  115. /**
  116. * @private
  117. * Possible deprecation.
  118. */
  119. public static function sync(target:Object, prop:String, index:Object, scope:Object = null):void
  120. {
  121. desync(target, prop);
  122. var syncDetail:Array = arguments;
  123. if (watcherByIndex[index] == null) {
  124. watcherByIndex[index] = [];
  125. }
  126. watcherByIndex[index].push(syncDetail);
  127. if (watcherByTarget[target] == null) {
  128. watcherByTarget[target] = {};
  129. }
  130. watcherByTarget[target][prop] = syncDetail;
  131. target[prop] = lookup(index, scope);
  132. }
  133. /**
  134. * @private
  135. * Possible deprecation.
  136. */
  137. public static function desync(target:Object, prop:String):void
  138. {
  139. var byTarget:Object = watcherByTarget[target];
  140. if (byTarget == null) {
  141. return;
  142. }
  143. var syncDetail:Array = byTarget[prop];
  144. if (syncDetail == null) {
  145. return;
  146. }
  147. var byIndex:Array = watcherByIndex[ syncDetail[2] ];
  148. byIndex.splice(byIndex.indexOf(syncDetail), 1);
  149. delete watcherByTarget[target][prop];
  150. }
  151. }
  152. }