zng_ext_l10n/sources/
swap.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
use std::{collections::HashMap, path::PathBuf, sync::Arc};

use zng_var::{types::WeakArcVar, var, ArcEq, ArcVar, BoxedVar, BoxedWeakVar, Var as _, VarHandle, WeakVar as _};

use crate::{L10nSource, Lang, LangFilePath, LangMap, LangResourceStatus};

use super::NilL10nSource;

/// Represents localization source that can swap the actual source without disconnecting variables
/// taken on resources.
///
/// Note that [`L10N.load`] already uses this source internally.
///
/// [`L10N.load`]: crate::L10N::load
pub struct SwapL10nSource {
    actual: Box<dyn L10nSource>,

    available_langs: ArcVar<Arc<LangMap<HashMap<LangFilePath, PathBuf>>>>,
    available_langs_status: ArcVar<LangResourceStatus>,

    res: HashMap<(Lang, LangFilePath), SwapFile>,
}
impl SwapL10nSource {
    /// New with [`NilL10nSource`].
    pub fn new() -> Self {
        Self {
            actual: Box::new(NilL10nSource),
            available_langs: var(Arc::default()),
            available_langs_status: var(LangResourceStatus::NotAvailable),
            res: HashMap::new(),
        }
    }

    /// Swaps the backend source with `source`.
    pub fn load(&mut self, source: impl L10nSource) {
        self.swap_source(Box::new(source))
    }
    fn swap_source(&mut self, new: Box<dyn L10nSource>) {
        self.actual = new;

        let actual_langs = self.actual.available_langs();
        self.available_langs.set_from(&actual_langs);
        actual_langs.bind(&self.available_langs).perm();

        let actual_status = self.actual.available_langs_status();
        self.available_langs_status.set_from(&actual_status);
        actual_status.bind(&self.available_langs_status).perm();

        for ((lang, file), f) in &mut self.res {
            if let Some(res) = f.res.upgrade() {
                let actual_f = self.actual.lang_resource(lang.clone(), file.clone());
                f.actual_weak_res = actual_f.bind(&res); // weak ref to `res` is held by `actual_f`
                f.res_strong_actual = res.hold(actual_f); // strong ref to `actual_f` is held by `res`.

                let actual_s = self.actual.lang_resource_status(lang.clone(), file.clone());
                f.status.set_from(&actual_s);
                f.actual_weak_status = actual_s.bind(&f.status);
            } else {
                f.status.set(LangResourceStatus::NotAvailable);
            }
        }
    }
}
impl Default for SwapL10nSource {
    fn default() -> Self {
        Self::new()
    }
}
impl L10nSource for SwapL10nSource {
    fn available_langs(&mut self) -> BoxedVar<Arc<LangMap<HashMap<LangFilePath, PathBuf>>>> {
        self.available_langs.read_only().boxed()
    }

    fn available_langs_status(&mut self) -> BoxedVar<LangResourceStatus> {
        self.available_langs_status.read_only().boxed()
    }

    fn lang_resource(&mut self, lang: Lang, file: LangFilePath) -> BoxedVar<Option<ArcEq<fluent::FluentResource>>> {
        match self.res.entry((lang, file)) {
            std::collections::hash_map::Entry::Occupied(mut e) => {
                if let Some(res) = e.get().res.upgrade() {
                    res
                } else {
                    let (lang, file) = e.key();
                    let actual_f = self.actual.lang_resource(lang.clone(), file.clone());
                    let actual_s = self.actual.lang_resource_status(lang.clone(), file.clone());

                    let f = e.get_mut();

                    let res = var(actual_f.get());
                    f.actual_weak_res = actual_f.bind(&res); // weak ref to `res` is held by `actual_f`
                    f.res_strong_actual = res.hold(actual_f); // strong ref to `actual_f` is held by `res`.
                    let res = res.boxed();
                    f.res = res.downgrade();

                    f.status.set_from(&actual_s);
                    f.actual_weak_status = actual_s.bind(&f.status);

                    res
                }
            }
            std::collections::hash_map::Entry::Vacant(e) => {
                let mut f = SwapFile::new();
                let (lang, file) = e.key();
                let actual_f = self.actual.lang_resource(lang.clone(), file.clone());
                let actual_s = self.actual.lang_resource_status(lang.clone(), file.clone());

                let res = var(actual_f.get());
                f.actual_weak_res = actual_f.bind(&res); // weak ref to `res` is held by `actual_f`
                f.res_strong_actual = res.hold(actual_f); // strong ref to `actual_f` is held by `res`.
                let res = res.boxed();
                f.res = res.downgrade();

                f.status.set_from(&actual_s);
                f.actual_weak_status = actual_s.bind(&f.status);

                e.insert(f);

                res
            }
        }
    }

    fn lang_resource_status(&mut self, lang: Lang, file: LangFilePath) -> BoxedVar<LangResourceStatus> {
        self.res
            .entry((lang, file))
            .or_insert_with(SwapFile::new)
            .status
            .read_only()
            .boxed()
    }
}
struct SwapFile {
    res: BoxedWeakVar<Option<ArcEq<fluent::FluentResource>>>,
    status: ArcVar<LangResourceStatus>,
    actual_weak_res: VarHandle,
    res_strong_actual: VarHandle,
    actual_weak_status: VarHandle,
}
impl SwapFile {
    fn new() -> Self {
        Self {
            res: WeakArcVar::default().boxed(),
            status: var(LangResourceStatus::Loading),
            actual_weak_res: VarHandle::dummy(),
            res_strong_actual: VarHandle::dummy(),
            actual_weak_status: VarHandle::dummy(),
        }
    }
}