Skip to content

Commit d8cbba6

Browse files
committed
perf: faster chainable lookup
1 parent a85f725 commit d8cbba6

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

packages/cbjs/src/services/kv/lookupIn/ChainableLookupIn.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from '../../../clusterTypes/clusterTypes.js';
2323
import type { AnyCollection } from '../../../clusterTypes/index.js';
2424
import type {
25-
LookupInSpecResults,
25+
LookupInSpecResult,
2626
MakeLookupInSpec,
2727
} from '../../../clusterTypes/kv/lookup/lookupIn.types.js';
2828
import type {
@@ -38,60 +38,63 @@ import type { LookupMethodName } from './types.js';
3838
// prettier-ignore
3939
type LookupResult<
4040
Method extends LookupMethodName,
41-
MatchingDocDef,
42-
SpecDefinitions,
41+
SpecResults extends ReadonlyArray<unknown>,
4342
ThrowOnSpecError extends boolean,
4443
> =
4544
Method extends 'lookupIn' ?
46-
LookupInResult<LookupInSpecResults<SpecDefinitions, MatchingDocDef>, ThrowOnSpecError> :
45+
LookupInResult<SpecResults, ThrowOnSpecError> :
4746
Method extends 'lookupInAnyReplica' ?
4847
LookupInReplicaResult<
49-
LookupInSpecResults<SpecDefinitions, MatchingDocDef>,
48+
SpecResults,
5049
ThrowOnSpecError
5150
> :
5251
Method extends 'lookupInAllReplicas' ?
5352
LookupInReplicaResult<
54-
LookupInSpecResults<SpecDefinitions, MatchingDocDef>,
53+
SpecResults,
5554
ThrowOnSpecError
5655
>[] :
5756
never
5857
;
5958

60-
type ThisAnd<T, Spec extends LookupInSpec> =
59+
type ThisAnd<T, Spec> =
6160
T extends ChainableLookupIn<
6261
infer C,
6362
infer Method,
6463
infer Key,
65-
infer SpecDefinitions,
64+
infer SpecResults,
6665
infer ThrowOnSpecError,
67-
infer Doc
66+
infer Def
6867
>
69-
? ChainableLookupIn<C, Method, Key, [...SpecDefinitions, Spec], ThrowOnSpecError, Doc>
68+
? ChainableLookupIn<
69+
C,
70+
Method,
71+
Key,
72+
[...SpecResults, LookupInSpecResult<Spec, Def>],
73+
ThrowOnSpecError,
74+
Def
75+
>
7076
: never;
7177

7278
export class ChainableLookupIn<
7379
out C extends AnyCollection,
7480
out Method extends LookupMethodName,
7581
out Key extends ExtractCollectionJsonDocKey<C>,
76-
out SpecDefinitions extends ReadonlyArray<LookupInSpec>,
82+
in out SpecResults extends ReadonlyArray<unknown>,
7783
out ThrowOnSpecError extends boolean,
7884
in out Def extends CollectionDocDefMatchingKey<C, Key> = CollectionDocDefMatchingKey<
7985
C,
8086
Key
8187
>,
82-
> implements Promise<LookupResult<Method, Def, SpecDefinitions, ThrowOnSpecError>>
88+
> implements Promise<LookupResult<Method, SpecResults, ThrowOnSpecError>>
8389
{
8490
// Promise stuff
8591

8692
[Symbol.toStringTag] = 'ChainableLookupInSpecs';
8793

88-
then<
89-
TResult1 = LookupResult<Method, Def, SpecDefinitions, ThrowOnSpecError>,
90-
TResult2 = never,
91-
>(
94+
then<TResult1 = LookupResult<Method, SpecResults, ThrowOnSpecError>, TResult2 = never>(
9295
onFulfilled?:
9396
| ((
94-
value: LookupResult<Method, Def, SpecDefinitions, ThrowOnSpecError>
97+
value: LookupResult<Method, SpecResults, ThrowOnSpecError>
9598
) => TResult1 | PromiseLike<TResult1>)
9699
| undefined
97100
| null,
@@ -105,13 +108,13 @@ export class ChainableLookupIn<
105108

106109
catch<TResult = never>(
107110
onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null | undefined
108-
): Promise<LookupResult<Method, Def, SpecDefinitions, ThrowOnSpecError> | TResult> {
111+
): Promise<LookupResult<Method, SpecResults, ThrowOnSpecError> | TResult> {
109112
return this.then(undefined, onRejected);
110113
}
111114

112115
finally(
113116
onFinally?: (() => void) | null | undefined
114-
): Promise<LookupResult<Method, Def, SpecDefinitions, ThrowOnSpecError>> {
117+
): Promise<LookupResult<Method, SpecResults, ThrowOnSpecError>> {
115118
return this.then(
116119
(value) => {
117120
onFinally?.();
@@ -130,7 +133,7 @@ export class ChainableLookupIn<
130133
protected method: Method,
131134
protected key: Key,
132135
protected options: LookupInOptions<ThrowOnSpecError> | undefined,
133-
protected specs: SpecDefinitions
136+
protected specs: LookupInSpec[]
134137
) {}
135138

136139
static for<
@@ -148,12 +151,11 @@ export class ChainableLookupIn<
148151
}
149152

150153
push<Spec extends LookupInSpec>(spec: Spec): ThisAnd<this, Spec> {
151-
const newSpecs: [...SpecDefinitions, Spec] = [...this.getSpecs(), spec];
152-
this.specs = newSpecs as never;
153-
return this as never as ThisAnd<this, Spec>;
154+
this.specs = [...this.getSpecs(), spec];
155+
return this as never as ThisAnd<this, LookupInSpecResult<Spec, Def>>;
154156
}
155157

156-
execute(): Promise<LookupResult<Method, Def, SpecDefinitions, ThrowOnSpecError>> {
158+
execute(): Promise<LookupResult<Method, SpecResults, ThrowOnSpecError>> {
157159
const lookupMethod = this.collection[this.method as Method & keyof C];
158160
const lookup = lookupMethod.bind(this.collection) as any;
159161

@@ -231,7 +233,7 @@ export class ChainableLookupIn<
231233
/**
232234
* Return the array of specs.
233235
*/
234-
getSpecs(): SpecDefinitions {
236+
getSpecs(): LookupInSpec[] {
235237
return this.specs;
236238
}
237239
}

0 commit comments

Comments
 (0)