The RemoteSource that answers JEV functions was a package global; it is now a field of the Workbook, set with SetRemote, which recomputes the functions so a loaded file's questions are asked. Formulas reach it through their reader. The global stays for now as the source new workbooks start with, until its callers move to SetRemote.
- commit
- 487c1640c10c0c5064bef31056da9dc265c9a110
- parent
- b6ca379d9bc67600481c1928f3dce24e80dc9f67
- author
- Dami <dami.etoile@gmail.com>
| M | internal/ | 8 | +++++--- |
| M | internal/ | 12 | ++---------- |
| M | internal/ | 18 | +++++++++++++++--- |
| M | internal/ | 8 | +++++--- |
@@ -47,7 +47,8 @@ const (
4747 // failure; the context line says why.4848 var ErrRemote = Value{Kind: Error, Str: "#ERROR!"}4949 50-// remoteEval evaluates a JEV function through Remote, converting the50+// remoteEval evaluates a JEV function through the workbook's51+// RemoteSource, converting the5152 // answer with result. Errors in the inputs come back as-is so they can be5253 // fixed; they're never sent.5354 func remoteEval(build func([]Node, lookup) (RemoteCall, error), result func(RemoteAnswer) Value) func([]Node, lookup) Value {@@ -56,10 +57,11 @@ func remoteEval(build func([]Node, lookup) (RemoteCall, error), result func(Remo
5657 if err != nil {5758 return err.(inputError).v5859 }59- if Remote == nil {60+ remote := get.w.remote61+ if remote == nil {6062 return ErrNoRemote6163 }62- ans, ok := Remote.Lookup(call)64+ ans, ok := remote.Lookup(call)6365 switch {6466 case !ok:6567 return Pending@@ -17,13 +17,6 @@ func (f *fakeRemote) Lookup(c RemoteCall) (RemoteAnswer, bool) {
1717 return a, ok1818 }1919 20-func useRemote(t *testing.T, r RemoteSource) {21- t.Helper()22- prev := Remote23- Remote = r24- t.Cleanup(func() { Remote = prev })25-}26-2720 func TestJEVFunctions(t *testing.T) {2821 f := &fakeRemote{answers: map[string]RemoteAnswer{2922 "Is this a complaint?": {Noul: 0.83},@@ -31,8 +24,8 @@ func TestJEVFunctions(t *testing.T) {
3124 "Urgency": {Score: 1.6, Confidence: 0.7},3225 "Broken": {Failed: "HTTP 500"},3326 }}34- useRemote(t, f)3527 s := New()28+ s.SetRemote(f)3629 s.Set(at("A1"), "The package arrived crushed and support never replied.")3730 s.Set(at("B1"), "positive")3831 s.Set(at("B2"), "negative")@@ -70,8 +63,8 @@ func TestJEVFunctions(t *testing.T) {
7063 7164 func TestJEVCalls(t *testing.T) {7265 f := &fakeRemote{}73- useRemote(t, f)7466 s := New()67+ s.SetRemote(f)7568 s.Set(at("A1"), "text")7669 s.Set(at("A2"), "42")7770 s.Set(at("B1"), "good")@@ -110,7 +103,6 @@ func TestJEVCalls(t *testing.T) {
110103 }111104 112105 func TestJEVWithoutKey(t *testing.T) {113- useRemote(t, nil)114106 s := New()115107 s.Set(at("A1"), `=JEV.TEST("x", "Q")`)116108 if s.Value(at("A1")) != ErrNoRemote {@@ -4,7 +4,7 @@ import "github.com/FelineStateMachine/012/internal/formula"
44 55 // Some functions (JEV.*) are answered by a hosted model. The engine never66 // talks to the network: it describes each question as a RemoteCall and7-// asks Remote for the answer. Until the answer arrives the cell shows7+// asks the workbook's RemoteSource for the answer. Until the answer arrives the cell shows88 // Loading…, and the source queues the call; when answers arrive the UI99 // calls RecalcVolatile, and the functions, being volatile, look again.1010 @@ -36,10 +36,22 @@ type RemoteSource interface {
3636 Lookup(RemoteCall) (RemoteAnswer, bool)3737 }3838 39-// Remote answers JEV functions. It is nil when no API key is configured,40-// and the functions then evaluate to ErrNoRemote.39+// Remote is the source new workbooks start with.40+//41+// Deprecated: set each workbook's source with SetRemote.4142 var Remote RemoteSource4243 44+// SetRemote sets what answers the workbook's JEV functions: nil when no45+// API key is configured, and the functions then evaluate to ErrNoRemote.46+// It recomputes them, so a loaded file's questions are asked.47+func (w *Workbook) SetRemote(r RemoteSource) {48+ w.remote = r49+ w.recalc(nil)50+}51+52+// SetRemote on a sheet is its workbook's.53+func (s *Sheet) SetRemote(r RemoteSource) { s.wb.SetRemote(r) }54+4355 var (4456 // Pending is shown while an answer is on its way.4557 Pending = Value{Kind: Error, Str: "Loading…"}@@ -39,9 +39,10 @@ type Workbook struct {
3939 // Circular is set when the last recalculation found a cycle.4040 Circular bool4141 42- hist history // undo and redo, see history.go43- active int // the sheet last shown, saved in the file44- decimal bool // decimal arithmetic, see decimal.go42+ remote RemoteSource // answers JEV functions, see remote.go43+ hist history // undo and redo, see history.go44+ active int // the sheet last shown, saved in the file45+ decimal bool // decimal arithmetic, see decimal.go4546 4647 // structural is set when sheets were added, deleted or renamed during4748 // the open change, which then recalculates everything: references by@@ -71,6 +72,7 @@ func emptyBook() *Workbook {
7172 nameUsers: map[string]map[loc]struct{}{},7273 crossUsers: map[loc]struct{}{},7374 crossKeys: map[string]int{},75+ remote: Remote,7476 }7577 }7678