Skip to content

Commit 71a3f59

Browse files
Merge pull request #116 from christopher-buss/feature/janitor-connections
perf: cleanup player connections
2 parents 404c0a9 + 67e7809 commit 71a3f59

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

src/server/player/character/character-service.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ export default class CharacterService implements OnStart, OnPlayerJoin {
139139
}
140140

141141
private onRigLoaded(playerEntity: PlayerEntity, rig: CharacterRig): void {
142-
const { name, player } = playerEntity;
142+
const { name, janitor, player } = playerEntity;
143143

144-
addToCollisionGroup(rig, CollisionGroup.Character);
144+
janitor.Add(addToCollisionGroup(rig, CollisionGroup.Character));
145145
rig.AddTag(Tag.PlayerCharacter);
146146
this.characterRigs.set(player, rig);
147147

@@ -150,18 +150,22 @@ export default class CharacterService implements OnStart, OnPlayerJoin {
150150
debug.profilebegin("Lifecycle_Character_Added");
151151
{
152152
for (const { id, event } of this.characterAddedEvents) {
153-
Promise.defer(() => {
154-
debug.profilebegin(id);
155-
event.onCharacterAdded(rig, playerEntity);
156-
}).catch(err => {
157-
this.logger.Error(`Error in character lifecycle ${id}: ${err}`);
158-
});
153+
janitor
154+
.Add(
155+
Promise.defer(() => {
156+
debug.profilebegin(id);
157+
event.onCharacterAdded(rig, playerEntity);
158+
}),
159+
)
160+
.catch(err => {
161+
this.logger.Error(`Error in character lifecycle ${id}: ${err}`);
162+
});
159163
}
160164
}
161165

162166
debug.profileend();
163167

164-
this.characterAppearanceLoaded(player, rig).catch(err => {
168+
janitor.Add(this.characterAppearanceLoaded(player, rig)).catch(err => {
165169
this.logger.Info(`Character appearance did not load for`, player, err);
166170
});
167171
}

src/server/player/player-service.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,16 @@ export default class PlayerService implements OnStart {
175175
debug.profilebegin("Lifecycle_Player_Join");
176176
{
177177
for (const { id, event } of this.playerJoinEvents) {
178-
Promise.defer(() => {
179-
debug.profilebegin(id);
180-
event.onPlayerJoin(playerEntity);
181-
}).catch(err => {
182-
this.logger.Error(`Error in player lifecycle ${id}: ${err}`);
183-
});
178+
janitor
179+
.Add(
180+
Promise.defer(() => {
181+
debug.profilebegin(id);
182+
event.onPlayerJoin(playerEntity);
183+
}),
184+
)
185+
.catch(err => {
186+
this.logger.Error(`Error in player lifecycle ${id}: ${err}`);
187+
});
184188
}
185189
}
186190

src/shared/util/physics-util.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
*
55
* @param object - The instance to add to the collision group.
66
* @param group - The collision group to add the object to.
7+
* @param trackNewDescendants - Whether to handle future descendants of the
8+
* object to the collision group. Defaults to false.
9+
* @returns A connection that will add any new descendants to the collision
10+
* group.
711
*/
8-
export function addToCollisionGroup(object: Instance, group: string): void {
12+
export function addToCollisionGroup(
13+
object: Instance,
14+
group: string,
15+
trackNewDescendants?: true,
16+
): () => void;
17+
export function addToCollisionGroup(
18+
object: Instance,
19+
group: string,
20+
trackNewDescendants?: false,
21+
): undefined;
22+
export function addToCollisionGroup(
23+
object: Instance,
24+
group: string,
25+
trackNewDescendants = false,
26+
): (() => void) | undefined {
927
if (object.IsA("BasePart")) {
1028
object.CollisionGroup = group;
1129
}
@@ -15,4 +33,18 @@ export function addToCollisionGroup(object: Instance, group: string): void {
1533
descendant.CollisionGroup = group;
1634
}
1735
}
36+
37+
if (!trackNewDescendants) {
38+
return undefined;
39+
}
40+
41+
const connection = object.DescendantAdded.Connect(descendant => {
42+
if (descendant.IsA("BasePart")) {
43+
descendant.CollisionGroup = group;
44+
}
45+
});
46+
47+
return () => {
48+
connection.Disconnect();
49+
};
1850
}

0 commit comments

Comments
 (0)