kutility.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /// Kernel utility functions and objects.
  7. ///
  8. /// @module vwf/kernel/utility
  9. class KUtility {
  10. constructor() {
  11. //console.log("Kernel Utility constructor");
  12. /// ID of the pseudo node that appears as the parent of the simulation's global nodes.
  13. ///
  14. /// @field
  15. this.globalRootID = 0
  16. /// The URI of the VWF proto-prototype node `node.vwf`. `protoNodeDescriptor` contains the
  17. /// descriptor associated with this URI.
  18. ///
  19. /// @field
  20. this.protoNodeURI = "proxy/node.vwf"
  21. /// The component descriptor of the VWF proto-prototype node `node.vwf`.
  22. ///
  23. /// @field
  24. this.protoNodeDescriptor = {
  25. extends: null
  26. } // TODO: detect protoNodeDescriptor in createChild() a different way and remove this explicit null prototype
  27. /// The key prototype for application values that reference nodes.
  28. ///
  29. /// Application values that reference VWF nodes are objects of the form `{ id: id }` that also
  30. /// extend this object. Application values include property values, method parameters and
  31. /// results, and event listener parameters.
  32. ///
  33. /// `nodeReferencePrototype` serves as a key to distinguish real node references from other
  34. /// arbitrary values.
  35. ///
  36. /// @field
  37. this.nodeReferencePrototype = {}
  38. }
  39. /// Wrap `nodeID` in an object in such a way that it can stand in for a node reference
  40. /// without being confused with any other application value. The returned object will
  41. /// contain `nodeID` in the `id` field. `valueIsNodeReference` may be used to determine if
  42. /// an arbitrary value is such a node reference.
  43. ///
  44. /// @param {ID} nodeID
  45. ///
  46. /// @returns {Object}
  47. nodeReference(nodeID) {
  48. return Object.create(this.nodeReferencePrototype, {
  49. id: {
  50. value: nodeID
  51. } // TODO: same wrapper for same id so that === works
  52. });
  53. }
  54. /// Determine if a value is a node reference. If it is, it will contain the `nodeID` in the
  55. /// `id` field.
  56. ///
  57. /// @param {Object} value
  58. ///
  59. /// @returns {Boolean}
  60. valueIsNodeReference(value) {
  61. return this.nodeReferencePrototype.isPrototypeOf(value);
  62. }
  63. }
  64. export {
  65. KUtility
  66. }