Skip to content

Commit

Permalink
Refactor structure of presence updates
Browse files Browse the repository at this point in the history
This commit changes how we handle updates for presence spaces. An presence update becomes a PresenceMember:

export type PresenceMember = {
  data: {
    profileUpdate: {
      id: string | null;
      current: ProfileData;
    };
    locationUpdate: {
      id: string | null;
      previous: unknown;
      current: unknown;
    };
  };
} & Omit<Types.PresenceMessage, 'data'>;

Which then gets translated for the developer to a SpaceMember:

export type SpaceMember = {
  clientId: string;
  connectionId: string;
  isConnected: boolean;
  profileData: ProfileData;
  location: unknown;
  lastEvent: {
    name: Types.PresenceAction;
    timestamp: number;
  };
};

data on PresenceMember contains the last update for profileData an location. The current key is the value of these properties on SpaceMember.

profileUpdate and locationUpdate contain an id. This id is set on publish, but only when we are providing new data, not copying already set data.
The handlers check the id to decide if an update should be emitted (it will still be applied, and it should be the same).
  • Loading branch information
Dominik Piatek committed Aug 8, 2023
1 parent 704ed02 commit 10a2350
Show file tree
Hide file tree
Showing 22 changed files with 1,277 additions and 1,493 deletions.
10 changes: 8 additions & 2 deletions __mocks__/ably/promises/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const mockPresence = {
enter: methodReturningVoidPromise,
leave: methodReturningVoidPromise,
subscriptions: {
once: async (_, fn) => {
return await fn();
once: (_: unknown, fn: Function) => {
fn();
},
},
subscribe: () => {},
Expand Down Expand Up @@ -51,8 +51,11 @@ class MockRealtime {
};
public connection: {
id?: string;
state: string;
};

public time() {}

constructor() {
this.channels = {
get: () => mockChannel,
Expand All @@ -62,7 +65,10 @@ class MockRealtime {
};
this.connection = {
id: '1',
state: 'connected',
};

this['options'] = {};
}
}

Expand Down
7 changes: 7 additions & 0 deletions __mocks__/nanoid/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const nanoidId = 'NanoidID';

function nanoid(): string {
return nanoidId;
}

export { nanoid, nanoidId };
Loading

0 comments on commit 10a2350

Please sign in to comment.