@fluent-windows/hooks
Version:
Fluent-Windows React hooks.
37 lines (32 loc) • 908 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.subscribe = subscribe;
exports.unsubscribe = unsubscribe;
exports.dispatch = dispatch;
var subscribers = new Map();
function getAction(type) {
return subscribers.get(type);
}
function subscribe(type, callback) {
if (!type || !callback) return;
if (!subscribers.has(type)) subscribers.set(type, new Set());
getAction(type).add(callback);
}
function unsubscribe(type, callback) {
if (!type || !callback) return;
if (!subscribers.has(type)) return;
getAction(type)["delete"](callback);
if (getAction(type).size === 0) subscribers["delete"](type);
}
function dispatch(param) {
var _ref = param,
type = _ref.type,
payload = _ref.payload;
if (!type) return;
if (!subscribers.has(type)) return;
getAction(type).forEach(function (callback) {
callback.call(undefined, payload);
});
}