Skip to content

Commit

Permalink
fix preludeShim & error handling for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
munrocket committed Sep 5, 2023
1 parent 0bb1cb0 commit 9b899b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 6 additions & 4 deletions components/editor/preludeshim.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { useAtomValue } from 'jotai';
import { isSafeContext, wgputoyAtom } from 'lib/atoms/wgputoyatoms';
import { isSafeContext, wgpuAvailabilityAtom, wgputoyAtom } from 'lib/atoms/wgputoyatoms';
import { Fragment, useEffect, useState } from 'react';

/*
We need to put this in its own component because any access to wgpu
data must be through dynamic imports (i.e. client-side only)
Is there a less bogus way of doing this??
*/
export default function PreludeShim() {
const wgpuToy = useAtomValue(wgputoyAtom);
const wgpuAvailability = useAtomValue(wgpuAvailabilityAtom);
const [prelude, setPrelude] = useState('');

const wgpuToy = useAtomValue(wgputoyAtom);
useEffect(() => {
setPrelude(isSafeContext(wgpuToy) ? wgpuToy.prelude() : '');
if (wgpuAvailability === 'available' && wgpuToy ) {
setPrelude(wgpuToy.prelude());
}
}, [wgpuToy]);

return <Fragment>{prelude}</Fragment>;
Expand Down
10 changes: 7 additions & 3 deletions components/wgputoy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ export const WgpuToyWrapper = props => {
navigator.gpu
.requestAdapter()
.then(adapter => {
setWgpuAvailability('available');
setCanvasEl(canvas);
Controller = dynamic(() => import('./wgputoycontroller'), { ssr: false });
adapter.requestDevice().then(device => {
setWgpuAvailability('available');
setCanvasEl(canvas);
Controller = dynamic(() => import('./wgputoycontroller'), { ssr: false });
}).catch(() => {
setWgpuAvailability('unavailable');
})
})
.catch(() => {
setWgpuAvailability('unavailable');
Expand Down

0 comments on commit 9b899b1

Please sign in to comment.