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

@simple-ui/stateful

v1.1.2

Published

A (in)finite stateful machine specialized in simplifying presentation logic

Downloads

10

Readme

Stateful

Coverage Status Build Status Dependency Status npm version

@simple-ui/stateful

A (in)finite state machine specialized in simplifying presentation logic.

The Stateful decorator can add state to any object. Typically an object would add a state variable or inherit state. With Stateful you add state capabilities to an object and manage that through a simple interface.

Install

Install with npm.

npm install --save @simple-ui/stateful

Install with bower.

bower install --save @simple-ui/stateful

Lodash Dependency

This library requires a small set of lodash. Use lodash-modularize to limit how much of lodash is included in your project.

Quick Usage

import Stateful from "@simple-ui/stateful"

var object = {
  value: 'no state',
  action: function() {
    return this.value
  }
};

var stateful = Stateful(object);
var state = {
  onInvoke() {
    // first time it is assigned for an object
  },
  onEnter() {
    // every time it is transitioned to
  },  
  onExit() {
    // every time it is transitioned away
  },
  onDecorate(hostObject) {
    const { action } = hostObject;
    return {
      value: 'state',
      action() {
        return action() + ' applied';
      }
    }
  }
};

object.action() === 'no state';
stateful.pushState(state);
object.action() === 'state applied';

stateful.current() === [state];
state === stateful.popState();

Usage

Stateful API

When creating a Stateful for a host object you manage the current set of states applied through the Stateful object itself.

var object = {
  action(a, b) {
    return a + b;
  }
};
var stateful = Stateful(object);

Once a Stateful object is created it can be used to transition to different states. Typically a state machine will benefit from allowing multiple states to be applied together. In this way, state definitions do not suffer from permutation explosion. For instance, instead of OpenWarningState and OpenSuccessState you can have OpenState, CloseState, WarningState, and SuccessState.

stateful.transition([stateA, stateB]);
stateful.transition(stateA);

A Stateful object can have states applied over time, if different aspects of an application control the state of an object.

stateful.transition([stateA, stateB]);
// ...is the same as...
stateful.popStates();
stateful.pushState(stateA);
stateful.pushState(stateB);
// ...is the same as...
stateful.popStates();
stateful.pushStates([stateA, stateB]);

A Stateful object remembers the transitions over time and can rewind state values, this is useful for blips where you apply a state and then return to your previous state. As an example, if you were building a game you may want to go into attach mode, but go back to whatever you were doing after danger the danger is gone. A Stateful object tracks your last state.

AttackState = {
  onEnter() {
    mediator.on('isEnemyFarAway', function() {
      // attack state does not know the last state, but it wants to end attacking
      stateful.rewind();
    });
  }
};
DetectEnemyState = {
  onEnter() {
    mediator.on('isEnemyNearby', function() {
      stateful.transition(AttackState);
    });
  }
};
stateful.transition([FarmState, DetectEnemyState]);

State API

A state is a normal object with the following methods.

onInvoke

A method called the first time a state is applied to an object.

onEnter

A method called every time a state is applied to an object.

onExit

A method called every time a state is unapplied to an object.

onDecorate

A method which returns an object to override values and functions on the host object.

Example

A state with all properties and a sample object.

var object = {
  a: 1,
  b: 2,
  action1(a, b) {
    return a + b;
  },
  action2(a, b) {
    return this.a + this.b;
  }
};

A state might change or override this object could look like this. The onEnter and onExit methods should be functional reflections.

var state = {
  onEnter() {
    this.a === 1;
    this.a = 2;
  },
  onExit() {
    this.a === 2;
    this.a = 1;
  }
}

A state may need to run setup functions once, with onInvoke.

var state = function() {
  this.used = 0;
  this.onInvoke = function() {
    this.used += 1;
  }
}

A state can alter the host object with onDecorate.

var state = {
  onDecorate(hostObject) {
    const { a } = hostObject;
    return {
      a(a, b) {
        // call the host method (this may have been altered by another state)
        return a(a, b) + a(a, b);
      },
      b() {
        // change the functionality completely
        return (this.a * 2) + (this.b * 2);
      }
    }
  }
}

License

MIT © mwjaworski

This software is released under the MIT license:

Copyright (c) 2017 mwjaworski [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.))