
Research
Using Trusted Protocols Against You: Gmail as a C2 Mechanism
Socket uncovers malicious packages on PyPI using Gmail's SMTP protocol for command and control (C2) to exfiltrate data and execute commands.
@fluentui-react-native/memo-cache
Advanced tools
This package implements a hierarchical memoization cache using an API pattern that mimics the react.js useMemo hook. It also provides an implementation of traditional JavaScript memoization built using react style utility.
Memoization is an optimization pattern used when a discrete set of inputs, typically parameters to an expensive function, yield a deterministic output. In this case, if the inputs match a previous call, a cached result can be retrieved. This is typically implemented as a factory function, which wraps a function in a closure, adding implicit caching.
React.js provides a hook called useMemo
which is shifts to a more explicit model, where the keys are listed explicitly. This allows more control over the inputs than the older pattern. It's worth mentioning that the react useMemo
hook is not a global cache, it is attached to a given component instance and compares the current execution with the previous one.
This package can be beneficial in two scenarios:
If the routine to be memoized is expensive, then caching the results can boost performance. Note that cache lookups have cost themselves so memoizing a trivial function will likely be slower.
Also note that every additional key adds a level of depth to the hierarchical cache. This has expense and reduces the likelihood of data being already in the cache. Collapsing the inputs to a manageable set helps optimize this. For instance, if building a style from a theme definition pulls 8 values from a theme, it is more efficient to key the resulting object on the theme, than to key each property individually.
The other benefit to this pattern is maintaining object identity between subsequent calls. In react-native the object identity of the style property will sometimes be compared, even if the values within are identical, the shallow props compare will not see the objects as the same.
Similarly if a style is being turned into a CSS class (which is expensive), a WeakMap
to map style objects to CSS classes will only work if the object identities are maintained.
The baseline cache pattern is defined by the following type:
export type GetMemoValue<T, TGet = any> = (
factory: T | () => T,
keys: any[]
) => [T, GetMemoValue<TGet>];
The parameters are used as follows:
Param | Description |
---|---|
factory | This is typically a function, often a simple closure, which returns a value. This value will be cached for a given set of keys. Subsequent calls will just return the value without executing the function. This can also be a value type in which case that will be returned directly. |
keys | Variable parameter list, used as the keys for caching. Note that the order of keys matter. [A, B] resolves differently than [B, A]. |
The return result is a tuple with two elements containing:
This recursive calling pattern allows for a natural pipelining of caching and memoization. Because the cache is effectively implemented as a tree, this pattern falls out fairly easily. See the examples below for more detail.
To get an instance of the memo cache to work with, a caller starts by calling getMemoCache
.
export function getMemoCache<T = any>(globalKey?: object): GetMemoValue<T>;
This function takes an optional parameter globalKey
which can be an object to use as a base cache reference.
globalKey
is specified, the same cache will be retrieved from the global call with the same object reference.globalKey
is not specified the cache instance will be unique and contained entirely within the returned function.This library also provides a standard memoization wrapper:
export function memoize<T extends Function>(fn: T): T;
This should be able to handle any function as an input. It will create its own instance of the cache, use any parameters to the function as key values, and return a closure with the same signature as the input.
This should support the following:
The following are some examples for how to use the functions above for various optimizations.
Given a function to merge styles together, wrap it in a memoization helper to ensure object identities don't change. Then add a helper for ensuring a CSS rule exists for a set of styles.
// standard function which will be memoized
function mergeStylesWorker(...cssStyles: CSSStyle[]): CSSStyle {
// do the work of merging multiple styles together to form a new CSS style
}
// exported function internally has a caching layer with memoize
export const mergeStyles = memoize(mergeStylesWorker);
// all-in-one authoring of memoized function, this one to turn a style into a CSS class, traditionally
// an expensive operation in browsers
export const createRuleForStyle = memoize((style: CSSStyle) => {
const className = // do the work of creating the rule
return className;
});
This demonstrates a component called MyComponent
that:
These three levels of caching are effectively instance -> theme -> props.style.
import { getMemoCache } from '@fluentui-react-native/memo-cache';
// get a unique cache for this component
const myComponentCache = getMemoCache();
// component is a function that takes props
export const MyComponent = (props: IMyComponentProps) => {
const theme = useContext(ThemeContext);
const newProps = { ...props };
// get the style, cached against the theme so it will only be called once, note that because
const [style, themeLocalCache] = myComponentCache(() => {
const colors = theme.colors;
return {
backgroundColor: colors.neutralBackground,
color: colors.neutralForeground
// more stuff from theme
};
}, [theme]);
// merge the styles if a style is passed in via props, caching the union to ensure consistent object identity
newProps.style = newProps.style ? themeLocalCache(() => mergeStyles(style, newProps.style), [newProps.style])[0] : style;
return <InnerControl {...newProps} />;
};
FAQs
Layered memoization style cache helper
The npm package @fluentui-react-native/memo-cache receives a total of 1,008 weekly downloads. As such, @fluentui-react-native/memo-cache popularity was classified as popular.
We found that @fluentui-react-native/memo-cache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious packages on PyPI using Gmail's SMTP protocol for command and control (C2) to exfiltrate data and execute commands.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.