github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/js/doc.go (about)

     1  /*
     2  Package js is a drop-in replacement for syscall/js that provides identical behavior in a WebAssembly
     3  environment, and useful non-functional behavior outside of WebAssembly.
     4  
     5  To use it, simply import this package instead of "syscall/js" and use it in exactly the same way.
     6  Your code will compile targeting either wasm or non-wasm environments.  In wasm, the functionality
     7  is exactly the same, the calls and types are delegated directly to "syscall/js".  The compiler
     8  will optimize away virtually (if not literally) all of the overhead associated with this aliasing.
     9  When run outside of wasm, appropriate functionality indicates that the environment is not
    10  availble: All js.Value instances are undefined.  Global(), Value.Get() always return undefined.
    11  Value.Call(), FuncOf() and other such functions will panic.  Value.Truthy() will always return false.
    12  For example, Global().Truthy() can be used to determine if the js environment is functional.
    13  
    14  Rationale: Since syscall/js is only available when the GOOS is "js", this means programs which run server-side
    15  cannot access that package and will fail to compile.  Since Vugu components are inherently closely
    16  integrated with browsers and may often need to do things like declare variables of type js.Value,
    17  this is a problem for components which are rendered not only in wasm client-side but also server-side.
    18  Build tags can be used to provide multiple implementations of a components but this can become tedious.
    19  
    20  Usually what you want is that the majority of your code which is not js-specific can be written once
    21  and execute in the browser or on the server, and the relatively small amount of functionality that
    22  is uses "js" will compile properly in both environments but just not be executed on the server.
    23  Or allow for if statements to easily disable functionality not available server-side.
    24  That's what this package provides.
    25  */
    26  package js