Coder Social home page Coder Social logo

aleksplus / stomp-redux-subscriptions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jaszczw/stomp-redux-subscriptions

0.0 0.0 0.0 345 KB

DEPRECATED. Forked of @jaszczw. A POC library that allows to create subscriptions via STOMP communication, maintaining results in redux state.

License: MIT License

TypeScript 100.00%

stomp-redux-subscriptions's Introduction

DEPRECATED

Do not use in new projects.

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

Example usages

There are three libraries in this repository.

  • redux-subscriptions-manager
  • redux-saga-subscriptions
  • stomp-subscriptions

They can be used separately to achieve 'reactive' application that is aware of changes on backend, without per case pooling/fetches.

It resolves problem that is as follow:

You have table of "Patients" let's say on the '8th' floor. So you want to see their heart-rates in real-time went you open given patient, you also want his data to be up to date. Maybe there is more things to track then just heart-rate.

So normally you would do something like:

class PatientsList extends React.Component {
    componentDidMount() {
      socket.on('patients', () => {
          //update.state/fetchData etc.
      });
    }
    
    componentWillUnmount() {
        socket.off('patients')
    }
    
    //...
}

But then if you had more components dependent on same data you would have to extract it to higher level component or just a top level component.

class PatientsDataProvider extends React.Component {
   componentDidMount() {
    socket.on('patients', () => {
        //update.state
    });
   }
   
   componentWillUnmount() {
      socket.off('patients')
   }

   //...
}

So now as we have this logic of 'listening' to patients message - we need to also subscribe for example for specific floor info:

So we would extend this class with props passing etc. Imagine there will be another entity that we need to do the same for it:

We would have to duplicate exactly the same code, so instead I introduced: SubscritionManager component - very simple component that abstracts this logic.

export default class SubscriptionManager extends React.Component {
 componentWillMount() {
   this.props.subscribe(this.props.payload);
 }

 componentWillUnmount() {
   this.props.unsubscribe(this.props.payload);
 }

 componentWillReceiveProps(newProps) {
   if (!isEqual(this.props.payload, newProps.payload)) {
     unsubscribe(this.props);
     subscribe(newProps);
   }
 }

 render() {
   return this.props.children;
 }
}

It's role is just to run subscribe action and unsubscribe when it's props changes it is mounted/unmounted.

So you get generic handling of 'subscribe/unsubscribe' and usage looks like this:

import {SubsriptionManager} from '../components'
import {connect} from 'redux';

const mapDispatchToProps = ({
    subscribe: () => {
        socket.on('patients', () => {})
    }, 
    unsubscribe: () => {
        socket.off('patients');
    }
});

export const ProvidePatients = connect(null, mapDispatchToProps)(SubscriptionManager);

It looks better, and is generic, however the main benefit that we get is props passing:

import {SubsriptionManager} from '../components'

const mapDispatchToProps = ({
    subscribe: (filter) => {
        socket.on(`patients-${filter.floor}-floor`, () => {
        });
    }, 
    unsubscribe: (filter) => {
        socket.off(`patients-${filter.floor}-floor`);
    }
});

const mapStateToProps = (state, ownProps) => ({
    payload: ownProps
})

export const ProvidePatients = connect(null, mapDispatchToProps)(SubscriptionManager);


<ProvidePatients floor={8} />

You can probably already see where this is going : The manager will take care of subscribe/unsubscribe whenever the payload changes So you just need to define, what you want track and how do you handle it.

Those are just examples, in samples I have extracted the actions for subscribe, unsubscribe to sagas. So provider just dispatches action with payloads. So you can save them in state, handle it however you see fit in saga.

I hope SubscriptionManager - defends itself well.

The rest of the libraries are there to help you manage subscription/unsubscription running, restart handling etc.

stomp-redux-subscriptions's People

Contributors

aleksplus avatar jaszczw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.