Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast GRAPH_DESERIALIZED to all components aswell. #21

Open
plojo opened this issue Sep 13, 2024 · 1 comment
Open

Broadcast GRAPH_DESERIALIZED to all components aswell. #21

plojo opened this issue Sep 13, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@plojo
Copy link
Collaborator

plojo commented Sep 13, 2024

If a ComponentScript needs to perform an action after the entire graph is deserialized, such as accessing nodes that are higher up or parallel in the hierarchy, it can do so by listening for the NODE_DESERIALIZED event. Inside the listener, it can then attach another listener for the GRAPH_DESERIALIZED event to its node. See example:

const hndNodeDeserialized: EventListenerUnified = () => {
  const hndGraphDeserialized: EventListenerUnified = () => {
    this.skeleton = Node.FIND(this, _serialization.skeleton); // do the work.
    this.node.removeEventListener(EVENT.GRAPH_DESERIALIZED, hndGraphDeserialized, true);
    this.removeEventListener(EVENT.NODE_DESERIALIZED, hndNodeDeserialized);
  };
  this.node.addEventListener(EVENT.GRAPH_DESERIALIZED, hndGraphDeserialized, true);
};
this.addEventListener(EVENT.NODE_DESERIALIZED, hndNodeDeserialized);

This approach introduces a lot of overhead for a simple functionality. Therefore, I propose also broadcasting the GRAPH_DESERIALIZED event to all components of all nodes. This would make it behave more similarly to the NODE_DESERIALIZED event. The example would then look something like this:

const hndGraphDeserialized: EventListenerUnified = () => {
  this.skeleton = Node.FIND(this, _serialization.skeleton); // do the work.
  this.removeEventListener(EVENT.GRAPH_DESERIALIZED, hndGraphDeserialized, true);
};
this.addEventListener(EVENT.GRAPH_DESERIALIZED, hndGraphDeserialized, true);

Or we can skip removing the listener, since GRAPH_DESERIALIZED is only dispatched once per graph. The example would look like this:

this.addEventListener(EVENT.GRAPH_DESERIALIZED, () => {
  this.skeleton = Node.FIND(this, _serialization.skeleton); // do the work.
}, true);
@plojo plojo added the enhancement New feature or request label Sep 13, 2024
@JirkaDellOro
Copy link
Member

The template for the ComponentScript shows this

      this.addEventListener(ƒ.EVENT.COMPONENT_ADD, this.hndEvent);
      this.addEventListener(ƒ.EVENT.COMPONENT_REMOVE, this.hndEvent);
      this.addEventListener(ƒ.EVENT.NODE_DESERIALIZED, this.hndEvent);
    }

    // Activate the functions of this component as response to events
    public hndEvent = (_event: Event): void => {
      switch (_event.type) {
        case ƒ.EVENT.COMPONENT_ADD:
          break;
        case ƒ.EVENT.COMPONENT_REMOVE:
          this.removeEventListener(ƒ.EVENT.COMPONENT_ADD, this.hndEvent);
          this.removeEventListener(ƒ.EVENT.COMPONENT_REMOVE, this.hndEvent);
          break;
        case ƒ.EVENT.NODE_DESERIALIZED:
          // if deserialized the node is now fully reconstructed and access to all its components and children is possible
         
            break;
      }
    }

Sticking to this pattern, one only needs to extend the switch at the end like this

      switch (_event.type) {
        ....
        case ƒ.EVENT.NODE_DESERIALIZED:
          // if deserialized the node is now fully reconstructed and access to all its components and children is possible
          this.node.addEventListener(EVENT.GRAPH_DESERIALIZED, this.hndEvent, true);
          break;
        case ƒ.EVENT.GRAPH_DESERIALIZED:
          this.skeleton = Node.FIND(this, _serialization.skeleton); // do the work. 
          break;
      }
    }

The number of automatic broadcasts should be kept as low as possible. I don't see the need in this particular case and once we start extending, we will probably find many other desirable broadcasts...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants