Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #735 from SuperViz/fix/destroy-sdk-erros
Browse files Browse the repository at this point in the history
fix: only update active instances when a component is added or removed
  • Loading branch information
carlossantos74 authored Aug 7, 2024
2 parents 18f08fb + 6288215 commit ddd8a64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/core/launcher/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('Launcher', () => {

LauncherInstance.addComponent(MOCK_COMPONENT);

expect(MOCK_COMPONENT.attach).not.toBeCalled();
expect(MOCK_COMPONENT.attach).not.toHaveBeenCalled();
});

test('should remove component', () => {
Expand Down
47 changes: 20 additions & 27 deletions src/core/launcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,25 @@ export class Launcher extends Observable implements DefaultLauncher {
});

this.activeComponents.push(component.name);
this.activeComponentsInstances.push(component);

if (this.activeComponentsInstances.some((c) => c.name === component.name)) {
this.activeComponentsInstances = this.activeComponentsInstances.map((ac) => {
if (ac.name === component.name) {
return component;
}
return ac;
});
} else {
this.activeComponentsInstances.push(component);
}

localParticipant.publish({
...this.participant,
...localParticipant.value,
activeComponents: this.activeComponents,
});

this.room.presence.update({
...this.participant,
...localParticipant.value,
slot: this.slotService.slot,
activeComponents: this.activeComponents,
});
Expand Down Expand Up @@ -141,7 +151,7 @@ export class Launcher extends Observable implements DefaultLauncher {
*/
public removeComponent = (component: Partial<BaseComponent>): void => {
if (!this.activeComponents.includes(component.name)) {
const message = `Component ${component.name} is not initialized yet.`;
const message = `[SuperViz] Component ${component.name} is not initialized yet.`;
this.logger.log(message);
console.error(message);
return;
Expand Down Expand Up @@ -176,15 +186,14 @@ export class Launcher extends Observable implements DefaultLauncher {
this.activeComponents = [];
this.activeComponentsInstances = [];

useGlobalStore().destroy();
useGlobalStore()?.destroy();

this.eventBus.destroy();
this.eventBus?.destroy();
this.eventBus = undefined;

this.room?.presence.off(Socket.PresenceEvents.JOINED_ROOM);
this.room?.presence.off(Socket.PresenceEvents.LEAVE);
this.room?.presence.off(Socket.PresenceEvents.UPDATE);
this.ioc.stateSubject.unsubscribe();
this.ioc?.destroy();

this.isDestroyed = true;
Expand All @@ -207,20 +216,20 @@ export class Launcher extends Observable implements DefaultLauncher {
const verifications = [
{
isValid: isProvidedFeature,
message: `Component ${component.name} is not enabled in the room`,
message: `[SuperViz] Component ${component.name} is not enabled in the room`,
},
{
isValid: !this.isDestroyed,
message:
'Component can not be added because the superviz room is destroyed. Initialize a new room to add and use components.',
'[SuperViz] Component can not be added because the superviz room is destroyed. Initialize a new room to add and use components.',
},
{
isValid: !isComponentActive,
message: `Component ${component.name} is already active. Please remove it first`,
message: `[SuperViz] Component ${component.name} is already active. Please remove it first`,
},
{
isValid: componentLimit.canUse,
message: `You reached the limit usage of ${component.name}`,
message: `[SuperViz] You reached the limit usage of ${component.name}`,
},
];

Expand Down Expand Up @@ -252,16 +261,6 @@ export class Launcher extends Observable implements DefaultLauncher {
);
};

private onLocalParticipantUpdate = (participant: Participant): void => {
this.activeComponents = participant.activeComponents || [];

if (this.activeComponents.length) {
this.activeComponentsInstances = this.activeComponentsInstances.filter((ac) => {
return this.activeComponents.includes(ac.name);
});
}
};

/**
* @function onLocalParticipantUpdateOnStore
* @description handles the update of the local participant in the store.
Expand All @@ -271,12 +270,6 @@ export class Launcher extends Observable implements DefaultLauncher {
private onLocalParticipantUpdateOnStore = (participant: Participant): void => {
this.participant = participant;
this.activeComponents = participant.activeComponents || [];

if (this.activeComponents.length) {
this.activeComponentsInstances = this.activeComponentsInstances.filter((component) => {
return this.activeComponents.includes(component.name);
});
}
};

private onSameAccount = (): void => {
Expand Down
1 change: 1 addition & 0 deletions src/services/io/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class IOC {
* @returns {void}
*/
public destroy(): void {
this.stateSubject.complete();
this.client.destroy();
}

Expand Down

0 comments on commit ddd8a64

Please sign in to comment.