@magnetarjs/utils
Version:
Magnetar utils like a logger for easier development
32 lines (31 loc) • 1.02 kB
JavaScript
import { isAnyObject } from 'is-what';
/** Using an actual symbol causes issues in production host apps for whatever reason... */
export const storeSplitSymbol = 'MAGNETAR_SYMBOL_storeSplit';
/**
* A storeSplit function allows you to apply a different payload between your cache store vs your other stores.
*
* It will let TypeScript know that you're trying to apply the type of whatever you pass to the `cache` key, even though your other stores might receive other values.
*
* ### Example Use Case
* ```ts
* import { storeSplit } from '@magnetarjs/utils'
*
* magnetar.collection('user').doc('1').merge({
* name: 'updated name',
* // ...
* dateUpdated: storeSplit({
* cache: new Date(),
* remote: serverTimestamp(),
* })
* })
* ```
*/
export function storeSplit(payload) {
return {
storeSplitSymbol,
storePayloadDic: payload,
};
}
export function isStoreSplit(payload) {
return isAnyObject(payload) && payload['storeSplitSymbol'] === storeSplitSymbol;
}