Skip to content

Commit

Permalink
locks: Fix JSON serialising lock attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Lewis Marshall <lewis.marshall@ably.com>
  • Loading branch information
lmars committed Aug 11, 2023
1 parent 8272d29 commit 5faea09
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 3 additions & 3 deletions examples/locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -21,8 +21,8 @@ class SlideElement {
}

// the attributes to use when locking this SlideElement.
lockAttributes(): Map<string, string> {
const attributes = new Map();
lockAttributes(): LockAttributes {
const attributes = new LockAttributes();
attributes.set('slideId', this.slideId);
attributes.set('elementId', this.elementId);
return attributes;
Expand Down
4 changes: 2 additions & 2 deletions src/Locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 });
Expand Down
10 changes: 8 additions & 2 deletions src/Locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ export type LockRequest = {
id: string;
status: LockStatus;
timestamp: number;
attributes?: Map<string, string>;
attributes?: LockAttributes;
reason?: Types.ErrorInfo;
};

export class LockAttributes extends Map<string, string> {
toJSON() {
return Object.fromEntries(this);
}
}

interface LockOptions {
attributes: Map<string, string>;
attributes: LockAttributes;
}

type LockEventMap = {
Expand Down

0 comments on commit 5faea09

Please sign in to comment.