1.0.1 • Published 7 years ago

feathers-subscriptions-manager v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

feathers-subscriptions-manager

Build Status Downloads

A subscriptions manager for feathers.js reactive subscriptions through feathers-reactive.

The Feathers Subscription Manager allows you to wait for multiple subscriptions and then call a shared callback function. After the initial load this function will be called every time one of the subscriptions get updated.

Requirements

This Subscriptions Manager will only work for feathers-reactive. Please follow their setup instructions before you start using the "feathers-subscriptions-manager".

npm install --save feathers-reactive

How does it work

The Feathers Subscription Manager will let you register all the subscriptions (addSubscription()) you want and watch them. Each of the subscriptions will have some kind of a callback action (examples below). This action has to return an object, that will be merged into the Subscriptions Manager's state.

When all subscriptions have received their data the .ready(callback(data, initial)) callback method will be called and will have the merged state object as first parameter. In this function you can e.g. have a "renderLayout" function.

The callback will now be called on every change of one of the watched subscriptions as well.

(optional) The second parameter of the callback function (initial) is a boolean that will let you know if it is the initial call (True) or an update call (False).

Usage

Set up subsManager:

import { SubsManager } from 'feathers-subscriptions-manager';
const subsManager = new SubsManager();

addSubscription(cursor, action)

cursor is a feathers service call.

const userService = app.service('users');
const findUsersCursor = userService.find(...);

action can be a string or a function.

/* Use with strings */
/* Recommended if you e.g. make a "get" call */
/* Result: {users: data} */
subsManager.addSubscription(findUsersCursor, 'users');


/* Use with function */
/* Recommended if you want to have a proxy between getting the data from the server and finishing the the call */
/* Result: {users: users.data} */
subsManager.addSubscription(findUsersCursor, (users) => {
	return {users: users.data};
});

ready(callback(data, inital))

subsManager.ready((data, inital) => {
	// all subscriptions are ready 
	// you can use the resulting object as "data"
	if(intial) {
		renderLayout(data);
	} else {
		updateLayout(data);
	}
});