Skip to content

Commit bbfe6c5

Browse files
committed
fix: build errors
1 parent 0aebaef commit bbfe6c5

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

apps/dashboard/src/App.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ function SharedWorkerTest({iframeRef}: {iframeRef: React.RefObject<HTMLIFrameEle
6464
const handleQueryRequest = useCallback(async (data: unknown) => {
6565
console.log('[Dashboard] Received query request:', data)
6666

67-
try {
68-
// Type assert the data to the expected structure
69-
const queryData = data as {
70-
queryOptions: {
71-
projectId: string
72-
dataset: string
73-
query: string
74-
params?: Record<string, unknown>
75-
}
76-
requestId: string
67+
// Type assert the data to the expected structure
68+
const queryData = data as {
69+
queryOptions: {
70+
projectId: string
71+
dataset: string
72+
query: string
73+
params?: Record<string, unknown>
7774
}
75+
requestId: string
76+
}
7877

78+
try {
7979
// Create a subscription request from the incoming query data
8080
const subscription = createSubscriptionRequest({
8181
storeName: 'query',
@@ -105,6 +105,7 @@ function SharedWorkerTest({iframeRef}: {iframeRef: React.RefObject<HTMLIFrameEle
105105
} catch (error) {
106106
console.error('[Dashboard] Error handling query request:', error)
107107
return {
108+
requestId: queryData.requestId,
108109
error: error instanceof Error ? error.message : String(error),
109110
subscriptionId: null,
110111
}
@@ -115,7 +116,7 @@ function SharedWorkerTest({iframeRef}: {iframeRef: React.RefObject<HTMLIFrameEle
115116
name: 'dashboard',
116117
connectTo: 'sdk-app',
117118
targetOrigin: '*',
118-
onStatus: handleStatus,
119+
onStatus: (workerStatus) => handleStatus(workerStatus as WorkerStatus),
119120
heartbeat: false, // Disable heartbeat to reduce cycling
120121
onMessage: {
121122
'dashboard/v1/query/request': handleQueryRequest,
@@ -161,7 +162,10 @@ function SharedWorkerTest({iframeRef}: {iframeRef: React.RefObject<HTMLIFrameEle
161162
iframe.removeEventListener('load', handleIframeLoad)
162163
}
163164
}
164-
}, [connect])
165+
166+
// Return empty cleanup function when no iframe
167+
return () => {}
168+
}, [connect, iframeRef])
165169

166170
const testSubscription = async () => {
167171
console.log('testSubscription')

apps/dashboard/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export default defineConfig(({mode}) => {
3232
'@sanity/sdk-react': resolve(import.meta.dirname, '../../packages/react/src/_exports'),
3333
},
3434
},
35+
worker: {
36+
format: 'es',
37+
},
3538
define: {
3639
'import.meta.env.VITE_IS_E2E': JSON.stringify(isE2E),
3740
'import.meta.env.VITE_E2E_PROJECT_ID': JSON.stringify(

apps/sdk-app/src/App.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {SanityApp, SanityConfig, useQuery, useWindowConnection} from '@sanity/sdk-react'
22
import {Spinner, ThemeProvider} from '@sanity/ui'
33
import {buildTheme} from '@sanity/ui/theme'
4-
import {type JSX, useEffect, useRef, useState} from 'react'
4+
import {type JSX} from 'react'
55

66
const theme = buildTheme({})
77

@@ -25,7 +25,7 @@ type QueryRequestMessage = {
2525
type: 'dashboard/v1/query/request'
2626
data: {
2727
queryId: string
28-
queryOptions: any
28+
queryOptions: unknown
2929
requestId: string
3030
}
3131
}
@@ -42,20 +42,20 @@ type QueryResponseMessage = {
4242

4343
// Test component to demonstrate query forwarding
4444
function QueryTest() {
45-
4645
// hack -- something in the node setup in the query store has a race condition
4746
useWindowConnection<QueryResponseMessage, QueryRequestMessage>({
4847
name: 'sdk-app',
4948
connectTo: 'dashboard',
5049
})
5150

5251
// This query should be forwarded to Dashboard when in iframe context
53-
const {data, isPending} = useQuery({
52+
const {data} = useQuery({
5453
query: '*[_type == "movie"][0...5]{_id, title, releaseYear}',
5554
projectId: 'ppsg7ml5',
5655
dataset: 'test',
5756
})
5857

58+
// eslint-disable-next-line no-console
5959
console.log('data', data)
6060

6161
return (
@@ -66,7 +66,15 @@ function QueryTest() {
6666
</div>
6767
<div style={{marginTop: 8}}>
6868
<strong>Data:</strong>
69-
<pre style={{marginTop: 4, padding: 8, backgroundColor: '#f5f5f5', borderRadius: 4, fontSize: '12px'}}>
69+
<pre
70+
style={{
71+
marginTop: 4,
72+
padding: 8,
73+
backgroundColor: '#f5f5f5',
74+
borderRadius: 4,
75+
fontSize: '12px',
76+
}}
77+
>
7078
{/* {JSON.stringify(data, null, 2)} */}
7179
</pre>
7280
</div>
@@ -81,9 +89,9 @@ export default function App(): JSX.Element {
8189
return (
8290
<ThemeProvider theme={theme}>
8391
<SanityApp fallback={<Spinner />} config={devConfigs}>
84-
<div style={{height: '100vh', width: '100vw', overflow: 'auto'}}>
85-
<QueryTest />
86-
</div>
92+
<div style={{height: '100vh', width: '100vw', overflow: 'auto'}}>
93+
<QueryTest />
94+
</div>
8795
</SanityApp>
8896
</ThemeProvider>
8997
)

packages/core/src/_exports/worker.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
*/
88

99
import {createClient} from '@sanity/client'
10+
1011
import {sharedWorkerStore} from '../sharedWorkerStore/sharedWorkerStore'
1112
import {type SubscriptionRequest} from '../sharedWorkerStore/types'
1213

1314
declare const self: SharedWorkerGlobalScope
1415

1516
// Cache to store query results
16-
const queryCache = new Map<string, {
17-
result: unknown
18-
timestamp: number
19-
subscribers: Set<MessagePort>
20-
}>()
17+
const queryCache = new Map<
18+
string,
19+
{
20+
result: unknown
21+
timestamp: number
22+
subscribers: Set<MessagePort>
23+
}
24+
>()
2125

2226
// Helper to create stable cache keys
2327
function getCacheKey(subscription: SubscriptionRequest): string {
@@ -85,7 +89,7 @@ function handleRegisterSubscription(subscription: SubscriptionRequest, port: Mes
8589
try {
8690
// Register the subscription in the store
8791
sharedWorkerStore.getState().registerSubscription(subscription)
88-
92+
8993
// Check if we need to execute a query for this subscription
9094
if (subscription.storeName === 'query' && subscription.params?.['query']) {
9195
handleQuerySubscription(subscription, port)
@@ -113,12 +117,15 @@ function handleRegisterSubscription(subscription: SubscriptionRequest, port: Mes
113117
* @internal
114118
* Handle query subscription - execute the query and cache results
115119
*/
116-
async function handleQuerySubscription(subscription: SubscriptionRequest, port: MessagePort): Promise<void> {
120+
async function handleQuerySubscription(
121+
subscription: SubscriptionRequest,
122+
port: MessagePort,
123+
): Promise<void> {
117124
const cacheKey = getCacheKey(subscription)
118-
125+
119126
// Check if we already have this query result cached
120127
let cacheEntry = queryCache.get(cacheKey)
121-
128+
122129
if (!cacheEntry) {
123130
try {
124131
// Create Sanity client for this project/dataset
@@ -128,50 +135,50 @@ async function handleQuerySubscription(subscription: SubscriptionRequest, port:
128135
apiVersion: '2024-01-01',
129136
useCdn: true,
130137
})
131-
138+
132139
// Execute the query
133140
console.log('[SharedWorker] Executing query:', subscription.params?.['query'])
134141
const result = await client.fetch(
135142
subscription.params?.['query'] as string,
136-
subscription.params?.['options'] as Record<string, unknown> || {}
143+
(subscription.params?.['options'] as Record<string, unknown>) || {},
137144
)
138-
145+
139146
// Cache the result
140147
cacheEntry = {
141148
result,
142149
timestamp: Date.now(),
143-
subscribers: new Set()
150+
subscribers: new Set(),
144151
}
145152
queryCache.set(cacheKey, cacheEntry)
146-
153+
147154
console.log('[SharedWorker] Query executed and cached:', cacheKey)
148155
} catch (error) {
149156
console.error('[SharedWorker] Query execution failed:', error)
150157
port.postMessage({
151158
type: 'SUBSCRIPTION_ERROR',
152159
data: {
153160
error: `Query execution failed: ${(error as Error).message}`,
154-
subscriptionId: subscription.subscriptionId
161+
subscriptionId: subscription.subscriptionId,
155162
},
156163
})
157164
return
158165
}
159166
}
160-
167+
161168
// Add this port as a subscriber to the cache entry
162169
cacheEntry.subscribers.add(port)
163-
170+
164171
// Send the query result back to the subscriber
165172
port.postMessage({
166173
type: 'SUBSCRIPTION_REGISTERED',
167174
data: {
168175
subscriptionId: subscription.subscriptionId,
169176
result: cacheEntry.result,
170177
cached: cacheEntry.timestamp !== Date.now(),
171-
cacheKey
178+
cacheKey,
172179
},
173180
})
174-
181+
175182
console.log('[SharedWorker] Query result sent to subscriber:', subscription.subscriptionId)
176183
}
177184

0 commit comments

Comments
 (0)