Skip to content

Commit

Permalink
chore: Rename presence state key name (#34)
Browse files Browse the repository at this point in the history
To -/p/ since the feature name we landed on is called presence state.
  • Loading branch information
arv authored Dec 15, 2023
1 parent 43232c2 commit ed98815
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 10 additions & 15 deletions packages/reflect-yjs/src/awareness.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {expect, test, beforeEach, suite, afterEach} from 'vitest';
import {Awareness} from './awareness.js';
import {Reflect} from '@rocicorp/reflect/client';
import {resolver} from '@rocicorp/resolver';

import {Mutators, mutators} from './mutators.js';
import {afterEach, beforeEach, expect, suite, test} from 'vitest';
import * as Y from 'yjs';

import {Reflect} from '@rocicorp/reflect/client';
import {Awareness} from './awareness.js';
import {Mutators, mutators} from './mutators.js';

let reflect: Reflect<Mutators>;

Expand All @@ -33,10 +30,10 @@ suite('Awareness', () => {
doc2.clientID = 1;
const aw2 = new Awareness(reflect, 'testName', doc2);

expect(aw1).to.be.an.instanceof(Awareness);
expect(aw1).toBeInstanceOf(Awareness);

const initialState = aw1.getLocalState();
expect(initialState).to.deep.equal(null);
expect(initialState).toBeNull();

const aw1changes: {
added: number[];
Expand Down Expand Up @@ -71,10 +68,8 @@ suite('Awareness', () => {
]),
);

expect(aw1changes).to.deep.equal(aw2changes);
expect(aw2changes).to.deep.equal([
{added: [0, 1], updated: [], removed: []},
]);
expect(aw1changes).toEqual(aw2changes);
expect(aw2changes).toEqual([{added: [0, 1], updated: [], removed: []}]);

aw1.setLocalState({x: 3});

Expand All @@ -87,8 +82,8 @@ suite('Awareness', () => {
]),
);

expect(aw1changes).to.deep.equal(aw2changes);
expect(aw2changes).to.deep.equal([
expect(aw1changes).toEqual(aw2changes);
expect(aw2changes).toEqual([
{added: [0, 1], updated: [], removed: []},
{added: [], updated: [0], removed: []},
]);
Expand Down
20 changes: 20 additions & 0 deletions packages/reflect-yjs/src/mutators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {expect, test} from 'vitest';
import {parseKeyIntoClientIDs} from './mutators.js';

test('parseKeyIntoClientIDs', () => {
// Mostly just sanity checking that the function is up to date with the
// implementation.
expect(parseKeyIntoClientIDs('', '')).toBeUndefined();
expect(
parseKeyIntoClientIDs(
'-/p/ReflectClientID/yjs/awareness/name/yjsClientID',
'name',
),
).toEqual(['ReflectClientID', 'yjsClientID']);
expect(
parseKeyIntoClientIDs(
'-/p/ReflectClientID/yjs/awareness/name/yjsClientID',
'x',
),
).toBeUndefined();
});
12 changes: 6 additions & 6 deletions packages/reflect-yjs/src/mutators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,21 @@ export function yjsAwarenessKey(
reflectClientID: ClientID,
yjsClientID: number,
): string {
// -c/${reflectClientID} is a client key space and these are ephemeral. They
// -/p/${reflectClientID} is a presence key space and these are ephemeral. They
// get deleted when Reflect knows that the client can never come back.
return `-/c/${reflectClientID}/yjs/awareness/${name}/${yjsClientID}`;
return `-/p/${reflectClientID}/yjs/awareness/${name}/${yjsClientID}`;
}

function parseKeyIntoClientIDs(
export function parseKeyIntoClientIDs(
key: string,
name: string,
): undefined | [ClientID, string] {
// `-/c/${reflectClientID}/yjs/awareness/${name}/${yjsClientID}`;
// `-/p/${reflectClientID}/yjs/awareness/${name}/${yjsClientID}`;
// 0/1/2 /3 /4 /5 /6
const parts = key.split('/');
if (
parts[0] !== '-' ||
parts[1] !== 'c' ||
parts[1] !== 'p' ||
parts[3] !== 'yjs' ||
parts[4] !== 'awareness' ||
parts[5] !== name
Expand Down Expand Up @@ -292,7 +292,7 @@ export async function listClientStates(
name: string,
): Promise<[ClientID, number, ReadonlyJSONObject][]> {
const entries: [ClientID, number, ReadonlyJSONObject][] = [];
const prefix = '-/c/';
const prefix = '-/p/';
for await (const [key, value] of tx.scan({prefix}).entries()) {
const parts = parseKeyIntoClientIDs(key, name);
if (parts) {
Expand Down

0 comments on commit ed98815

Please sign in to comment.