diff --git a/examples/locks.ts b/examples/locks.ts index 6569902e..872725ee 100644 --- a/examples/locks.ts +++ b/examples/locks.ts @@ -2,7 +2,7 @@ // application. import Ably from 'ably/promises'; import Spaces from '../dist/cjs/Spaces.js'; -import { Lock } from '../dist/cjs/Locks.js'; +import { Lock, LockAttributes } from '../dist/cjs/Locks.js'; // SlideElement represents an element on a slide which a member can both be // located at and attempt to lock (e.g. an editable text box). @@ -21,8 +21,8 @@ class SlideElement { } // the attributes to use when locking this SlideElement. - lockAttributes(): Map { - const attributes = new Map(); + lockAttributes(): LockAttributes { + const attributes = new LockAttributes(); attributes.set('slideId', this.slideId); attributes.set('elementId', this.elementId); return attributes; diff --git a/src/Locks.test.ts b/src/Locks.test.ts index 2f0b7b4b..a4ce6256 100644 --- a/src/Locks.test.ts +++ b/src/Locks.test.ts @@ -3,7 +3,7 @@ import { Realtime, Types } from 'ably/promises'; import Space from './Space.js'; import type { SpaceMember } from './types.js'; -import { LockStatus } from './Locks.js'; +import { LockAttributes, LockStatus } from './Locks.js'; import { createPresenceMessage } from './utilities/test/fakes.js'; interface SpaceTestContext { @@ -70,7 +70,7 @@ describe('Locks (mockClient)', () => { const presenceUpdate = vi.spyOn(presence, 'update'); const lockID = 'test'; - const attributes = new Map(); + const attributes = new LockAttributes(); attributes.set('key1', 'foo'); attributes.set('key2', 'bar'); const req = await space.locks.acquire(lockID, { attributes }); diff --git a/src/Locks.ts b/src/Locks.ts index 4fa0055e..f1da12b7 100644 --- a/src/Locks.ts +++ b/src/Locks.ts @@ -22,12 +22,18 @@ export type LockRequest = { id: string; status: LockStatus; timestamp: number; - attributes?: Map; + attributes?: LockAttributes; reason?: Types.ErrorInfo; }; +export class LockAttributes extends Map { + toJSON() { + return Object.fromEntries(this); + } +} + interface LockOptions { - attributes: Map; + attributes: LockAttributes; } type LockEventMap = {