-
Notifications
You must be signed in to change notification settings - Fork 11
/
file.go
71 lines (59 loc) · 1.57 KB
/
file.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
package clientside
import (
"qlova.org/seed"
"qlova.org/seed/client"
"qlova.org/seed/use/js"
)
//File is a (potentially zipped) file in client memory.
type File struct {
Name string
address Address
Memory Memory
}
//Variable implements Variable
func (f *File) Variable() (Address, Memory) {
if f.address == "" {
if f.Name != "" {
f.address = Address(f.Name)
} else {
f.address = NewAddress()
}
}
return f.address, f.Memory
}
//GetFile implements client.File
func (f *File) GetFile() js.Value {
address, memory := f.Variable()
return js.Call(js.Func("q.getvar"),
client.NewString(string(address)), client.NewString(string(memory)))
}
//GetValue implements client.Value
func (f *File) GetValue() js.Value {
return f.GetFile().GetValue()
}
//GetBool implements client.Value
func (f *File) GetBool() js.Bool {
return f.GetFile().GetValue().GetBool()
}
//GetDefaultValue implements Variable
func (f *File) GetDefaultValue() client.Value {
return js.Null()
}
//SetToRaw returns a script that sets the file to the given client value.
func (f *File) SetToRaw(value client.Value) client.Script {
address, memory := f.Variable()
return js.Run(js.Func("q.setvar"), client.NewString(string(address)),
client.NewString(string(memory)), value)
}
//OnChange runs the given script when the value of this string is changed.
func (f *File) OnChange(do ...client.Script) seed.Option {
return seed.NewOption(func(c seed.Seed) {
var data data
c.Load(&data)
data.hooks = append(data.hooks, hook{
variable: f,
do: client.NewScript(do...),
})
c.Save(data)
})
}