forked from KurokuLabs/margo
/
store.go
357 lines (303 loc) · 7.56 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package mg
import (
"margo.sh/mgpf"
yotsuba "margo.sh/why_would_you_make_yotsuba_cry"
"path/filepath"
"strings"
"sync"
)
var _ Dispatcher = (&Store{}).Dispatch
// Dispatcher is the signature of the Store.Dispatch method
type Dispatcher func(Action)
// Subscriber is the signature of the function accepted by Store.Subscribe
type Subscriber func(*Ctx)
type dispatchHandler func()
type storeReducers struct {
before reducerList
use reducerList
after reducerList
}
func (sr storeReducers) Reduce(mx *Ctx) *Ctx {
mx.Profile.Do("Before", func() {
mx = sr.before.reduction(mx)
})
mx.Profile.Do("Use", func() {
mx = sr.use.reduction(mx)
})
mx.Profile.Do("After", func() {
mx = sr.after.reduction(mx)
})
return mx.defr.reduction(mx)
}
func (sr storeReducers) Copy(updaters ...func(*storeReducers)) storeReducers {
for _, f := range updaters {
f(&sr)
}
return sr
}
// Store holds global, shared state
type Store struct {
// KVMap is an in-memory cache of data with automatic eviction.
// Eviction might happen if the active view changes.
//
// NOTE: it's not safe to store values with *Ctx objects here; use *Ctx.KVMap instead
KVMap
mu sync.Mutex
state *State
subs []*struct{ Subscriber }
sub Subscriber
reducers struct {
sync.Mutex
storeReducers
}
cfg EditorConfig `mg.Nillable:"true"`
ag *Agent
tasks *taskTracker
cache struct {
sync.RWMutex
vName string
vHash string
}
dsp struct {
sync.RWMutex
lo chan dispatchHandler
hi chan dispatchHandler
unmounted bool
}
}
func (sto *Store) mount() {
go sto.dispatcher()
}
func (sto *Store) unmount() {
done := make(chan struct{})
sto.dsp.hi <- func() {
defer close(done)
sto.dsp.Lock()
defer sto.dsp.Unlock()
if sto.dsp.unmounted {
return
}
sto.dsp.unmounted = true
sto.handleAct(unmount{}, nil)
}
<-done
}
// Dispatch schedules a new reduction with Action act
//
// * actions coming from the editor has a higher priority
// * as a result, if Shutdown is dispatched, the action might be dropped
func (sto *Store) Dispatch(act Action) {
c := sto.dsp.lo
f := func() { sto.handleAct(act, nil) }
select {
case c <- f:
default:
go func() { c <- f }()
}
}
func (sto *Store) nextDispatcher() dispatchHandler {
var h dispatchHandler
select {
case h = <-sto.dsp.hi:
default:
select {
case h = <-sto.dsp.hi:
case h = <-sto.dsp.lo:
}
}
sto.dsp.RLock()
defer sto.dsp.RUnlock()
if sto.dsp.unmounted {
return nil
}
return h
}
func (sto *Store) dispatcher() {
sto.ag.Log.Println("started")
sto.handleAct(initAction{}, nil)
for {
if f := sto.nextDispatcher(); f != nil {
f()
} else {
return
}
}
}
func (sto *Store) handleReduce(mx *Ctx) *Ctx {
defer mx.Profile.Push("action|" + ActionLabel(mx.Action)).Pop()
return sto.reducers.Reduce(mx)
}
func (sto *Store) handle(h func() *Ctx, p *mgpf.Profile) {
p.Push("handleRequest")
sto.mu.Lock()
mx := h()
sto.state = mx.State
subs := sto.subs
sto.mu.Unlock()
p.Pop()
for _, p := range subs {
p.Subscriber(mx)
}
}
func (sto *Store) handleAct(act Action, p *mgpf.Profile) {
if p == nil {
p = mgpf.NewProfile("")
}
sto.handle(func() *Ctx {
mx := newCtx(sto, nil, act, "", p)
return sto.handleReduce(mx)
}, p)
}
func (sto *Store) handleReq(rq *agentReq) {
sto.handle(func() *Ctx {
newMx := func(st *State, act Action) *Ctx {
return newCtx(sto, st, act, rq.Cookie, rq.Profile)
}
mx, acts := sto.handleReqInit(rq, newMx(nil, nil))
for _, act := range acts {
st := mx.State.new()
st.Errors = mx.State.Errors
mx = newMx(st, act)
mx = sto.handleReduce(mx)
}
return mx
}, rq.Profile)
}
func (sto *Store) handleReqInit(rq *agentReq, mx *Ctx) (*Ctx, []Action) {
defer mx.Profile.Push("init").Pop()
acts := make([]Action, 0, len(rq.Actions))
for _, ra := range rq.Actions {
act, err := sto.ag.createAction(ra)
if err != nil {
mx.State = mx.AddErrorf("createAction(%s): %s", ra.Name, err)
} else {
acts = append(acts, act)
}
}
if cfg := sto.cfg; cfg != nil {
mx.Config = cfg
}
props := rq.Props
if ep := props.Editor.EditorProps; ep.Name != "" {
mx.Editor = ep
}
if v := props.View; v != nil && v.Name != "" {
mx.View = v
sto.initCache(v)
v.finalize()
}
if len(props.Env) != 0 {
mx.Env = props.Env
}
mx.Env = sto.autoSwitchInternalGOPATH(mx)
return mx, acts
}
// autoSwitchInternalGOPATH returns mx.Env with GOPATH set to the agent's GOPATH
// if mx.View.Filename is a child of said GOPATH
func (sto *Store) autoSwitchInternalGOPATH(mx *Ctx) EnvMap {
fn := mx.View.Path
if fn == "" {
return mx.Env
}
gp := yotsuba.AgentBuildContext.GOPATH
for _, dir := range strings.Split(gp, string(filepath.ListSeparator)) {
if IsParentDir(dir, fn) {
return mx.Env.Add("GOPATH", gp)
}
}
return mx.Env
}
// NewCtx returns a new Ctx initialized using the internal StickyState.
// The caller is responsible for calling Ctx.Cancel() when done with the Ctx
func (sto *Store) NewCtx(act Action) *Ctx {
sto.mu.Lock()
defer sto.mu.Unlock()
return newCtx(sto, nil, act, "", nil)
}
func newStore(ag *Agent, sub Subscriber) *Store {
sto := &Store{
sub: sub,
ag: ag,
}
sto.state = &State{
StickyState: StickyState{View: newView(sto)},
}
sto.tasks = &taskTracker{}
sto.After(sto.tasks)
// 640 slots ought to be enough for anybody
sto.dsp.lo = make(chan dispatchHandler, 640)
sto.dsp.hi = make(chan dispatchHandler, 640)
return sto
}
// Subscribe arranges for sub to be called after each reduction takes place
// the function returned can be used to unsubscribe from further notifications
func (sto *Store) Subscribe(sub Subscriber) (unsubscribe func()) {
sto.mu.Lock()
defer sto.mu.Unlock()
p := &struct{ Subscriber }{sub}
sto.subs = append(sto.subs[:len(sto.subs):len(sto.subs)], p)
return func() {
sto.mu.Lock()
defer sto.mu.Unlock()
subs := make([]*struct{ Subscriber }, 0, len(sto.subs)-1)
for _, q := range sto.subs {
if p != q {
subs = append(subs, q)
}
}
sto.subs = subs
}
}
func (sto *Store) updateReducers(updaters ...func(*storeReducers)) *Store {
sto.reducers.Lock()
defer sto.reducers.Unlock()
sto.reducers.storeReducers = sto.reducers.Copy(updaters...)
return sto
}
// Before adds reducers to the list of reducers
// they're are called before normal (Store.Use) reducers
func (sto *Store) Before(reducers ...Reducer) *Store {
return sto.updateReducers(func(sr *storeReducers) {
sr.before = sr.before.Add(reducers...)
})
}
// Use adds reducers to the list of reducers
// they're called after reducers added with Store.Before
// and before reducers added with Store.After
func (sto *Store) Use(reducers ...Reducer) *Store {
return sto.updateReducers(func(sr *storeReducers) {
sr.use = sr.use.Add(reducers...)
})
}
// After adds reducers to the list of reducers
// they're are called after normal (Store.Use) reducers
func (sto *Store) After(reducers ...Reducer) *Store {
return sto.updateReducers(func(sr *storeReducers) {
sr.after = sr.after.Add(reducers...)
})
}
// SetBaseConfig sets the EditorConfig on which State.Config is based
//
// this method is made available for use by editor/client integration
// normal users should use State.SetConfig instead
func (sto *Store) SetBaseConfig(cfg EditorConfig) *Store {
sto.mu.Lock()
defer sto.mu.Unlock()
sto.cfg = cfg
return sto
}
// Begin starts a new task and returns its ticket
func (sto *Store) Begin(t Task) *TaskTicket {
return sto.tasks.Begin(t)
}
func (sto *Store) initCache(v *View) {
cc := &sto.cache
cc.Lock()
defer cc.Unlock()
if cc.vHash == v.Hash && cc.vName == v.Name {
return
}
sto.KVMap.Clear()
cc.vHash = v.Hash
cc.vName = v.Name
}