Skip to content

Commit

Permalink
refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
101arrowz committed Sep 9, 2023
1 parent 4f40c20 commit 9aafbb2
Show file tree
Hide file tree
Showing 11 changed files with 4,264 additions and 5,032 deletions.
4,207 changes: 4,207 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
"scripts": {
"start": "parcel src/index.html",
"build-wasm": "wasm-pack build --target web",
"build": "parcel build src/index.html --public-url ."
"build": "tsc && tsc -p src/workers/tsconfig.json && parcel build src/index.html --public-url ."
},
"devDependencies": {
"@parcel/compressor-brotli": "^2.0.1",
"@parcel/compressor-gzip": "^2.0.1",
"@parcel/packager-raw-url": "^2.0.1",
"@types/w3c-image-capture": "^1.0.5",
"parcel": "^2.0.1",
"parcel-config-pwa-manifest": "^0.1.0",
"typescript": "^4.4.4"
"@parcel/compressor-brotli": "^2.9.3",
"@parcel/compressor-gzip": "^2.9.3",
"@parcel/packager-raw-url": "^2.9.3",
"@types/w3c-image-capture": "^1.0.7",
"parcel": "^2.9.3",
"parcel-config-pwa-manifest": "^0.1.5",
"typescript": "^5.2.2"
},
"dependencies": {
"@parcel/service-worker": "^2.0.1",
"@parcel/service-worker": "^2.9.3",
"image-capture": "^0.4.0"
}
}
1 change: 0 additions & 1 deletion src-rs/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#![feature(int_abs_diff)]
#[macro_use]
extern crate alloc;

Expand Down
6 changes: 6 additions & 0 deletions src/parcel.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
declare module 'url:*' {
const url: string;
export default url;
}

declare const process: {
env: {
NODE_ENV: string
}
}
2 changes: 1 addition & 1 deletion src/process.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, Messages, Quad, Point } from './workers/process';
import type { Message, Messages, Quad, Point } from './workers/ipc';

const newWorker = () => new Worker(new URL('./workers/process.ts', import.meta.url), { type: 'module' });

Expand Down
32 changes: 32 additions & 0 deletions src/workers/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export type Point = {
x: number;
y: number;
}

export type Quad = {
a: Point;
b: Point;
c: Point;
d: Point;
};

export type Messages = {
'find-document': [{
data: ImageData;
}, Quad | undefined];
'extract-document': [{
data: ImageData;
region: Quad;
targetWidth: number;
targetHeight?: number;
}, ImageData];
'get-data': [{
bitmap: ImageBitmap;
}, ImageData];
};

export type Message = {
[T in keyof Messages]: {
type: T;
} & Messages[T][0];
}[keyof Messages];
9 changes: 0 additions & 9 deletions src/workers/offscreencanvas.d.ts

This file was deleted.

42 changes: 6 additions & 36 deletions src/workers/process.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
import init, { find_document, extract_document, Quad as WasmQuad } from '../../pkg/scanner';
import { Message, Messages } from './ipc'

export type Point = {
x: number;
y: number;
}

export type Quad = {
a: Point;
b: Point;
c: Point;
d: Point;
};

export type Messages = {
'find-document': [{
data: ImageData;
}, Quad | undefined];
'extract-document': [{
data: ImageData;
region: Quad;
targetWidth: number;
targetHeight?: number;
}, ImageData];
'get-data': [{
bitmap: ImageBitmap;
}, ImageData];
};

export type Message = {
[T in keyof Messages]: {
type: T;
} & Messages[T][0];
}[keyof Messages];
declare const self: DedicatedWorkerGlobalScope

const handle = <T extends Message>(message: T): { result: Messages[T['type']][1]; transfer?: Transferable[] } => {
if (message.type == 'find-document') {
Expand Down Expand Up @@ -77,17 +47,17 @@ const handle = <T extends Message>(message: T): { result: Messages[T['type']][1]

let load = init().catch(() => {});

onmessage = async evt => {
self.onmessage = async (evt: MessageEvent<{ msg: Message }>) => {
await load;
const { msg, ...data } = evt.data;
try {
const { result, transfer } = handle(msg as Message);
postMessage({ result, ...data }, transfer || []);
const { result, transfer } = handle(msg);
self.postMessage({ result, ...data }, transfer || []);
} catch (err) {
if (!(err instanceof Error)) {
err = new Error(`Error in process worker: ${err}`);
}
postMessage({
self.postMessage({
error: {
message: (err as Error).message,
stack: (err as Error).stack,
Expand Down
3 changes: 2 additions & 1 deletion src/workers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"module": "ESNext",
"moduleResolution": "node",
"noEmit": true,
"lib": ["ESNext", "WebWorker"]
"lib": ["ESNext", "WebWorker"],
"typeRoots": []
},
"include": ["."]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"noEmit": true
},
"include": ["src"],
"exclude": ["src/workers"]
"exclude": ["src/workers/**/*"]
}
Loading

0 comments on commit 9aafbb2

Please sign in to comment.