Merge pull request #45 from osui-rs/access-cell

Access cell
This commit is contained in:
Klesti Selimaj
2026-01-31 15:04:56 +01:00
committed by GitHub
7 changed files with 390 additions and 317 deletions
+1
View File
@@ -23,6 +23,7 @@ path = "src/main.rs"
cc = "1.0"
[dependencies]
access-cell = "0.1.3"
crossterm = "0.28.1"
figlet-rs = "0.1.5"
osui-macros = { path = "macros" }
+226 -226
View File
@@ -1,227 +1,227 @@
Iterx72,Nestingx72,Time µs
14,14,21945
2,0,625
13,3,1177
0,13,581
13,13,1134
12,6,1266
0,14,530
8,11,1003
12,1,1147
8,10,1060
14,12,1142
6,12,1136
10,6,1057
13,1,1182
8,13,1230
3,6,1165
11,4,17474
6,11,1050
7,5,1155
11,11,1322
12,7,1094
12,8,1045
10,11,1079
5,8,1142
2,1,1038
3,0,638
1,9,1027
0,11,522
4,13,1049
3,9,1079
11,5,1111
1,12,1037
9,7,1106
4,11,1172
2,9,1226
8,3,1124
8,7,1123
4,0,744
10,10,1146
10,2,1184
7,2,1813
10,13,1113
14,9,1925
2,14,3131
9,11,1112
11,9,1069
1,14,1137
5,13,1097
4,6,3971
14,6,1603
7,0,1663
3,12,1205
10,12,1083
0,0,1429
0,4,687
4,2,1504
3,13,1190
7,3,1402
6,14,1047
2,2,1133
4,12,1099
10,3,3732
6,10,1193
4,14,1457
5,12,1716
3,1,1130
13,12,1157
6,13,1201
6,5,1693
12,10,1045
7,10,2915
14,8,1831
3,2,1142
9,9,7308
7,7,6848
12,12,1053
8,14,1144
10,5,1117
11,13,1056
0,9,524
7,4,1888
7,13,1136
0,10,522
12,4,2243
0,3,812
2,6,1721
0,1,1237
6,4,1187
1,3,559
11,0,1186
1,8,1068
2,11,1147
11,1,1006
7,12,1183
12,13,1214
2,3,1149
8,4,1135
9,2,1030
13,4,858
3,8,1163
13,0,1392
4,3,1170
8,2,2436
5,10,3718
13,5,3525
5,0,1120
5,11,4088
10,8,1205
4,10,1032
10,4,1072
6,7,1394
3,5,3701
12,11,1012
7,6,1110
11,10,2031
14,4,1088
13,2,2757
7,9,1647
11,2,1225
9,0,1117
14,0,1063
4,1,1204
12,2,1098
0,7,599
3,3,1366
14,5,1161
10,14,1130
4,4,1057
9,3,1085
6,1,1092
11,7,1168
13,9,1214
14,3,889
2,8,1329
9,6,1104
14,7,2015
9,10,1102
2,13,1177
4,5,2013
2,5,1099
1,7,1083
1,1,812
1,13,892
7,11,1310
6,6,5371
8,8,1164
3,7,2720
14,11,1263
14,14,1111
5,9,1105
1,5,982
10,1,3444
12,5,1016
2,10,1120
9,1,1865
4,7,4106
1,0,648
3,4,1566
5,3,1112
11,8,951
2,4,1134
3,10,1430
6,0,727
7,14,1216
13,14,1940
9,12,1322
6,2,1115
9,4,1129
4,9,4591
2,7,1120
3,14,2022
6,3,1107
14,13,1028
6,8,6169
7,8,1256
12,3,1042
8,1,1500
6,9,1069
12,14,1044
13,7,21445
10,7,1166
0,2,808
12,9,1067
8,12,1182
13,10,1615
1,6,1091
9,8,1183
8,6,1090
10,9,1130
5,2,1382
12,0,1302
1,11,984
10,0,1070
1,4,1162
7,1,1454
8,9,1125
4,8,3043
14,1,1119
5,4,1077
5,14,1345
11,14,1040
0,6,604
9,13,2238
13,8,1379
11,6,1041
11,3,5384
3,11,1130
9,14,859
0,12,525
14,2,1527
8,0,1137
14,10,6363
0,5,686
11,12,1118
5,6,3832
5,7,1065
5,1,1082
1,10,990
0,8,609
1,2,1036
9,5,1046
13,6,3712
13,11,1184
2,12,1164
5,5,3398
8,5,1119
14,14,14236
11,8,10608
8,13,7793
13,7,12073
11,6,10495
5,2,5135
6,12,6144
6,3,6341
12,7,11304
11,5,10622
0,9,497
3,2,3303
5,1,5186
10,8,9781
1,11,1358
2,9,2341
10,11,9788
14,5,13036
9,1,8807
4,12,4249
8,1,8306
14,4,12985
5,0,419
4,13,4234
10,1,9793
3,13,3255
0,4,625
7,6,7066
8,12,7954
10,12,9876
8,11,8015
6,8,6104
0,11,514
2,10,2373
8,2,7891
2,14,2337
2,13,2337
7,12,7006
3,10,3300
11,10,10772
4,5,4299
10,14,9909
12,5,11532
9,10,8809
12,12,11954
14,11,13452
8,8,7965
4,0,449
5,6,5127
7,8,6921
4,4,4244
1,10,1370
5,9,5122
1,7,1369
0,3,844
8,3,7937
9,9,8777
13,6,12476
9,3,8961
2,5,2368
1,12,1363
5,12,5075
13,11,12363
6,13,5963
0,8,484
2,12,2370
8,5,7946
14,9,13504
12,1,11839
12,2,11601
2,6,2329
10,10,9602
7,3,6970
14,6,13394
0,7,628
6,9,6100
12,13,11391
0,12,458
1,14,1370
12,10,11719
9,8,8807
4,7,4235
1,0,462
3,11,3538
11,7,10786
11,13,10710
11,2,10394
3,9,3315
4,6,4174
13,8,12538
9,11,8710
12,14,11814
13,2,12647
8,7,8019
3,1,3287
4,8,4591
4,3,4197
6,10,5985
5,7,5171
12,9,11579
10,4,9714
12,4,11811
2,3,2391
8,9,7997
9,7,8853
11,1,10700
11,11,10798
13,4,12644
6,4,6290
6,2,5916
2,0,427
2,8,2355
10,2,9637
6,5,6069
6,11,6048
12,11,11921
9,4,8754
5,4,5111
13,10,12518
8,14,7837
5,8,5127
8,10,8085
0,2,1028
2,1,2400
0,13,408
6,14,6027
12,8,11785
2,2,2326
1,2,1394
4,1,4251
1,1,1446
3,0,433
10,13,9693
9,0,432
1,3,1441
9,13,8858
9,14,8795
11,9,10632
12,0,417
7,1,7013
6,1,6097
10,7,10114
10,9,9876
10,5,9802
2,4,2363
11,12,10781
11,14,10677
3,7,3262
6,7,6201
4,9,4380
3,8,3345
13,9,12485
0,14,419
1,6,1347
4,2,4216
4,14,4209
7,9,6976
13,0,458
0,1,361
5,14,5204
14,2,13169
5,10,5096
1,5,1370
8,0,431
6,6,6031
8,4,7809
9,12,11983
3,4,3276
12,6,11921
6,0,485
1,13,1375
4,11,4237
0,10,481
13,13,12545
14,12,13253
3,5,3210
5,5,5158
7,7,6992
5,11,5076
7,13,7056
11,3,10584
3,3,3302
14,13,13280
13,3,12771
14,0,429
7,5,7065
7,14,6952
8,6,7835
2,7,2334
2,11,2367
11,4,10606
7,11,6845
14,8,13736
0,5,844
1,9,1333
10,6,9721
9,6,8769
7,10,6998
10,3,9810
14,7,13311
14,14,13481
11,0,426
5,3,5102
1,4,1457
13,14,12431
12,3,11717
13,5,12523
14,10,13457
0,6,705
3,14,3335
13,12,12714
7,4,6951
3,6,3247
9,5,8713
3,12,3401
14,1,13199
4,10,4260
10,0,428
5,13,5103
0,0,421
14,3,13070
9,2,8816
7,0,451
7,2,6974
1,8,1363
13,1,12699
1 Iterx72 Nestingx72 Time µs
2 14 14 21945 14236
3 2 11 0 8 625 10608
4 13 8 3 13 1177 7793
5 0 13 13 7 581 12073
6 13 11 13 6 1134 10495
7 12 5 6 2 1266 5135
8 0 6 14 12 530 6144
9 8 6 11 3 1003 6341
10 12 1 7 1147 11304
11 8 11 10 5 1060 10622
12 14 0 12 9 1142 497
13 6 3 12 2 1136 3303
14 10 5 6 1 1057 5186
15 13 10 1 8 1182 9781
16 8 1 13 11 1230 1358
17 3 2 6 9 1165 2341
18 11 10 4 11 17474 9788
19 6 14 11 5 1050 13036
20 7 9 5 1 1155 8807
21 11 4 11 12 1322 4249
22 12 8 7 1 1094 8306
23 12 14 8 4 1045 12985
24 10 5 11 0 1079 419
25 5 4 8 13 1142 4234
26 2 10 1 1038 9793
27 3 0 13 638 3255
28 1 0 9 4 1027 625
29 0 7 11 6 522 7066
30 4 8 13 12 1049 7954
31 3 10 9 12 1079 9876
32 11 8 5 11 1111 8015
33 1 6 12 8 1037 6104
34 9 0 7 11 1106 514
35 4 2 11 10 1172 2373
36 2 8 9 2 1226 7891
37 8 2 3 14 1124 2337
38 8 2 7 13 1123 2337
39 4 7 0 12 744 7006
40 10 3 10 1146 3300
41 10 11 2 10 1184 10772
42 7 4 2 5 1813 4299
43 10 13 14 1113 9909
44 14 12 9 5 1925 11532
45 2 9 14 10 3131 8809
46 9 12 11 12 1112 11954
47 11 14 9 11 1069 13452
48 1 8 14 8 1137 7965
49 5 4 13 0 1097 449
50 4 5 6 3971 5127
51 14 7 6 8 1603 6921
52 7 4 0 4 1663 4244
53 3 1 12 10 1205 1370
54 10 5 12 9 1083 5122
55 0 1 0 7 1429 1369
56 0 4 3 687 844
57 4 8 2 3 1504 7937
58 3 9 13 9 1190 8777
59 7 13 3 6 1402 12476
60 6 9 14 3 1047 8961
61 2 2 5 1133 2368
62 4 1 12 1099 1363
63 10 5 3 12 3732 5075
64 6 13 10 11 1193 12363
65 4 6 14 13 1457 5963
66 5 0 12 8 1716 484
67 3 2 1 12 1130 2370
68 13 8 12 5 1157 7946
69 6 14 13 9 1201 13504
70 6 12 5 1 1693 11839
71 12 10 2 1045 11601
72 7 2 10 6 2915 2329
73 14 10 8 10 1831 9602
74 3 7 2 3 1142 6970
75 9 14 9 6 7308 13394
76 7 0 7 6848 628
77 12 6 12 9 1053 6100
78 8 12 14 13 1144 11391
79 10 0 5 12 1117 458
80 11 1 13 14 1056 1370
81 0 12 9 10 524 11719
82 7 9 4 8 1888 8807
83 7 4 13 7 1136 4235
84 0 1 10 0 522 462
85 12 3 4 11 2243 3538
86 0 11 3 7 812 10786
87 2 11 6 13 1721 10710
88 0 11 1 2 1237 10394
89 6 3 4 9 1187 3315
90 1 4 3 6 559 4174
91 11 13 0 8 1186 12538
92 1 9 8 11 1068 8710
93 2 12 11 14 1147 11814
94 11 13 1 2 1006 12647
95 7 8 12 7 1183 8019
96 12 3 13 1 1214 3287
97 2 4 3 8 1149 4591
98 8 4 4 3 1135 4197
99 9 6 2 10 1030 5985
100 13 5 4 7 858 5171
101 3 12 8 9 1163 11579
102 13 10 0 4 1392 9714
103 4 12 3 4 1170 11811
104 8 2 2 3 2436 2391
105 5 8 10 9 3718 7997
106 13 9 5 7 3525 8853
107 5 11 0 1 1120 10700
108 5 11 11 4088 10798
109 10 13 8 4 1205 12644
110 4 6 10 4 1032 6290
111 10 6 4 2 1072 5916
112 6 2 7 0 1394 427
113 3 2 5 8 3701 2355
114 12 10 11 2 1012 9637
115 7 6 6 5 1110 6069
116 11 6 10 11 2031 6048
117 14 12 4 11 1088 11921
118 13 9 2 4 2757 8754
119 7 5 9 4 1647 5111
120 11 13 2 10 1225 12518
121 9 8 0 14 1117 7837
122 14 5 0 8 1063 5127
123 4 8 1 10 1204 8085
124 12 0 2 1098 1028
125 0 2 7 1 599 2400
126 3 0 3 13 1366 408
127 14 6 5 14 1161 6027
128 10 12 14 8 1130 11785
129 4 2 4 2 1057 2326
130 9 1 3 2 1085 1394
131 6 4 1 1092 4251
132 11 1 7 1 1168 1446
133 13 3 9 0 1214 433
134 14 10 3 13 889 9693
135 2 9 8 0 1329 432
136 9 1 6 3 1104 1441
137 14 9 7 13 2015 8858
138 9 10 14 1102 8795
139 2 11 13 9 1177 10632
140 4 12 5 0 2013 417
141 2 7 5 1 1099 7013
142 1 6 7 1 1083 6097
143 1 10 1 7 812 10114
144 1 10 13 9 892 9876
145 7 10 11 5 1310 9802
146 6 2 6 4 5371 2363
147 8 11 8 12 1164 10781
148 3 11 7 14 2720 10677
149 14 3 11 7 1263 3262
150 14 6 14 7 1111 6201
151 5 4 9 1105 4380
152 1 3 5 8 982 3345
153 10 13 1 9 3444 12485
154 12 0 5 14 1016 419
155 2 1 10 6 1120 1347
156 9 4 1 2 1865 4216
157 4 7 14 4106 4209
158 1 7 0 9 648 6976
159 3 13 4 0 1566 458
160 5 0 3 1 1112 361
161 11 5 8 14 951 5204
162 2 14 4 2 1134 13169
163 3 5 10 1430 5096
164 6 1 0 5 727 1370
165 7 8 14 0 1216 431
166 13 6 14 6 1940 6031
167 9 8 12 4 1322 7809
168 6 9 2 12 1115 11983
169 9 3 4 1129 3276
170 4 12 9 6 4591 11921
171 2 6 7 0 1120 485
172 3 1 14 13 2022 1375
173 6 4 3 11 1107 4237
174 14 0 13 10 1028 481
175 6 13 8 13 6169 12545
176 7 14 8 12 1256 13253
177 12 3 3 5 1042 3210
178 8 5 1 5 1500 5158
179 6 7 9 7 1069 6992
180 12 5 14 11 1044 5076
181 13 7 7 13 21445 7056
182 10 11 7 3 1166 10584
183 0 3 2 3 808 3302
184 12 14 9 13 1067 13280
185 8 13 12 3 1182 12771
186 13 14 10 0 1615 429
187 1 7 6 5 1091 7065
188 9 7 8 14 1183 6952
189 8 6 1090 7835
190 10 2 9 7 1130 2334
191 5 2 2 11 1382 2367
192 12 11 0 4 1302 10606
193 1 7 11 984 6845
194 10 14 0 8 1070 13736
195 1 0 4 5 1162 844
196 7 1 1 9 1454 1333
197 8 10 9 6 1125 9721
198 4 9 8 6 3043 8769
199 14 7 1 10 1119 6998
200 5 10 4 3 1077 9810
201 5 14 14 7 1345 13311
202 11 14 14 1040 13481
203 0 11 6 0 604 426
204 9 5 13 3 2238 5102
205 13 1 8 4 1379 1457
206 11 13 6 14 1041 12431
207 11 12 3 5384 11717
208 3 13 11 5 1130 12523
209 9 14 14 10 859 13457
210 0 12 6 525 705
211 14 3 2 14 1527 3335
212 8 13 0 12 1137 12714
213 14 7 10 4 6363 6951
214 0 3 5 6 686 3247
215 11 9 12 5 1118 8713
216 5 3 6 12 3832 3401
217 5 14 7 1 1065 13199
218 5 4 1 10 1082 4260
219 1 10 10 0 990 428
220 0 5 8 13 609 5103
221 1 0 2 0 1036 421
222 9 14 5 3 1046 13070
223 13 9 6 2 3712 8816
224 13 7 11 0 1184 451
225 2 7 12 2 1164 6974
226 5 1 5 8 3398 1363
227 8 13 5 1 1119 12699
+9 -7
View File
@@ -76,13 +76,15 @@ fn emit_node_scope(node: &RsxNode) -> TokenStream {
{
#deps_emit
r.dynamic_scope(move |scope| {
if #cond {
if scope.children.lock().unwrap().is_empty() {
#(#kids)*
scope.access_children(|children| {
if #cond {
if children.is_empty() {
#(#kids)*
}
} else {
.children.clear();
}
} else {
scope.children.lock().unwrap().clear();
}
})
}, #deps_vec_emit);
}
}
@@ -103,7 +105,7 @@ fn emit_node_scope(node: &RsxNode) -> TokenStream {
#deps_emit
#[allow(unused_parens)]
r.dynamic_scope(move |scope| {
scope.children.lock().unwrap().clear();
scope.access_children(|children| children.clear());
for #pat in #expr {
#(#kids)*
}
+127 -73
View File
@@ -4,6 +4,8 @@ use std::{
sync::{Arc, Mutex},
};
use access_cell::AccessCell;
use crate::{
component::EventHandler,
engine::{Command, CommandExecutor},
@@ -15,10 +17,10 @@ use crate::{
use super::{scope::Scope, Component, ComponentImpl};
pub struct Context {
component: Mutex<Component>,
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
view: Mutex<View>,
pub(crate) scopes: Mutex<Vec<Arc<Scope>>>,
component: AccessCell<Component>,
view: AccessCell<View>,
event_handlers: AccessCell<HashMap<TypeId, Vec<EventHandler>>>,
scopes: AccessCell<Vec<Arc<Scope>>>,
executor: Arc<dyn CommandExecutor>,
}
@@ -28,104 +30,135 @@ impl Context {
executor: Arc<dyn CommandExecutor>,
) -> Arc<Self> {
Arc::new(Self {
component: Mutex::new(Arc::new(component)),
event_handlers: Mutex::new(HashMap::new()),
view: Mutex::new(Arc::new(|_| {})),
scopes: Mutex::new(Vec::new()),
component: AccessCell::new(Arc::new(component)),
view: AccessCell::new(Arc::new(|_| {})),
event_handlers: AccessCell::new(HashMap::new()),
scopes: AccessCell::new(Vec::new()),
executor,
})
}
pub fn refresh(self: &Arc<Self>) {
let s = self.clone();
std::thread::spawn({
move || {
s.event_handlers.lock().unwrap().clear();
let c = s.component.lock().unwrap().clone();
*s.view.lock().unwrap() = c.call(&s);
self.event_handlers
.access(|event_handlers| event_handlers.clear());
self.component.access({
let s = self.clone();
move |component| {
let component = component.clone();
s.view.access({
let s = s.clone();
move |view| *view = component.call(&s)
})
}
});
}
pub fn refresh_atomic(self: &Arc<Self>) -> Arc<Mutex<bool>> {
let s = self.clone();
let done = Arc::new(Mutex::new(false));
pub fn refresh_sync(self: &Arc<Self>) {
let (tx, rx) = std::sync::mpsc::channel::<()>();
std::thread::spawn({
let done = done.clone();
move || {
s.event_handlers.lock().unwrap().clear();
let c = s.component.lock().unwrap().clone();
*s.view.lock().unwrap() = c.call(&s);
*done.lock().unwrap() = true;
self.event_handlers
.access(|event_handlers| event_handlers.clear());
self.component.access({
let s = self.clone();
move |component| {
let component = component.clone();
s.view.access({
let s = s.clone();
let tx = tx.clone();
move |view| {
*view = component.call(&s);
let _ = tx.send(()); // signal completion
}
});
}
});
done
// BLOCK until view closure finishes
let _ = rx.recv();
}
pub fn get_view(self: &Arc<Self>) -> View {
self.view.lock().unwrap().clone()
self.view.access_ref().clone()
}
pub fn on_event<T: Any + 'static, F: Fn(&Arc<Self>, &T) + Send + Sync + 'static>(
self: &Arc<Self>,
handler: F,
) {
self.event_handlers
.lock()
.unwrap()
.entry(TypeId::of::<T>())
.or_insert_with(|| Vec::new())
.push(Arc::new(Mutex::new(
move |ctx: &Arc<Self>, event: &dyn Any| {
if let Some(e) = event.downcast_ref::<T>() {
(handler)(ctx, e);
}
},
)));
let new_handler: EventHandler =
Arc::new(Mutex::new(move |ctx: &Arc<Context>, event: &dyn Any| {
if let Some(e) = event.downcast_ref::<T>() {
(handler)(ctx, e);
}
}));
self.event_handlers.access(|event_handlers| {
event_handlers
.entry(TypeId::of::<T>())
.or_insert_with(Vec::new)
.push(new_handler);
});
}
pub fn emit_event<E: Any + 'static>(self: &Arc<Self>, event: &E) {
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
for i in v {
(i.lock().unwrap())(self, event);
}
pub fn emit_event<E: Send + Sync + Any + 'static>(self: &Arc<Self>, event: E) {
let event = Arc::new(event);
let handlers_to_call: Vec<EventHandler> = {
let guard = self.event_handlers.access_ref();
guard.get(&TypeId::of::<E>()).cloned().unwrap_or_default()
};
for h in &handlers_to_call {
(h.lock().unwrap())(self, event.as_ref());
}
for scope in self.scopes.lock().unwrap().iter() {
for (child, _) in scope.children.lock().unwrap().iter() {
child.emit_event(event);
self.scopes.access(move |scopes| {
for scope in scopes {
let event = event.clone();
scope.access_children(move |children| {
for (child, _) in children {
child.emit_event(event.clone());
}
});
}
}
});
}
pub fn emit_event_threaded<E: Any + Send + Sync + Clone + 'static>(
self: &Arc<Self>,
event: &E,
) {
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
for i in v {
let i = i.clone();
let event = event.clone();
let s = self.clone();
std::thread::spawn(move || {
(i.lock().unwrap())(&s, &event);
});
}
let handlers_to_call: Vec<EventHandler> = {
let guard = self.event_handlers.access_ref();
guard.get(&TypeId::of::<E>()).cloned().unwrap_or_default()
};
for h in handlers_to_call {
let event = event.clone();
let s = self.clone();
std::thread::spawn(move || {
(h.lock().unwrap())(&s, &event);
});
}
for scope in self.scopes.lock().unwrap().iter() {
for (child, _) in scope.children.lock().unwrap().iter() {
child.emit_event_threaded(event);
let event = event.clone();
self.scopes.access(move |scopes| {
for scope in scopes {
let event = event.clone();
scope.access_children(move |children| {
for (child, _) in children {
child.emit_event_threaded(&event);
}
});
}
}
});
}
pub fn scope(self: &Arc<Self>) -> Arc<Scope> {
let scope = Scope::new(self.executor.clone());
self.scopes.lock().unwrap().push(scope.clone());
self.scopes.access({
let scope = scope.clone();
move |scopes| scopes.push(scope)
});
scope
}
@@ -136,7 +169,11 @@ impl Context {
dependencies: &[&dyn HookDependency],
) -> Arc<Scope> {
let scope = Scope::new(self.executor.clone());
self.scopes.lock().unwrap().push(scope.clone());
self.scopes.access({
let scope = scope.clone();
move |scopes| scopes.push(scope)
});
drawer(&scope);
@@ -153,18 +190,35 @@ impl Context {
scope
}
pub fn draw_children(self: &Arc<Self>, ctx: &mut DrawContext) {
for scope in self.scopes.lock().unwrap().iter() {
for (child, view_wrapper) in scope.children.lock().unwrap().iter() {
let view = child.get_view();
pub fn add_scope(self: &Arc<Self>, scope: Arc<Scope>) {
self.scopes.access(|scopes| scopes.push(scope));
}
if let Some(view_wrapper) = view_wrapper {
view_wrapper(ctx, view)
} else {
ctx.draw_view(ctx.area.clone(), view);
}
pub fn draw_children(self: &Arc<Self>, ctx: &mut DrawContext) {
let c = ctx.clone();
let (tx, rx) = std::sync::mpsc::channel::<DrawContext>();
self.scopes.access(move |scopes| {
for scope in scopes {
let mut c = c.clone();
let tx = tx.clone();
scope.access_children(move |children| {
for (child, view_wrapper) in children {
let view = child.get_view();
if let Some(view_wrapper) = view_wrapper {
view_wrapper(&mut c, view)
} else {
c.draw_view(c.area.clone(), view);
}
}
tx.send(c.clone()).expect("Failed transmitting DrawContext");
});
}
}
});
*ctx = rx.recv().expect("Failed receiving DrawContext");
}
pub fn get_executor(self: &Arc<Self>) -> Arc<dyn CommandExecutor> {
+23 -8
View File
@@ -1,18 +1,19 @@
use std::sync::{Arc, Mutex};
use crate::{engine::CommandExecutor, View, ViewWrapper};
use super::{context::Context, ComponentImpl};
use crate::{engine::CommandExecutor, View, ViewWrapper};
use access_cell::AccessCell;
use std::sync::Arc;
pub type ScopeChildren = Vec<(Arc<Context>, Option<ViewWrapper>)>;
pub struct Scope {
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
children: AccessCell<ScopeChildren>,
executor: Arc<dyn CommandExecutor>,
}
impl Scope {
pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> {
Arc::new(Self {
children: Mutex::new(Vec::new()),
children: AccessCell::new(Vec::new()),
executor,
})
}
@@ -26,7 +27,8 @@ impl Scope {
ctx.refresh();
self.children.lock().unwrap().push((ctx, view_wrapper));
self.children
.access(|children| children.push((ctx, view_wrapper)));
}
pub fn view(self: &Arc<Self>, view: View) {
@@ -34,6 +36,19 @@ impl Scope {
ctx.refresh();
self.children.lock().unwrap().push((ctx, None));
self.children.access(|children| children.push((ctx, None)));
}
pub fn view_wrapper(self: &Arc<Self>, view: View, view_wrapper: Option<ViewWrapper>) {
let ctx = Context::new(view, self.executor.clone());
ctx.refresh();
self.children
.access(|children| children.push((ctx, view_wrapper)));
}
pub fn access_children(self: &Arc<Self>, f: impl FnOnce(&mut ScopeChildren) + Send + 'static) {
self.children.access(f);
}
}
+1 -1
View File
@@ -50,7 +50,7 @@ impl Rsx {
RsxScope::Static(scope_fn) => {
let scope = Scope::new(executor.clone());
(scope_fn)(&scope);
context.scopes.lock().unwrap().push(scope)
context.add_scope(scope)
}
RsxScope::Dynamic(drawer, dependencies) => {
let drawer = drawer.clone();
+3 -2
View File
@@ -192,7 +192,7 @@ pub fn use_sync_state<
pub fn use_sync_effect<
T: Send + Sync + 'static,
Ev: 'static,
Ev: Send + Sync + 'static,
E: Fn(&State<T>) -> Ev + Send + Sync + 'static,
>(
cx: &Arc<Context>,
@@ -205,7 +205,8 @@ pub fn use_sync_effect<
let state = state.clone();
let cx = cx.clone();
move || {
cx.emit_event(&encoder(&state));
let ev = encoder(&state);
cx.emit_event(ev);
}
},
deps,