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

flyd-run

v1.1.0

Published

'Smarter' Streams with Flyd - run/error/catch functional handlers

Downloads

10

Readme

🏃 Flyd Run

Build Status GitHub issues Dependencies

"Smarter" flyd streams - run/error/catch functional handlers

What?

This is just a wrapper around flyd's stream API that adds a couple few new properties: run, error, and catch. These are largely inspired by mithril's new m.prop.

The nice part of this particular module is that it's fully compatible with the flyd API - opening the door to powerful constructs.

Why?

Having a common API on every stream in an application allows you to do wrap other functionality and always provide the same API surface. For example, you could wrap a fetch request like so:

// vastly simplified for clarity
// note that flyd streams absorb promises
function request(url, ops = {}) {
  const req = stream();

  fetch(url, ops).then((response) => {
    if (response.ok) {
      req.error(null);
      return req(response.json());
    }
    return req.error(response.json());
  }).catch((rejection) => {
    return req.error(rejection);
  });

  return req;
}

This would allow you to fetch requests and retreive responses via the stream API...

const req = request('/api/user/1');

req.run((response) => {
  store.save('user', response);
});

req.error.run((rejection) => {
  alert(rejection.message);
});

But how is that any different from using Promises?

This technique allows you to sustitute a fetch request behind the scene for a locally cached response - just wrap the cached data with the same stream call and you get the same API - that way your view code et-all doesn't need to worry about caches etc.

A simplified example...

const cacheObj = {};

export function load({ name, locale = 'en' }) {
  const requestUrl = `${API_URL}?document=${name}&locale=${locale}`;
  const storeKey = `${name}_${locale}`;

  return get(requestUrl, storeKey);
}

function cache(name, obj) {
  if (!name) {
    throw new Error('no cache identifier provided');
  }

  if (isDefined(obj)) {
    cacheObj[name] = obj;
  }

  return cacheObj[name];
}

function get(url, key) {
  const stored = cache(key);

  if (stored) {
    return stream(stored);
  }

  const req = request(url);

  req.run((item) => {
    cache(key, item);
  });

  return req.catch(() => {
    return cache(key, null);
  });
}

That way in the view/router/wherever you might just run...

import { load } from 'store';

const dataReq = load({ name: 'post123', locale: 'zh'});

dataReq.run((post) => {
  // send to view state
});

This abstracts the caching concern away from the view and allows you to quietly refactor and improve without worrying about the view-facing API.

Also, by their very nature, streams may represent data in different states and therefore may run more than once. (Promises fire only once, by contrast). This could be very desireable in live-updating applications (e.g. websocket responses).

Wait, isn't 'Run' just 'Map'?

It's certainly similar, but does two important things:

  • Guarantees to never fire the callback when the stream is pending (undefined value).
  • Sounds cooler, and is easier to understand in the context of async requests.