|
3 | 3 | </template> |
4 | 4 |
|
5 | 5 | <script setup lang="ts"> |
6 | | - import {onMounted, onUnmounted, ref} from 'vue' |
| 6 | + import {onMounted, onUnmounted, ref, watch} from 'vue' |
| 7 | +
|
| 8 | + import {thumbnailGCcount} from './thumbnails' |
| 9 | +
|
7 | 10 | import {Specimen} from '@/shared/Specimen' |
8 | 11 | import {DrawingUnmounted} from '@/visualizers/VisualizerInterface' |
9 | 12 |
|
10 | 13 | const canvasContainer = ref<HTMLDivElement | null>(null) |
11 | 14 | let savedContainer: HTMLDivElement | null = null |
12 | 15 | let specimen: Specimen | undefined = undefined |
| 16 | + let usingGC = false |
| 17 | + let needsSetup = true |
| 18 | + const TGClim = 7 |
13 | 19 | const props = defineProps<{query: string}>() |
14 | 20 |
|
| 21 | + function setupSpecimen() { |
| 22 | + needsSetup = false |
| 23 | + if (!specimen || !savedContainer) return |
| 24 | + specimen.setup(savedContainer) |
| 25 | + setTimeout(() => { |
| 26 | + if (usingGC) { |
| 27 | + thumbnailGCcount.value -= 1 |
| 28 | + usingGC = false |
| 29 | + } |
| 30 | + if (!specimen || !savedContainer) return |
| 31 | + const canvas = savedContainer.querySelector('canvas') |
| 32 | + if (canvas instanceof HTMLCanvasElement) { |
| 33 | + const {width, height} = canvas.getBoundingClientRect() |
| 34 | + const vizShot = new Image(width, height) |
| 35 | + vizShot.src = canvas.toDataURL() |
| 36 | + specimen.visualizer.depart(savedContainer) |
| 37 | + savedContainer.appendChild(vizShot) |
| 38 | + } else { |
| 39 | + specimen.visualizer.stop() |
| 40 | + } |
| 41 | + }, 4000) |
| 42 | + } |
| 43 | +
|
15 | 44 | onMounted(async () => { |
16 | 45 | specimen = await Specimen.fromQuery(props.query) |
17 | 46 | if (!(canvasContainer.value instanceof HTMLElement)) return |
18 | 47 | savedContainer = canvasContainer.value |
19 | | - specimen.setup(savedContainer) |
20 | | - setTimeout(() => specimen?.visualizer.stop(), 4000) |
| 48 | + if (specimen.visualizer.usesGL()) { |
| 49 | + usingGC = true |
| 50 | + if (thumbnailGCcount.value > TGClim) { |
| 51 | + // defer setup |
| 52 | + const limitMsg = document.createTextNode( |
| 53 | + '[... waiting for WebGL graphics context]' |
| 54 | + ) |
| 55 | + savedContainer.appendChild(limitMsg) |
| 56 | + watch(thumbnailGCcount, newCount => { |
| 57 | + if (!savedContainer) return |
| 58 | + if (newCount <= TGClim && needsSetup) { |
| 59 | + if (savedContainer.lastChild) { |
| 60 | + savedContainer.removeChild( |
| 61 | + savedContainer.lastChild |
| 62 | + ) |
| 63 | + } |
| 64 | + setupSpecimen() |
| 65 | + thumbnailGCcount.value += 1 |
| 66 | + } |
| 67 | + }) |
| 68 | + } else { |
| 69 | + setupSpecimen() |
| 70 | + thumbnailGCcount.value += 1 |
| 71 | + } |
| 72 | + } else setupSpecimen() |
21 | 73 | }) |
22 | 74 |
|
23 | 75 | onUnmounted(() => { |
| 76 | + if (usingGC) { |
| 77 | + thumbnailGCcount.value -= 1 |
| 78 | + usingGC = false |
| 79 | + } |
24 | 80 | // Turns out canvasContainer has already been de-refed |
25 | 81 | // by the time we get here, so we can't depart using that. |
26 | 82 | // Hence the need for savedContainer, unfortunately. |
|
0 commit comments