Skip to content

Commit 8a77016

Browse files
committed
More untested Worker code
1 parent 7b5df29 commit 8a77016

3 files changed

Lines changed: 52 additions & 31 deletions

File tree

src/draw/patterns.worker.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
1-
import { State, ViewportPattern } from '../types'
1+
import { AbsolutePattern, RelativePattern, State, ViewportPattern } from '../types'
2+
import { streamBatchedDrawablePatterns } from './stream-batched-drawable-patterns'
23

34
type WorkerState =
45
| {
5-
type: 'idle'
6+
type: 'stopped'
67
}
78
| {
89
type: 'generating'
910
state: State
1011
}
11-
| {
12-
type: 'paused'
13-
state: State
14-
}
1512

1613
type WorkerMessageInput =
14+
| {
15+
type: 'initialize'
16+
ctx: OffscreenCanvasRenderingContext2D
17+
}
1718
| {
1819
type: 'generate'
1920
state: State
2021
}
2122
| {
2223
type: 'stop'
2324
}
25+
26+
type WorkerMessageOutput =
2427
| {
25-
type: 'pause'
28+
type: 'batch'
29+
depth: number
30+
patterns: ViewportPattern[]
2631
}
2732
| {
28-
type: 'resume'
33+
type: 'done'
2934
}
3035

31-
type WorkerMessageOutput = {
32-
type: 'batch'
33-
patterns: ViewportPattern[]
34-
}
35-
3636
let state: WorkerState = {
37-
type: 'idle',
37+
type: 'stopped',
3838
}
3939

4040
function handleMessage(message: WorkerMessageInput) {
4141
switch (message.type) {
4242
case 'generate': {
43+
if (state.type === 'generating') {
44+
console.warn('Already generating patterns')
45+
break
46+
}
47+
4348
// Start generating patterns
4449
state = {
4550
type: 'generating',
@@ -49,7 +54,9 @@ function handleMessage(message: WorkerMessageInput) {
4954
break
5055
}
5156
case 'stop': {
52-
// Stop
57+
state = {
58+
type: 'stopped',
59+
}
5360
break
5461
}
5562
default: {
@@ -68,9 +75,26 @@ self.onmessage = event => {
6875
}
6976

7077
function generatePatterns() {
71-
// Post batches of patterns back to main thread
78+
for (const { depth, patterns } of streamBatchedDrawablePatterns({
79+
state: {
80+
patterns: [{ anchor: [0, 0], target: [0.5, 0.5] } as RelativePattern],
81+
screens: [{ anchor: [0.2, 0.2], target: [0.8, 0.8] } as AbsolutePattern],
82+
},
83+
chunkSize: 5,
84+
screenSize: [600, 400],
85+
})) {
86+
if (state.type !== 'generating') {
87+
break
88+
}
89+
90+
sendMessage({
91+
type: 'batch',
92+
depth,
93+
patterns,
94+
})
95+
}
96+
7297
sendMessage({
73-
type: 'batch',
74-
patterns: [], // TODO: Generate actual patterns
98+
type: 'done',
7599
})
76100
}

src/draw/stream-batched-drawable-patterns.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import { Size, State, ViewportPattern } from '../types'
22
import { streamDrawablePatterns } from './stream-drawable-patterns'
33

4-
type Chunk = {
4+
export type DrawableChunk = {
55
depth: number
66
patterns: ViewportPattern[]
77
}
88

99
/**
10-
* Creates a generator that groups drawable patterns from `streamDrawablePatterns` into chunks.
10+
* Creates a generator that groups drawable patterns from {@link streamDrawablePatterns} into chunks.
1111
*
1212
* Patterns are batched together based on their generation depth or a maximum
1313
* chunk size. This is useful for processing or rendering patterns in groups.
14-
*
15-
* @param state The current application state.
16-
* @param chunkSize The maximum number of patterns to include in a single chunk.
17-
* @param screenSize The current size of the screen/viewport.
18-
* @returns A generator yielding `Chunk` objects ({ depth: number, patterns: ViewportPattern[] }).
1914
*/
2015
export function* streamBatchedDrawablePatterns({
2116
state,
@@ -25,10 +20,10 @@ export function* streamBatchedDrawablePatterns({
2520
state: State
2621
chunkSize: number
2722
screenSize: Size
28-
}): Generator<Chunk, void, void> {
23+
}): Generator<DrawableChunk, void, void> {
2924
const drawQueueIterator = streamDrawablePatterns({ state, screenSize })
3025

31-
let chunk: Chunk = {
26+
let chunk: DrawableChunk = {
3227
depth: 0,
3328
patterns: [],
3429
}

src/draw/stream-drawable-patterns.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ function getViewportBoundaries(screenSize: Size): Boundaries<ViewportNumber> {
1515
}
1616
}
1717

18+
type QueueEntry = {
19+
currentPattern: ViewportPattern
20+
depth: number
21+
}
22+
1823
// We'll use a global queue to avoid reallocating it on every preview frame.
1924
const patternQueue = new Queue<QueueEntry>({
2025
initialItems: [],
@@ -48,10 +53,7 @@ const isValidPattern = (() => {
4853
}
4954
})()
5055

51-
type QueueEntry = {
52-
currentPattern: ViewportPattern
53-
depth: number
54-
}
56+
5557

5658
/**
5759
* Creates a generator that yields drawable patterns one by one.

0 commit comments

Comments
 (0)