Skip to content

Commit 9efc6a5

Browse files
authored
refactor: Enhance cache util perf (#256)
* refactor: opt form cache speed * fix: lint
1 parent 124fbeb commit 9efc6a5

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ module.exports = {
1818
'@typescript-eslint/no-redeclare': 0,
1919
'@typescript-eslint/method-signature-style': 0,
2020
'no-async-promise-executor': 0,
21+
'@typescript-eslint/consistent-type-definitions': 0,
2122
},
2223
};

src/useForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import warning from 'rc-util/lib/warning';
3-
import {
3+
import type {
44
Callbacks,
55
FieldData,
66
FieldEntity,

src/utils/NameMap.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
import { InternalNamePath } from '../interface';
2-
import { matchNamePath } from './valueUtil';
1+
import type { InternalNamePath } from '../interface';
32

43
interface KV<T> {
54
key: InternalNamePath;
65
value: T;
76
}
87

8+
const SPLIT = '__@field_split__';
9+
10+
/**
11+
* Convert name path into string to fast the fetch speed of Map.
12+
*/
13+
function normalize(namePath: InternalNamePath): string {
14+
return (
15+
namePath
16+
.map(cell => `${typeof cell}:${cell}`)
17+
// Magic split
18+
.join(SPLIT)
19+
);
20+
}
21+
922
/**
1023
* NameMap like a `Map` but accepts `string[]` as key.
1124
*/
1225
class NameMap<T> {
13-
private list: KV<T>[] = [];
26+
private kvs = new Map<string, T>();
1427

1528
public set(key: InternalNamePath, value: T) {
16-
const index = this.list.findIndex(item => matchNamePath(item.key, key));
17-
if (index !== -1) {
18-
this.list[index].value = value;
19-
} else {
20-
this.list.push({
21-
key,
22-
value,
23-
});
24-
}
29+
this.kvs.set(normalize(key), value);
2530
}
2631

2732
public get(key: InternalNamePath) {
28-
const result = this.list.find(item => matchNamePath(item.key, key));
29-
return result && result.value;
33+
return this.kvs.get(normalize(key));
3034
}
3135

3236
public update(key: InternalNamePath, updater: (origin: T) => T | null) {
@@ -41,15 +45,26 @@ class NameMap<T> {
4145
}
4246

4347
public delete(key: InternalNamePath) {
44-
this.list = this.list.filter(item => !matchNamePath(item.key, key));
48+
this.kvs.delete(normalize(key));
4549
}
4650

51+
// Since we only use this in test, let simply realize this
4752
public map<U>(callback: (kv: KV<T>) => U) {
48-
return this.list.map(callback);
53+
return [...this.kvs.entries()].map(([key, value]) => {
54+
const cells = key.split(SPLIT);
55+
56+
return callback({
57+
key: cells.map(cell => {
58+
const [, type, unit] = cell.match(/^([^:]*):(.*)$/);
59+
return type === 'number' ? Number(unit) : unit;
60+
}),
61+
value,
62+
});
63+
});
4964
}
5065

51-
public toJSON(): { [name: string]: T } {
52-
const json: { [name: string]: T } = {};
66+
public toJSON(): Record<string, T> {
67+
const json: Record<string, T> = {};
5368
this.map(({ key, value }) => {
5469
json[key.join('.')] = value;
5570
return null;

0 commit comments

Comments
 (0)