@tldraw/tlsync-client
Advanced tools
Comparing version 2.0.0-canary.f7652e409 to 2.0.0-canary.f8b6ee6ab998
import { RecordsDiff } from '@tldraw/tlstore'; | ||
import { SerializedSchema } from '@tldraw/tlstore'; | ||
import { Store } from '@tldraw/tlstore'; | ||
import { StoreSnapshot } from '@tldraw/tlstore'; | ||
@@ -11,4 +10,2 @@ import { SyncedStore } from '@tldraw/editor'; | ||
import { TLStoreSchema } from '@tldraw/editor'; | ||
import { TLUser } from '@tldraw/editor'; | ||
import { TLUserId } from '@tldraw/editor'; | ||
@@ -41,5 +38,2 @@ /** @public */ | ||
/** @public */ | ||
export declare function getUserData(): TLUser; | ||
/** @public */ | ||
export declare function hardReset({ shouldReload }?: { | ||
@@ -72,5 +66,2 @@ shouldReload?: boolean | undefined; | ||
/** @public */ | ||
export declare function subscribeToUserData(store: Store<any>): () => void; | ||
/** | ||
@@ -131,9 +122,8 @@ * IMPORTANT!!! | ||
*/ | ||
export declare function useLocalSyncClient({ universalPersistenceKey, instanceId, userId, config, }: { | ||
export declare function useLocalSyncClient({ universalPersistenceKey, instanceId, config, }: { | ||
universalPersistenceKey: string; | ||
instanceId: TLInstanceId; | ||
userId: TLUserId; | ||
config?: TldrawEditorConfig; | ||
config: TldrawEditorConfig; | ||
}): SyncedStore; | ||
export { } |
@@ -29,3 +29,2 @@ "use strict"; | ||
getAllIndexDbNames: () => import_persistence_constants.getAllIndexDbNames, | ||
getUserData: () => import_persistence_constants.getUserData, | ||
hardReset: () => import_hardReset.hardReset, | ||
@@ -35,3 +34,2 @@ loadDataFromStore: () => import_indexedDb.loadDataFromStore, | ||
storeSnapshotInIndexedDb: () => import_indexedDb.storeSnapshotInIndexedDb, | ||
subscribeToUserData: () => import_persistence_constants.subscribeToUserData, | ||
useLocalSyncClient: () => import_useLocalSyncClient.useLocalSyncClient | ||
@@ -38,0 +36,0 @@ }); |
@@ -27,3 +27,2 @@ "use strict"; | ||
var import_hardReset = require("../hardReset"); | ||
var import_persistence_constants = require("../persistence-constants"); | ||
var import_TLLocalSyncClient = require("../TLLocalSyncClient"); | ||
@@ -33,4 +32,3 @@ function useLocalSyncClient({ | ||
instanceId, | ||
userId, | ||
config = import_editor.TldrawEditorConfig.default | ||
config | ||
}) { | ||
@@ -52,3 +50,3 @@ const [state, setState] = (0, import_react.useState)(null); | ||
}; | ||
const store = config.createStore({ userId, instanceId }); | ||
const store = config.createStore({ instanceId }); | ||
const client = new import_TLLocalSyncClient.TLLocalSyncClient(store, { | ||
@@ -63,11 +61,9 @@ universalPersistenceKey, | ||
}); | ||
const userDataUnsubcribe = (0, import_persistence_constants.subscribeToUserData)(store); | ||
return () => { | ||
setState((prevState) => prevState?.id === id ? null : prevState); | ||
userDataUnsubcribe(); | ||
client.close(); | ||
}; | ||
}, [instanceId, universalPersistenceKey, config, userId]); | ||
}, [instanceId, universalPersistenceKey, config]); | ||
return state?.syncedStore ?? { status: "loading" }; | ||
} | ||
//# sourceMappingURL=useLocalSyncClient.js.map |
@@ -25,9 +25,6 @@ "use strict"; | ||
addDbName: () => addDbName, | ||
getAllIndexDbNames: () => getAllIndexDbNames, | ||
getUserData: () => getUserData, | ||
subscribeToUserData: () => subscribeToUserData | ||
getAllIndexDbNames: () => getAllIndexDbNames | ||
}); | ||
module.exports = __toCommonJS(persistence_constants_exports); | ||
var import_editor = require("@tldraw/editor"); | ||
var import_signia = require("signia"); | ||
const tabIdKey = "TLDRAW_TAB_ID_v2"; | ||
@@ -43,30 +40,2 @@ const window = globalThis.window; | ||
} | ||
const USER_DATA_KEY = "TLDRAW_USER_DATA_v2"; | ||
const globalUserData = (0, import_signia.atom)( | ||
"globalUserData", | ||
JSON.parse(window?.localStorage.getItem(USER_DATA_KEY) || "null") ?? import_editor.TLUser.create({}) | ||
); | ||
(0, import_signia.react)("set global user data", () => { | ||
if (window) { | ||
window.localStorage.setItem(USER_DATA_KEY, JSON.stringify(globalUserData.value)); | ||
} | ||
}); | ||
function getUserData() { | ||
return globalUserData.value; | ||
} | ||
function subscribeToUserData(store) { | ||
const userId = globalUserData.value.id; | ||
return store.listen(({ changes }) => { | ||
for (const record of Object.values(changes.added)) { | ||
if (record.typeName === "user" && userId === record.id) { | ||
globalUserData.set(record); | ||
} | ||
} | ||
for (const [_, record] of Object.values(changes.updated)) { | ||
if (record.typeName === "user" && userId === record.id) { | ||
globalUserData.set(record); | ||
} | ||
} | ||
}); | ||
} | ||
const defaultDocumentKey = "TLDRAW_DEFAULT_DOCUMENT_NAME_v2"; | ||
@@ -73,0 +42,0 @@ const DEFAULT_DOCUMENT_NAME = window?.localStorage.getItem(defaultDocumentKey) ?? (0, import_editor.uniqueId)(); |
@@ -123,3 +123,8 @@ "use strict"; | ||
this.store.mergeRemoteChanges(() => { | ||
this.store.put(Object.values(migrationResult.value), "initialize"); | ||
this.store.put( | ||
Object.values(migrationResult.value).filter( | ||
(r) => this.store.schema.types[r.typeName].scope !== "presence" | ||
), | ||
"initialize" | ||
); | ||
}); | ||
@@ -126,0 +131,0 @@ } |
{ | ||
"name": "@tldraw/tlsync-client", | ||
"description": "A tiny little drawing app (multiplayer sync).", | ||
"version": "2.0.0-canary.f7652e409", | ||
"version": "2.0.0-canary.f8b6ee6ab998", | ||
"packageManager": "yarn@3.5.0", | ||
"author": { | ||
@@ -34,9 +35,9 @@ "name": "tldraw GB Ltd.", | ||
"scripts": { | ||
"test": "yarn run -T jest", | ||
"test:coverage": "yarn run -T jest --coverage", | ||
"build": "echo 'build should be run by turbo'", | ||
"build:package": "yarn run -T tsx ../../scripts/build-package.ts", | ||
"build:api": "yarn run -T tsx ../../scripts/build-api.ts", | ||
"test": "lazy inherit", | ||
"test-coverage": "lazy inherit", | ||
"build": "yarn run -T tsx ../../scripts/build-package.ts", | ||
"build-api": "yarn run -T tsx ../../scripts/build-api.ts", | ||
"prepack": "yarn run -T tsx ../../scripts/prepack.ts", | ||
"postpack": "../../scripts/postpack.sh", | ||
"pack-tarball": "yarn pack", | ||
"lint": "yarn run -T tsx ../../scripts/lint.ts" | ||
@@ -47,2 +48,3 @@ }, | ||
"@types/react-dom": "*", | ||
"lazyrepo": "0.0.0-alpha.26", | ||
"ws": "^8.10.0" | ||
@@ -71,5 +73,5 @@ }, | ||
"dependencies": { | ||
"@tldraw/editor": "2.0.0-canary.f7652e409", | ||
"@tldraw/tlstore": "2.0.0-canary.f7652e409", | ||
"@tldraw/utils": "2.0.0-canary.f7652e409", | ||
"@tldraw/editor": "2.0.0-canary.f8b6ee6ab998", | ||
"@tldraw/tlstore": "2.0.0-canary.f8b6ee6ab998", | ||
"@tldraw/utils": "2.0.0-canary.f8b6ee6ab998", | ||
"idb": "^7.1.0" | ||
@@ -76,0 +78,0 @@ }, |
# @tldraw/tlsync-client | ||
## License | ||
The source code in this repository (as well as our 2.0+ distributions and releases) are currently licensed under Apache-2.0. These licenses are subject to change in our upcoming 2.0 release. If you are planning to use tldraw in a commercial product, please reach out at [hello@tldraw.com](mailto://hello@tldraw.com). |
@@ -16,4 +16,2 @@ export { BroadcastChannelMock, TLLocalSyncClient } from './lib/TLLocalSyncClient' | ||
getAllIndexDbNames, | ||
getUserData, | ||
subscribeToUserData, | ||
} from './lib/persistence-constants' |
@@ -1,5 +0,4 @@ | ||
import { SyncedStore, TldrawEditorConfig, TLInstanceId, TLUserId, uniqueId } from '@tldraw/editor' | ||
import { SyncedStore, TldrawEditorConfig, TLInstanceId, uniqueId } from '@tldraw/editor' | ||
import { useEffect, useState } from 'react' | ||
import '../hardReset' | ||
import { subscribeToUserData } from '../persistence-constants' | ||
import { TLLocalSyncClient } from '../TLLocalSyncClient' | ||
@@ -16,9 +15,7 @@ | ||
instanceId, | ||
userId, | ||
config = TldrawEditorConfig.default, | ||
config, | ||
}: { | ||
universalPersistenceKey: string | ||
instanceId: TLInstanceId | ||
userId: TLUserId | ||
config?: TldrawEditorConfig | ||
config: TldrawEditorConfig | ||
}): SyncedStore { | ||
@@ -42,3 +39,3 @@ const [state, setState] = useState<{ id: string; syncedStore: SyncedStore } | null>(null) | ||
const store = config.createStore({ userId, instanceId }) | ||
const store = config.createStore({ instanceId }) | ||
@@ -55,12 +52,9 @@ const client = new TLLocalSyncClient(store, { | ||
const userDataUnsubcribe = subscribeToUserData(store) | ||
return () => { | ||
setState((prevState) => (prevState?.id === id ? null : prevState)) | ||
userDataUnsubcribe() | ||
client.close() | ||
} | ||
}, [instanceId, universalPersistenceKey, config, userId]) | ||
}, [instanceId, universalPersistenceKey, config]) | ||
return state?.syncedStore ?? { status: 'loading' } | ||
} |
@@ -1,4 +0,2 @@ | ||
import { TLInstance, TLInstanceId, TLUser, uniqueId } from '@tldraw/editor' | ||
import { Store } from '@tldraw/tlstore' | ||
import { atom, react } from 'signia' | ||
import { TLInstance, TLInstanceId, uniqueId } from '@tldraw/editor' | ||
@@ -29,38 +27,2 @@ const tabIdKey = 'TLDRAW_TAB_ID_v2' as const | ||
// the id of the user, stored in localStorage so that it persists across sessions | ||
const USER_DATA_KEY = 'TLDRAW_USER_DATA_v2' | ||
const globalUserData = atom<TLUser>( | ||
'globalUserData', | ||
JSON.parse(window?.localStorage.getItem(USER_DATA_KEY) || 'null') ?? TLUser.create({}) | ||
) | ||
react('set global user data', () => { | ||
if (window) { | ||
window.localStorage.setItem(USER_DATA_KEY, JSON.stringify(globalUserData.value)) | ||
} | ||
}) | ||
/** @public */ | ||
export function getUserData() { | ||
return globalUserData.value | ||
} | ||
/** @public */ | ||
export function subscribeToUserData(store: Store<any>) { | ||
const userId = globalUserData.value.id | ||
return store.listen(({ changes }) => { | ||
for (const record of Object.values(changes.added)) { | ||
if (record.typeName === 'user' && userId === record.id) { | ||
globalUserData.set(record) | ||
} | ||
} | ||
for (const [_, record] of Object.values(changes.updated)) { | ||
if (record.typeName === 'user' && userId === record.id) { | ||
globalUserData.set(record) | ||
} | ||
} | ||
}) | ||
} | ||
// the id of the document that will be loaded if the URL doesn't contain a document id | ||
@@ -67,0 +29,0 @@ // again, stored in localStorage |
@@ -1,9 +0,2 @@ | ||
import { | ||
TldrawEditorConfig, | ||
TLInstance, | ||
TLInstanceId, | ||
TLPage, | ||
TLUser, | ||
TLUserId, | ||
} from '@tldraw/editor' | ||
import { TldrawEditorConfig, TLInstance, TLInstanceId, TLPage } from '@tldraw/editor' | ||
import { promiseWithResolve } from '@tldraw/utils' | ||
@@ -34,7 +27,5 @@ import * as idb from './indexedDb' | ||
instanceId: TLInstanceId = TLInstance.createCustomId('test'), | ||
userId: TLUserId = TLUser.createCustomId('test'), | ||
channel = new BroadcastChannelMock('test') | ||
) { | ||
const store = TldrawEditorConfig.default.createStore({ | ||
userId, | ||
const store = new TldrawEditorConfig().createStore({ | ||
instanceId, | ||
@@ -89,3 +80,3 @@ }) | ||
test('when a client receives an annouce with a newer schema version it reloads itself', async () => { | ||
test('when a client receives an announce with a newer schema version it reloads itself', async () => { | ||
const { client, channel, onLoadError } = testClient() | ||
@@ -108,3 +99,3 @@ await tick() | ||
test('when a client receives an annouce with a newer schema version shortly after loading it does not reload but instead reports a loadError', async () => { | ||
test('when a client receives an announce with a newer schema version shortly after loading it does not reload but instead reports a loadError', async () => { | ||
const { client, channel, onLoadError } = testClient() | ||
@@ -111,0 +102,0 @@ await tick() |
@@ -158,3 +158,8 @@ import { TLInstanceId, TLRecord, TLStore } from '@tldraw/editor' | ||
// Calling put will validate the records! | ||
this.store.put(Object.values(migrationResult.value), 'initialize') | ||
this.store.put( | ||
Object.values(migrationResult.value).filter( | ||
(r) => this.store.schema.types[r.typeName].scope !== 'presence' | ||
), | ||
'initialize' | ||
) | ||
}) | ||
@@ -161,0 +166,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
6
153019
4
1986
+ Added@tldraw/editor@2.0.0-canary.f8b6ee6ab998(transitive)
+ Added@tldraw/indices@2.0.0-canary.f8b6ee6ab998(transitive)
+ Added@tldraw/primitives@2.0.0-canary.f8b6ee6ab998(transitive)
+ Added@tldraw/tlschema@2.0.0-canary.f8b6ee6ab998(transitive)
+ Added@tldraw/tlstore@2.0.0-canary.f8b6ee6ab998(transitive)
+ Added@tldraw/tlvalidate@2.0.0-canary.f8b6ee6ab998(transitive)
+ Added@tldraw/utils@2.0.0-canary.f8b6ee6ab998(transitive)
+ Addedcrc@4.3.2(transitive)
+ Addednanoid@4.0.2(transitive)
- Removed@tldraw/editor@2.0.0-canary.f7652e409(transitive)
- Removed@tldraw/primitives@2.0.0-canary.f7652e409(transitive)
- Removed@tldraw/tlschema@2.0.0-canary.f7652e409(transitive)
- Removed@tldraw/tlstore@2.0.0-canary.f7652e409(transitive)
- Removed@tldraw/tlvalidate@2.0.0-canary.f7652e409(transitive)
- Removed@tldraw/utils@2.0.0-canary.f7652e409(transitive)
- Removednanoid@3.3.8(transitive)