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

transitions

v2.0.2

Published

promise-based transition util

Downloads

76

Readme

transitions

experimental

Using promises for cleaner animation and transitional states.

This handles views which are abstract features of any UI or visual application (buttons, effects, entire pages, etc).

A view might look like this:

var view = {
	create: function(data) {
		//e.g. render a template with data
	},
	show: function(data) {
		//e.g. animate in a DIV
		return Promise.delay(1000)
	},
	hide: function(data) {
		//e.g. animate out a DIV
	}
}

Now you can sequence events for this view by using transitions:

//get a "state" which gracefully wraps missing functions
var state = transition(view, { /* template data */ }) 

//first we initialize the state
state.create()
	//then we can animate it in
	.then(state.show)
	//and do some stuff while it's visible
	.then(function() {
		return Promise.delay(1000)
	})
	//and then animate out
	.then(state.hide)
	//and dispose of it
	.then(state.dispose)
	//and handle the callback when it's all done
	.then(function() {
		t.ok(true, 'async finished')
	})

views

A "view" is just an object which may or may not expose any of the following asynchronous methods:

  • create - called to instantiate the element
  • show - called to show (animate in) the element
  • hide - called to hide (animate out) the element
  • dispose - called to dispose/destroy the element

Usage

NPM

transitions(view[, data])

Returns a new object with the functions create, show, hide, dispose. Calling any of them will return a promise for that view's function (or a fulfilled Promise if none exist).

transition(view, { name: 'foo' }).create()
	.then(function() {
		console.log("view created")
	})

transitions.create(view[, data])

transitions.show(view[, data])

transitions.hide(view[, data])

transitions.dispose(view[, data])

Calls a view's create(), show(), etc functions with the specified data. If the view doesn't define the function, this will return a resolved Promise so it can be treated in the same way.

transitions.all(views[, data])

This is a convenience function to handle an array of views (or a single view) in parallel. The same can be achieved with map and Promise.all. Simple example:

//say we are sending our "app" context as data to the views
var states = transitions.all(views, app)

states.create()
	.then( states.show )
	.then( app.doSomethingCool )
	.then( states.hide )
	.then( states.dispose )

If views is not an array, it will be made into a single-element array.

examples

See the test.js and demo for more examples of animating in parallel and in series.

The real beauty comes from composing transitions together in a functional manner. For example, a typical "carousel" might requires previous states to animate out and be disposed before animating in the next state.

function carousel(prev, next) {
	//previous and next views, or "dummy" views if they don't exist
	var prevState = Transition(prev||{}, this)
	var nextState = Transition(next||{}, this)

	//sequencing
	return prevState.hide()
		.then(prevState.dispose)
		.then(nextState.create)
		.then(nextState.show)
}

coursel(views[i], views[i+1])
	.then(doSomething)

License

MIT, see LICENSE.md for details.