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-state-component

v7.1.1

Published

State management for A-Frame using single global state modified through actions. State flows down to application via declarative binding.

Downloads

1,037

Readme

aframe-state-component

State management for A-Frame using single global state modified through actions. Features declarative bindings to easily bind state to application. By separating state from components and being able to bind state to component properties, components can be decoupled from the application, not needing to know about its state.

No dependencies and tailored for A-Frame. Bindings will only update their entities if relevant pieces of the global state are modified.

v2 requires A-Frame v0.8.0.

Examples

Usage

Defining State

The application state is a singleton defining an initial state and handler functions that modify the state.

AFRAME.registerState({
  initialState: {
    score: 0
  },

  handlers: {
    decreaseScore: function (state, action) {
      state.score -= action.points;
    },

    increaseScore: function (state, action) {
      state.score += action.points;
    }
  }
});

Best practices is to keep the state purely state (numbers, strings, booleans) and functions that act on the state. Don't store or work upon entities in state (store IDs instead). State should be serializable and bindable.

Modifying State

To update the state, we can dispatch an action using an event:

AFRAME.scenes[0].emit('increaseScore', {points: 50});

// Or manually dispatched:
// AFRAME.scenes[0].systems.state.dispatch('increaseScore', {points: 50});

The binding components will automatically and selectively update the entities in response to state changes.

A stateupdate event will be fired, but we probably don't need to use it. The event might later be useful if we develop a debugging front-end for the state.

Binding State

Then we can declarative bind pieces of the state into the A-Frame application with the bind component. The components will be automatically updated when the state changes.

<a-scene>
  <a-entity bind__text="value: app.score"></a-entity>
  <!-- Or <a-entity bind="text.value: score"> -->
</a-scene>

There's a bind-toggle component which will attach and detach a component entirely based on a boolean value.

<a-entity bind-toggle__raycastable="isRaycastable"></a-entity>

Expressions

Simple expressions are supported in binding.

NOT (!) and BOOL (!!):

<a-entity bind="visible: !enabled"></a-entity>
<a-entity bind__visible="!!enabled"></a-entity>

OR (||) and AND (&&):

<a-entity bind__menu="active: menuOpened && controllerEnabled"></a-entity>
<a-entity bind__gun="enabled: gunGrabbed && !!bullets || isRobot"></a-entity>

Comparisons (==, ===, !=, !==):

<a-entity bind__fly="enabled: animal === 'bird'"></a-entity>
<a-entity bind__legs="enabled: animal !== 'snake'"></a-entity>

Rendering Lists

The state component comes with a bind-for component that can render an array in state from a template. Say we have an array in state (currently must be at the root of the state):

AFRAME.registerState({
  initialState: {
    shoppingList: [
      {name: 'milk', amount: 2},
      {name: 'eggs', amount: 12}
    ]
  }
});

Note when updating the array in state, use array methods. Don't rewrite the array because the state component has wrapped the array to detect changes.

We use bind-for. We provide for (the iterator variable name), in (pointer to the array in state), and name (name of key representing unique identifier between every element). Then we have a <template> which will be used to render each individual item.

Then we can bind properties to the individual array element either using the bind-item component, using the for value as the pointer (i.e., shoppingItem.name). Or we can use braces ({{ }}), which the bind-for component will statically interpolate the variable. We are moving to updateInPlace: true which will be more performant and caches entities:

<a-entity bind-for="for: item; in: shoppingList; key: name; updateInPlace: true">
  <template>
    <a-entity bind-item__text="value: item.name"
              data-amount="{{ amount }}"></a-entity>
  </template>
</a-entity>

This will result in:

<a-entity bind-for="for: item; in: shoppingList; key: name">
  <template>
    <!-- ... -->
  </template>
  <a-entity bind-item__text="value: item.name"
            data-amount="2"
            text="value: milk"
            data-bind-for-key="milk"></a-entity>
  <a-entity bind__text="value: item.name"
            bind-item__data-amount="item.amount"
            text="value: eggs"
            data-amount="12"
            data-bind-for-key="eggs"></a-entity>
</a-entity>

bind-for will automatically render new entities, deactivate old entities respective to changes in the array in the state. Updates are handled through individual binds.

Detecting Changes in Arrays

Arrays are references so the state system tries to detect when a change is made before re-rendering. Changes to the array itself are detecting by wrapping array methods and setting the __dirty flag.

Although detecting changes to objects within an array is a bit harder. For now, I developed a dirty solution. If you touch an object within an array, set state.myArray.__dirty = true;. Perhaps in the future, we will have dedicated methods for modifying arrays and objects in a way the state component can detect.

Computed State

To attach additional computed state after the action is processed, specify a computeState function to update the state:

AFRAME.registerState({
  // ...

  computeState: function (newState, payload) {
    newState.isRedOrBlue = newState.isRed || newState.isBlue;
  }
});

Utils

The component exports utilities to help organize the state definition by helping to split up handlers and compute state functions into separate modules.

composeHandlers (handlers1, handlers2, ...)

Combines together objects containing handlers. If two objects have a handler function for the same action name, those two handler functions will be composed together.

var composeHandlers = require('aframe-state-component').composeHandlers;
AFRAME.registerState({
  computeHandlers: composeHandlers(
    {
      quxAction: state => { state.counter++; }
      qazAction: state => { state.counter--; }
    },
    require('./fooState').handlers,
    require('./barState').handlers
  )
});

composeFunctions (fn1, fn2, ...)

Combines functions together. Useful for combining multiple computeState functions together.

var composeFunctions = require('aframe-state-component').composeFunctions;
AFRAME.registerState({
  computeState: composeFunctions(
    require('./fooState').composeState,
    require('./barState').composeState
  )
});

Optimizing State

If you are storing objects in state that don't need to be checked for changes nor binded to entities, then you can optimize the state component on updates as it checks for changes. By setting nonBindedStateKeys array, on state updates, these keys will be skipped when doing state diffing and copying.

AFRAME.registerState({
  nonBindedStateKeys: ['starMap'],

  state: {
    starMap: {
      alphaCentari: {
        // ...
      },
      // ...
    }
  }
});

Installation

Browser

Install and use by directly including the browser files:

<head>
  <title>My A-Frame Scene</title>
  <script src="https://aframe.io/releases/1.0.4/aframe.min.js"></script>
  <script src="https://unpkg.com/[email protected]/dist/aframe-state-component.min.js"></script>
  <script>
    AFRAME.registerState({
      initialState: {
        enemyPosition: {x: 0, y: 1, z: 2}
      },

      handlers: {
        enemyMoved: function (state, action) {
          state.enemyPosition = action.newPosition;
        }
      },
    });
  </script>
</head>
<body>
  <a-scene>
    <a-entity bind__position="enemyPosition"></a-entity>
  </a-scene>
</body>

npm

Install via npm:

npm install aframe-state-component

Then register and use.

require('aframe');
require('aframe-state-component');