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
use crate::collection::SharedCollection;
use crate::js::Js;
use crate::transaction::YTransaction;
use crate::{ImplicitTransaction, Observer, Result};
use std::sync::Arc;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use yrs::branch::BranchPtr;
use yrs::types::weak::{LinkSource, WeakEvent};
use yrs::types::TYPE_REFS_WEAK;
use yrs::{
    DeepObservable, Doc, GetString, Observable, SharedRef, Transact, TransactionMut, WeakPrelim,
    WeakRef,
};

pub(crate) struct PrelimWrapper {
    prelim: WeakPrelim<BranchPtr>,
    doc: Doc,
}

#[wasm_bindgen]
#[repr(transparent)]
pub struct YWeakLink(pub(crate) SharedCollection<PrelimWrapper, WeakRef<BranchPtr>>);

impl YWeakLink {
    pub(crate) fn from_prelim<S: SharedRef>(prelim: WeakPrelim<S>, doc: Doc) -> Self {
        let prelim = prelim.upcast();
        YWeakLink(SharedCollection::Prelim(PrelimWrapper { prelim, doc }))
    }

    pub(crate) fn source(&self, txn: &TransactionMut) -> Arc<LinkSource> {
        match &self.0 {
            SharedCollection::Integrated(c) => {
                if let Some(shared_ref) = c.hook.get(txn) {
                    shared_ref.source().clone()
                } else {
                    panic!("{}", crate::js::errors::REF_DISPOSED);
                }
            }
            SharedCollection::Prelim(v) => v.prelim.source().clone(),
        }
    }
}

#[wasm_bindgen]
impl YWeakLink {
    /// Returns true if this is a preliminary instance of `YWeakLink`.
    ///
    /// Preliminary instances can be nested into other shared data types such as `YArray` and `YMap`.
    /// Once a preliminary instance has been inserted this way, it becomes integrated into ywasm
    /// document store and cannot be nested again: attempt to do so will result in an exception.
    #[wasm_bindgen(getter)]
    pub fn prelim(&self) -> bool {
        self.0.is_prelim()
    }

    #[wasm_bindgen(getter, js_name = type)]
    #[inline]
    pub fn get_type(&self) -> u8 {
        TYPE_REFS_WEAK
    }

    /// Gets unique logical identifier of this type, shared across peers collaborating on the same
    /// document.
    #[wasm_bindgen(getter, js_name = id)]
    #[inline]
    pub fn id(&self) -> crate::Result<JsValue> {
        self.0.id()
    }

    /// Checks if current YWeakLink reference is alive and has not been deleted by its parent collection.
    /// This method only works on already integrated shared types and will return false is current
    /// type is preliminary (has not been integrated into document).
    #[wasm_bindgen(js_name = alive)]
    pub fn alive(&self, txn: &YTransaction) -> bool {
        self.0.is_alive(txn)
    }

