Skip to content

Commit

Permalink
Update class definitions
Browse files Browse the repository at this point in the history
I've tried to organise this to include the members namespace, and tried
to keep the examples consistent.
  • Loading branch information
surminus committed Aug 22, 2023
1 parent c490f38 commit 3990730
Showing 1 changed file with 148 additions and 71 deletions.
219 changes: 148 additions & 71 deletions docs/class-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ An instance of a Space created using [spaces.get](#get). Inherits from [EventEmi

## Properties

### members

An instance of [Members](#members).

```ts
type members = instanceof Members;
```

### cursors

An instance of [Cursors](#cursors).
Expand Down Expand Up @@ -154,66 +162,112 @@ await space.updateProfileData((oldProfileData) => {
})
```

# Members

Handles members within a space.

## Methods

### subscribe

Listen to events for the space. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.
Listen to member events for the space. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.

Available events:

- #### **membersUpdate**
- #### **update**

Listen to updates to members.
Listen to profile data updates of members.

```ts
space.subscribe('membersUpdate', (members: SpaceMember[]) => {});
space.members.subscribe('update', (member: SpaceMember) => {});
```

Triggers on:
- presence updates ([`enter`, `leave`, `update` and `present` events](https://ably.com/docs/presence-occupancy/presence?lang=javascript))
- [location updates](#locationupdate)

The argument supplied to the callback is an array of [SpaceMember](#spacemember) (members) objects within the space.
The argument supplied to the callback is the [SpaceMember](#spacemember) object representing the member that triggered the event.

- #### **enter**

Listen to enter events of members.

```ts
space.subscribe('enter', (member: SpaceMember) => {})
space.members.subscribe('enter', (member: SpaceMember) => {})
```
The argument supplied to the callback is a [SpaceMember](#spacemember) object representing the member entering the space.

- #### **leave**
Listen to leave events of members. Note that the leave event will only fire once the [offlineTimeout](#spaceoptions) has passed.

Listen to leave events of members. The leave event will be issued when a member calls `space.leave()` or is disconnected.

```ts
space.subscribe('leave', (member: SpaceMember) => {})
space.members.subscribe('leave', (member: SpaceMember) => {})
```

The argument supplied to the callback is a [SpaceMember](#spacemember) object representing the member leaving the space.

### off
- #### **remove**

Listen to remove events of members. The remove event will be issued when the [offlineTimeout](#spaceoptions) has passed.

```ts
space.members.subscribe('remove', (member: SpaceMember) => {})
```

The argument supplied to the callback is a [SpaceMember](#spacemember) object representing the member removed from the space.

### unsubscribe

Remove all event listeners, all event listeners for an event, or specific listeners. See [EventEmitter](/docs/usage.md#event-emitters) for detailed usage.

```ts
space.off('enter');
// Unsubscribe from all events
space.members.unsubscribe();
// Unsubscribe from enter events
space.members.unsubscribe('enter');
// Unsubscribe from leave events
space.members.unsubscribe('leave');
```

### getMembers
### getSelf

Returns an array of all [SpaceMember](#spacemember) objects (members) currently in the space, including any who have left and not yet timed out. (_see: [offlineTimeout](#spaceoptions)_)
Returns a Promise which resolves to the [SpaceMember](#spacemember) object relating to the local connection. Will resolve to `undefined` if the client hasn't entered the space yet.

```ts
type getMembers = () => SpaceMember[];
type getSelf = () => Promise<SpaceMember | undefined>;
```

### getSelf
Example:

```ts
const myMember = await space.members.getSelf();
```

### getAll

Returns a Promise which resolves to an array of all [SpaceMember](#spacemember) objects (members) currently in the space, including any who have left and not yet timed out. (_see: [offlineTimeout](#spaceoptions)_)

Gets the [SpaceMember](#spacemember) object which relates to the local connection. Will return `undefined` if the client hasn't entered the space yet.
```ts
type getAll = () => Promise<SpaceMember[]>;
```

Example:

```ts
type getSelf = () => SpaceMember | undefined;
const allMembers = await space.members.getAll();
```

### getOthers

Returns a Promise which resolves to an array of all [SpaceMember](#spacemember) objects (members) currently in the space, excluding your own member object.

```ts
type getSelf = () => Promise<SpaceMember[]>;
```

Example:

```ts
const otherMembers = await space.members.getOthers();
```

## Related Types
Expand Down Expand Up @@ -272,53 +326,76 @@ Handles the tracking of member locations within a space. Inherits from [EventEmi

## Methods

### subscribe

Listen to events for locations. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.

Available events:

- #### **update**

Fires when a member updates their location. The argument supplied to the event listener is a [LocationUpdate](#locationupdate-1).

```ts
space.locations.subscribe('update', (locationUpdate: LocationUpdate) => {});
```

### set

Set your current location. [Location](#location-1) can be any JSON-serializable object. Emits a [locationUpdate](#locationupdate) event to all connected clients in this space.

```ts
type set = (update: Location) => void;
type set = (update: Location) => Promise<void>;
```
### getSelf
Get location for self

### unsubscribe

Remove all event listeners, all event listeners for an event, or specific listeners. See [EventEmitter](/docs/usage.md#event-emitters) for detailed usage.

```ts
await space.locations.getSelf()
space.locations.unsubscribe('update');
```
### getAll
Get location for all members

### getSelf

Get location for self.

```ts
await space.locations.getAll()
type getSelf = () => Promise<Location | undefined>;
```
### getOthers
Get location for other members

Example:

```ts
await space.locations.getOthers()
const myLocation = await space.locations.getSelf();
```

### getAll

Get location for all members.

### subscribe
Listen to events for locations. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.
```ts
type getAll = () => Promise<Record<ConnectionId, Location>>;
```

Available events:
Example:

- #### **locationUpdate**
```ts
const allLocations = await space.locations.getAll();
```

Fires when a member updates their location. The argument supplied to the event listener is an [LocationUpdate](#locationupdate-1).
### getOthers

```ts
space.locations.subscribe('locationUpdate', (locationUpdate: LocationUpdate) => {});
```
Get location for other members

### off
```ts
type getOthers = () => Promise<Record<ConnectionId, Location>>;
```

Remove all event listeners, all event listeners for an event, or specific listeners. See [EventEmitter](/docs/usage.md#event-emitters) for detailed usage.
Example:

```ts
space.locations.off('locationUpdate');
const otherLocations = await space.locations.getOthers()
```

## Related types
Expand Down Expand Up @@ -349,6 +426,20 @@ Handles tracking of member cursors within a space. Inherits from [EventEmitter](

## Methods

### subscribe

Listen to `CursorUpdate` events. See [EventEmitter](/docs/usage.md#event-emitters) for overloaded usage.

Available events:

- #### **update**

Emits an event when a new cursor position is set. The argument supplied to the event listener is a [CursorUpdate](#cursorupdate).

```ts
space.cursors.subscribe('update', (cursorUpdate: CursorUpdate) => {});
```

### set

Set the position of a cursor. This will emit a `CursorUpdate` event. If a member has not yet entered the space, this method will error.
Expand All @@ -367,18 +458,12 @@ window.addEventListener('mousemove', ({ clientX, clientY }) => {
});
```

### getAll
Get the last CursorUpdate for each connection.
```ts
type getAll = () => Record<ConnectionId, CursorUpdate>;
```
### unsubscribe

Example:
Remove all event listeners, all event listeners for an event, or specific listeners. See [EventEmitter](/docs/usage.md#event-emitters) for detailed usage.

```ts
const lastPositions = space.cursors.getAll();
space.cursors.unsubscribe('update');
```

### getSelf
Expand All @@ -395,40 +480,32 @@ Example:
const selfPosition = space.cursors.getSelf();
```

### getOthers
### getAll

Get the last CursorUpdate for each connection.

```ts
type getOthers = () => Record<ConnectionId, CursorUpdate>;
type getAll = () => Record<ConnectionId, CursorUpdate>;
```

Example:

```ts
const otherPositions = space.cursors.getOthers();
const allLatestPositions = space.cursors.getAll();
```

### subscribe
Listen to `CursorUpdate` events. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.
Available events:
- #### **cursorsUpdate**
Emits an event when a new cursor position is set. The argument supplied to the event listener is a [CursorUpdate](#cursorupdate).
### getOthers

```ts
space.cursors.subscribe('cursorsUpdate', (cursorUpdate: CursorUpdate) => {});
```
Get the last CursorUpdate for each connection.

### off
```ts
type getOthers = () => Record<ConnectionId, CursorUpdate>;
```

Remove all event listeners, all event listeners for an event, or specific listeners. See [EventEmitter](/docs/usage.md#event-emitters) for detailed usage.
Example:

```ts
space.cursors.off('cursorsUpdate');
const otherPositions = space.cursors.getOthers();
```

## Related types
Expand Down Expand Up @@ -464,4 +541,4 @@ Represent data that can be associated with a cursor update.

```ts
type CursorData = Record<string, unknown>;
```
```

0 comments on commit 3990730

Please sign in to comment.