Common Galaxis utils.
yarn add @galaxis/utils
You need to install Galaxis Core as well, directly or indirectly.
The library is compiled to modern JS, but it should work in all reasonable browsers with the help of properly configured Babel.
⚠ Anything that is not documented here is not considered a part of public API and may change at any time.
This utility simplifies cache updates using Immer. With it, you can just update cacheData
without worrying about immutability.
const query: AppQuery = {
requestParams: { foo: 'foo' },
toCache: immerify(({ cacheData, data }) => {
cacheData.field = data;
}),
};
Name | Type | Description | Required |
---|---|---|---|
toCache | (opts: ToCacheOptions) => void; |
A function like normal toCache , but the cacheData parameter can be updated directly. |
Yes |
(opts: ToCacheOptions) => TCacheData;
This utility helps with memoization of data, that is calculated based on the cache data.
const query: AppQuery = {
requestParams: { id: '1' },
fromCache: memoize(
({ cacheData, requestParams }) => {
const firstDataPiece = cacheData.first[requestParams.id];
const secondDataPiece = cacheData.second[requestParams.id];
return firstDataPiece && secondDataPiece ? { ...firstDataPiece, ...secondDataPiece } : undefined;
},
({ cacheData, requestParams }) => [cacheData.first[requestParams.id], cacheData.second[requestParams.id]],
),
};
Name | Type | Description | Required |
---|---|---|---|
fromCache | (opts: FromCacheOptions) => TData | undefined |
A normal fromCache function. |
Yes |
getDeps | (opts: FromCacheOptions) => unknown[] |
A function that returns the parts of cacheData that this fromCache depends on. These dependencies are compared by reference. The new value will be calculated only if some dependency changes. Note that the length of dependencies can't change. |
Yes |
(opts: FromCacheOptions) => TData | undefined
It's the object-hash without any changes.