forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.rs
690 lines (589 loc) · 20.9 KB
/
scope.rs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Component scope module
#[cfg(any(feature = "csr", feature = "ssr"))]
use crate::scheduler::Shared;
#[cfg(any(feature = "csr", feature = "ssr"))]
use std::cell::RefCell;
#[cfg(any(feature = "csr", feature = "ssr"))]
use super::lifecycle::{ComponentState, UpdateEvent, UpdateRunner};
use super::BaseComponent;
use crate::callback::Callback;
use crate::context::{ContextHandle, ContextProvider};
use std::any::{Any, TypeId};
use std::marker::PhantomData;
use std::ops::Deref;
use std::rc::Rc;
use std::{fmt, iter};
/// Untyped scope used for accessing parent scope
#[derive(Clone)]
pub struct AnyScope {
type_id: TypeId,
parent: Option<Rc<AnyScope>>,
typed_scope: Rc<dyn Any>,
}
impl fmt::Debug for AnyScope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("AnyScope<_>")
}
}
impl<COMP: BaseComponent> From<Scope<COMP>> for AnyScope {
fn from(scope: Scope<COMP>) -> Self {
AnyScope {
type_id: TypeId::of::<COMP>(),
parent: scope.parent.clone(),
typed_scope: Rc::new(scope),
}
}
}
impl AnyScope {
/// Returns the parent scope
pub fn get_parent(&self) -> Option<&AnyScope> {
self.parent.as_deref()
}
/// Returns the type of the linked component
pub fn get_type_id(&self) -> &TypeId {
&self.type_id
}
/// Attempts to downcast into a typed scope
///
/// # Panics
///
/// If the self value can't be cast into the target type.
pub fn downcast<COMP: BaseComponent>(&self) -> Scope<COMP> {
self.try_downcast::<COMP>().unwrap()
}
/// Attempts to downcast into a typed scope
///
/// Returns [`None`] if the self value can't be cast into the target type.
pub fn try_downcast<COMP: BaseComponent>(&self) -> Option<Scope<COMP>> {
self.typed_scope.downcast_ref::<Scope<COMP>>().cloned()
}
/// Attempts to find a parent scope of a certain type
///
/// Returns [`None`] if no parent scope with the specified type was found.
pub fn find_parent_scope<COMP: BaseComponent>(&self) -> Option<Scope<COMP>> {
iter::successors(Some(self), |scope| scope.get_parent())
.find_map(AnyScope::try_downcast::<COMP>)
}
/// Accesses a value provided by a parent `ContextProvider` component of the
/// same type.
pub fn context<T: Clone + PartialEq + 'static>(
&self,
callback: Callback<T>,
) -> Option<(T, ContextHandle<T>)> {
let scope = self.find_parent_scope::<ContextProvider<T>>()?;
let scope_clone = scope.clone();
let component = scope.get_component()?;
Some(component.subscribe_consumer(callback, scope_clone))
}
}
/// A context which allows sending messages to a component.
pub struct Scope<COMP: BaseComponent> {
_marker: PhantomData<COMP>,
parent: Option<Rc<AnyScope>>,
#[cfg(any(feature = "csr", feature = "ssr"))]
pub(crate) pending_messages: MsgQueue<COMP::Message>,
#[cfg(any(feature = "csr", feature = "ssr"))]
pub(crate) state: Shared<Option<ComponentState>>,
pub(crate) id: usize,
}
impl<COMP: BaseComponent> fmt::Debug for Scope<COMP> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Scope<_>")
}
}
impl<COMP: BaseComponent> Clone for Scope<COMP> {
fn clone(&self) -> Self {
Scope {
_marker: PhantomData,
#[cfg(any(feature = "csr", feature = "ssr"))]
pending_messages: self.pending_messages.clone(),
parent: self.parent.clone(),
#[cfg(any(feature = "csr", feature = "ssr"))]
state: self.state.clone(),
id: self.id,
}
}
}
impl<COMP: BaseComponent> Scope<COMP> {
/// Returns the parent scope
pub fn get_parent(&self) -> Option<&AnyScope> {
self.parent.as_deref()
}
/// Creates a `Callback` which will send a message to the linked
/// component's update method when invoked.
pub fn callback<F, IN, M>(&self, function: F) -> Callback<IN>
where
M: Into<COMP::Message>,
F: Fn(IN) -> M + 'static,
{
let scope = self.clone();
let closure = move |input| {
let output = function(input);
scope.send_message(output);
};
Callback::from(closure)
}
/// Creates a `Callback` which will send a batch of messages back
/// to the linked component's update method when invoked.
///
/// The callback function's return type is generic to allow for dealing with both
/// `Option` and `Vec` nicely. `Option` can be used when dealing with a callback that
/// might not need to send an update.
///
/// ```ignore
/// link.batch_callback(|_| vec![Msg::A, Msg::B]);
/// link.batch_callback(|_| Some(Msg::A));
/// ```
pub fn batch_callback<F, IN, OUT>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> OUT + 'static,
OUT: SendAsMessage<COMP>,
{
let scope = self.clone();
let closure = move |input| {
let messages = function(input);
messages.send(&scope);
};
closure.into()
}
/// Accesses a value provided by a parent `ContextProvider` component of the
/// same type.
pub fn context<T: Clone + PartialEq + 'static>(
&self,
callback: Callback<T>,
) -> Option<(T, ContextHandle<T>)> {
AnyScope::from(self.clone()).context(callback)
}
}
#[cfg(feature = "ssr")]
mod feat_ssr {
use super::*;
use crate::scheduler;
use futures::channel::oneshot;
use crate::html::component::lifecycle::{
ComponentRenderState, CreateRunner, DestroyRunner, RenderRunner,
};
use crate::virtual_dom::Collectable;
impl<COMP: BaseComponent> Scope<COMP> {
pub(crate) async fn render_to_string(
self,
w: &mut String,
props: Rc<COMP::Properties>,
hydratable: bool,
) {
let (tx, rx) = oneshot::channel();
let state = ComponentRenderState::Ssr { sender: Some(tx) };
scheduler::push_component_create(
Box::new(CreateRunner {
initial_render_state: state,
props,
scope: self.clone(),
}),
Box::new(RenderRunner {
state: self.state.clone(),
}),
);
scheduler::start();
#[cfg(debug_assertions)]
let collectable = Collectable::Component(std::any::type_name::<COMP>());
#[cfg(not(debug_assertions))]
let collectable = Collectable::Component;
if hydratable {
collectable.write_open_tag(w);
}
let html = rx.await.unwrap();
let self_any_scope = AnyScope::from(self.clone());
html.render_to_string(w, &self_any_scope, hydratable).await;
if hydratable {
collectable.write_close_tag(w);
}
scheduler::push_component_destroy(Box::new(DestroyRunner {
state: self.state.clone(),
#[cfg(feature = "csr")]
parent_to_detach: false,
}));
scheduler::start();
}
}
}
#[cfg(not(any(feature = "ssr", feature = "csr")))]
mod feat_no_csr_ssr {
use super::*;
// Skeleton code to provide public methods when no renderer are enabled.
impl<COMP: BaseComponent> Scope<COMP> {
/// Returns the linked component if available
pub fn get_component(&self) -> Option<impl Deref<Target = COMP> + '_> {
Option::<&COMP>::None
}
/// Send a message to the component.
pub fn send_message<T>(&self, _msg: T)
where
T: Into<COMP::Message>,
{
}
/// Send a batch of messages to the component.
///
/// This is slightly more efficient than calling [`send_message`](Self::send_message)
/// in a loop.
pub fn send_message_batch(&self, _messages: Vec<COMP::Message>) {}
}
}
#[cfg(any(feature = "ssr", feature = "csr"))]
mod feat_csr_ssr {
use super::*;
use crate::scheduler::{self, Shared};
use std::cell::Ref;
use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Debug)]
pub(crate) struct MsgQueue<Msg>(Shared<Vec<Msg>>);
impl<Msg> MsgQueue<Msg> {
pub fn new() -> Self {
MsgQueue(Rc::default())
}
pub fn push(&self, msg: Msg) -> usize {
let mut inner = self.0.borrow_mut();
inner.push(msg);
inner.len()
}
pub fn append(&self, other: &mut Vec<Msg>) -> usize {
let mut inner = self.0.borrow_mut();
inner.append(other);
inner.len()
}
pub fn drain(&self) -> Vec<Msg> {
let mut other_queue = Vec::new();
let mut inner = self.0.borrow_mut();
std::mem::swap(&mut *inner, &mut other_queue);
other_queue
}
}
impl<Msg> Clone for MsgQueue<Msg> {
fn clone(&self) -> Self {
MsgQueue(self.0.clone())
}
}
static COMP_ID_COUNTER: AtomicUsize = AtomicUsize::new(0);
impl<COMP: BaseComponent> Scope<COMP> {
/// Crate a scope with an optional parent scope
pub(crate) fn new(parent: Option<AnyScope>) -> Self {
let parent = parent.map(Rc::new);
let state = Rc::new(RefCell::new(None));
let pending_messages = MsgQueue::new();
Scope {
_marker: PhantomData,
pending_messages,
state,
parent,
id: COMP_ID_COUNTER.fetch_add(1, Ordering::SeqCst),
}
}
/// Returns the linked component if available
pub fn get_component(&self) -> Option<impl Deref<Target = COMP> + '_> {
self.state.try_borrow().ok().and_then(|state_ref| {
state_ref.as_ref()?;
// TODO: Replace unwrap with Ref::filter_map once it becomes stable.
Some(Ref::map(state_ref, |state| {
state
.as_ref()
.and_then(|m| m.downcast_comp_ref::<COMP>())
.unwrap()
}))
})
}
pub(super) fn push_update(&self, event: UpdateEvent) {
scheduler::push_component_update(Box::new(UpdateRunner {
state: self.state.clone(),
event,
}));
// Not guaranteed to already have the scheduler started
scheduler::start();
}
/// Send a message to the component.
pub fn send_message<T>(&self, msg: T)
where
T: Into<COMP::Message>,
{
// We are the first message in queue, so we queue the update.
if self.pending_messages.push(msg.into()) == 1 {
self.push_update(UpdateEvent::Message);
}
}
/// Send a batch of messages to the component.
///
/// This is slightly more efficient than calling [`send_message`](Self::send_message)
/// in a loop.
pub fn send_message_batch(&self, mut messages: Vec<COMP::Message>) {
let msg_len = messages.len();
// The queue was empty, so we queue the update
if self.pending_messages.append(&mut messages) == msg_len {
self.push_update(UpdateEvent::Message);
}
}
}
}
#[cfg(any(feature = "ssr", feature = "csr"))]
pub(crate) use feat_csr_ssr::*;
#[cfg(feature = "csr")]
mod feat_csr {
use super::*;
use crate::dom_bundle::{BSubtree, Bundle};
use crate::html::component::lifecycle::{
ComponentRenderState, CreateRunner, DestroyRunner, RenderRunner,
};
use crate::html::NodeRef;
use crate::scheduler;
use std::cell::Ref;
use web_sys::Element;
impl AnyScope {
#[cfg(test)]
pub(crate) fn test() -> Self {
Self {
type_id: TypeId::of::<()>(),
parent: None,
typed_scope: Rc::new(()),
}
}
}
impl<COMP> Scope<COMP>
where
COMP: BaseComponent,
{
/// Mounts a component with `props` to the specified `element` in the DOM.
pub(crate) fn mount_in_place(
&self,
root: BSubtree,
parent: Element,
next_sibling: NodeRef,
node_ref: NodeRef,
props: Rc<COMP::Properties>,
) {
let bundle = Bundle::new();
node_ref.link(next_sibling.clone());
let state = ComponentRenderState::Render {
bundle,
root,
node_ref,
parent,
next_sibling,
};
scheduler::push_component_create(
Box::new(CreateRunner {
initial_render_state: state,
props,
scope: self.clone(),
}),
Box::new(RenderRunner {
state: self.state.clone(),
}),
);
// Not guaranteed to already have the scheduler started
scheduler::start();
}
pub(crate) fn reuse(
&self,
props: Rc<COMP::Properties>,
node_ref: NodeRef,
next_sibling: NodeRef,
) {
#[cfg(debug_assertions)]
super::super::log_event(self.id, "reuse");
self.push_update(UpdateEvent::Properties(props, node_ref, next_sibling));
}
}
pub(crate) trait Scoped {
fn to_any(&self) -> AnyScope;
/// Get the render state if it hasn't already been destroyed
fn render_state(&self) -> Option<Ref<'_, ComponentRenderState>>;
/// Shift the node associated with this scope to a new place
fn shift_node(&self, parent: Element, next_sibling: NodeRef);
/// Process an event to destroy a component
fn destroy(self, parent_to_detach: bool);
fn destroy_boxed(self: Box<Self>, parent_to_detach: bool);
}
impl<COMP: BaseComponent> Scoped for Scope<COMP> {
fn to_any(&self) -> AnyScope {
self.clone().into()
}
fn render_state(&self) -> Option<Ref<'_, ComponentRenderState>> {
let state_ref = self.state.borrow();
// check that component hasn't been destroyed
state_ref.as_ref()?;
Some(Ref::map(state_ref, |state_ref| {
&state_ref.as_ref().unwrap().render_state
}))
}
/// Process an event to destroy a component
fn destroy(self, parent_to_detach: bool) {
scheduler::push_component_destroy(Box::new(DestroyRunner {
state: self.state,
parent_to_detach,
}));
// Not guaranteed to already have the scheduler started
scheduler::start();
}
fn destroy_boxed(self: Box<Self>, parent_to_detach: bool) {
self.destroy(parent_to_detach)
}
fn shift_node(&self, parent: Element, next_sibling: NodeRef) {
let mut state_ref = self.state.borrow_mut();
if let Some(render_state) = state_ref.as_mut() {
render_state.render_state.shift(parent, next_sibling)
}
}
}
}
#[cfg_attr(documenting, doc(cfg(feature = "hydration")))]
#[cfg(feature = "hydration")]
mod feat_hydration {
use super::*;
use crate::dom_bundle::{BSubtree, Fragment};
use crate::html::component::lifecycle::{ComponentRenderState, CreateRunner, RenderRunner};
use crate::html::NodeRef;
use crate::scheduler;
use crate::virtual_dom::Collectable;
use web_sys::Element;
impl<COMP> Scope<COMP>
where
COMP: BaseComponent,
{
/// Hydrates the component.
///
/// Returns a pending NodeRef of the next sibling.
///
/// # Note
///
/// This method is expected to collect all the elements belongs to the current component
/// immediately.
pub(crate) fn hydrate_in_place(
&self,
root: BSubtree,
parent: Element,
fragment: &mut Fragment,
node_ref: NodeRef,
props: Rc<COMP::Properties>,
) {
// This is very helpful to see which component is failing during hydration
// which means this component may not having a stable layout / differs between
// client-side and server-side.
#[cfg(all(debug_assertions, feature = "trace_hydration"))]
gloo::console::trace!(format!(
"queuing hydration of: {}(ID: {:?})",
std::any::type_name::<COMP>(),
self.id
));
#[cfg(debug_assertions)]
let collectable = Collectable::Component(std::any::type_name::<COMP>());
#[cfg(not(debug_assertions))]
let collectable = Collectable::Component;
let fragment = Fragment::collect_between(fragment, &collectable, &parent);
node_ref.set(fragment.front().cloned());
let next_sibling = NodeRef::default();
let state = ComponentRenderState::Hydration {
root,
parent,
node_ref,
next_sibling,
fragment,
};
scheduler::push_component_hydrate(
Box::new(CreateRunner {
initial_render_state: state,
props,
scope: self.clone(),
}),
Box::new(RenderRunner {
state: self.state.clone(),
}),
);
// Not guaranteed to already have the scheduler started
scheduler::start();
}
}
}
#[cfg(feature = "csr")]
pub(crate) use feat_csr::*;
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
mod feat_io {
use std::future::Future;
use super::*;
use crate::io_coop::spawn_local;
impl<COMP: BaseComponent> Scope<COMP> {
/// This method creates a [`Callback`] which, when emitted, asynchronously awaits the
/// message returned from the passed function before sending it to the linked component.
///
/// # Panics
/// If the future panics, then the promise will not resolve, and will leak.
pub fn callback_future<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where
M: Into<COMP::Message>,
FU: Future<Output = M> + 'static,
FN: Fn(IN) -> FU + 'static,
{
let link = self.clone();
let closure = move |input: IN| {
let future: FU = function(input);
link.send_future(future);
};
closure.into()
}
/// This method asynchronously awaits a [Future] that returns a message and sends it
/// to the linked component.
///
/// # Panics
/// If the future panics, then the promise will not resolve, and will leak.
pub fn send_future<F, M>(&self, future: F)
where
M: Into<COMP::Message>,
F: Future<Output = M> + 'static,
{
let link = self.clone();
let js_future = async move {
let message: COMP::Message = future.await.into();
link.send_message(message);
};
spawn_local(js_future);
}
/// Asynchronously send a batch of messages to a component. This asynchronously awaits the
/// passed [Future], before sending the message batch to the linked component.
///
/// # Panics
/// If the future panics, then the promise will not resolve, and will leak.
pub fn send_future_batch<F>(&self, future: F)
where
F: Future + 'static,
F::Output: SendAsMessage<COMP>,
{
let link = self.clone();
let js_future = async move {
future.await.send(&link);
};
spawn_local(js_future);
}
}
}
/// Defines a message type that can be sent to a component.
/// Used for the return value of closure given to [Scope::batch_callback](struct.Scope.html#method.batch_callback).
pub trait SendAsMessage<COMP: BaseComponent> {
/// Sends the message to the given component's scope.
/// See [Scope::batch_callback](struct.Scope.html#method.batch_callback).
fn send(self, scope: &Scope<COMP>);
}
impl<COMP> SendAsMessage<COMP> for Option<COMP::Message>
where
COMP: BaseComponent,
{
fn send(self, scope: &Scope<COMP>) {
if let Some(msg) = self {
scope.send_message(msg);
}
}
}
impl<COMP> SendAsMessage<COMP> for Vec<COMP::Message>
where
COMP: BaseComponent,
{
fn send(self, scope: &Scope<COMP>) {
scope.send_message_batch(self);
}
}