-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProxy.py
130 lines (115 loc) · 4.77 KB
/
Proxy.py
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
from pythonfrp import Globals
from pythonfrp.Factory import *
from pythonfrp.Types import proxyType, anyType
from pythonfrp import Errors
class Reaction:
def __init__(self, fn, m, v):
self.fn = fn
self.m = m
self.v = v
def react(self):
self.fn(self.m, self.v)
class Proxy:
def __init__(self, name, updater, types = {}, duration = 0):
self._types = types
self._alive = True;
self._type = proxyType
self._signals = {}
self._1Reactions = []
self._gReactions = []
self._updateSignals = {}
self._name = name
self._updater = updater
Globals.newObjects.append(self)
def __setattr__(self, name, value):
if name[0] == '_':
self.__dict__[name] = value
else:
#if value.type == SignalType:
self._updateSignals[name] = value
#else:
# print("Error: Tried to set attribute to non-signal.")
def __getattr__(self, name):
if name[0] == '_':
return self.__dict__[name]
else:
return ObserverF(lambda x: self._get(name))
#return self._signals[name]
def _get(self, name):
if name in self._signals:
return self._signals[name].now()
print( str(name) + " does not exist or has not been started in this Proxy: " + repr(self))
def _initialize(self):
for k, v in self._updateSignals.items():
#print(k, v)
# print("Object: " + self._name + " is initializing field: " + k + " to " + str(v))
if k in self._types:
ty = self._types[k]
else:
ty = anyType
v = maybeLift(v)
Globals.error = "On Line 51 of Proxy, In object " + k + ", attribute " + str(v)
#print(str(self._signals))
#print(str(v))
sig = v.start(expectedType = ty, obj = self)[0] # This is screwing up Integral
#print("initilize signal = " + repr(sig))
self._signals[k] = cache(sig)
#print("----- Initialized " + str(self._name) + " with :" + repr(self._signals))
self._updateSignals = {}
def _react(self, when, what):
if self._alive:
when = maybeLift(when)
Globals.error = "On Line 59 of Proxy, In object " + self._name + ", initializing reaction " + when.name
self._gReactions.append((when.start()[0], what))
def _react1(self, when, what):
if self._alive:
when = maybeLift(when)
Globals.error = "On Line 59 of Proxy, In object " + self._name + ", initializing one time reaction " + when.name
self._1Reactions.append((when.start()[0], what))
def _update(self):
if self._alive:
# print("Object: " + str(self) + " signals: " + str(self._signals))
for k, v in self._signals.items():
# print("Objects: " + str(self) + " is updating: " + k)
v.now()
thunks = []
#Evaluate one time reactions:
for c in self._1Reactions:
# print("Object: " + str(self) + " is updating: " + str(c[0]))
temp = c[0].now()
Errors.checkEvent(temp, "One time reaction in " + self._name)
if temp.occurs():
#print(" " + str(temp) + " is being added to thunks")
thunks.append(Reaction(c[1], self, temp.value))
self._1Reactions.remove(c)
break
if (len(thunks) >= 2):
print("Multiple one time reactions in a heartbeat in object " + self._name)
#Evaluate recurring reactions
# print("Number of reactions in " + self._name + " " + str(len(self._gReactions)))
for d in self._gReactions:
temp = d[0].now()
Errors.checkEvent(temp, "recurring reaction in " + self._name)
# print("Object: " + str(self) + " is updating: " + str(d[0]))
if temp.occurs():
#print(" " + repr(temp) + " is being added to thunks")
#print("Thunks" + str(thunks) + " " + str(d))
thunks.append(Reaction(d[1], self, temp.value))
#push to the actuall object
self._updater(self)
return thunks
def __str__(self):
#print(self._signals)
#print(self._updateSignals)
return self._name
def __repr__(self):
return str(self._name)
def _remove(self):
pass
def _exit(self):
Globals.worldObjects = [x for x in Globals.worldObjects if x is not self]
self._remove() # This is in the subclass
self._alive = False
def clearReactions(m):
m._1Reactions = []
m._gReactions = []