Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: opt the array creation #173

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ type ValueType = [number, any]; // [times, realValue]

const SPLIT = '%';

/** Connect key with `SPLIT` */
export function pathKey(keys: KeyType[]) {
return keys.join(SPLIT);
}

class Entity {
instanceId: string;
constructor(instanceId: string) {
Expand All @@ -13,21 +18,33 @@ class Entity {
cache = new Map<string, ValueType>();

get(keys: KeyType[]): ValueType | null {
return this.cache.get(keys.join(SPLIT)) || null;
return this.opGet(pathKey(keys));
}

/** A fast get cache with `get` concat. */
opGet(keyPathStr: string): ValueType | null {
return this.cache.get(keyPathStr) || null;
}

update(
keys: KeyType[],
valueFn: (origin: ValueType | null) => ValueType | null,
) {
const path = keys.join(SPLIT);
const prevValue = this.cache.get(path)!;
return this.opUpdate(pathKey(keys), valueFn);
}

/** A fast get cache with `get` concat. */
opUpdate(
keyPathStr: string,
valueFn: (origin: ValueType | null) => ValueType | null,
) {
const prevValue = this.cache.get(keyPathStr)!;
const nextValue = valueFn(prevValue);

if (nextValue === null) {
this.cache.delete(path);
this.cache.delete(keyPathStr);
} else {
this.cache.set(path, nextValue);
this.cache.set(keyPathStr, nextValue);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/hooks/useGlobalCache.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import type { KeyType } from '../Cache';
import { pathKey, type KeyType } from '../Cache';
import StyleContext from '../StyleContext';
import useCompatibleInsertionEffect from './useCompatibleInsertionEffect';
import useEffectCleanupRegister from './useEffectCleanupRegister';
Expand All @@ -23,16 +23,16 @@ export default function useGlobalCache<CacheType>(
): CacheType {
const { cache: globalCache } = React.useContext(StyleContext);
const fullPath = [prefix, ...keyPath];
const deps = fullPath.join('_');
const fullPathStr = pathKey(fullPath);

const register = useEffectCleanupRegister([deps]);
const register = useEffectCleanupRegister([fullPathStr]);

const HMRUpdate = useHMR();

type UpdaterArgs = [times: number, cache: CacheType];

const buildCache = (updater?: (data: UpdaterArgs) => UpdaterArgs) => {
globalCache.update(fullPath, (prevCache) => {
globalCache.opUpdate(fullPathStr, (prevCache) => {
const [times = 0, cache] = prevCache || [undefined, undefined];

// HMR should always ignore cache since developer may change it
Expand All @@ -57,18 +57,18 @@ export default function useGlobalCache<CacheType>(
buildCache();
},
/* eslint-disable react-hooks/exhaustive-deps */
[deps],
[fullPathStr],
/* eslint-enable */
);

let cacheEntity = globalCache.get(fullPath);
let cacheEntity = globalCache.opGet(fullPathStr);

// HMR clean the cache but not trigger `useMemo` again
// Let's fallback of this
// ref https://github.com/ant-design/cssinjs/issues/127
if (process.env.NODE_ENV !== 'production' && !cacheEntity) {
buildCache();
cacheEntity = globalCache.get(fullPath);
cacheEntity = globalCache.opGet(fullPathStr);
}

const cacheContent = cacheEntity![1];
Expand All @@ -90,7 +90,7 @@ export default function useGlobalCache<CacheType>(
});

return () => {
globalCache.update(fullPath, (prevCache) => {
globalCache.opUpdate(fullPathStr, (prevCache) => {
const [times = 0, cache] = prevCache || [];
const nextCount = times - 1;

Expand All @@ -100,7 +100,7 @@ export default function useGlobalCache<CacheType>(
// With polyfill, registered callback will always be called synchronously
// But without polyfill, it will be called in effect clean up,
// And by that time this cache is cleaned up.
if (polyfill || !globalCache.get(fullPath)) {
if (polyfill || !globalCache.opGet(fullPathStr)) {
onCacheRemove?.(cache, false);
}
});
Expand All @@ -111,7 +111,7 @@ export default function useGlobalCache<CacheType>(
});
};
},
[deps],
[fullPathStr],
);

return cacheContent;
Expand Down
Loading