    #[wasm_bindgen(js_name = deref)]
    pub fn deref(&self, txn: &ImplicitTransaction) -> Result<JsValue> {
        use yrs::MapRef;

        match &self.0 {
            SharedCollection::Prelim(c) => {
                let weak_ref: WeakPrelim<MapRef> = WeakPrelim::from(c.prelim.clone());
                let value = match YTransaction::from_implicit(txn)? {
                    Some(txn) => {
                        let txn: &TransactionMut = &*txn;
                        weak_ref.try_deref_raw(txn)
                    }
                    None => {
                        let txn = c
                            .doc
                            .try_transact()
                            .map_err(|_| JsValue::from_str(crate::js::errors::ANOTHER_RW_TX))?;
                        weak_ref.try_deref_raw(&txn)
                    }
                };
                match value {
                    None => Ok(JsValue::UNDEFINED),
                    Some(value) => Ok(Js::from_value(&value, &c.doc).into()),
                }
            }
            SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| {
                let weak_ref: WeakRef<MapRef> = WeakRef::from(c.clone());
                let value = weak_ref.try_deref_value(txn);
                match value {
                    None => Ok(JsValue::UNDEFINED),
                    Some(value) => Ok(Js::from_value(&value, txn.doc()).into()),
                }
            }),
        }
    }

    #[wasm_bindgen(js_name = unquote)]
    pub fn unquote(&self, txn: &ImplicitTransaction) -> Result<js_sys::Array> {
        use std::iter::FromIterator;
        use yrs::ArrayRef;

        match &self.0 {
            SharedCollection::Prelim(c) => {
                let weak_ref: WeakPrelim<ArrayRef> = WeakPrelim::from(c.prelim.clone());
                let doc = &c.doc;
                let values: Vec<_> = match YTransaction::from_implicit(txn)? {
                    Some(txn) => {
                        let txn: &TransactionMut = &*txn;
                        weak_ref
                            .unquote(txn)
                            .map(|value| Js::from_value(&value, doc))
                            .collect()
                    }
                    None => {
                        let txn = c
                            .doc
                            .try_transact()
                            .map_err(|_| JsValue::from_str(crate::js::errors::ANOTHER_RW_TX))?;
                        weak_ref
                            .unquote(&txn)
                            .map(|value| Js::from_value(&value, doc))
                            .collect()
                    }
                };
                Ok(js_sys::Array::from_iter(values))
            }
            SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| {
                let weak_ref: WeakRef<ArrayRef> = WeakRef::from(c.clone());
                let doc = txn.doc();
                let iter = weak_ref
                    .unquote(txn)
                    .map(|value| Js::from_value(&value, doc));
                Ok(js_sys::Array::from_iter(iter))
            }),
        }
    }

    #[wasm_bindgen(js_name = toString)]
    pub fn to_string(&self, txn: &ImplicitTransaction) -> Result<String> {
        use yrs::XmlTextRef;

        match &self.0 {
            SharedCollection::Prelim(c) => {
                let weak_ref: WeakPrelim<XmlTextRef> = WeakPrelim::from(c.prelim.clone());
                let string = match YTransaction::from_implicit(txn)? {
                    Some(txn) => {
                        let txn: &TransactionMut = &*txn;
                        weak_ref.get_string(txn)
                    }
                    None => {
                        let txn = c
                            .doc
                            .try_transact()
                            .map_err(|_| JsValue::from_str(crate::js::errors::ANOTHER_RW_TX))?;
                        weak_ref.get_string(&txn)
                    }
                };
                Ok(string)
            }
            SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| {
                let weak_ref: WeakRef<XmlTextRef> = WeakRef::from(c.clone());
                let string = weak_ref.get_string(txn);
                Ok(string)
            }),
        }
    }

    /// Subscribes to all operations happening over this instance of `YMap`. All changes are
    /// batched and eventually triggered during transaction commit phase.
    /// Returns an `YObserver` which, when free'd, will unsubscribe current callback.
    #[wasm_bindgen(js_name = observe)]
    pub fn observe(&mut self, f: js_sys::Function) -> Result<crate::Observer> {
        match &self.0 {
            SharedCollection::Prelim(_) => {
                Err(JsValue::from_str(crate::js::errors::INVALID_PRELIM_OP))
            }
            SharedCollection::Integrated(c) => {
                let txn = c.transact()?;
                let weak = c.resolve(&txn)?;
                Ok(Observer(weak.observe(move |txn, e| {
                    let e = YWeakLinkEvent::new(e, txn).into();
                    let txn = YTransaction::from_ref(txn);
                    f.call2(&JsValue::UNDEFINED, &e, &txn.into()).unwrap();
                })))
            }
        }
    }

    /// Subscribes to all operations happening over this Y shared type, as well as events in
    /// shared types stored within this one. All changes are batched and eventually triggered
    /// during transaction commit phase.
    /// Returns an `YEventObserver` which, when free'd, will unsubscribe current callback.
    #[wasm_bindgen(js_name = observeDeep)]
    pub fn observe_deep(&mut self, f: js_sys::Function) -> Result<crate::Observer> {
        match &self.0 {
            SharedCollection::Prelim(_) => {
                Err(JsValue::from_str(crate::js::errors::INVALID_PRELIM_OP))
            }
            SharedCollection::Integrated(c) => {
                let txn = c.transact()?;
                let weak = c.resolve(&txn)?;
                Ok(Observer(weak.observe_deep(move |txn, e| {
                    let e = crate::js::convert::events_into_js(txn, e);
                    let txn = YTransaction::from_ref(txn);
                    f.call2(&JsValue::UNDEFINED, &e, &txn.into()).unwrap();
                })))
            }
        }
    }
}

/// Event generated by `YXmlElement.observe` method. Emitted during transaction commit phase.
#[wasm_bindgen]
pub struct YWeakLinkEvent {
    inner: &'static WeakEvent,
    txn: &'static TransactionMut<'static>,
    target: Option<JsValue>,
    origin: JsValue,
}

#[wasm_bindgen]
impl YWeakLinkEvent {
    pub(crate) fn new<'doc>(event: &WeakEvent, txn: &TransactionMut<'doc>) -> Self {
        let inner: &'static WeakEvent = unsafe { std::mem::transmute(event) };
        let txn: &'static TransactionMut<'static> = unsafe { std::mem::transmute(txn) };
        let origin = if let Some(origin) = txn.origin() {
            Js::from(origin).into()
        } else {
            JsValue::UNDEFINED
        };
        YWeakLinkEvent {
            inner,
            txn,
            origin,
            target: None,
        }
    }

    #[wasm_bindgen(getter, js_name = origin)]
    pub fn origin(&self) -> JsValue {
        self.origin.clone()
    }

    /// Returns a current shared type instance, that current event changes refer to.
    #[wasm_bindgen(getter)]
    pub fn target(&mut self) -> JsValue {
        let target: WeakRef<BranchPtr> = self.inner.as_target();
        let doc = self.txn.doc();
        let js = self.target.get_or_insert_with(|| {
            let target = target.clone();
            YWeakLink(SharedCollection::integrated(target, doc.clone())).into()
        });
        js.clone()
    }

    /// Returns an array of keys and indexes creating a path from root type down to current instance
    /// of shared type (accessible via `target` getter).
    #[wasm_bindgen]
    pub fn path(&self) -> JsValue {
        crate::js::convert::path_into_js(self.inner.path())
    }
}