jopher

package module
Version: v0.0.0-...-56bcab3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2020 License: MIT Imports: 3 Imported by: 0

README

jopher CircleCI Documentation

jopher provides utility functions for working with GopherJS. Various functions are exposed, from promise wrappers to helpers for calling JS functions.

Installation

Use dep to install:

dep ensure -add github.com/miratronix/jopher

Examples

Promisify

The simplest way to create promises is to wrap existing functions with Promisify:

import "github.com/miratronix/jopher"

func main() {

	// As part of a global
	js.Global.Set("httpCall", jopher.Promisify(httpCall))

	// or as part of a structured object:
	js.Global.Set("api", map[string]interface{}{
		"httpCall": jopher.Promisify(httpCall),
	})
}

// This is a blocking function -- it doesn't return until the http call completes or fails.
func httpCall() (SomeResponse, error) {
	response, err := http.Get("/someAPI")
	if err != nil {
		return nil, err
	}
	return response, nil
}

Promisify allows JS to call the underlying function via reflection and automatically detects an 'error' return type, using the following rules, in order:

  • If the function panics, the promise is rejected with the panic value.
  • If the last return is of type 'error', then the promise is rejected if the returned error is non-nil.
  • The promise is resolved with the remaining return values, according to how many there are:
    • 0: resolved with nil
    • 1: resolved with that value
    • 2+: resolved with a slice of the values
New Promise

You can also construct a new promise and manage the resolve/reject yourself:

import "github.com/miratronix/jopher"

func main() {
	js.Global.Set("httpCall", jopher.NewPromise(httpCall))
}

// A blocking function, as before
func httpCall(resolve, reject func(interface{})) {
	response, err := http.Get("/someAPI")
	if err != nil {
		reject(err)
	}
	resolve(response)
}
Resolve/Reject

For small methods that don't block, it can be useful to quickly return a promise:

import "github.com/miratronix/jopher"

func main() {
	js.Global.Set("httpCall", httpCall)
}

func httpCall() *js.Object {

	// Return an immediately resolved promise
	return jopher.Resolve(1)

	// Or a rejected one
	return jopher.Reject(2)
}
Calling Javascript Functions

jopher supplies 2 functions for calling javascript functions:

import "github.com/miratronix/jopher"

// Some JS object with functions that accept a callback
var jsObject *js.Object

// Attaches a callback to the supplied argument list and returns when it's called
result, err := jopher.CallWithResultCallback(jsObject, "someFunction", "someArgument")

// Attaches a callback to the supplied argument list and returns once the callback is called
err := jopher.CallWithErrorCallback(jsObject, "someOtherFunction", "someArgument")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallOnPanic

func CallOnPanic(reject func(interface{}))

CallOnPanic calls the supplied function when a panic is recovered. This should be called in a defer.

func CallWithErrorCallback

func CallWithErrorCallback(jsObject *js.Object, fn string, args ...interface{}) error

CallWithErrorCallback calls a function in the supplied JS object with the supplied arguments, automatically attaching a callback to the end of the argument list that accepts an error.

func CallWithResultCallback

func CallWithResultCallback(jsObject *js.Object, fn string, args ...interface{}) (*js.Object, error)

CallWithResultCallback calls a function in the provided JS object, automatically attaching a callback parameter to the end of the argument list. Returns when the JS callback is called with the appropriate value or error.

func CatchPanic

func CatchPanic(returnedError *error, failureMessage string)

CatchPanic catches a panic and puts the error into the supplied error. This should be called in a defer.

func ForEach

func ForEach(object *js.Object, iterator func(key string, value *js.Object))

ForEach iterates over the keys in a javascript object.

func HasKey

func HasKey(object *js.Object, key string) bool

HasKey determines if the supplied javascript object has the specified key.

func IsArray

func IsArray(object *js.Object) bool

IsArray determines if the supplied javascript object is an array.

func IsFunction

func IsFunction(object *js.Object) bool

IsFunction determines if the supplied javascript object is a function.

func Keys

func Keys(object *js.Object) []string

Keys gets the keys in a javascript object.

func NewError

func NewError(message string) *js.Object

NewError creates a new JS error.

func NewISODate

func NewISODate() string

NewISODate creates a new RFC3339 date string using a javascript native call.

func NewObject

func NewObject() *js.Object

NewObject create a new javascript object.

func NewPromise

func NewPromise(function func(resolve func(interface{}), reject func(interface{}))) *js.Object

NewPromise constructs a new promise using a `(resolve, reject)` callback, similar to javascript.

func Parse

func Parse(bytes []byte) (_ map[string]interface{}, returnedError error)

Parse is a shortcut function for using the native JS JSON parse()

func Promisify

func Promisify(function interface{}) func(args ...interface{}) *js.Object

Promisify promisifies an existing function, returning the new version.

func Reject

func Reject(value interface{}) *js.Object

Reject returns a new promise that is rejected with the supplied value.

func Require

func Require(module string) *js.Object

Require requires a module (only works in node or if a `require` polyfill is supplied).

func Resolve

func Resolve(value interface{}) *js.Object

Resolve returns a new promise that is resolved with the supplied value.

func Stringify

func Stringify(data map[string]interface{}) (_ []byte, returnedError error)

Stringify is a shortcut function for using the native JS JSON stringify()

func Throw

func Throw(object *js.Object)

Throw throws the supplied javascript object.

func ThrowOnError

func ThrowOnError(err error)

ThrowOnError throws when the supplied error is not nil.

func ToGoError

func ToGoError(jsError *js.Error) error

ToGoError converts a javascript error object to a Go error.

func ToMap

func ToMap(object *js.Object) map[string]interface{}

ToMap converts a javascript object to a map.

func ToSlice

func ToSlice(array *js.Object) []interface{}

ToSlice converts a javascript object to a slice.

func ToString

func ToString(object *js.Object) string

ToString converts a javascript object to a string.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL