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

Support passing initial value for when creating documents #274

Merged
merged 15 commits into from
Jan 23, 2024
Merged

Conversation

HerbCaudill
Copy link
Collaborator

@HerbCaudill HerbCaudill commented Jan 21, 2024

Why

Creating a document in a repo is currently a two-step process: First you create the handle -- at which point the type of your
document is a filthy lie, because its value is {} regardless of what you may have asserted. Then you have to use DocHandle.change() to set the various properties of your desired initial state:

const rootDocHandle = repo.create<SharedState>() // <- LIES!!!
rootDocHandle.change(s => {
  s.dones = []
  s.contacts = []
})

A better world is possible! One where this is done in a single step, and you don't have to endure that terrifying millisecond
without type safety.

How

When creating a new DocHandle with isNew, you can pass an initial value for the document:

const handle = new DocHandle(documentId, {
  isNew: true,
  initialValue: { dones: [], contacts: [] },
})

Similarly, when creating a new document with Repo.create(), you can pass an initial value:

const handle = repo.create({ dones: [], contacts: [] })

@pvh pvh force-pushed the repo-initial-value branch from 91b8f3f to f536d65 Compare January 23, 2024 22:52
@pvh pvh merged commit f536d65 into main Jan 23, 2024
1 of 2 checks passed
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