| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- //go:build js && wasm
- package ultraviolet
- import (
- "context"
- "fmt"
- "sync"
- fw "git.buran.team/main/fairwind"
- )
- type UVContext struct {
- ctx context.Context
- log *fw.Log
- mutex sync.Mutex
- factory Factory
- state *State
- source *Source
- values []Value
- router *Router
- }
- func NewUVContext(ctx context.Context, log *fw.Log, platform int, routing Routing) (*UVContext, error) {
- this := &UVContext{
- ctx: ctx,
- factory: NewFactory(platform),
- state: NewStateWithOpts(ctx),
- values: []Value{},
- }
- router, err := NewRouter(ctx, log, this, routing)
- if err != nil {
- return nil, fmt.Errorf("can't create uv-context: %w", err)
- }
- this.router = router
- this.source = NewSource(ctx, this.state)
- return this, nil
- }
- func (this *UVContext) Start() error {
- err := this.state.Start()
- if err != nil {
- return fmt.Errorf("can't start context: %w", err)
- }
- err = this.source.Start()
- if err != nil {
- return fmt.Errorf("can't start context: %w", err)
- }
- return nil
- }
- func (this *UVContext) Stop() error {
- err := this.state.Stop()
- if err != nil {
- return fmt.Errorf("can't stop context: %w", err)
- }
- err = this.source.Stop()
- if err != nil {
- return fmt.Errorf("can't stop context: %w", err)
- }
- for _, value := range this.values {
- err = value.Stop()
- if err != nil {
- return fmt.Errorf("can't stop context: %w", err)
- }
- }
- return nil
- }
- func (this *UVContext) Update() {
- this.router.Update()
- }
- func (this *UVContext) Factory() Factory {
- return this.factory
- }
- func (this *UVContext) Router() *Router {
- return this.router
- }
- func (this *UVContext) State() *State {
- return this.state
- }
- func (this *UVContext) Value(expression string) (Value, error) {
- this.mutex.Lock()
- defer this.mutex.Unlock()
- result := NewValueReactive(this.ctx, this.source, ExpressionToSelector(expression))
- err := result.Start()
- if err != nil {
- return nil, fmt.Errorf("can't create value: %w", err)
- }
- this.values = append(this.values, result)
- return result, nil
- }
|