github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/syscall/js/func.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build js && wasm 6 7 package js 8 9 // Func is a wrapped Go function to be called by JavaScript. 10 type Func struct { 11 Value 12 id uint32 13 } 14 15 // FuncOf returns a function to be used by JavaScript. 16 // 17 // The Go function fn is called with the value of JavaScript's "this" keyword and the 18 // arguments of the invocation. The return value of the invocation is 19 // the result of the Go function mapped back to JavaScript according to ValueOf. 20 // 21 // Invoking the wrapped Go function from JavaScript will 22 // pause the event loop and spawn a new goroutine. 23 // Other wrapped functions which are triggered during a call from Go to JavaScript 24 // get executed on the same goroutine. 25 // 26 // As a consequence, if one wrapped function blocks, JavaScript's event loop 27 // is blocked until that function returns. Hence, calling any async JavaScript 28 // API, which requires the event loop, like fetch (http.Client), will cause an 29 // immediate deadlock. Therefore a blocking function should explicitly start a 30 // new goroutine. 31 // 32 // Func.Release must be called to free up resources when the function will not be invoked any more. 33 func FuncOf(fn func(this Value, args []Value) any) Func 34 35 // Release frees up resources allocated for the function. 36 // The function must not be invoked after calling Release. 37 // It is allowed to call Release while the function is still running. 38 func (c Func) Release()