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

cyclejs-group

v1.0.0

Published

Utility for CycleJS framework for reducing boilerplate when creating groups of streams

Downloads

22

Readme

cyclejs-group

Utility for the Cycle.js framework for reducing boilerplate when creating many dependent streams.

Why may I need it?

Usually in a Cycle.js application or component you want to create more than one stream, especially for intent and model parts. It's 100% possible to do it with pure JS, but it requires a lot of boilerplate code when different streams depend on each other. This utility covers common case and makes creating complicated programs easier.

Example usage

Let's say, you want to create simple application, that allows you to add two numbers. With pure Cycle.js and cyclejs-stream you can do it like this:

import { applyToDOM, h, Rx } from 'cyclejs';
import createStream from 'cyclejs-stream';

let a$ = createStream((changeA$) =>
  changeA$
    .map(value => parseInt(value, 10))
    .filter(value => !isNaN(value))
    .startWith(1)
    .distinctUntilChanged()
);

let b$ = createStream((changeB$) =>
  changeB$
    .map(value => parseInt(value, 10))
    .filter(value => !isNaN(value))
    .startWith(1)
    .distinctUntilChanged()
);

let c$ = createStream((a$, b$) =>
  Rx.Observable.combineLatest(
    a$, b$,
    (a, b) =>
      a + b
));

let vtree$ = createStream((a$, b$, c$) =>
  Rx.Observable.combineLatest(
    a$, b$, c$,
    (a, b, c) =>
    h('form',
      h('fieldset', [
        h('legend', 'Add two numbers'),
        h('input#a', {
          type: 'number',
          value: a,
        }),
        h('input#b', {
          type: 'number',
          value: b,
        }),
        h('output', {
          value: c,
          htmlFor: 'a,b'
        })
      ])
    )
  )
);

let changeA$ = createStream((interactions) =>
   interactions
    .get('#a', 'input')
    .map(({ target }) => target.value)
);

let changeA$ = createStream((interactions) =>
  interactions
    .get('#b', 'input')
    .map(({ target }) => target.value)
);

applyToDOM('.js-container', (interactions) => {
  a$.inject(changeA$);
  b$.inject(changeB$);
  c$.inject(a$, b$);
  vtree$.inject(a$, b$, c$);
  interactions.inject(vtree$);
  changeA$.inject(interactions);
  changeB$.inject(interactions);

  return vtree$;
});

Seems easy for now, but when streams number grows, amount of boilerplate will grow proportionally. With createGroup you can achieve the same effect in more compact way and create batch of streams from plain functions. Thanks to inject method of the group, you can make streams form one group available for streams from another one. Connection is detected based on names of function parameters and keys of the group object. The whole concept of grouping streams can help with separation of concerns and increase readability of your code.

import { applyToDOM, h, Rx } from 'cyclejs';
import createGroup from 'cyclejs-group';

let model = createGroup({
  a$: (changeA$) =>
    changeA$
      .map(value => parseInt(value, 10))
      .filter(value => !isNaN(value))
      .startWith(1)
      .distinctUntilChanged()
  b$: (changeB$) =>
    changeB$
      .map(value => parseInt(value, 10))
      .filter(value => !isNaN(value))
      .startWith(1)
      .distinctUntilChanged(),
  c$: (a$, b$) =>
    Rx.Observable.combineLatest(
      a$, b$,
      (a, b) =>
        a + b
  )
});

let intent = createGroup({
  changeA$: (interactions) =>
    interactions
      .get('#a', 'input')
      .map(({ target }) => target.value),
  changeB$: (interactions) =>
    interactions
      .get('#b', 'input')
      .map(({ target }) => target.value)
});

let view = createGroup({
  vtree$: (a$, b$, c$) =>
    Rx.Observable.combineLatest(
      a$, b$, c$,
      (a, b, c) =>
        h('form',
          h('fieldset', [
            h('legend', 'Add two numbers'),
              h('input#a', {
                type: 'number',
                value: a,
              }),
              h('input#b', {
                type: 'number',
                value: b,
              }),
              h('output', {
                value: c,
                htmlFor: 'a,b'
              })
            ]
          )
        )
    )
});

applyToDOM('.js-container', (interactions) => {
  model.inject(intent, model); // self-injection to make a$ and b$ available for c$
  view.inject(model);
  intent.inject({ interactions });

  return view.vtree$;
});