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

@p.aleks/redux-saga-subscriptions-manager

v0.0.5

Published

Do not use in new projects.

Downloads

2

Readme

DEPRECATED

Do not use in new projects.

Fork of @jaszczw/stomp-redux-subscriptions version, add types, minor changed.

API

High-level description of API.

  exports {
      createSubscriptionWatcher: (options : SubscriptionOptions, createChannel) => handler;
      createStartHandler: (stopSubActions: string[]) => (createChannel: any) => handler
      createSubscriptionHandler: (selector: (state: any, payload: any) => handler
      createErrorHandler: (startType: any, stopType: any, reconnectTimeout?: number) => handler
  }
      
interface SubscriptionOptions {
  subIdentifier: string;
  selector: ((state, payload) => any[]) | ((state) => any[]);
  startType?: string;
  stopType?: string | string[];
  errorType?: string | string[];
}

interface handler extends GeneratorFunction;

Use cases:

Library works best together with @p.aleks/redux-subscriptions-manager - reduced state, but can be used without it.

Problem it solves: maintaining multiple subscriptions with different payloads, handling cleanup and creation for actions.

Below you there is very simple approach of handling each Action .START_SUBSCRIBE starting polling and stopping it on STOP_SUBSCRIBE.

It doesn't allow for multiple subscriptions based on payload, duplicate subscriptions will run twice. Creating and destroying is written down by hand.


const createChannel = (payload?) => eventChannel((emit) => {
    const interval = setInterval(() =>
        Promise.resolve(fetch()).then(emit),
        1000
    );

    return () => {clearInterval(interval)}; //Cleanup
});

export const startSubscription = function *() {
    const channel = createChannel();

    while (true) {
        const {action, cancel} = yield race({
            action: take(channel),
            cancel: take(STOP_SUBSCRIBE),
        });

        if (action) {
            yield action;
        }

        if (cancel) {
            channel.close();
            return;
        }
    }
};

export const watchPatientsSubscription = function *() {
  yield takeLatest(START_SUBSCRIBE, startSubscription);
};

With the same constraints but with library abstracting start/stop, code would look as below:


const createChannel = (payload?) => eventChannel((emit) => {
    const interval = setInterval(() =>
        Promise.resolve(fetch()).then(emit),
        1000
    );

    return () => {clearInterval(interval)}; //Cleanup
});

export const watchPatientsSubscription = function *() {
    const startHandler = createStartHandler([STOP_SUBSCRIBE]);
    yield takeLatest(START_SUBSCRIBE, startHandler(createChannel));
};

This simplifies things quite a bit.

With this approach you can either change implementation from pooling to socket just by modifing the createChannel method. You can also add state maintained subscriptions, which allow you for example to wrap each component that needs the entity and be sure that it will start pooling/fetching/listening to this only once, per unique payload.

If you decided to use this library together with redux-subscriptions-manager and state managed by it.

After applying redux-subscriptions-manager to your subscriptions you may change the usage to match more complete one that is

instead of:

export const watchPatientsSubscription = function *() {
   const startHandler = createStartHandler([STOP_SUBSCRIBE]);
   yield takeLatest(START_SUBSCRIBE, startHandler(createChannel));
};

use:

export const watchPatientsSubscription = createSubscriptionWatcher({
   subIdentifier: PATIENTS_SUBSCRIPTIONS,
   selector: getPatientsSubscriptions
 }, createChannel);

Code about would work with assumption that:

There are actions of type: PATIENTS_SUBSCRIPTIONS dispatched with methods argument of SUBSCRIBE/UNSBUSCRIBE from @p.aleks/redux-subscriptions-manager and optional payload. (Exactly what library creates when used as in readme);

From selector it expects that it will take (globalState, payload) => any[].

The Start action will be dispatched when there was action with method SUBSCRIBE and we've got first entry in the resulting array. The Stop action will be dispatched when there was action with method UNSBUSCRIBE and we've got nothing left in the resulting array.

See examples/simple-subscriptions for consulting. But you can imagine that for example:

Given state after reducing equal

{
    patientsSubState: [{id:1}, {id: 2}];
}

and action:

{
  type: PATIENTS_SUBSCRIPTIONS,
  method: SUBSCRIBE,
  payload: {id: 2}
}

Depending on selector passed ->

const anySub = (state, payload )= > state;
const idSub = (state, payload )= > _filter(state, payload);

anySub(state)// [{id: 1},{id: 2}] meaning there was subscriptions, so we do not create new one (don't call createChannel) idSub(state) // [{id: 2] meaning we created first subscription of it kind so we call createChannel.

So you can see that you can modify subscriptions handling quite easily depending on your need.