[bugfix] Avoid randomness at init time to allow running on cloudflare. (#3016)

Describe what your pull request does. If appropriate, add GIFs or images
showing the before and after.

Follow up to #2987 

### Change Type

- [x] `patch` — Bug fix


### Release Notes

- Prevent using randomness API at init time, to allow importing the
tldraw package in a cloudflare worker.
pull/3017/head
David Sheldrick 2024-03-01 15:34:16 +00:00 zatwierdzone przez GitHub
rodzic ba6cba64c6
commit 1d5a9efa17
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
2 zmienionych plików z 12 dodań i 5 usunięć

Wyświetl plik

@ -50,8 +50,9 @@ function iOS() {
* A string that is unique per browser tab
* @public
*/
export const TAB_ID: string =
window?.[tabIdKey] ?? window?.sessionStorage[tabIdKey] ?? `TLDRAW_INSTANCE_STATE_V1_` + uniqueId()
export const TAB_ID: string = window
? window[tabIdKey] ?? window.sessionStorage[tabIdKey] ?? `TLDRAW_INSTANCE_STATE_V1_` + uniqueId()
: '<error>'
if (window) {
window[tabIdKey] = TAB_ID
if (iOS()) {

Wyświetl plik

@ -240,18 +240,24 @@ const channel =
channel?.addEventListener('message', (e) => {
const data = e.data as undefined | UserChangeBroadcastMessage
if (data?.type === broadcastEventKey && data?.origin !== broadcastOrigin) {
if (data?.type === broadcastEventKey && data?.origin !== getBroadcastOrigin()) {
globalUserPreferences.set(migrateUserPreferences(data.data))
}
})
const broadcastOrigin = uniqueId()
let _broadcastOrigin = null as null | string
function getBroadcastOrigin() {
if (_broadcastOrigin === null) {
_broadcastOrigin = uniqueId()
}
return _broadcastOrigin
}
const broadcastEventKey = 'tldraw-user-preferences-change' as const
function broadcastUserPreferencesChange() {
channel?.postMessage({
type: broadcastEventKey,
origin: broadcastOrigin,
origin: getBroadcastOrigin(),
data: {
user: getUserPreferences(),
version: userMigrations.currentVersion,