Skip to content

Instantly share code, notes, and snippets.

@jpidelatorre
Last active October 17, 2018 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpidelatorre/a65a114933a5d614031b22a40f4cd4db to your computer and use it in GitHub Desktop.
Save jpidelatorre/a65a114933a5d614031b22a40f4cd4db to your computer and use it in GitHub Desktop.
Pub/Sub golf edition

Pub/Sub golf edition

This algorithm does heavy use of arrow functions. Use Babel if you need wider support.

Example

var pubsub = require('pub-sub');

pubsub.sub('channel', function (message) {
    if (message === 'close') { // Unsubscribe when 'close' is received'
        console.log('Shuting down...');
        return false;
    } else {
        console.log(message);
        return true;
    }
});

var countdown = 10;
var interval = setInterval(function () {
    if (--countdown) {
        pubsub.pub('channel', 'Seconds until shutdown: ' + countdown);
    } else {
        pubsub.pub('channel', 'close');
        clearInterval(interval);
    }
}, 1000);

Heavily inspired by this

(
l // List of subscriptions
=> ( // Returns a pub/sub object
{
pub: ( // Publish message
c, // Channel
m // Message
) => // Returns the subscriptions list
l = l.filter(
i => // Filter subscriptions list
i[0] != c || // Ignore other channels
i[1](m) // Call subscriber with the message
!== !1 // Unsubscribe if it returns false
)
,
sub: ( // Subscribe to channel
c, // Channel
f // Subscriber
) => // Returns the lenght of the subscriptions list
l.push([c,f]) // Add subscriber to the subscriptions list
}
)
)(
[] // Initialize with an empty
)

DON'T BLAME ME LICENSE

v0 Jun 2018

You are allowed to use, modify or distribute this piece of code as pleased. The author cannot be made responsible for anything that could derive from the use or misuse of this software.

{
"name": "pub-sub",
"description": "Golfed down Pub/Sub pattern in 86 bytes",
"version": "0.0.0",
"engines": { "node": ">=4.0.0" },
"main": "pubsub.clean.js
}
exports=
// Same as pubsub.js but functions return nothing
(l=>({pub:(c,m)=>{l=l.filter(i=>i[0]!=c||i[1](m))},sub:(c,f)=>{l.push([c,f])}}))([]) // 85 bytes
exports=
(l=>({pub:(c,m)=>l=l.filter(i=>i[0]!=c||i[1](m)),sub:(c,f)=>l.push([c,f])}))([]) // 81 bytes
exports=
/**
* Version without channels and the pubsub object is an actual array with the listeners.
* @example
* var channel = (l=>Object.assign(l=[],{pub:m=>l=l.filter(f=>f(m)),sub:f=>l.push(f)}))([])
*
* channel.sub(function (message) {
* if (message === 'close') {
* console.log('Shutting down...');
* return false;
* } else {
* console.log(message);
* return true;
* }
* });
*
* console.log(channel.length); // 1
*
* channel.forEach(function (f, i) {
* channel[i] = function (message) {
* console.log('Running function number ' + i);
* return f(message);
* };
* });
*
* var countdown = 10;
* var interval = setInterval(function () {
* if (--countdown) {
* channel.pub('Seconds until shutdown: ' + countdown);
* } else {
* channel.pub('close');
* clearInterval(interval);
* }
* }, 1000);
**/
(l=>Object.assign(l=[],{pub:m=>l=l.filter(f=>f(m)),sub:f=>l.push(f)}))([]) // 75 bytes
exports=
/**
* Version without channels.
* @example
* var channel = (l=>({pub:m=>l=l.filter(f=>f(m)),sub:f=>l.push(f)}))([])
*
* channel.sub(function (message) {
* if (message === 'close') {
* console.log('Shutting down...');
* return false;
* } else {
* console.log(message);
* return true;
* }
* });
*
* var countdown = 10;
* var interval = setInterval(function () {
* if (--countdown) {
* channel.pub('Seconds until shutdown: ' + countdown);
* } else {
* channel.pub('close');
* clearInterval(interval);
* }
* }, 1000);
**/
(l=>({pub:m=>l=l.filter(f=>f(m)),sub:f=>l.push(f)}))([]) // 57 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment