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

Commit

Permalink
feat: update WIO channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Raspincel committed Dec 13, 2023
1 parent f8005c1 commit 3fecc4d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
47 changes: 33 additions & 14 deletions src/services/realtime/ably/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import Ably from 'ably';
import {
MOCK_ABLY_PARTICIPANT,
MOCK_LOCAL_PARTICIPANT,
MOCK_ABLY_PARTICIPANT_DATA_1,
} from '../../../../__mocks__/participants.mock';
import { TranscriptState } from '../../../common/types/events.types';
import { ParticipantType } from '../../../common/types/participant.types';
import { Participant, ParticipantType } from '../../../common/types/participant.types';
import { RealtimeStateTypes } from '../../../common/types/realtime.types';

import { AblyParticipant, AblyRealtimeData } from './types';
Expand Down Expand Up @@ -1714,41 +1715,59 @@ describe('AblyRealtimeService', () => {
MOCK_ABLY_PARTICIPANT;
});

test('should subscribe to update channel on enter', () => {
expect(AblyRealtimeServiceInstance['presenceWIOChannel'].publish).toBeCalledWith(
'update',
MOCK_LOCAL_PARTICIPANT,
);
});

test('should publish new data on update', () => {
const id = 'unit-test-participant-id';
const isPrivate = true;
AblyRealtimeServiceInstance.updateWIOParticipant(id, isPrivate);
expect(AblyRealtimeServiceInstance['presenceWIOChannel'].publish).toBeCalledWith('update', {
AblyRealtimeServiceInstance.setPrivateWIOParticipant(id, isPrivate);
expect(AblyRealtimeServiceInstance['presenceWIOChannel'].publish).toBeCalledWith('private', {
id,
isPrivate,
});
});

test('should update participant list and publish new list', () => {
test('should update participant list and publish new list on private', () => {
const isPrivate = 'value';
AblyRealtimeServiceInstance['presenceWIOObserver'].publish = jest.fn();
AblyRealtimeServiceInstance['privateModeWIOObserver'].publish = jest.fn();

const data = {
id: MOCK_ABLY_PARTICIPANT.data.id,
isPrivate,
};

AblyRealtimeServiceInstance['onWIOChannelUpdate']({ data });
AblyRealtimeServiceInstance['onSetPrivate']({ data });

expect(
AblyRealtimeServiceInstance['participants'][MOCK_ABLY_PARTICIPANT.data.id].data.isPrivate,
).toEqual(isPrivate);

expect(AblyRealtimeServiceInstance['presenceWIOObserver'].publish).toBeCalledWith(
expect(AblyRealtimeServiceInstance['privateModeWIOObserver'].publish).toBeCalledWith(
AblyRealtimeServiceInstance['participants'],
);
});

test('should publish to gatherWIOObserver when calling setGather', () => {
const data = {
...MOCK_ABLY_PARTICIPANT_DATA_1,
gather: MOCK_ABLY_PARTICIPANT_DATA_1.id,
} as Participant;

AblyRealtimeServiceInstance['gatherWIOObserver'].publish = jest.fn();
AblyRealtimeServiceInstance['onSetGather'](data);

expect(AblyRealtimeServiceInstance['gatherWIOObserver'].publish).toBeCalledWith(data);
});

test('should publish to WIO Channel when calling setGather', () => {
const data = {
...MOCK_ABLY_PARTICIPANT_DATA_1,
gather: MOCK_ABLY_PARTICIPANT_DATA_1.id,
} as Participant;

AblyRealtimeServiceInstance.setGatherWIOParticipant(data);

expect(AblyRealtimeServiceInstance['presenceWIOChannel'].publish).toBeCalledWith('gather', {
...data,
});
});
});
});
23 changes: 15 additions & 8 deletions src/services/realtime/ably/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,21 +1386,28 @@ export default class AblyRealtimeService extends RealtimeService implements Ably
public enterWIOChannel = (participant: Participant): void => {
if (!this.presenceWIOChannel) {
this.presenceWIOChannel = this.client.channels.get(`${this.roomId.toLowerCase()}-wio`);
this.presenceWIOChannel.attach();

this.presenceWIOChannel.subscribe('update', this.onWIOChannelUpdate);
}

this.presenceWIOChannel.publish('update', participant);
this.presenceWIOChannel.attach();
this.presenceWIOChannel.subscribe('private', this.onSetPrivate);
this.presenceWIOChannel.subscribe('gather', this.onSetGather);
};

private onWIOChannelUpdate = ({ data: { id, isPrivate } }): void => {
private onSetPrivate = ({ data: { id, isPrivate } }): void => {
this.participants[id].data.isPrivate = isPrivate;
this.presenceWIOObserver.publish(this.participants);
this.privateModeWIOObserver.publish(this.participants);
};

private onSetGather = (data: Participant): void => {
this.gatherWIOObserver.publish(data);
};

public setPrivateWIOParticipant = (id: string, isPrivate: boolean): void => {
this.presenceWIOChannel.publish('private', { id, isPrivate });
};

public updateWIOParticipant = (id: string, isPrivate: boolean): void => {
this.presenceWIOChannel.publish('update', { id, isPrivate });
public setGatherWIOParticipant = (data): void => {
this.presenceWIOChannel.publish('gather', { ...data });
};

/** Presence 3D */
Expand Down
6 changes: 4 additions & 2 deletions src/services/realtime/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class RealtimeService implements DefaultRealtimeService {
public authenticationObserver: Observer;
public commentsObserver: Observer;
public presenceMouseObserver: Observer;
public presenceWIOObserver: Observer;
public privateModeWIOObserver: Observer;
public gatherWIOObserver: Observer;
public presenceMouseParticipantLeaveObserver: Observer;
public presenceMouseParticipantJoinedObserver: Observer;
public presenceSlotsInfosObserver: Observer;
Expand Down Expand Up @@ -60,7 +61,8 @@ export class RealtimeService implements DefaultRealtimeService {

// presence mouse
this.presenceMouseObserver = new Observer({ logger: this.logger });
this.presenceWIOObserver = new Observer({ logger: this.logger });
this.privateModeWIOObserver = new Observer({ logger: this.logger });
this.gatherWIOObserver = new Observer({ logger: this.logger });

this.presenceMouseParticipantLeaveObserver = new Observer({ logger: this.logger });
this.presenceMouseParticipantJoinedObserver = new Observer({ logger: this.logger });
Expand Down

0 comments on commit 3fecc4d

Please sign in to comment.