2024-04-15 12:53:42 +00:00
|
|
|
import { createMigrationIds, createMigrationSequence } from '@tldraw/store'
|
|
|
|
import { objectMapEntries } from '@tldraw/utils'
|
|
|
|
import { TLShape } from './records/TLShape'
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-04-15 12:53:42 +00:00
|
|
|
const Versions = createMigrationIds('com.tldraw.store', {
|
2023-04-25 11:01:25 +00:00
|
|
|
RemoveCodeAndIconShapeTypes: 1,
|
derived presence state (#1204)
This PR adds
- A new `TLInstancePresence` record type, to collect info about the
presence state in a particular instance of the editor. This will
eventually be used to sync presence data instead of sending
instance-only state across the wire.
- **Record Scopes**
`RecordType` now has a `scope` property which can be one of three
things:
- `document`: the record belongs to the document and should be synced
and persisted freely. Currently: `TLDocument`, `TLPage`, `TLShape`, and
`TLAsset`
- `instance`: the record belongs to a single instance of the store and
should not be synced at all. It should not be persisted directly in most
cases, but rather compiled into a kind of 'instance configuration' to
store alongside the local document data so that when reopening the
associated document it can remember some of the previous instance state.
Currently: `TLInstance`, `TLInstancePageState`, `TLCamera`, `TLUser`,
`TLUserDocument`, `TLUserPresence`
- `presence`: the record belongs to a single instance of the store and
should not be persisted, but may be synced using the special presence
sync protocol. Currently just `TLInstancePresence`
This sets us up for the following changes, which are gonna be pretty
high-impact in terms of integrating tldraw into existing systems:
- Removing `instanceId` as a config option. Each instance gets a
randomly generated ID.
- We'd replace it with an `instanceConfig` option that has stuff like
selectedIds, camera positions, and so on. Then it's up to library users
to get and reinstate the instance config at persistence boundaries.
- Removing `userId` as config option, and removing the `TLUser` type
altogether.
- We might need to revisit when doing auth-enabled features like locking
shapes, but I suspect that will be separate.
2023-04-27 18:03:19 +00:00
|
|
|
AddInstancePresenceType: 2,
|
2023-05-25 09:54:29 +00:00
|
|
|
RemoveTLUserAndPresenceAndAddPointer: 3,
|
Independent instance state persistence (#1493)
This PR
- Removes UserDocumentRecordType
- moving isSnapMode to user preferences
- moving isGridMode and isPenMode to InstanceRecordType
- deleting the other properties which are no longer needed.
- Creates a separate pipeline for persisting instance state.
Previously the instance state records were stored alongside the document
state records, and in order to load the state for a particular instance
(in our case, a particular tab) you needed to pass the 'instanceId'
prop. This prop ended up totally pervading the public API and people ran
into all kinds of issues with it, e.g. using the same instance id in
multiple editor instances.
There was also an issue whereby it was hard for us to clean up old
instance state so the idb table ended up bloating over time.
This PR makes it so that rather than passing an instanceId, you load the
instance state yourself while creating the store. It provides tools to
make that easy.
- Undoes the assumption that we might have more than one instance's
state in the store.
- Like `document`, `instance` now has a singleton id
`instance:instance`.
- Page state ids and camera ids are no longer random, but rather derive
from the page they belong to. This is like having a foreign primary key
in SQL databases. It's something i'd love to support fully as part of
the RecordType/Store api.
Tests to do
- [x] Test Migrations
- [x] Test Store.listen filtering
- [x] Make type sets in Store public and readonly
- [x] Test RecordType.createId
- [x] Test Instance state snapshot loading/exporting
- [x] Manual test File I/O
- [x] Manual test Vscode extension with multiple tabs
- [x] Audit usages of store.query
- [x] Audit usages of changed types: InstanceRecordType, 'instance',
InstancePageStateRecordType, 'instance_page_state', 'user_document',
'camera', CameraRecordType, InstancePresenceRecordType,
'instance_presence'
- [x] Test user preferences
- [x] Manual test isSnapMode and isGridMode and isPenMode
- [ ] Test indexedDb functions
- [x] Add instanceId stuff back
### Change Type
- [x] `major` — Breaking Change
### Test Plan
1. Add a step-by-step description of how to test your PR here.
2.
- [ ] Unit Tests
- [ ] Webdriver tests
### Release Notes
- Add a brief release note for your PR here.
2023-06-05 14:11:07 +00:00
|
|
|
RemoveUserDocument: 4,
|
2024-04-15 12:53:42 +00:00
|
|
|
} as const)
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2023-05-25 09:54:29 +00:00
|
|
|
export { Versions as storeVersions }
|
|
|
|
|
2023-04-25 11:01:25 +00:00
|
|
|
/** @public */
|
2024-04-15 12:53:42 +00:00
|
|
|
export const storeMigrations = createMigrationSequence({
|
|
|
|
sequenceId: 'com.tldraw.store',
|
|
|
|
retroactive: false,
|
|
|
|
sequence: [
|
|
|
|
{
|
|
|
|
id: Versions.RemoveCodeAndIconShapeTypes,
|
|
|
|
scope: 'store',
|
|
|
|
up: (store) => {
|
|
|
|
for (const [id, record] of objectMapEntries(store)) {
|
|
|
|
if (
|
|
|
|
record.typeName === 'shape' &&
|
|
|
|
((record as TLShape).type === 'icon' || (record as TLShape).type === 'code')
|
|
|
|
) {
|
|
|
|
delete store[id]
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 11:01:25 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-15 12:53:42 +00:00
|
|
|
{
|
|
|
|
id: Versions.AddInstancePresenceType,
|
|
|
|
scope: 'store',
|
|
|
|
up(_store) {
|
|
|
|
// noop
|
|
|
|
// there used to be a down migration for this but we made down migrations optional
|
|
|
|
// and we don't use them on store-level migrations so we can just remove it
|
derived presence state (#1204)
This PR adds
- A new `TLInstancePresence` record type, to collect info about the
presence state in a particular instance of the editor. This will
eventually be used to sync presence data instead of sending
instance-only state across the wire.
- **Record Scopes**
`RecordType` now has a `scope` property which can be one of three
things:
- `document`: the record belongs to the document and should be synced
and persisted freely. Currently: `TLDocument`, `TLPage`, `TLShape`, and
`TLAsset`
- `instance`: the record belongs to a single instance of the store and
should not be synced at all. It should not be persisted directly in most
cases, but rather compiled into a kind of 'instance configuration' to
store alongside the local document data so that when reopening the
associated document it can remember some of the previous instance state.
Currently: `TLInstance`, `TLInstancePageState`, `TLCamera`, `TLUser`,
`TLUserDocument`, `TLUserPresence`
- `presence`: the record belongs to a single instance of the store and
should not be persisted, but may be synced using the special presence
sync protocol. Currently just `TLInstancePresence`
This sets us up for the following changes, which are gonna be pretty
high-impact in terms of integrating tldraw into existing systems:
- Removing `instanceId` as a config option. Each instance gets a
randomly generated ID.
- We'd replace it with an `instanceConfig` option that has stuff like
selectedIds, camera positions, and so on. Then it's up to library users
to get and reinstate the instance config at persistence boundaries.
- Removing `userId` as config option, and removing the `TLUser` type
altogether.
- We might need to revisit when doing auth-enabled features like locking
shapes, but I suspect that will be separate.
2023-04-27 18:03:19 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-15 12:53:42 +00:00
|
|
|
{
|
|
|
|
// remove user and presence records and add pointer records
|
|
|
|
id: Versions.RemoveTLUserAndPresenceAndAddPointer,
|
|
|
|
scope: 'store',
|
|
|
|
up: (store) => {
|
|
|
|
for (const [id, record] of objectMapEntries(store)) {
|
|
|
|
if (record.typeName.match(/^(user|user_presence)$/)) {
|
|
|
|
delete store[id]
|
|
|
|
}
|
|
|
|
}
|
2023-05-25 09:54:29 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-15 12:53:42 +00:00
|
|
|
{
|
|
|
|
// remove user document records
|
|
|
|
id: Versions.RemoveUserDocument,
|
|
|
|
scope: 'store',
|
|
|
|
up: (store) => {
|
|
|
|
for (const [id, record] of objectMapEntries(store)) {
|
|
|
|
if (record.typeName.match('user_document')) {
|
|
|
|
delete store[id]
|
|
|
|
}
|
|
|
|
}
|
Independent instance state persistence (#1493)
This PR
- Removes UserDocumentRecordType
- moving isSnapMode to user preferences
- moving isGridMode and isPenMode to InstanceRecordType
- deleting the other properties which are no longer needed.
- Creates a separate pipeline for persisting instance state.
Previously the instance state records were stored alongside the document
state records, and in order to load the state for a particular instance
(in our case, a particular tab) you needed to pass the 'instanceId'
prop. This prop ended up totally pervading the public API and people ran
into all kinds of issues with it, e.g. using the same instance id in
multiple editor instances.
There was also an issue whereby it was hard for us to clean up old
instance state so the idb table ended up bloating over time.
This PR makes it so that rather than passing an instanceId, you load the
instance state yourself while creating the store. It provides tools to
make that easy.
- Undoes the assumption that we might have more than one instance's
state in the store.
- Like `document`, `instance` now has a singleton id
`instance:instance`.
- Page state ids and camera ids are no longer random, but rather derive
from the page they belong to. This is like having a foreign primary key
in SQL databases. It's something i'd love to support fully as part of
the RecordType/Store api.
Tests to do
- [x] Test Migrations
- [x] Test Store.listen filtering
- [x] Make type sets in Store public and readonly
- [x] Test RecordType.createId
- [x] Test Instance state snapshot loading/exporting
- [x] Manual test File I/O
- [x] Manual test Vscode extension with multiple tabs
- [x] Audit usages of store.query
- [x] Audit usages of changed types: InstanceRecordType, 'instance',
InstancePageStateRecordType, 'instance_page_state', 'user_document',
'camera', CameraRecordType, InstancePresenceRecordType,
'instance_presence'
- [x] Test user preferences
- [x] Manual test isSnapMode and isGridMode and isPenMode
- [ ] Test indexedDb functions
- [x] Add instanceId stuff back
### Change Type
- [x] `major` — Breaking Change
### Test Plan
1. Add a step-by-step description of how to test your PR here.
2.
- [ ] Unit Tests
- [ ] Webdriver tests
### Release Notes
- Add a brief release note for your PR here.
2023-06-05 14:11:07 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-15 12:53:42 +00:00
|
|
|
],
|
2023-04-25 11:01:25 +00:00
|
|
|
})
|