Skip to content

Commit 7315e59

Browse files
committed
add workerType to demo app
1 parent 2030653 commit 7315e59

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

packages/idb-cache-app/src/App.tsx

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ const App = () => {
7878
const stored = localStorage.priority;
7979
return ["normal", "low"].includes(stored) ? stored : "normal";
8080
});
81+
const [workerType, setWorkerType] = useState<"worker" | "sharedWorker">(
82+
() => {
83+
const stored = localStorage.workerType;
84+
return ["worker", "sharedWorker"].includes(stored)
85+
? stored
86+
: "sharedWorker";
87+
}
88+
);
8189

8290
const [cacheReady, setCacheReady] = useState<boolean>(false);
8391

@@ -117,6 +125,7 @@ const App = () => {
117125
chunkSize: chunkSize,
118126
maxTotalChunks: maxTotalChunksStored,
119127
priority,
128+
useSharedWorker: workerType === "sharedWorker",
120129
});
121130

122131
if (!isCancelled) {
@@ -145,7 +154,7 @@ const App = () => {
145154
setCacheReady(false);
146155
}
147156
};
148-
}, [chunkSize, maxTotalChunksStored, priority]);
157+
}, [chunkSize, maxTotalChunksStored, priority, workerType]);
149158

150159
useEffect(() => {
151160
localStorage.setItem("maxTotalChunksStored", String(maxTotalChunksStored));
@@ -155,6 +164,10 @@ const App = () => {
155164
localStorage.setItem("priority", String(priority));
156165
}, [priority]);
157166

167+
useEffect(() => {
168+
localStorage.setItem("workerType", String(workerType));
169+
}, [workerType]);
170+
158171
const encryptAndStore = useCallback(async () => {
159172
const cache = cacheRef.current;
160173
if (!cache) {
@@ -441,6 +454,39 @@ const App = () => {
441454
<RadioInput label="Low" value="low" />
442455
</RadioInputGroup>
443456
</WrappedFlexItem>
457+
458+
<WrappedFlexItem>
459+
<RadioInputGroup
460+
name="worker"
461+
value={workerType}
462+
data-testid="worker-input"
463+
description={
464+
<Flex alignItems="end">
465+
<Flex.Item as="div">
466+
<View margin="0 xx-small 0 0">Worker Type</View>
467+
</Flex.Item>
468+
<Tooltip
469+
color="primary-inverse"
470+
renderTip="Worker (isolated) or SharedWorker (shared across tabs)."
471+
offsetY="5px"
472+
>
473+
<Flex.Item as="div">
474+
<IconInfoLine />
475+
</Flex.Item>
476+
</Tooltip>
477+
</Flex>
478+
}
479+
variant="toggle"
480+
onChange={(e) => {
481+
setWorkerType(
482+
e.target.value === "worker" ? "worker" : "sharedWorker"
483+
);
484+
}}
485+
>
486+
<RadioInput label="Worker" value="worker" />
487+
<RadioInput label="SharedWorker" value="sharedWorker" />
488+
</RadioInputGroup>
489+
</WrappedFlexItem>
444490
</WrappedFlexContainer>
445491

446492
<Heading level="h2" margin="medium 0 small 0">

packages/idb-cache/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export interface IDBCacheConfig {
9191
priority?: "normal" | "low";
9292
/**
9393
* Controls whether to use SharedWorker or Worker for encryption/decryption.
94-
* When true (default), uses SharedWorker if available, falling back to Worker.
94+
* When true (default), uses SharedWorker if available.
9595
* When false, always uses Worker even if SharedWorker is available.
9696
*/
9797
useSharedWorker?: boolean;

0 commit comments

Comments
 (0)