object.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. "use strict";
  2. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  3. // Secretary of Defense (Personnel & Readiness).
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the License
  11. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  12. // or implied. See the License for the specific language governing permissions and limitations under
  13. // the License.
  14. /// vwf/model/object.js is a backstop property store.
  15. ///
  16. /// @module vwf/model/object
  17. /// @requires vwf/model
  18. /// @requires vwf/configuration
  19. define( [ "module", "vwf/model", "vwf/utility", "vwf/configuration" ],
  20. function( module, model, utility, configuration ) {
  21. return model.load( module, {
  22. // == Module Definition ====================================================================
  23. // -- initialize ---------------------------------------------------------------------------
  24. initialize: function() {
  25. this.objects = {}; // maps id => { property: value, ... }
  26. this.creatingNode( undefined, 0 ); // global root // TODO: to allow vwf.children( 0 ), vwf.getNode( 0 ); is this the best way, or should the kernel createNode( global-root-id /* 0 */ )?
  27. },
  28. // == Model API ============================================================================
  29. // -- creatingNode -------------------------------------------------------------------------
  30. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  31. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  32. // The kernel calls vwf/model/object's `creatingNode` multiple times: once at the start
  33. // of `createChild` so that we can claim our spot in the parent's children array before
  34. // doing any async operations, a second time after loading the prototype and behaviors,
  35. // then a third time in the normal order as the last driver.
  36. var parent = nodeID != 0 && this.objects[nodeID];
  37. var child = this.objects[childID];
  38. if ( ! child ) {
  39. // First time: initialize the node.
  40. child = this.objects[childID] = {
  41. id: childID,
  42. prototype: undefined,
  43. behaviors: undefined,
  44. source: childSource,
  45. type: childType,
  46. uri: parent ? undefined : childIndex,
  47. name: childName,
  48. properties: {},
  49. methods: {},
  50. events: {},
  51. parent: undefined,
  52. children: [],
  53. sequence: 0, // counter for child ID assignments
  54. prng: parent ? // pseudorandom number generator, seeded by ...
  55. new Alea( JSON.stringify( parent.prng.state ), childID ) : // ... the parent's prng and the child ID, or
  56. new Alea( configuration.active["random-seed"], childID ), // ... the global seed and the child ID
  57. // TODO: The 'patches' object is in the process of moving to the kernel
  58. // Those objects that are double-commented out have already moved
  59. // Those that are single-commented out have yet to move.
  60. // Change list for patchable objects. This comment shows the structure of the
  61. // object, but it is created later dynamically as needed
  62. // patches: {
  63. // // root: true, // node is the root of the component -- moved to kernel's node registry
  64. // // descendant: true, // node is a descendant still within the component -- moved to kernel's node registry
  65. // internals: true, // random, seed, or sequence has changed
  66. // // properties: true, // placeholder for a property change list -- moved to kernel's node registry
  67. // // methods: [], // array of method names for methods that changed -- moved to kernel's node registry
  68. // },
  69. // END TODO
  70. initialized: false,
  71. };
  72. // Connect to the parent.
  73. if ( parent ) {
  74. child.parent = parent;
  75. parent.children[childIndex] = child;
  76. }
  77. // Create the `patches` field for tracking changes if the node is patchable (if it's
  78. // the root or a descendant in a component).
  79. if ( child.uri ) {
  80. child.patches = { /* root: true */ };
  81. } else if ( parent && ! parent.initialized && parent.patches ) {
  82. child.patches = { /* descendant: true */ };
  83. }
  84. } else if ( ! child.prototype ) {
  85. // Second time: fill in the prototype and behaviors.
  86. child.prototype = childExtendsID && this.objects[childExtendsID];
  87. child.behaviors = ( childImplementsIDs || [] ).map( function( childImplementsID ) {
  88. return this.objects[childImplementsID];
  89. }, this );
  90. } else {
  91. // Third time: ignore since nothing is new.
  92. }
  93. },
  94. // -- initializingNode ---------------------------------------------------------------------
  95. initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  96. childSource, childType, childIndex, childName ) {
  97. this.objects[childID].initialized = true;
  98. },
  99. // -- deletingNode -------------------------------------------------------------------------
  100. deletingNode: function( nodeID ) {
  101. var child = this.objects[nodeID];
  102. var object = child.parent;
  103. if ( object ) {
  104. var index = object.children.indexOf( child );
  105. if ( index >= 0 ) {
  106. object.children.splice( index, 1 );
  107. }
  108. child.parent = undefined;
  109. }
  110. delete this.objects[nodeID];
  111. },
  112. // -- addingChild --------------------------------------------------------------------------
  113. addingChild: function( nodeID, childID, childName ) { // ... doesn't validate arguments or check for moving to/from 0 // TODO: not for global anchor node 0
  114. var object = this.objects[nodeID];
  115. var child = this.objects[childID];
  116. child.parent = object;
  117. object.children.push( child );
  118. },
  119. // -- removingChild ------------------------------------------------------------------------
  120. removingChild: function( nodeID, childID ) { // ... doesn't validate arguments or check for moving to/from 0
  121. var object = this.objects[nodeID];
  122. var child = this.objects[childID];
  123. child.parent = undefined;
  124. object.children.splice( object.children.indexOf( child ), 1 );
  125. },
  126. // TODO: creatingProperties, initializingProperties
  127. // -- settingProperties --------------------------------------------------------------------
  128. settingProperties: function( nodeID, properties ) {
  129. var object = this.objects[nodeID];
  130. if ( ! object ) return; // TODO: patch until full-graph sync is working; drivers should be able to assume that nodeIDs refer to valid objects
  131. var node_properties = object.properties;
  132. for ( var propertyName in properties ) { // TODO: since undefined values don't serialize to json, interate over node_properties (has-own only) instead and set to undefined if missing from properties?
  133. if ( ! node_properties.hasOwnProperty( propertyName ) ) {
  134. this.kernel.setProperty( nodeID, propertyName, properties[propertyName] );
  135. } // TODO: this needs to be handled in vwf.js for setProperties() the way it's now handling setProperty() create vs. initialize vs. set
  136. node_properties[propertyName] = properties[propertyName];
  137. }
  138. return node_properties;
  139. },
  140. // -- gettingProperties --------------------------------------------------------------------
  141. gettingProperties: function( nodeID, properties ) {
  142. return this.objects[nodeID].properties;
  143. },
  144. // -- creatingProperty ---------------------------------------------------------------------
  145. creatingProperty: function( nodeID, propertyName, propertyValue ) {
  146. return this.initializingProperty( nodeID, propertyName, propertyValue );
  147. },
  148. // -- initializingProperty -----------------------------------------------------------------
  149. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  150. return this.settingProperty( nodeID, propertyName, propertyValue );
  151. },
  152. // TODO: deletingProperty
  153. // -- settingProperty ----------------------------------------------------------------------
  154. settingProperty: function( nodeID, propertyName, propertyValue ) {
  155. var object = this.objects[nodeID];
  156. return object.properties[propertyName] = propertyValue;
  157. },
  158. // -- gettingProperty ----------------------------------------------------------------------
  159. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  160. return this.objects[nodeID].properties[propertyName];
  161. },
  162. // -- creatingMethod -----------------------------------------------------------------------
  163. creatingMethod: function( nodeID, methodName, methodParameters, methodBody ) {
  164. return this.settingMethod( nodeID, methodName,
  165. { parameters: methodParameters, body: methodBody } );
  166. },
  167. // -- settingMethod ------------------------------------------------------------------------
  168. settingMethod: function( nodeID, methodName, methodHandler ) {
  169. return this.objects[nodeID].methods[methodName] = methodHandler;
  170. },
  171. // -- gettingMethod ------------------------------------------------------------------------
  172. gettingMethod: function( nodeID, methodName ) {
  173. return this.objects[nodeID].methods[methodName];
  174. },
  175. // -- addingEventListener ------------------------------------------------------------------
  176. addingEventListener: function( nodeID, eventName, eventListenerID, eventHandler, eventContextID, eventPhases ) {
  177. if ( ! this.objects[ nodeID ].events[ eventName ] ) {
  178. this.objects[ nodeID ].events[ eventName ] = {};
  179. }
  180. return this.settingEventListener( nodeID, eventName, eventListenerID,
  181. utility.merge( eventHandler, { context: eventContextID, phases: eventPhases } ) ) ?
  182. true : undefined;
  183. },
  184. // -- removingEventListener ----------------------------------------------------------------
  185. removingEventListener: function( nodeID, eventName, eventListenerID ) {
  186. var listeners = this.objects[ nodeID ].events[ eventName ];
  187. if ( listeners && listeners[ eventListenerID ] ) {
  188. delete listeners[ eventListenerID ];
  189. return true;
  190. }
  191. return undefined;
  192. },
  193. // -- settingEventListener -----------------------------------------------------------------
  194. settingEventListener: function( nodeID, eventName, eventListenerID, eventListener ) {
  195. return this.objects[ nodeID ].events[ eventName ][ eventListenerID ] = eventListener;
  196. },
  197. // -- gettingEventListener -----------------------------------------------------------------
  198. gettingEventListener: function( nodeID, eventName, eventListenerID ) {
  199. return this.objects[ nodeID ].events[ eventName ][ eventListenerID ];
  200. },
  201. // -- flushingEventListeners ---------------------------------------------------------------
  202. flushingEventListeners: function( nodeID, eventName, eventContextID ) {
  203. var listeners = this.objects[ nodeID ].events[ eventName ];
  204. Object.keys( listeners ).forEach( function( eventListenerID ) {
  205. if ( listeners[ eventListenerID ].context === eventContextID ) {
  206. delete listeners[ eventListenerID ];
  207. }
  208. } );
  209. },
  210. // == Special Model API ====================================================================
  211. // The kernel delegates the corresponding API calls exclusively to vwf/model/object without
  212. // calling any other models.
  213. // -- random -------------------------------------------------------------------------------
  214. random: function( nodeID ) {
  215. var object = this.objects[nodeID];
  216. object.initialized && object.patches && ( object.patches.internals = true );
  217. return object.prng();
  218. },
  219. // -- seed ---------------------------------------------------------------------------------
  220. seed: function( nodeID, seed ) {
  221. var object = this.objects[nodeID];
  222. object.initialized && object.patches && ( object.patches.internals = true );
  223. object.prng = new Alea( seed );
  224. },
  225. // -- intrinsics ---------------------------------------------------------------------------
  226. intrinsics: function( nodeID, result ) {
  227. var object = this.objects[nodeID];
  228. result = result || {};
  229. // TODO: extends and implements IDs
  230. result.source = object.source;
  231. result.type = object.type;
  232. return result;
  233. },
  234. // -- uri ----------------------------------------------------------------------------------
  235. uri: function( nodeID ) {
  236. var node = this.objects[ nodeID ];
  237. if ( node ) {
  238. return node.uri;
  239. } else {
  240. this.logger.warnx( "Could not find uri of nonexistent node: '" + nodeID + "'" );
  241. }
  242. },
  243. // -- name ---------------------------------------------------------------------------------
  244. name: function( nodeID ) {
  245. return this.objects[nodeID].name || "";
  246. },
  247. // -- prototype ----------------------------------------------------------------------------
  248. prototype: function( nodeID ) { // TODO: not for global anchor node 0
  249. var object = this.objects[nodeID];
  250. return object.prototype && object.prototype.id;
  251. },
  252. // -- behaviors ----------------------------------------------------------------------------
  253. behaviors: function( nodeID ) { // TODO: not for global anchor node 0
  254. var behaviors = this.objects[nodeID].behaviors;
  255. if ( behaviors ) {
  256. return behaviors.map( function( behavior ) {
  257. return behavior.id;
  258. } );
  259. } else {
  260. this.logger.warnx( "Node '" + nodeID + "' does not have a valid behaviors array" );
  261. }
  262. },
  263. // -- parent -------------------------------------------------------------------------------
  264. parent: function( nodeID, initializedOnly ) {
  265. var object = this.objects[ nodeID ];
  266. if ( object ) {
  267. return ( ! initializedOnly || object.initialized ) ?
  268. ( object.parent && object.parent.id || 0 ) : undefined;
  269. } else {
  270. this.logger.error( "Cannot find node: '" + nodeID + "'" );
  271. }
  272. },
  273. // -- children -----------------------------------------------------------------------------
  274. children: function( nodeID, initializedOnly ) {
  275. if ( nodeID === undefined ) {
  276. this.logger.errorx( "children", "cannot retrieve children of nonexistent node");
  277. return;
  278. }
  279. var node = this.objects[ nodeID ];
  280. if ( node ) {
  281. return node.children.map( function( child ) {
  282. return ( ! initializedOnly || child.initialized ) ?
  283. child.id : undefined;
  284. } );
  285. } else {
  286. this.logger.error( "Cannot find node: " + nodeID );
  287. }
  288. },
  289. // == Special utilities ====================================================================
  290. // The kernel depends on these utility functions but does not expose them directly in the
  291. // public API.
  292. // -- properties ---------------------------------------------------------------------------
  293. properties: function( nodeID ) {
  294. return this.objects[nodeID].properties;
  295. },
  296. // -- internals ----------------------------------------------------------------------------
  297. internals: function( nodeID, internals ) {
  298. var object = this.objects[nodeID];
  299. if ( !object ) {
  300. this.logger.errorx( "internals: object does not exist with id = '" + nodeID + "'" );
  301. return;
  302. }
  303. if ( internals ) { // set
  304. if ( internals.sequence !== undefined ) {
  305. object.sequence = internals.sequence;
  306. object.initialized && object.patches && ( object.patches.internals = true );
  307. }
  308. if ( internals.random !== undefined ) {
  309. merge( object.prng.state, internals.random );
  310. object.initialized && object.patches && ( object.patches.internals = true );
  311. }
  312. } else { // get
  313. internals = {};
  314. internals.sequence = object.sequence;
  315. internals.random = object.prng.state; // TODO: tag as Alea data
  316. }
  317. return internals;
  318. },
  319. // -- sequence -----------------------------------------------------------------------------
  320. sequence: function( nodeID ) {
  321. var object = this.objects[nodeID];
  322. object.initialized && object.patches && ( object.patches.internals = true );
  323. return object && ++object.sequence;
  324. },
  325. // -- patches ------------------------------------------------------------------------------
  326. patches: function( nodeID ) {
  327. return this.objects[nodeID].patches;
  328. },
  329. // -- exists -------------------------------------------------------------------------------
  330. exists: function( nodeID ) {
  331. return !! this.objects[nodeID];
  332. },
  333. // -- initialized --------------------------------------------------------------------------
  334. initialized: function( nodeID ) {
  335. return this.objects[nodeID].initialized;
  336. },
  337. } );
  338. /// Merge fields from the `source` objects into `target`.
  339. function merge( target /* [, source1 [, source2 ... ] ] */ ) {
  340. for ( var index = 1; index < arguments.length; index++ ) {
  341. var source = arguments[index];
  342. Object.keys( source ).forEach( function( key ) {
  343. target[key] = source[key];
  344. } );
  345. }
  346. return target;
  347. }
  348. } );