stupid-subscribe

0.2.1 • Public • Published

stupid-subscribe

Tiny JS snippet for an extremely simple, extremely stupid data subscriber.

###API API consists of 2 stupid functions subscribe and action;

action: (fn) => (fn) wraps a function and introduces the sole side effect of triggering all subscribed actions.

subscribe: (listenerFn, readFn) subscribes listenerFn to all actions.

Whenever a function wrapped with action is called, all subscribed listeners are called with the results of their read function, i.e. listenerFn(readFn()) is called

Brief pseudocoded example:

This implements an <h1> that increments a counter when you click on it.

// index.jsx
import {subscribe} from 'stupidsubscribe';
import appInterface from './app.js';
import AppView from './appview.jsx';

subscribe(
  (appProps) => {
    render(<AppView {...appProps} />);
  },
  app.getProps
);

// app.js
import {action} from 'stupidsubscribe';

let numTimes = 0;

export default {
  getProps: () => ({
    text: `Clicked ${numTimes} times`,
    clickHeader: action(() => (numTimes++)),
  }),
};
// index.jsx
const AppView = (props) => (
  <article>
    <h1 onClick={props.clickHeader}>Click Here!</h1>
    {props.text}!
  </article>
);

export default AppView;

###Motivation something something left-pad

Contributing

There's almost certainly no reason to contribute.

FAQ

Does the API even deserve to be called an API? It's not really an interface, as much as it is ~6 lines of utility functions

@evinism

No, probably not, but I went to a hackathon once and the speaker said "API" a lot and I guess it stuck with me.

Can't we modify it so that actions trigger certain subscriptions, rather than all subscriptions, always?

@evinism

I don't want to rename the repo to SlightlyLessStupidSubscribe.

Could I use this on something that's not stupid?

@evinism

Yes, I strongly encourage it. It falls directly in line with a quintessential software design principle of which I am a big proponent, namely:

Software should be an absolute trainwreck

Isn't this a dumbed down version of [x]?

@evinism

Yes, in the same way that everyone is a dumbed down version of Einstein, so too is this a dumbed down version of literally everything.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.1
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.2.1
    1
  • 0.2.0
    1
  • 0.1.0
    1

Package Sidebar

Install

npm i stupid-subscribe

Weekly Downloads

3

Version

0.2.1

License

MIT

Last publish

Collaborators

  • evinism