Created
September 6, 2022 16:48
-
-
Save farishan/e2807f4ad580e16fbb2fbd1875ac4ebf to your computer and use it in GitHub Desktop.
Basic pubsub pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* AMD Module for RequireJS */ | |
define(function () { | |
function PubSub() { | |
const self = this | |
let subscriberCount = 0 | |
this.eventMap = {} | |
this.subscribe = function (key, callback) { | |
const token = `${generateUID()}_${++subscriberCount}` | |
if (!self.eventMap[key]) self.eventMap[key] = {} | |
self.eventMap[key][token] = callback | |
return token | |
} | |
this.publish = function (key, payload) { | |
if (!self.eventMap[key]) { | |
console.error('Unknown event:', key) | |
return | |
} | |
const callbackKeys = Object.keys(self.eventMap[key]) | |
callbackKeys.forEach(callbackKey => { | |
self.eventMap[key][callbackKey](payload) | |
}); | |
} | |
/* @TODO define unsubscribe method */ | |
/** | |
* Built-in Module | |
* @see https://stackoverflow.com/questions/6248666/how-to-generate-short-uid-like-ax4j9z-in-js | |
*/ | |
function generateUID() { | |
var a = (Math.random() * 46656) | 0; | |
var b = (Math.random() * 46656) | 0; | |
a = ("000" + a.toString(36)).slice(-3); | |
b = ("000" + b.toString(36)).slice(-3); | |
return a + b; | |
} | |
} | |
return PubSub | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment