space.go 862 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package app
  2. type ID int
  3. // Context of a function call from proxy to app.
  4. type Context interface {
  5. CallerTag() string
  6. }
  7. type Caller interface {
  8. Tag() string
  9. }
  10. // A Space contains all apps that may be available in a V2Ray runtime.
  11. // Caller must check the availability of an app by calling HasXXX before getting its instance.
  12. type Space interface {
  13. HasApp(ID) bool
  14. GetApp(ID) interface{}
  15. BindApp(ID, interface{})
  16. }
  17. type spaceImpl struct {
  18. cache map[ID]interface{}
  19. }
  20. func NewSpace() Space {
  21. return &spaceImpl{
  22. cache: make(map[ID]interface{}),
  23. }
  24. }
  25. func (this *spaceImpl) HasApp(id ID) bool {
  26. _, found := this.cache[id]
  27. return found
  28. }
  29. func (this *spaceImpl) GetApp(id ID) interface{} {
  30. obj, found := this.cache[id]
  31. if !found {
  32. return nil
  33. }
  34. return obj
  35. }
  36. func (this *spaceImpl) BindApp(id ID, object interface{}) {
  37. this.cache[id] = object
  38. }