Skip to content

Commit 4cded80

Browse files
committed
better error-handling and memoize fetch
1 parent e67783d commit 4cded80

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

sqldef-wasm.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
// +build js
12
// This is a light wasm wraper around just the DDL conversion stuff
23
package main
34

45
import (
5-
"log"
66
"strings"
77
"syscall/js"
88
"github.com/k0kubun/sqldef/schema"
@@ -19,14 +19,15 @@ func diff(this js.Value, args []js.Value) interface {} {
1919
}
2020
ddls, err := schema.GenerateIdempotentDDLs(generatorMode, desiredDDLs, currentDDLs)
2121
out := strings.Join(ddls, ";\n")
22-
callback.Invoke(js.Null(), out)
2322

2423
// TODO: Need to figure out how to pass error in callback
2524
if err != nil {
26-
log.Fatal(err)
25+
callback.Invoke(err.Error(), out)
26+
return false
27+
} else {
28+
callback.Invoke(js.Null(), out)
29+
return true
2730
}
28-
_ = err
29-
return true
3031
}
3132

3233
func main() {

sqldef.wasm

-250 Bytes
Binary file not shown.

sqldef_browser.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
let source
2+
13
/* global WebAssembly, fetch, Go,_SQLDEF */
24
window.sqldef = async (dbType, desiredDDLs, currentDDLs) => {
35
if (WebAssembly) {
4-
if (WebAssembly && !WebAssembly.instantiateStreaming) { // polyfill
5-
WebAssembly.instantiateStreaming = async (resp, importObject) => {
6-
const source = await (await resp).arrayBuffer()
7-
return WebAssembly.instantiate(source, importObject)
8-
}
9-
}
6+
source = source || (await (await fetch('sqldef.wasm')).arrayBuffer())
107
const go = new Go()
11-
const result = await WebAssembly.instantiateStreaming(fetch('sqldef.wasm'), go.importObject)
8+
const result = await WebAssembly.instantiate(source, go.importObject)
129
go.run(result.instance)
1310
return new Promise((resolve, reject) => {
1411
_SQLDEF(dbType, desiredDDLs, currentDDLs, (err, ret) => {

0 commit comments

Comments
 (0)