@fluent-windows/hooks
Version:
Fluent-Windows React hooks.
28 lines (26 loc) • 711 B
JavaScript
const subscribers = new Map();
function getAction(type) {
return subscribers.get(type);
}
export function subscribe(type, callback) {
if (!type || !callback) return;
if (!subscribers.has(type)) subscribers.set(type, new Set());
getAction(type).add(callback);
}
export 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);
}
export function dispatch(param) {
const {
type,
payload
} = param;
if (!type) return;
if (!subscribers.has(type)) return;
getAction(type).forEach(callback => {
callback.call(undefined, payload);
});
}