object.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. var object = this.objects[nodeID];
  143. if ( ! object ) return;
  144. return this.objects[nodeID].properties;
  145. },
  146. // -- creatingProperty ---------------------------------------------------------------------
  147. creatingProperty: function( nodeID, propertyName, propertyValue ) {
  148. return this.initializingProperty( nodeID, propertyName, propertyValue );
  149. },
  150. // -- initializingProperty -----------------------------------------------------------------
  151. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  152. return this.settingProperty( nodeID, propertyName, propertyValue );
  153. },
  154. // TODO: deletingProperty
  155. // -- settingProperty ----------------------------------------------------------------------
  156. settingProperty: function( nodeID, propertyName, propertyValue ) {
  157. var object = this.objects[nodeID];
  158. return object.properties[propertyName] = propertyValue;
  159. },
  160. // -- gettingProperty ----------------------------------------------------------------------
  161. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  162. var object = this.objects[nodeID];
  163. if ( ! object ) return;
  164. return this.objects[nodeID].properties[propertyName];
  165. },
  166. // -- creatingMethod -----------------------------------------------------------------------
  167. creatingMethod: function( nodeID, methodName, methodParameters, methodBody ) {
  168. return this.settingMethod( nodeID, methodName,
  169. { parameters: methodParameters, body: methodBody } );
  170. },
  171. // -- settingMethod ------------------------------------------------------------------------
  172. settingMethod: function( nodeID, methodName, methodHandler ) {
  173. return this.objects[nodeID].methods[methodName] = methodHandler;
  174. },
  175. // -- gettingMethod ------------------------------------------------------------------------
  176. gettingMethod: function( nodeID, methodName ) {
  177. return this.objects[nodeID].methods[methodName];
  178. },
  179. // -- addingEventListener ------------------------------------------------------------------
  180. addingEventListener: function( nodeID, eventName, eventListenerID, eventHandler, eventContextID, eventPhases ) {
  181. if ( ! this.objects[ nodeID ].events[ eventName ] ) {
  182. this.objects[ nodeID ].events[ eventName ] = {};
  183. }
  184. return this.settingEventListener( nodeID, eventName, eventListenerID,
  185. _self_.utility.merge( eventHandler, { context: eventContextID, phases: eventPhases } ) ) ?
  186. true : undefined;
  187. },
  188. // -- removingEventListener ----------------------------------------------------------------
  189. removingEventListener: function( nodeID, eventName, eventListenerID ) {
  190. var listeners = this.objects[ nodeID ].events[ eventName ];
  191. if ( listeners && listeners[ eventListenerID ] ) {
  192. delete listeners[ eventListenerID ];
  193. return true;
  194. }
  195. return undefined;
  196. },
  197. // -- settingEventListener -----------------------------------------------------------------
  198. settingEventListener: function( nodeID, eventName, eventListenerID, eventListener ) {
  199. return this.objects[ nodeID ].events[ eventName ][ eventListenerID ] = eventListener;
  200. },
  201. // -- gettingEventListener -----------------------------------------------------------------
  202. gettingEventListener: function( nodeID, eventName, eventListenerID ) {
  203. return this.objects[ nodeID ].events[ eventName ][ eventListenerID ];
  204. },
  205. // -- flushingEventListeners ---------------------------------------------------------------
  206. flushingEventListeners: function( nodeID, eventName, eventContextID ) {
  207. var listeners = this.objects[ nodeID ].events[ eventName ];
  208. Object.keys( listeners ).forEach( function( eventListenerID ) {
  209. if ( listeners[ eventListenerID ].context === eventContextID ) {
  210. delete listeners[ eventListenerID ];
  211. }
  212. } );
  213. },
  214. // == Special Model API ====================================================================
  215. // The kernel delegates the corresponding API calls exclusively to vwf/model/object without
  216. // calling any other models.
  217. // -- random -------------------------------------------------------------------------------
  218. random: function( nodeID ) {
  219. var object = this.objects[nodeID];
  220. object.initialized && object.patches && ( object.patches.internals = true );
  221. return object.prng();
  222. },
  223. // -- seed ---------------------------------------------------------------------------------
  224. seed: function( nodeID, seed ) {
  225. var object = this.objects[nodeID];
  226. object.initialized && object.patches && ( object.patches.internals = true );
  227. object.prng = new Alea( seed );
  228. },
  229. // -- intrinsics ---------------------------------------------------------------------------
  230. intrinsics: function( nodeID, result ) {
  231. var object = this.objects[nodeID];
  232. result = result || {};
  233. // TODO: extends and implements IDs
  234. result.source = object.source;
  235. result.type = object.type;
  236. return result;
  237. },
  238. // -- uri ----------------------------------------------------------------------------------
  239. uri: function( nodeID ) {
  240. var node = this.objects[ nodeID ];
  241. if ( node ) {
  242. return node.uri;
  243. } else {
  244. this.logger.warnx( "Could not find uri of nonexistent node: '" + nodeID + "'" );
  245. }
  246. },
  247. // -- name ---------------------------------------------------------------------------------
  248. name: function( nodeID ) {
  249. return this.objects[nodeID].name || "";
  250. },
  251. // -- prototype ----------------------------------------------------------------------------
  252. prototype: function( nodeID ) { // TODO: not for global anchor node 0
  253. var object = this.objects[nodeID];
  254. return object.prototype && object.prototype.id;
  255. },
  256. // -- behaviors ----------------------------------------------------------------------------
  257. behaviors: function( nodeID ) { // TODO: not for global anchor node 0
  258. var object = this.objects[nodeID];
  259. if ( ! object ) return;
  260. var behaviors = this.objects[nodeID].behaviors;
  261. if ( behaviors ) {
  262. return behaviors.map( function( behavior ) {
  263. return behavior.id;
  264. } );
  265. } else {
  266. this.logger.warnx( "Node '" + nodeID + "' does not have a valid behaviors array" );
  267. }
  268. },
  269. // -- parent -------------------------------------------------------------------------------
  270. parent: function( nodeID, initializedOnly ) {
  271. var object = this.objects[ nodeID ];
  272. if ( object ) {
  273. return ( ! initializedOnly || object.initialized ) ?
  274. ( object.parent && object.parent.id || 0 ) : undefined;
  275. } else {
  276. this.logger.error( "Cannot find node: '" + nodeID + "'" );
  277. }
  278. },
  279. // -- children -----------------------------------------------------------------------------
  280. children: function( nodeID, initializedOnly ) {
  281. if ( nodeID === undefined ) {
  282. this.logger.errorx( "children", "cannot retrieve children of nonexistent node");
  283. return;
  284. }
  285. var node = this.objects[ nodeID ];
  286. if ( node ) {
  287. return node.children.map( function( child ) {
  288. return ( ! initializedOnly || child.initialized ) ?
  289. child.id : undefined;
  290. } );
  291. } else {
  292. this.logger.error( "Cannot find node: " + nodeID );
  293. }
  294. },
  295. // == Special utilities ====================================================================
  296. // The kernel depends on these utility functions but does not expose them directly in the
  297. // public API.
  298. // -- properties ---------------------------------------------------------------------------
  299. properties: function( nodeID ) {
  300. return this.objects[nodeID].properties;
  301. },
  302. // -- internals ----------------------------------------------------------------------------
  303. internals: function( nodeID, internals ) {
  304. var object = this.objects[nodeID];
  305. if ( !object ) {
  306. this.logger.errorx( "internals: object does not exist with id = '" + nodeID + "'" );
  307. return;
  308. }
  309. if ( internals ) { // set
  310. if ( internals.sequence !== undefined ) {
  311. object.sequence = internals.sequence;
  312. object.initialized && object.patches && ( object.patches.internals = true );
  313. }
  314. if ( internals.random !== undefined ) {
  315. _self_.merge( object.prng.state, internals.random );
  316. object.initialized && object.patches && ( object.patches.internals = true );
  317. }
  318. } else { // get
  319. internals = {};
  320. internals.sequence = object.sequence;
  321. internals.random = object.prng.state; // TODO: tag as Alea data
  322. }
  323. return internals;
  324. },
  325. // -- sequence -----------------------------------------------------------------------------
  326. sequence: function( nodeID ) {
  327. var object = this.objects[nodeID];
  328. object.initialized && object.patches && ( object.patches.internals = true );
  329. return object && ++object.sequence;
  330. },
  331. // -- patches ------------------------------------------------------------------------------
  332. patches: function( nodeID ) {
  333. return this.objects[nodeID].patches;
  334. },
  335. // -- exists -------------------------------------------------------------------------------
  336. exists: function( nodeID ) {
  337. return !! this.objects[nodeID];
  338. },
  339. // -- initialized --------------------------------------------------------------------------
  340. initialized: function( nodeID ) {
  341. return this.objects[nodeID].initialized;
  342. },
  343. } );
  344. }
  345. /// Merge fields from the `source` objects into `target`.
  346. merge( target /* [, source1 [, source2 ... ] ] */ ) {
  347. for ( var index = 1; index < arguments.length; index++ ) {
  348. var source = arguments[index];
  349. Object.keys( source ).forEach( function( key ) {
  350. target[key] = source[key];
  351. } );
  352. }
  353. return target;
  354. }
  355. }
  356. export {
  357. VWFObject as default
  358. }