From bda17546caff4e17627061008f59e6300d3b8b81 Mon Sep 17 00:00:00 2001 From: Laura Martin Date: Thu, 17 Aug 2023 16:31:50 +0100 Subject: [PATCH] fixup! Update class definitions --- docs/class-definitions.md | 178 ++++++++++++++++++++++++++------------ 1 file changed, 122 insertions(+), 56 deletions(-) diff --git a/docs/class-definitions.md b/docs/class-definitions.md index aa05b20c..13ec5927 100644 --- a/docs/class-definitions.md +++ b/docs/class-definitions.md @@ -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). @@ -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. @@ -203,7 +218,28 @@ 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 = () => ; +``` + +Example: + +```ts +const myMember = space.members.getSelf(); ``` ### getAll @@ -211,15 +247,27 @@ space.members.unsubscribe('enter'); 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 @@ -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 = () => ; ``` +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; +``` -- #### **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; +``` + +Example: ```ts -space.locations.unsubscribe('locationUpdate'); +const otherLocations = await space.locations.getOthers() ``` ## Related types @@ -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; -``` +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 @@ -406,40 +480,32 @@ Example: const selfPosition = space.cursors.getSelf(); ``` -### getOthers +### getAll Get the last CursorUpdate for each connection. ```ts -type getOthers = () => Record; +type getAll = () => Record; ``` 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; +``` -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