MichalW / subscription-organizer

Meteor package for Caching & Sharing same subscriptions between many views/places. Dependant under subscriptions.

Home Page:https://atmospherejs.com/cristo/subscription-organizer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Subscription Organizer

Normally you invoke subscriptions inside the template level and you use many times this template subscriptions will rerun again etc. Subscription Improver will share same subscriptions between many views or/and places. Additionally any subscription is cached for while before will be really stopped, so any resubscribtion is fast.

This package brings possibility of making additional subscription under subscriptions. Thus, after user is subscribed you can subscribe for his avatar.

Instalation

$ meteor add cristo:subscription-organizer

Motivation

Implemented because any other packages for subscriptions disappointed me.

How it works

Memoize subscriptions

const h1 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
const h2 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
const h3 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});

// Only onetime it will be subscribed (h1 === h2 === h3)

h1.stop(); //Still is subscribed
h3.stop(); //Still is subscribed
h2.stop(); //Subscription will be stopped soon

Dependant subscriptions

const h1 = SubscriptionOrganizer.subscribe('user', 'afaf123');
// this autorun will be launched when h1 will be ready
h1.autorun(function () {
    const user = userMeteor.users.findOne();
    //following subscriptions will stopped after h1
    this.subscribe('avatar', user.avatarId);
    SubscriptionOrganizer.subscribe('user', user.friendId);
});
const h2 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});

// Only onetime it will be subscribed (h1 === h2 === h3)

h1.stop(); //Still is subscribed
h2.stop(); //Subscription will be stopped soon with dependants (autoruns)

Cache

Unique subscription is holden before stop wasn't called as many times as subscribe was. After that, this subscription will passed to stopping. As a default stopping of subscription will wait one minute before subscription will be really stopped.

const h1 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
h1.stop();

Meteor.setTimeout(function () {
    // reusing old subscription and clear the timeout to stopping
    const h2 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
}, 30000);

increase or decrease timeout to stop

SubscriptionOrganizer.setConfig({waitTimeUntilUnsubscribe: 0});
const h1 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
h1.stop(); //will be stop (but async)

Subscription count

const h1 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
const h2 = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
 //reactive data source
h1.count(); //output: 2

Many instances

const mySubsImprover = new SubscriptionOrganizer({waitTimeUntilUnsubscribe: 60000, connection: Meteor.connection});
const h1 = mySubsImprover.subscribe('test', {a:1, b:2});

Example in blaze template

within autorun

Template.someView.onCreated(function(){
    this.autorun(function() {
        var data = Template.currentData();
        SubscriptionOrganizer.subscribe('test', data._id);
    });
});

without autorun

Template.someView.onCreated(function(){
    this.mySub = SubscriptionOrganizer.subscribe('test', {a:1, b:2});
});

Template.someView.onDestroyed(function(){
    this.mySub.stop();
});

About

Meteor package for Caching & Sharing same subscriptions between many views/places. Dependant under subscriptions.

https://atmospherejs.com/cristo/subscription-organizer


Languages

Language:JavaScript 100.0%