Skip to main content

perspective_viewer/utils/
weak_scope.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::cell::RefCell;
14use std::ops::Deref;
15use std::rc::Rc;
16
17use derivative::Derivative;
18use yew::html::Scope;
19use yew::prelude::*;
20
21/// A mutable reference for capturing the [`Scope`] props arguments, a useful
22/// function for tests.
23#[derive(Derivative)]
24#[derivative(Clone(bound = ""), Default(bound = ""))]
25pub struct WeakScope<C: Component>(Rc<RefCell<Option<Scope<C>>>>);
26
27impl<C: Component> Deref for WeakScope<C> {
28    type Target = Rc<RefCell<Option<Scope<C>>>>;
29
30    fn deref(&self) -> &Self::Target {
31        &self.0
32    }
33}
34
35impl<C: Component> PartialEq for WeakScope<C> {
36    fn eq(&self, other: &Self) -> bool {
37        Rc::ptr_eq(&self.0, &other.0)
38    }
39}
40
41// #[extend::ext]
42// pub impl<T: Component> Context<T> {
43//     fn context<U: Clone + PartialEq + 'static>(&self) -> U {
44//         self.link()
45//             .context::<U>(Callback::from(|_| {}))
46//             .map(|(c, _)| c)
47//             .unwrap()
48//     }
49// }