npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aframe-event-decorators

v1.0.2

Published

Decorate component functions to have them automatically subscribe/unsubscribe to events.

Downloads

55

Readme

aframe-event-decorators

Version License

Decorate component functions to have them automatically subscribe/unsubscribe to events.

For A-Frame.

API

Decorate your functions in your Component definitions to make them bind and unbind from events automatically. With this package there is no need to call addEventListener/removeEventListener, just create normal functions in your components augmented with a decorator functor.

Decorate your functions before calling AFRAME.registerComponent. Here is the simplest example:

const bindEvent = require('aframe-event-decorators').bindEvent;

AFRAME.registerComponent("foo", {
  componentchanged: bindEvent( function(evt) {
    console.log(evt.detail);
  })
})

By default the function will be bound to events corresponding to its property name, in this case: 'componentchanged'. It will listen for events on its parent element, and will begin listening or end listening when init or remove is called. However this can also be configured by passing a configuration object:

AFRAME.registerComponent("foo", {
  whenSceneLoads: bindEvent( {
    event: "loaded",    // Event to listen for. Defaults to functions propery name.
    target: "a-scene",  // Selector string for which element to listen on, defaults to this.el
    listenIn: "init",   // Which function addEventListener is called in, defaults to 'init'
    removeIn: "remove", // Which function removeEventListener is called in, defaults to 'remove'
  }, function(evt) {
    console.log(evt.detail);
  })
})

There is also the bindEventPlayPause convenience decorator which will always bind/unbind in play and pause respectively.

Functions will only be bound to events when a new component is created. Decorating a function with bindEvent() in a components init, or tick functions for example will have no effect. Don't bind to arrow functions because they don't have their own this attribute.

Installation

npm

Install via npm:

npm install aframe-event-decorators

Then require and use.

require('aframe');
require('aframe-event-decorators');

Create your own decorators

This next bit may or may not be useful to anyone. You dont need to know any of what follows to use the event decorators.

I've also exposed an abstract component decorator system so you can create your own decorators. These decorators are special because they are only executed when a component is instantiated, with their this attribute set to the new instance. The event binding decorators are built on top of this. Here's an example:

const decorate = require('aframe-event-decorators').decorate;

// It's useful to wrap the Functor object to create a closure with any data, such as this 'message' parameter.
function sayHello(message, targetFunction) {

  // This function will be executed when the component which has a decorated function is instanciated. 'this' will be set
  // to the instance and 'funcPropertyName' is the property name which maps to the decorated function. The functor should
  // return a function.
  function Functor(funcPropertyName) {
    const func = this[funcPropertyName]; // 'this' is assigned to the component instance.
    // Functor should return a function
  	return function() {
  	  console.log(this.el.id + " has a decorated function with message: " + message);
  	  return func.apply(this, arguments);
  	}
  }

  return decorate(
  	targetFunction,	// The component function to decorate
  	functor         // The functor which is executed on the target function.
  )
}

// Now our new decorator 'sayHello' is ready to be used in a Component definition.
AFRAME.registerComponent("foo", {
  play: sayHello("flimflam", function(){
  	console.log("component is playing.");
  })
})

// Whenever the 'foo' component is added to an Entity its play function is augmented with a message that has details
// about the component instance!