]> zoso.dev Git - libnemo.git/commitdiff
Add and fix typings. Throw if any shader issues arise.
authorChris Duncan <chris@zoso.dev>
Tue, 3 Dec 2024 23:36:22 +0000 (15:36 -0800)
committerChris Duncan <chris@zoso.dev>
Tue, 3 Dec 2024 23:36:22 +0000 (15:36 -0800)
src/lib/pow.ts

index 547a7582ba8cc903f85887625a732a258b7f46bd..f7b975f50844b74fe773c8a26eb2136c2dd578de 100644 (file)
@@ -23,8 +23,8 @@ const p = () => {
                }
        })
 
-       async function find (hash: string, threshold: string = SEND_THRESHOLD): Promise<void> {
-               return new Promise(resolve => {
+       async function find (hash: string, threshold: string = SEND_THRESHOLD): Promise<string> {
+               return new Promise<string>(resolve => {
                        calculate(hash, resolve, undefined, threshold)
                })
        }
@@ -67,7 +67,7 @@ const p = () => {
                return out
        }
 
-       function calculate (hashHex: string, callback: (nonce: string) => any, progressCallback: (frames: number) => any, threshold: number | string = '0xFFFFFFF8'): void {
+       function calculate (hashHex: string, callback: (nonce: string | PromiseLike<string>) => any, progressCallback?: (frames: number) => any, threshold: number | string = '0xFFFFFFF8'): void {
                if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16)
 
                const canvas = new OffscreenCanvas(webglWidth, webglHeight)
@@ -260,6 +260,8 @@ const p = () => {
     }`
 
                const vertexShader = gl.createShader(gl.VERTEX_SHADER)
+               if (vertexShader == null)
+                       throw 'error creating vertex shader'
                gl.shaderSource(vertexShader, vsSource)
                gl.compileShader(vertexShader)
 
@@ -267,6 +269,8 @@ const p = () => {
                        throw gl.getShaderInfoLog(vertexShader)
 
                const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
+               if (fragmentShader == null)
+                       throw 'error creating fragment shader'
                gl.shaderSource(fragmentShader, fsSource)
                gl.compileShader(fragmentShader)
 
@@ -274,6 +278,8 @@ const p = () => {
                        throw gl.getShaderInfoLog(fragmentShader)
 
                const program = gl.createProgram()
+               if (program == null)
+                       throw 'error creating program'
                gl.attachShader(program, vertexShader)
                gl.attachShader(program, fragmentShader)
                gl.linkProgram(program)
@@ -316,7 +322,7 @@ const p = () => {
                const work1 = new Uint8Array(4)
                let n = 0
 
-               function draw () {
+               const draw = (): void => {
                        n++
                        crypto.getRandomValues(work0)
                        crypto.getRandomValues(work1)
@@ -338,14 +344,12 @@ const p = () => {
                                if (pixels[i] !== 0) {
                                        // Return the work value with the custom bits
                                        typeof callback === 'function' &&
-                                               callback(
-                                                       hexify(work1) +
-                                                       hexify([
-                                                               pixels[i + 2],
-                                                               pixels[i + 3],
-                                                               work0[2] ^ (pixels[i] - 1),
-                                                               work0[3] ^ (pixels[i + 1] - 1)
-                                                       ]), n)
+                                               callback(hexify(work1) + hexify([
+                                                       pixels[i + 2],
+                                                       pixels[i + 3],
+                                                       work0[2] ^ (pixels[i] - 1),
+                                                       work0[3] ^ (pixels[i + 1] - 1)
+                                               ]))
                                        return
                                }
                        }