|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | +) |
| 7 | + |
| 8 | +var _ RuntimeValue = &RxFuture{} |
| 9 | + |
| 10 | +type internalPromise struct { |
| 11 | + lock *sync.RWMutex |
| 12 | + result *promiseResult |
| 13 | + channel chan promiseResult |
| 14 | +} |
| 15 | + |
| 16 | +type promiseResult struct { |
| 17 | + value *RuntimeValue |
| 18 | + err *RuntimeError |
| 19 | +} |
| 20 | + |
| 21 | +type RxFuture struct { |
| 22 | + futureType *RxFutureType |
| 23 | + storage *internalPromise |
| 24 | +} |
| 25 | + |
| 26 | +func MakeRxFuture(futureType *RxFutureType, configure CallableRuntimeValue) RxFuture { |
| 27 | + future := RxFuture{ |
| 28 | + futureType: futureType, |
| 29 | + storage: &internalPromise{ |
| 30 | + lock: &sync.RWMutex{}, |
| 31 | + result: nil, |
| 32 | + channel: make(chan promiseResult, 1), |
| 33 | + }, |
| 34 | + } |
| 35 | + go func() { |
| 36 | + receive := MakeAnonymousFunction("receive", []string{"event"}, func(args []Evaluatable) (RuntimeValue, *RuntimeError) { |
| 37 | + value, err := args[0].Evaluate() |
| 38 | + if err != nil { |
| 39 | + return nil, err.CascadeDecl(futureType.DeclExternType) |
| 40 | + } |
| 41 | + resultTypeRef := MakeRuntimeTypeRef("Result", "results") |
| 42 | + isResult, err := resultTypeRef.HasInstance(value) |
| 43 | + if err != nil { |
| 44 | + return nil, err.CascadeDecl(futureType.DeclExternType) |
| 45 | + } else if !isResult { |
| 46 | + return nil, NewRuntimeErrorf("future %s received non-result %s", fmt.Sprint(future), fmt.Sprint(value)).CascadeDecl(futureType.DeclExternType) |
| 47 | + } |
| 48 | + future.accept(value) |
| 49 | + return future, nil |
| 50 | + }) |
| 51 | + _, err := configure.Call([]Evaluatable{ |
| 52 | + NewConstantRuntimeValue(receive), |
| 53 | + }, nil) |
| 54 | + if err != nil { |
| 55 | + future.fail(*err) |
| 56 | + } |
| 57 | + }() |
| 58 | + return future |
| 59 | +} |
| 60 | + |
| 61 | +func (RxFuture) RuntimeType() RuntimeTypeRef { |
| 62 | + return RxVariableTypeRef |
| 63 | +} |
| 64 | + |
| 65 | +func (v RxFuture) String() string { |
| 66 | + v.storage.lock.RLock() |
| 67 | + defer v.storage.lock.RUnlock() |
| 68 | + if v.storage.result.err != nil { |
| 69 | + return fmt.Sprintf("(%s %s)", v.RuntimeType().Name, *v.storage.result.err) |
| 70 | + } else if v.storage.result.value != nil { |
| 71 | + return fmt.Sprintf("(%s %s)", v.RuntimeType().Name, *v.storage.result.value) |
| 72 | + } else { |
| 73 | + return "Future" |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (v RxFuture) Lookup(member string) (Evaluatable, *RuntimeError) { |
| 78 | + switch member { |
| 79 | + case "await": |
| 80 | + return NewLazyRuntimeValue(func() (RuntimeValue, *RuntimeError) { |
| 81 | + return v.Await() |
| 82 | + }), nil |
| 83 | + default: |
| 84 | + return nil, NewRuntimeErrorf("future %s has no member %s", fmt.Sprint(v), member) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (v RxFuture) Await() (RuntimeValue, *RuntimeError) { |
| 89 | + v.storage.lock.RLock() |
| 90 | + |
| 91 | + if v.storage.result != nil { |
| 92 | + defer v.storage.lock.RUnlock() |
| 93 | + if v.storage.result.err != nil { |
| 94 | + return nil, v.storage.result.err |
| 95 | + } else { |
| 96 | + return *v.storage.result.value, nil |
| 97 | + } |
| 98 | + } else { |
| 99 | + v.storage.lock.RUnlock() |
| 100 | + result := <-v.storage.channel |
| 101 | + v.storage.lock.Lock() |
| 102 | + defer v.storage.lock.Unlock() |
| 103 | + |
| 104 | + v.storage.result = &result |
| 105 | + if result.err != nil { |
| 106 | + return nil, result.err |
| 107 | + } else { |
| 108 | + return *result.value, nil |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func (v RxFuture) accept(value RuntimeValue) (RuntimeValue, *RuntimeError) { |
| 114 | + v.storage.lock.RLock() |
| 115 | + if v.storage.result != nil { |
| 116 | + defer v.storage.lock.RUnlock() |
| 117 | + if v.storage.result.err != nil { |
| 118 | + return nil, v.storage.result.err |
| 119 | + } else { |
| 120 | + return *v.storage.result.value, nil |
| 121 | + } |
| 122 | + } else { |
| 123 | + v.storage.lock.RUnlock() |
| 124 | + v.storage.lock.Lock() |
| 125 | + |
| 126 | + v.storage.result = &promiseResult{value: &value} |
| 127 | + v.storage.lock.Unlock() |
| 128 | + v.storage.channel <- promiseResult{value: &value} |
| 129 | + close(v.storage.channel) |
| 130 | + return value, nil |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func (v RxFuture) fail(err RuntimeError) (RuntimeValue, *RuntimeError) { |
| 135 | + v.storage.lock.Lock() |
| 136 | + defer v.storage.lock.Unlock() |
| 137 | + if v.storage.result != nil { |
| 138 | + if v.storage.result.err != nil { |
| 139 | + return nil, v.storage.result.err |
| 140 | + } else { |
| 141 | + return *v.storage.result.value, nil |
| 142 | + } |
| 143 | + } else { |
| 144 | + v.storage.result = &promiseResult{err: &err} |
| 145 | + v.storage.lock.Unlock() |
| 146 | + |
| 147 | + v.storage.channel <- promiseResult{err: &err} |
| 148 | + close(v.storage.channel) |
| 149 | + return nil, &err |
| 150 | + } |
| 151 | +} |
0 commit comments