yamori

有給休暇計算を主目的とした簡易勤怠管理システム

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
package sqlite3_js //nolint:golint

import (
	"fmt"
	"os"
	"runtime/debug"
	"syscall/js"
)

const (
	// The name of the global where sql.js has been loaded. This is the `SQL` var of:
	//     const initSqlJs = require('sql.js');
	//     const SQL = await initSqlJs({ ...})
	globalSQLJS = "_go_sqlite"

	// The name of the global where sql.js should store its databases. This is purely
	// for debugging as JS-land doesn't ever read this map, but Go stores databases here.
	// The value of this global is an empty Map. It is crucial this isn't an empty object
	// else database names like 'hasOwnProperty' will fail due to it existing but
	// not being a database object!
	globalSQLDBs = "_go_sqlite_dbs"
)

// jsEnsureGlobal is a helper function to set-if-not-exists and return whether the global existed.
func jsEnsureGlobal(globalName string, defaultVal *js.Value) (existed bool) {
	v := js.Global().Get(globalName)
	if v.Truthy() {
		return true
	}
	if defaultVal != nil {
		js.Global().Set(globalName, *defaultVal)
		v = *defaultVal
	}
	return false
}

// jsTryCatch is a helper function that catches exceptions/panics thrown by fn and returns them as error.
// This is useful for calling JS functions which can throw.
func jsTryCatch(fn func() js.Value) (val js.Value, err error) {
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("exception: %s", e)
		}
	}()
	return fn(), nil
}

// protect is a helper function which guards against panics, setting an error when it happens.
func protect(name string, setError func(error)) {
	err := recover()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s panicked: %s\n", name, err)
		debug.PrintStack()
		setError(fmt.Errorf("%s panicked: %s", name, err))
	}
}