Skip to content

Latest commit

 

History

History

utils

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Galaxis Utils

npm

Common Galaxis utils.

Installation

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.

Public API

⚠ Anything that is not documented here is not considered a part of public API and may change at any time.

immerify()

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;
    }),
};

Arguments

Name Type Description Required
toCache (opts: ToCacheOptions) => void; A function like normal toCache, but the cacheData parameter can be updated directly. Yes

Return value

(opts: ToCacheOptions) => TCacheData;

memoize()

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]],
    ),
};

Arguments

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

Return value

(opts: FromCacheOptions) => TData | undefined

objectHash()

It's the object-hash without any changes.