object.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
  5. */
  6. /// vwf/model/object.js is a backstop property store.
  7. ///
  8. /// @module vwf/model/object
  9. /// @requires vwf/model
  10. /// @requires vwf/configuration
  11. import { Fabric } from '/core/vwf/fabric.js';
  12. class VWFObject extends Fabric{
  13. constructor(module) {
  14. console.log("Object constructor");
  15. super(module, "Model");
  16. }
  17. factory(){
  18. let _self_ = this;
  19. return this.load( this.module,
  20. {
  21. // == Module Definition ====================================================================
  22. // -- initialize ---------------------------------------------------------------------------
  23. initialize: function() {
  24. this.objects = {}; // maps id => { property: value, ... }
  25. 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 */ )?
  26. },
  27. // == Model API ============================================================================
  28. // -- creatingNode -------------------------------------------------------------------------
  29. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  30. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  31. // The kernel calls vwf/model/object's `creatingNode` multiple times: once at the start
  32. // of `createChild` so that we can claim our spot in the parent's children array before
  33. // doing any async operations, a second time after loading the prototype and behaviors,
  34. // then a third time in the normal order as the last driver.
  35. var parent = nodeID != 0 && this.objects[nodeID];
  36. var child = this.objects[childID];
  37. if ( ! child ) {
  38. // First time: initialize the node.
  39. child = this.objects[childID] = {
  40. id: childID,
  41. prototype: undefined,
  42. behaviors: undefined,
  43. source: childSource,
  44. type: childType,
  45. uri: parent ? undefined : childIndex,
  46. name: childName,
  47. properties: {},
  48. methods: {},
  49. events: {},
  50. parent: undefined,
  51. children: [],
  52. sequence: 0, // counter for child ID assignments
  53. prng: parent ? // pseudorandom number generator, seeded by ...
  54. new Alea( JSON.stringify( parent.prng.state ), childID ) : // ... the parent's prng and the child ID, or
  55. new Alea( vwf.configuration["random-seed"], childID ), // ... the global seed and the child ID
  56. // TODO: The 'patches' object is in the process of moving to the kernel
  57. // Those objects that are double-commented out have already moved
  58. // Those that are single-commented out have yet to move.
  59. // Change list for patchable objects. This comment shows the structure of the
  60. // object, but it is created later dynamically as needed
  61. // patches: {
  62. // // root: true, // node is the root of the component -- moved to kernel's node registry
  63. // // descendant: true, // node is a descendant still within the component -- moved to kernel's node registry
  64. // internals: true, // random, seed, or sequence has changed
  65. // // properties: true, // placeholder for a property change list -- moved to kernel's node registry
  66. // // methods: [], // array of method names for methods that changed -- moved to kernel's node registry
  67. // },
  68. // END TODO
  69. initialized: false,
  70. };
  71. // Connect to the parent.
  72. if ( parent ) {
  73. child.parent = parent;
  74. parent.children[childIndex] = child;
  75. }
  76. // Create the `patches` field for tracking changes if the node is patchable (if it's
  77. // the root or a descendant in a component).
  78. if ( child.uri ) {
  79. child.patches = { /* root: true */ };
  80. } else if ( parent && ! parent.initialized && parent.patches ) {
  81. child.patches = { /* descendant: true */ };
  82. }
  83. } else if ( ! child.prototype ) {
  84. // Second time: fill in the prototype and behaviors.
  85. child.prototype = childExtendsID && this.objects[childExtendsID];
  86. child.behaviors = ( childImplementsIDs || [] ).map( function( childImplementsID ) {
  87. return this.objects[childImplementsID];
  88. }, this );
  89. } else {
  90. // Third time: ignore since nothing is new.
  91. }
  92. },
  93. // -- initializingNode ---------------------------------------------------------------------
  94. initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  95. childSource, childType, childIndex, childName ) {
  96. this.objects[childID].initialized = true;
  97. },
  98. // -- deletingNode -------------------------------------------------------------------------
  99. deletingNode: function( nodeID ) {
  100. var child = this.objects[nodeID];
  101. var object = child.parent;
  102. if ( object ) {
  103. var index = object.children.indexOf( child );
  104. if ( index >= 0 ) {
  105. object.children.splice( index, 1 );
  106. }
  107. child.parent = undefined;
  108. }
  109. delete this.objects[nodeID];
  110. },
  111. // -- addingChild --------------------------------------------------------------------------
  112. addingChild: function( nodeID, childID, childName ) { // ... doesn't validate arguments or check for moving to/from 0 // TODO: not for global anchor node 0
  113. var object = this.objects[nodeID];
  114. var child = this.objects[childID];
  115. child.parent = object;
  116. object.children.push( child );
  117. },
  118. // -- removingChild ------------------------------------------------------------------------
  119. removingChild: function( nodeID, childID ) { // ... doesn't validate arguments or check for moving to/from 0
  120. var object = this.objects[nodeID];
  121. var child = this.objects[childID];
  122. child.parent = undefined;
  123. object.children.splice( object.children.indexOf( child ), 1 );
  124. },
  125. // TODO: creatingProperties, initializingProperties
  126. // -- settingProperties --------------------------------------------------------------------
  127. settingProperties: function( nodeID, properties ) {
  128. var object = this.objects[nodeID];
  129. if ( ! object ) return;
  130. // 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. _self_.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. _self_.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. }
  339. /// Merge fields from the `source` objects into `target`.
  340. merge( target /* [, source1 [, source2 ... ] ] */ ) {
  341. for ( var index = 1; index < arguments.length; index++ ) {
  342. var source = arguments[index];
  343. Object.keys( source ).forEach( function( key ) {
  344. target[key] = source[key];
  345. } );
  346. }
  347. return target;
  348. }
  349. }
  350. export {
  351. VWFObject as default
  352. }