types: make instrumentations' types more succinct (#8558)

Co-authored-by: Haoqun Jiang <haoqunjiang@gmail.com>
This commit is contained in:
丶远方 2024-03-13 22:57:33 +08:00 committed by GitHub
parent d46df6bdb1
commit 384591a2a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 21 deletions

View File

@ -237,8 +237,10 @@ function createReadonlyMethod(type: TriggerOpTypes): Function {
} }
} }
type Instrumentations = Record<string | symbol, Function | number>
function createInstrumentations() { function createInstrumentations() {
const mutableInstrumentations: Record<string, Function | number> = { const mutableInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) { get(this: MapTypes, key: unknown) {
return get(this, key) return get(this, key)
}, },
@ -253,7 +255,7 @@ function createInstrumentations() {
forEach: createForEach(false, false), forEach: createForEach(false, false),
} }
const shallowInstrumentations: Record<string, Function | number> = { const shallowInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) { get(this: MapTypes, key: unknown) {
return get(this, key, false, true) return get(this, key, false, true)
}, },
@ -268,7 +270,7 @@ function createInstrumentations() {
forEach: createForEach(false, true), forEach: createForEach(false, true),
} }
const readonlyInstrumentations: Record<string, Function | number> = { const readonlyInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) { get(this: MapTypes, key: unknown) {
return get(this, key, true) return get(this, key, true)
}, },
@ -285,7 +287,7 @@ function createInstrumentations() {
forEach: createForEach(true, false), forEach: createForEach(true, false),
} }
const shallowReadonlyInstrumentations: Record<string, Function | number> = { const shallowReadonlyInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) { get(this: MapTypes, key: unknown) {
return get(this, key, true, true) return get(this, key, true, true)
}, },
@ -302,24 +304,18 @@ function createInstrumentations() {
forEach: createForEach(true, true), forEach: createForEach(true, true),
} }
const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator] const iteratorMethods = [
'keys',
'values',
'entries',
Symbol.iterator,
] as const
iteratorMethods.forEach(method => { iteratorMethods.forEach(method => {
mutableInstrumentations[method as string] = createIterableMethod( mutableInstrumentations[method] = createIterableMethod(method, false, false)
method, readonlyInstrumentations[method] = createIterableMethod(method, true, false)
false, shallowInstrumentations[method] = createIterableMethod(method, false, true)
false, shallowReadonlyInstrumentations[method] = createIterableMethod(
)
readonlyInstrumentations[method as string] = createIterableMethod(
method,
true,
false,
)
shallowInstrumentations[method as string] = createIterableMethod(
method,
false,
true,
)
shallowReadonlyInstrumentations[method as string] = createIterableMethod(
method, method,
true, true,
true, true,