Giter Site home page Giter Site logo

meteor-subs-cache's Introduction

Meteor Subscription Cache

This package is similar to meteorhacks:subs-manager. The goal is to cache subscriptions.

Suppose you are subscribing to a post:

sub = Meteor.subscribe('post', postId)

When the user clicks to another page, you typically want to stop the subscription

subs.stop()

This will clean up and throw away the data you don't need. But what if the user clicks back immediately after you stopped the subscription? Now you have resubscribe to the same data. This is a waste. SubsCache allows you to cache your subscriptions. When you stop a SubCache subscription, it will start a setTimeout and stop the subscription after expireAfter minutes. If you resubscribe to the same subscription before the cached subscription expired, then the setTimeout is cleared and the subscription is restored.

meteorhacks:subs-manager comparison

  1. SubsManager will stop your subscription "expireIn" minutes after your subscribe. SubsCache will stop your subscription "expireAfter" minutes after you stop (or the current reative computation stops).
  1. SubsManager does not have a ready function for each subscription. subsManager.ready tells you if all cached subscriptions are ready. SubsCache as subsCache.allReady() and individual sub.ready()

  2. SubsManager does not allow you to have subscriptions with different expiration times in the same cache. SubsCache allows you set the default expireAfter upon initialization but you can use subscribeFor(expireAfter, ...) to subscribe and cache for a different time.

  3. SubsManager does not allow infinite items in a cache. SubsCache does if you set cacheLimit to -1.

  1. SubsManager does not allow subscriptions that never expire. SubsCache does if you set expireAfter to -1.

Getting Started

meteor add ccorcos:subs-cache

Initialize with optional expireAfter (default 5) and cacheLimit (default 10). expireAfter is the number of minutes after a subscription is stopped without having been restarted before truely stopping it. If set to -1, the subscription will never expire. cacheLimit is the max number of subscriptions to cache. Set to -1 for unlimited capacity.

    subsCache = new SubsCache(5, 10);
    // first argument is expireAter -- default is 5 minutes
    // second argument is cacheLimit -- default is 10
  • sub = subsCache.subscribe(...) creates a subscription just like Meteor.subscribe

  • sub = subsCache.subscribeFor(expireIn, ...) allow you to set the expiration other than the defualt.

  • subsCache.clear() will stop all subscription immediately

  • subsCache.ready() tells you if all subscriptions in the cache are ready

  • subsCache.onReady(func) will call a function once all subscription are ready

  • sub.stop() will cache a subscription and stop after expireAfter unless restarted with sub.restart()

  • sub.stopNow() will stop a subscription immediately and remove it from the cache.

  • sub.ready() tells you if an individual subscription is ready

  • sub.onReady(func) will call a function once an individual subscription is ready

Testing

You can run the mocha-based tests in watch mode via:

meteor test-packages ./ --driver-package=cultofcoders:mocha

Known Issues

No data is injected when using with Fast Render

When using the Fast Render package the parameters passed to the subscription must the identical on both Fast Render and Subscache or no data will be injected.

meteor-subs-cache's People

Contributors

budgieinwa avatar ccorcos avatar gazhayes avatar gregory avatar icereed avatar janat08 avatar jankapunkt avatar lmachens avatar paranoico avatar ramezrafla avatar sakulstra avatar sbalmer avatar veered avatar

Watchers

 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.