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

lifecycle.js

v1.0.0

Published

JavaScript lifecycle manager.

Downloads

11

Readme

Lifecycle.js

Build Status Downloads Version

JavaScript lifecycle manager.

Table of contents

Main

dist/
├── lifecycle.js        (UMD)
├── lifecycle.min.js    (UMD, compressed)
├── lifecycle.common.js (CommonJS, default)
└── lifecycle.esm.js    (ES Module)

Getting started

Installation

npm install lifecycle.js

Usage

Syntax

new Lifecycle(target, stages[, options])
  • target

    • Type: Object
    • The lifecycle target.
  • stages

    • Type: Array
    • The lifecycle stages.
  • options (optional)

    • Type: Object
    • The lifecycle options. Check out the available options.

Example

const target = {};
const stages = ['created', 'mounted', 'destroyed'];
const lifecycle = new Lifecycle(target, stages, {
  autoEmit: true,
});

lifecycle.on('created', () => {
  console.log('created');
}).start();

lifecycle.on('mounted', () => {
  console.log('mounted');
}).next();

lifecycle.on('destroyed', () => {
  console.log('destroyed');
}).end();

⬆ back to top

Options

autoStart

  • Type: Boolean
  • Default: false

Indicate if start the lifecycle automatically when initialized or not.

autoEmit

  • Type: Boolean
  • Default: false

Indicate if execute the lifecycle stage's hooks when stage changed or not.

autoEnd

  • Type: Boolean
  • Default: false

Indicate if end the lifecycle automatically when the current stage is the last one or not.

⬆ back to top

Methods

If a method doesn't need to return any value, it will return the lifecycle instance (this) for chain composition.

start()

Start the lifecycle.

end()

End the lifecycle.

prev()

Move to the previous lifecycle stage.

next()

Move to the next lifecycle stage.

goto(stage)

  • stage:
    • Type: String
    • The target stage.

Go to the given stage.

on(stage[, hook, [options]])

  • stage:
    • Type: String, Array or Object
    • The target stage(s).
  • hook:
    • Type: Function or Array
    • The hook(s) to add.
    • This is optional when the first argument is an object.
  • options (optional):
    • Type: Object
    • Properties:
      • once:
        • Type: Boolean
        • Default: false
        • Indicate if the hook(s) will only be executed once or not.
      • prepend:
        • Type: Boolean
        • Default: false
        • Indicate if prepend the current hook(s) to the stage hook list or not.
        • If it is set to true, the current hook(s) will be executed first before others when emitted.

Add hook(s) to the given stage(s).

lifecycle.on('created', () => {});
lifecycle.on('mounted updated', () => {});
lifecycle.on(['mounted', 'updated'], () => {});
lifecycle.on('created', () => {}, {
  prepend: true,
});
lifecycle.on('mounted', [() => {}, () => {}], {
  once: true,
});
lifecycle.on({
  created() {},
  mounted: [() => {}, () => {}],
}, {
  once: true,
});

once(stage[, hook, [options]])

  • stage:
    • Type: String, Array or Object
    • The target stage(s).
  • hook:
    • Type: Function or Array
    • The hook(s) to add.
    • This is optional when the first argument is an object.
  • options (optional):
    • Type: Object
    • Properties:
      • prepend:
        • Type: Boolean
        • Default: false
        • Indicate if prepend the current hook(s) to the existing hook list or not.
        • If it is set to true, the current hook(s) will be executed first before others when emitted.

Add hook(s) to the given stage(s) and remove the hook(s) when emitted.

off(stage[, hook])

  • stage:
    • Type: String, Array or Object
    • The target stage(s).
  • hook:
    • Type: Function or Array
    • The hook(s) to remove.
    • This is optional when the first argument is an object.

Remove hook(s) from the given stage(s).

emit(stage, ...args)

  • stage:
    • Type: String or Array
    • The target stage(s).
  • args:
    • Type: Array
    • The parameters for passing to each hook.
  • (return value):
    • If only one hook is executed, then the return value of the hook will be returned.
    • If more than one hook is executed, then an array with the return values of the hooks will be returned.

Emit hook(s) on the given stage(s).

lifecycle.once('created', (...args) => {
  console.log(args);
  // > [3, 1, 2]
  return args.sort().pop();
});

console.log(lifecycle.emit('created', 3, 1, 2));
// > 3

lifecycle.once('updated', [
  (...args) => args.sort().pop(),
  (...args) => args.sort().pop(),
]);

console.log(lifecycle.emit('updated', 3, 1, 2));
// > [3, 3]

is(stage)

  • stage:
    • Type: String
    • The target stage.
  • (return value):
    • Type: Boolean

Check if the given stage is the current active stage.

if (lifecycle.is('mounted')) {
  // do something...
}

⬆ back to top

Browser and Node.js support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Opera (latest)
  • Edge (latest)
  • Internet Explorer 9+
  • Node.js 6+

Contributing

Please read through our contributing guidelines.

Versioning

Maintained under the Semantic Versioning guidelines.

License

MIT © Chen Fengyuan

⬆ back to top