Common isomorphic functionality in one kit.

opdetails.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // The Isomorphic Go Project
  2. // Copyright (c) Wirecog, LLC. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license, which can be found in the LICENSE file.
  5. package isokit
  6. import "github.com/gopherjs/gopherjs/js"
  7. type OperatingDetails struct {
  8. Environment int
  9. Runtime int
  10. }
  11. const (
  12. ServerEnvironment = iota
  13. WebBrowserEnvironment
  14. )
  15. const (
  16. GoRuntime = iota
  17. JSRuntime
  18. )
  19. var (
  20. operatingEnvironment int
  21. operatingRuntime int
  22. )
  23. func isJSRuntime() bool {
  24. return js.Global != nil
  25. }
  26. func isGoRuntime() bool {
  27. return !isJSRuntime()
  28. }
  29. func isWebBrowserEnvironment() bool {
  30. return isJSRuntime() && js.Global.Get("document") != js.Undefined
  31. }
  32. func isServerEnvironment() bool {
  33. if isGoRuntime() == true {
  34. return true
  35. } else if isJSRuntime() == true {
  36. return !isWebBrowserEnvironment()
  37. } else {
  38. return true
  39. }
  40. }
  41. func OperatingEnvironment() int {
  42. return operatingEnvironment
  43. }
  44. func OperatingRuntime() int {
  45. return operatingRuntime
  46. }
  47. func initializeOperatingDetails() {
  48. if isJSRuntime() == true {
  49. operatingRuntime = WebBrowserEnvironment
  50. }
  51. if isGoRuntime() == true {
  52. operatingRuntime = GoRuntime
  53. }
  54. if isServerEnvironment() == true {
  55. operatingEnvironment = ServerEnvironment
  56. }
  57. if isWebBrowserEnvironment() == true {
  58. operatingEnvironment = WebBrowserEnvironment
  59. }
  60. }
  61. func init() {
  62. initializeOperatingDetails()
  63. }