Skip to content

types: make instrumentations' types more succinct #8558

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

Merged
merged 8 commits into from
Mar 13, 2024
38 changes: 17 additions & 21 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ function createReadonlyMethod(type: TriggerOpTypes): Function {
}
}

type Instrumentations = Record<string | symbol, Function | number>

function createInstrumentations() {
const mutableInstrumentations: Record<string, Function | number> = {
const mutableInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) {
return get(this, key)
},
Expand All @@ -253,7 +255,7 @@ function createInstrumentations() {
forEach: createForEach(false, false),
}

const shallowInstrumentations: Record<string, Function | number> = {
const shallowInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) {
return get(this, key, false, true)
},
Expand All @@ -268,7 +270,7 @@ function createInstrumentations() {
forEach: createForEach(false, true),
}

const readonlyInstrumentations: Record<string, Function | number> = {
const readonlyInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) {
return get(this, key, true)
},
Expand All @@ -285,7 +287,7 @@ function createInstrumentations() {
forEach: createForEach(true, false),
}

const shallowReadonlyInstrumentations: Record<string, Function | number> = {
const shallowReadonlyInstrumentations: Instrumentations = {
get(this: MapTypes, key: unknown) {
return get(this, key, true, true)
},
Expand All @@ -302,24 +304,18 @@ function createInstrumentations() {
forEach: createForEach(true, true),
}

const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator]
const iteratorMethods = [
'keys',
'values',
'entries',
Symbol.iterator,
] as const

iteratorMethods.forEach(method => {
mutableInstrumentations[method as string] = createIterableMethod(
method,
false,
false,
)
readonlyInstrumentations[method as string] = createIterableMethod(
method,
true,
false,
)
shallowInstrumentations[method as string] = createIterableMethod(
method,
false,
true,
)
shallowReadonlyInstrumentations[method as string] = createIterableMethod(
mutableInstrumentations[method] = createIterableMethod(method, false, false)
readonlyInstrumentations[method] = createIterableMethod(method, true, false)
shallowInstrumentations[method] = createIterableMethod(method, false, true)
shallowReadonlyInstrumentations[method] = createIterableMethod(
method,
true,
true,
Expand Down