@empathyco/x-components
Version:
Empathy X Components
57 lines (54 loc) • 1.94 kB
JavaScript
import '@empathyco/x-utils';
import { createStoreEmitters } from '../../../store/utils/store-emitters.utils.js';
import { urlXStoreModule } from './module.js';
/**
* The params from {@link UrlParams} that provokes a replace instead of a push in the browser URL
* state.
*
* @internal
*/
const replaceableParams = ['scroll', 'page'];
/**
* Compares new and old {@link UrlParams} to know if not replaceable params have changed.
*
* @param newParams - The new {@link UrlParams} to compare.
* @param oldParams - The old {@link UrlParams} to compare.
*
* @returns True if is pushable change, false otherwise.
*/
function shouldPushUrl(newParams = {}, oldParams = {}) {
const keys = Object.keys({ ...oldParams, ...newParams });
return keys.some(key => !replaceableParams.includes(key) && oldParams[key] !== newParams[key]);
}
/**
* Compares new and old {@link UrlParams} to know if replaceable params have changed.
*
* @param newParams - The new {@link UrlParams} to compare.
* @param oldParams - The old {@link UrlParams} to compare.
*
* @returns True if is pushable change, false otherwise.
*/
function shouldReplaceUrl(newParams = {}, oldParams = {}) {
const keys = Object.keys({ ...oldParams, ...newParams });
return (keys.some(key => replaceableParams.includes(key) && oldParams[key] !== newParams[key]) &&
!shouldPushUrl(newParams, oldParams));
}
/**
* {@link StoreEmitters} For the URL module.
*
* @internal
*/
const urlEmitters = createStoreEmitters(urlXStoreModule, {
PushableUrlStateUpdated: {
selector: (_, getters) => getters.urlParams,
filter: shouldPushUrl,
metadata: { replaceable: false },
},
ReplaceableUrlStateUpdated: {
selector: (_, getters) => getters.urlParams,
filter: shouldReplaceUrl,
metadata: { replaceable: false },
},
});
export { replaceableParams, urlEmitters };
//# sourceMappingURL=emitters.js.map