Skip to content

Commit 2e3dcca

Browse files
lauckhartApollon77
andauthored
More descriptive "with" functions (#1379)
Includes MutableEndpointType.withBehaviors and ClusterBehavior.withFeatures The existing "with" now is a shortcut alias. Co-authored-by: Ingo Fischer <[email protected]>
1 parent b0fa14c commit 2e3dcca

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

packages/node/src/behavior/cluster/ClusterBehavior.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class ClusterBehavior extends Behavior {
106106
/**
107107
* Create a new behavior with different cluster features.
108108
*/
109-
static with<
109+
static withFeatures<
110110
This extends ClusterBehavior.Type,
111111
const FeaturesT extends ClusterComposer.FeatureSelection<This["cluster"]>,
112112
>(this: This, ...features: FeaturesT) {
@@ -117,6 +117,16 @@ export class ClusterBehavior extends Behavior {
117117
return this.for(newCluster);
118118
}
119119

120+
/**
121+
* Alias for {@link withFeatures}.
122+
*/
123+
static with<
124+
This extends ClusterBehavior.Type,
125+
const FeaturesT extends ClusterComposer.FeatureSelection<This["cluster"]>,
126+
>(this: This, ...features: FeaturesT) {
127+
return this.withFeatures<This, FeaturesT>(...features);
128+
}
129+
120130
/**
121131
* Create a new behavior with modified cluster elements.
122132
*/
@@ -231,21 +241,44 @@ export namespace ClusterBehavior {
231241
// Prior to TS 5.4 could do this. Sadly typing no longer carries through on these... This["cluster"] reverts
232242
// to ClusterType). So we have to define the long way.
233243
//
234-
// This also means intellisense doesn't work unless we copy comments here (or move here and cast ClusterBehavior
235-
// to ClusterBehavior.Type).
236-
//
237244
// - for: typeof ClusterBehavior.for;
238245
// - with: typeof ClusterBehavior.with;
239246
// - alter: typeof ClusterBehavior.alter;
240247
// - set: typeof ClusterBehavior.set;
241248
// - enable: typeof ClusterBehavior.enable;
249+
//
250+
// This also means intellisense doesn't work unless we copy comments here (or move here and cast ClusterBehavior
251+
// to ClusterBehavior.Type). Currently we do the former.
242252

253+
/**
254+
* Create a new behavior for a specific {@link ClusterType}.
255+
*
256+
* If you invoke directly on {@link ClusterBehavior} you will receive a new implementation that reports all commands
257+
* as unimplemented.
258+
*
259+
* If you invoke on an existing subclass, you will receive a new implementation with the cluster in the subclass
260+
* replaced. You should generally only do this with a {@link ClusterType} with the same ID.
261+
*/
243262
for<This extends ClusterBehavior.Type, const ClusterT extends ClusterType>(
244263
this: This,
245264
cluster: ClusterT,
246265
schema?: Schema,
247266
): ClusterBehavior.Type<ClusterT, This>;
248267

268+
/**
269+
* Create a new behavior with different cluster features.
270+
*/
271+
withFeatures<
272+
This extends ClusterBehavior.Type,
273+
const FeaturesT extends ClusterComposer.FeatureSelection<This["cluster"]>,
274+
>(
275+
this: This,
276+
...features: FeaturesT
277+
): ClusterBehavior.Type<ClusterComposer.WithFeatures<This["cluster"], FeaturesT>, This>;
278+
279+
/**
280+
* Alias for {@link withFeatures}.
281+
*/
249282
with<
250283
This extends ClusterBehavior.Type,
251284
const FeaturesT extends ClusterComposer.FeatureSelection<This["cluster"]>,
@@ -254,6 +287,9 @@ export namespace ClusterBehavior {
254287
...features: FeaturesT
255288
): ClusterBehavior.Type<ClusterComposer.WithFeatures<This["cluster"], FeaturesT>, This>;
256289

290+
/**
291+
* Create a new behavior with modified cluster elements.
292+
*/
257293
alter<
258294
This extends ClusterBehavior.Type,
259295
const AlterationsT extends ElementModifier.Alterations<This["cluster"]>,

packages/node/src/endpoint/type/MutableEndpoint.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export interface MutableEndpoint extends EndpointType {
2525
/**
2626
* Define an endpoint like this one with additional and/or replacement server behaviors.
2727
*/
28+
withBehaviors(...behaviors: SupportedBehaviors.List): MutableEndpoint;
29+
30+
/**
31+
* Alias for {@link withBehaviors}.
32+
*/
2833
with(...behaviors: SupportedBehaviors.List): MutableEndpoint;
2934
}
3035

@@ -96,6 +101,13 @@ export namespace MutableEndpoint {
96101
/**
97102
* Define an endpoint like this one with additional and/or replacement server behaviors.
98103
*/
104+
withBehaviors<const BL extends SupportedBehaviors.List>(
105+
...behaviors: BL
106+
): With<B, SupportedBehaviors.With<SB, BL>>;
107+
108+
/**
109+
* Alias for {@link withBehaviors}.
110+
*/
99111
with<const BL extends SupportedBehaviors.List>(...behaviors: BL): With<B, SupportedBehaviors.With<SB, BL>>;
100112
};
101113
}

0 commit comments

Comments
 (0)