Skip to content

Commit

Permalink
fixup! Update class definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
surminus committed Aug 17, 2023
1 parent bf32ade commit bda1754
Showing 1 changed file with 122 additions and 56 deletions.
178 changes: 122 additions & 56 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 @@ -162,23 +170,30 @@ Handles members within a space.

### 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:

- #### **update**

Listen to updates to members.
Listen to all updates for members in a space.

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

This is the same as not specifying the event:

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

Triggers on:
- presence updates ([`enter`, `leave`, `update` and `present` events](https://ably.com/docs/presence-occupancy/presence?lang=javascript))
- presence updates ([`enter`, `leave`, `update` 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.
Expand All @@ -203,23 +218,56 @@ Available events:
Remove all event listeners, all event listeners for an event, or specific listeners. See [EventEmitter](/docs/usage.md#event-emitters) for detailed usage.

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

### getSelf

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 getSelf = () => <SpaceMember | undefined>;
```

Example:

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

### getAll

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)_)

```ts
space.members.getAll();
type getAll = () => SpaceMember[];
```
### getSelf
Example:
Gets the [SpaceMember](#spacemember) object which relates to the local connection. Will return `undefined` if the client hasn't entered the space yet.
```ts
const allMembers = space.members.getAll();
```
### getOthers
Returns an array of all [SpaceMember](#spacemember) objects (members) currently in the space, excluding your own member object.
```ts
type getSelf = () => SpaceMember[];
```
Example:
```ts
space.members.getSelf();
const otherMembers = space.members.getOthers();
```
## Related Types
Expand Down Expand Up @@ -285,51 +333,69 @@ Set your current location. [Location](#location-1) can be any JSON-serializable
```ts
type set = (update: Location) => void;
```
### getSelf
Get location for self
```ts
await space.locations.getSelf()
```
### subscribe
### getAll
Listen to events for locations. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.
Available events:
- #### **locationUpdate**
Fires when a member updates their location. The argument supplied to the event listener is an [LocationUpdate](#locationupdate-1).
```ts
space.locations.subscribe('locationUpdate', (locationUpdate: LocationUpdate) => {});
```
Get location for all members
### 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.getAll()
space.locations.unsubscribe('locationUpdate');
```
### getOthers
### getSelf
Get location for other members
Get location for self.
```ts
await space.locations.getOthers()
type getSelf = () => <Location | undefined>;
```
Example:
```ts
const myLocation = await space.locations.getSelf();
```
### subscribe
### getAll
Listen to events for locations. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.
Get location for all members.
Available events:
```ts
type getAll = () => Record<ConnectionId, Location>;
```
- #### **locationUpdate**
Example:
Fires when a member updates their location. The argument supplied to the event listener is an [LocationUpdate](#locationupdate-1).
```ts
const allLocations = await space.locations.getAll();
```
```ts
space.locations.subscribe('locationUpdate', (locationUpdate: LocationUpdate) => {});
```
### getOthers
### unsubscribe
Get location for other members
Remove all event listeners, all event listeners for an event, or specific listeners. See [EventEmitter](/docs/usage.md#event-emitters) for detailed usage.
```ts
type getOthers = () => Record<ConnectionId, Location>;
```
Example:
```ts
space.locations.unsubscribe('locationUpdate');
const otherLocations = await space.locations.getOthers()
```
## Related types
Expand Down Expand Up @@ -378,18 +444,26 @@ window.addEventListener('mousemove', ({ clientX, clientY }) => {
});
```
### getAll
### subscribe
Get the last CursorUpdate for each connection.
Listen to `CursorUpdate` events. See [EventEmitter](/docs/usage.md#event-emitters) for overloading usage.
```ts
type getAll = () => Record<ConnectionId, CursorUpdate>;
```
Available events:
Example:
- #### **cursorsUpdate**
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('cursorsUpdate', (cursorUpdate: CursorUpdate) => {});
```
### 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
const lastPositions = space.cursors.getAll();
space.cursors.unsubscribe('update');
```
### getSelf
Expand All @@ -406,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 lastPositions = 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.
### unsubscribe
```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.unsubscribe('update');
const otherPositions = space.cursors.getOthers();
```
## Related types
Expand Down

0 comments on commit bda1754

Please sign in to comment.