Socket
Socket
Sign inDemoInstall

keep-posted

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 2.0.0

19

keep-posted.js

@@ -11,3 +11,3 @@ (function (root, factory) {

'use strict';
var create = function (options) {

@@ -17,13 +17,18 @@

var listeners = [];
var mostRecentStuffToSend;
var mostRecentFiredArguments = [];
var keepPosted = function () {
mostRecentStuffToSend = arguments;
mostRecentFiredArguments = arguments;
listeners.forEach(function (callback) {
callback.apply(null, mostRecentStuffToSend);
callback.apply(null, mostRecentFiredArguments);
});
};
keepPosted.subscribe = function (callback) {
keepPosted.subscribe = function (subscriberOptions, callback) {
if (typeof subscriberOptions === 'function') {
callback = subscriberOptions;
subscriberOptions = {};
}
listeners.push(callback);

@@ -35,4 +40,4 @@

if (options.updateNewSubscribers && mostRecentStuffToSend) {
callback.apply(null, mostRecentStuffToSend);
if (subscriberOptions.refireMostRecent) {
callback.apply(null, mostRecentFiredArguments);
}

@@ -39,0 +44,0 @@

@@ -60,20 +60,2 @@ 'use strict';

it("can resend last message to new subscribers", function (done) {
var count = 0;
var kp = keepPosted.create({
updateNewSubscribers: true
});
kp(1, 2, 3, 4);
// The call shoult be asynchronous. So here should be zero.
expect(count).toBe(0);
kp.subscribe(function () {
count += 1;
var args = Array.prototype.slice.call(arguments);
expect(args).toEqual([1, 2, 3, 4]);
done();
});
});
it("has onFirstSubscriber hook", function () {

@@ -108,2 +90,29 @@ var count = 0;

describe("refire last message to new subscribers", function () {
it("refires anyway with undefined if no event was fired yet", function (done) {
var kp = keepPosted.create();
kp.subscribe({
refireMostRecent: true
}, function (value) {
expect(value).toBe(undefined);
done();
});
});
it("refires", function (done) {
var kp = keepPosted.create();
kp(1, 2, 3, 4);
kp.subscribe({
refireMostRecent: true
}, function () {
var args = Array.prototype.slice.call(arguments);
expect(args).toEqual([1, 2, 3, 4]);
done();
});
});
});
});
{
"name": "keep-posted",
"description": "Event emitting done the simplest functional way",
"version": "1.0.1",
"version": "2.0.0",
"author": "Jakub Szwacz <jakub@szwacz.com>",

@@ -6,0 +6,0 @@ "keywords": [

@@ -30,3 +30,3 @@ keep-posted

// Call the function returned to you when you did the subscribe
// Call the function returned to you when you did the subscribe
// to stop listening on that event.

@@ -41,4 +41,4 @@ unsubscribe();

var store = [];
// One keep-posted instance is for only one event type.
// One keep-posted instance is for only one event type.
// Create more instances if you need to support more event types.

@@ -49,5 +49,6 @@ var newStuffAdded = keepPosted.create();

var addStuff = function (stuff) {
store.push(stuff);
newStuffAdded(stuff);
if (store.length > 99) {
if (store.length < 99) {
store.push(stuff);
newStuffAdded(stuff);
} else {
storeCapacityReached();

@@ -85,3 +86,2 @@ }

`options` - object with possible fields:
* `updateNewSubscribers` - (default: false) - when set to true will re-send to every new subscriber most recent event which happened before that subscriber jumped on board (if any event happened before).
* `onFirstSubscriber` - function called when first subscriber registers (e.g. hook for lazy instantiation).

@@ -102,3 +102,3 @@ * `onEveryoneUnsubscribed` - function called when all subscribers have unregistered (e.g. hook for destroying what has been constructed with lazy instantiation).

### keepPostedInstance.subscribe(callback)
### keepPostedInstance.subscribe([options], callback)

@@ -108,2 +108,4 @@ Registers new subscriber (event listener).

**Parameters:**
`options` - (optional) lets you pass config object for this listener. Possible keys:
* `refireMostRecent` - (default: false) - when set to true will re-send to this new subscriber most recent event which happened before this subscriber jumped on board (if no even happened before still will fire with `undefined`).
`callback` - well... you know what it does.

@@ -110,0 +112,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc