Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arv/presence state #21

Merged
merged 5 commits into from
Jan 12, 2024
Merged

Arv/presence state #21

merged 5 commits into from
Jan 12, 2024

Conversation

arv
Copy link
Contributor

@arv arv commented Dec 20, 2023

No description provided.

@arv arv requested a review from aboodman December 20, 2023 21:20
@arv
Copy link
Contributor Author

arv commented Dec 21, 2023

I got rid of the "singleton" case.

Started enforcing correct clientID in write but needs more tests.

Started writing tests for optional/empty id/clientID.

Needs more work!!!

This adds a function called `generatePresence` that generates a
collection of presence keys at `-/p/${clientID}/name/${id}`. The type of
these entities are called `PresenceEntity` and are defined as:

```ts
type PresenceEntity = {
  clientID: string;
  id: string;
};
```

When looking up a presence state value, if the `id` is omitted, the `id`
is set to `''` and if the `clientID` is omitted, the `clientID` is set
to the `clientID` of the current client.

When writing and mutating the value the `id` and the `clientID` are
added to the value if they are missing.

You can only mutate the presence state value for the current client. If
you try to mutate a value that has different `clientID` than the current
client a runtime error is thrown.
@arv
Copy link
Contributor Author

arv commented Jan 12, 2024

@aboodman This is notnow ready for review.

For `get`, `mustGet`, `has` and `delete`. When undefined we treat it as
`{clientID: tx.clientID, id: ''}`.
@arv arv merged commit 2c48412 into main Jan 12, 2024
4 checks passed
@arv arv deleted the arv/presence-state branch January 12, 2024 21:33
Copy link
Contributor

@aboodman aboodman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

```ts
type Cursor {
clientID: string;
id: '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could make id optional and default it to empty string, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around with that.

It has some weirdness to only do this at the type level.

  • If you use strict parse in zod (not default) or in valita (default) the extra field (id) in the results causes errors.
  • The id is always present in the result (get) and in the keys for the entries.

If we do this we should make the resulting types declare that it is always present at least.

export type OptionalIDs<T extends PresenceEntity> = Partial<T> &
Omit<T, keyof PresenceEntity>;

export type LookupID = Partial<PresenceEntity>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might call this PresenceID just to make it a little more clear from name only, when using the type from outside the package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants