dreamstate
Version:
Store management library based on react context and observers
37 lines (34 loc) • 1.67 kB
JavaScript
import { DreamstateError } from '../../error/DreamstateError.js';
import { EDreamstateErrorCode } from '../../../types/error.js';
import { isFunction, isCorrectQueryType } from '../../../utils/typechecking.js';
/**
* Unsubscribes the specified listener from handling queries of a given type.
*
* This function removes the provided listener for the given query type from the registry,
* ensuring it no longer handles future data queries for that type.
*
* @template T - The type of the query.
* @param {TQueryType} queryType - The type of query that the listener should be unsubscribed from.
* @param {TQueryListener<T, TAnyValue>} listener - The callback listener to be removed from query handling.
* @param {IRegistry} registry - The current scope registry containing the query providers.
* @returns {void} This function does not return a value; it performs the unsubscribe action.
*/
function unRegisterQueryProvider(queryType, listener, _a) {
var QUERY_PROVIDERS_REGISTRY = _a.QUERY_PROVIDERS_REGISTRY;
if (!isFunction(listener)) {
throw new DreamstateError(EDreamstateErrorCode.INCORRECT_QUERY_PROVIDER, typeof listener);
} else if (!isCorrectQueryType(queryType)) {
throw new DreamstateError(EDreamstateErrorCode.INCORRECT_QUERY_TYPE, typeof queryType);
}
if (QUERY_PROVIDERS_REGISTRY.has(queryType)) {
var nextProviders = QUERY_PROVIDERS_REGISTRY.get(queryType).filter(function (it) {
return it !== listener;
});
if (nextProviders.length) {
QUERY_PROVIDERS_REGISTRY.set(queryType, nextProviders);
} else {
QUERY_PROVIDERS_REGISTRY.delete(queryType);
}
}
}
export { unRegisterQueryProvider };