cacher.go 479 B

12345678910111213141516171819202122232425
  1. //go:build js && wasm
  2. // +build js,wasm
  3. // Package jscache caches expensive JavaScript results, like string encoding
  4. package jscache
  5. import (
  6. "github.com/hack-pad/safejs"
  7. )
  8. type cacher struct {
  9. cache map[string]safejs.Value
  10. }
  11. func (c *cacher) value(key string, valueFn func() safejs.Value) safejs.Value {
  12. if val, ok := c.cache[key]; ok {
  13. return val
  14. }
  15. if c.cache == nil {
  16. c.cache = make(map[string]safejs.Value)
  17. }
  18. val := valueFn()
  19. c.cache[key] = val
  20. return val
  21. }