-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.pyj
124 lines (97 loc) · 2.98 KB
/
events.pyj
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
# from __python__ import dict_literals, overload_getitem
# -*- coding: utf-8 -*-
# import vyom.types
from vyom import NotPassed
def merge(o1, o2):
o = {}
for key in o1:
if o1.hasOwnProperty(key):
o[key] = o1[key]
for key in o2:
if o2.hasOwnProperty(key):
o[key] = o2[key]
return o
def onlyone(*args):
found = False
for arg in args:
if found and arg:
return False
if arg:
found = True
return True
def ObservableList(ival=None):
value = ival
connections = list()
lovable = def (
val=NotPassed, replace=True, append=False, extend=False, prepend=False,
prextend=False
):
nonlocal value
if val is NotPassed:
return value
assert onlyone(replace, append, extend, prepend, prextend)
ovalue = value
if replace:
value = val
else:
value = merge(ovalue, val)
for fn in connections:
fn(value, ovalue)
def Observable(val=None):
# TODO: type is inspired by React's propType
# look at React's documentation to see all types
# and possibly how to implement the checks.
# it is optional feature, and call
# vyom.setup(check_types=True) to activate it
value = val
connections = list()
ovable = def (val=NotPassed, replace=False, delta=False, how="default"):
nonlocal value
if val is NotPassed:
return value
ovalue = value
if replace:
value = val
else:
value = merge(ovalue, val)
for fn in connections:
fn(value, ovalue, delta, how)
ovable.replace = def (data, delta=NotPassed, how="replace"):
if delta is NotPassed:
delta = data
ovable(data, replace=True, delta=data, how=how)
return ovable
ovable.update = def (data, how="update"):
ovable(data, replace=False, delta=data, how=how)
return ovable
ovable.append = def (elem):
data = ovable()
data.push(elem)
ovable.replace(data, delta=elem, how="append")
return ovable
ovable.extend = def (elems):
data = ovable()
Array.prototype.push.call(data, elems)
ovable.replace(elems, delta=elems, how="extend")
return ovable
ovable.prextend = def (elems):
data = ovable()
# Array.prototype.push.call(data, elems)
# TODO: implement this
ovable.replace(elems, delta=elems, how="prextend")
return ovable
ovable.prepend = def (elem):
data = ovable()
data.splice(0, 0, elem)
ovable.replace(data, delta=elem, how="prepend")
return ovable
ovable.connect = def (fn, bind=NotPassed, name=""):
connections.append(fn)
fn.connection_name = name
return {
"name": name,
"remove": def ():
connections.remove(fn)
}
ovable.is_observable = True
return ovable