2 Commits

Author SHA1 Message Date
Nicolas Lepage
c94dcd965d chore: v2.0.4 2024-11-27 22:33:50 +01:00
Nicolas Lepage
2d786bdb14 fix: listen for ReadableStream cancellation 2024-11-27 22:32:19 +01:00
5 changed files with 22 additions and 6 deletions

View File

@@ -97,7 +97,7 @@ Create a ServiceWorker file with the following code:
📄 `sw.js`
```js
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.18.4/misc/wasm/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.3/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.4/sw.js')
registerWasmHTTPListener('path/to/server.wasm')
```

View File

@@ -1,5 +1,5 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.3/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.4/sw.js')
const wasm = 'api.wasm'

View File

@@ -1,5 +1,5 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.3/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.4/sw.js')
const wasm = 'api.wasm'

View File

@@ -1,5 +1,5 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.3/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.4/sw.js')
const wasm = 'api.wasm'

View File

@@ -12,6 +12,7 @@ type Writer struct {
Value safejs.Value
controller safejs.Value
ctx context.Context
cancelled bool
}
var _ io.WriteCloser = (*Writer)(nil)
@@ -54,14 +55,25 @@ func NewWriter() (*Writer, error) {
return nil, err
}
return &Writer{
rs := &Writer{
Value: value,
controller: controller,
ctx: ctx,
}, nil
}
go func() {
<-ctx.Done()
rs.cancelled = true
}()
return rs, nil
}
func (rs *Writer) Write(b []byte) (int, error) {
if rs.cancelled {
return 0, nil
}
chunk, err := jstype.Uint8Array.New(len(b)) // FIXME reuse same Uint8Array?
if err != nil {
return 0, err
@@ -78,6 +90,10 @@ func (rs *Writer) Write(b []byte) (int, error) {
}
func (rs *Writer) Close() error {
if rs.cancelled {
return nil
}
_, err := rs.controller.Call("close")
return err
}