github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/examples/sites/globalstate/state/gen_state_stateGen.go (about)

     1  // Code generated by stateGen; DO NOT EDIT.
     2  
     3  package state
     4  
     5  import (
     6  	"errors"
     7  	"path"
     8  	"strings"
     9  
    10  	"myitcv.io/react/examples/sites/globalstate/model"
    11  )
    12  
    13  var _ Node = new(App)
    14  
    15  type App struct {
    16  	*rootNode
    17  	prefix string
    18  
    19  	_Root          *Data
    20  	_CurrentPerson *ModelPersonPLeaf
    21  }
    22  
    23  func newApp(r *rootNode, prefix string) *App {
    24  	prefix = path.Join(prefix, "App")
    25  
    26  	res := &App{
    27  		rootNode: r,
    28  		prefix:   prefix,
    29  	}
    30  	res._Root = newData(r, prefix)
    31  	res._CurrentPerson = newModelPersonPLeaf(r, prefix)
    32  	return res
    33  }
    34  
    35  func (n *App) Subscribe(cb func()) *Sub {
    36  	return n.rootNode.subscribe(n.prefix, cb)
    37  }
    38  func (n *App) Root() *Data {
    39  	return n._Root
    40  }
    41  func (n *App) CurrentPerson() *ModelPersonPLeaf {
    42  	return n._CurrentPerson
    43  }
    44  
    45  var _ Node = new(Data)
    46  
    47  type Data struct {
    48  	*rootNode
    49  	prefix string
    50  
    51  	_People *ModelPeoplePLeaf
    52  }
    53  
    54  func newData(r *rootNode, prefix string) *Data {
    55  	prefix = path.Join(prefix, "Data")
    56  
    57  	res := &Data{
    58  		rootNode: r,
    59  		prefix:   prefix,
    60  	}
    61  	res._People = newModelPeoplePLeaf(r, prefix)
    62  	return res
    63  }
    64  
    65  func (n *Data) Subscribe(cb func()) *Sub {
    66  	return n.rootNode.subscribe(n.prefix, cb)
    67  }
    68  func (n *Data) People() *ModelPeoplePLeaf {
    69  	return n._People
    70  }
    71  
    72  type ModelPersonPLeaf struct {
    73  	*rootNode
    74  	prefix string
    75  }
    76  
    77  var _ Node = new(ModelPersonPLeaf)
    78  
    79  func newModelPersonPLeaf(r *rootNode, prefix string) *ModelPersonPLeaf {
    80  	prefix = path.Join(prefix, "ModelPersonPLeaf")
    81  
    82  	return &ModelPersonPLeaf{
    83  		rootNode: r,
    84  		prefix:   prefix,
    85  	}
    86  }
    87  
    88  func (m *ModelPersonPLeaf) Get() *model.Person {
    89  	var res *model.Person
    90  	if v, ok := m.rootNode.get(m.prefix); ok {
    91  		return v.(*model.Person)
    92  	}
    93  	return res
    94  }
    95  
    96  func (m *ModelPersonPLeaf) Set(v *model.Person) {
    97  	m.rootNode.set(m.prefix, v)
    98  }
    99  
   100  func (m *ModelPersonPLeaf) Subscribe(cb func()) *Sub {
   101  	return m.rootNode.subscribe(m.prefix, cb)
   102  }
   103  
   104  type ModelPeoplePLeaf struct {
   105  	*rootNode
   106  	prefix string
   107  }
   108  
   109  var _ Node = new(ModelPeoplePLeaf)
   110  
   111  func newModelPeoplePLeaf(r *rootNode, prefix string) *ModelPeoplePLeaf {
   112  	prefix = path.Join(prefix, "ModelPeoplePLeaf")
   113  
   114  	return &ModelPeoplePLeaf{
   115  		rootNode: r,
   116  		prefix:   prefix,
   117  	}
   118  }
   119  
   120  func (m *ModelPeoplePLeaf) Get() *model.People {
   121  	var res *model.People
   122  	if v, ok := m.rootNode.get(m.prefix); ok {
   123  		return v.(*model.People)
   124  	}
   125  	return res
   126  }
   127  
   128  func (m *ModelPeoplePLeaf) Set(v *model.People) {
   129  	m.rootNode.set(m.prefix, v)
   130  }
   131  
   132  func (m *ModelPeoplePLeaf) Subscribe(cb func()) *Sub {
   133  	return m.rootNode.subscribe(m.prefix, cb)
   134  }
   135  func NewRoot() *App {
   136  	r := &rootNode{
   137  		store: make(map[string]interface{}),
   138  		cbs:   make(map[string]map[*Sub]struct{}),
   139  		subs:  make(map[*Sub]struct{}),
   140  	}
   141  
   142  	return newApp(r, "")
   143  }
   144  
   145  type Node interface {
   146  	Subscribe(cb func()) *Sub
   147  }
   148  
   149  type Sub struct {
   150  	*rootNode
   151  	prefix string
   152  	cb     func()
   153  }
   154  
   155  func (s *Sub) Clear() {
   156  	s.rootNode.unsubscribe(s)
   157  }
   158  
   159  var NoSuchSubErr = errors.New("No such sub")
   160  
   161  type rootNode struct {
   162  	store map[string]interface{}
   163  	cbs   map[string]map[*Sub]struct{}
   164  	subs  map[*Sub]struct{}
   165  }
   166  
   167  func (r *rootNode) subscribe(prefix string, cb func()) *Sub {
   168  
   169  	res := &Sub{
   170  		cb:       cb,
   171  		prefix:   prefix,
   172  		rootNode: r,
   173  	}
   174  
   175  	l, ok := r.cbs[prefix]
   176  	if !ok {
   177  		l = make(map[*Sub]struct{})
   178  		r.cbs[prefix] = l
   179  	}
   180  
   181  	l[res] = struct{}{}
   182  	r.subs[res] = struct{}{}
   183  
   184  	return res
   185  }
   186  
   187  func (r *rootNode) unsubscribe(s *Sub) {
   188  	if _, ok := r.subs[s]; !ok {
   189  		panic(NoSuchSubErr)
   190  	}
   191  
   192  	l, ok := r.cbs[s.prefix]
   193  	if !ok {
   194  		panic("Real problems...")
   195  	}
   196  
   197  	delete(l, s)
   198  	delete(r.subs, s)
   199  }
   200  
   201  func (r *rootNode) get(k string) (interface{}, bool) {
   202  	v, ok := r.store[k]
   203  	return v, ok
   204  }
   205  
   206  func (r rootNode) set(k string, v interface{}) {
   207  	if curr, ok := r.store[k]; ok && v == curr {
   208  		return
   209  	}
   210  
   211  	r.store[k] = v
   212  
   213  	parts := strings.Split(k, "/")
   214  
   215  	var subs []*Sub
   216  
   217  	var kk string
   218  
   219  	for _, p := range parts {
   220  		kk = path.Join(kk, p)
   221  
   222  		if ll, ok := r.cbs[kk]; ok {
   223  			for k := range ll {
   224  				subs = append(subs, k)
   225  			}
   226  		}
   227  
   228  	}
   229  
   230  	for _, s := range subs {
   231  		s.cb()
   232  	}
   233  }