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
//! Playwright parity: `client/disposable.ts` `Disposable` / `DisposableStub`.
//!
//! Playwright's `route`, `addInitScript`, `exposeBinding`, and
//! `exposeFunction` return a handle with an async `dispose()` method that
//! reverses the registration (unroute / remove-init-script / unbind). The
//! `DisposableStub` form wraps a `() => Promise<void>` closure that runs once
//! and is idempotent afterwards. This is that single canonical type, owned by
//! core; the `NAPI` and `QuickJS` layers wrap it as a JS object exposing
//! `dispose()` (and the `remove()` alias).
use crate::error::Result;
use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex;
/// Boxed one-shot async closure that reverses a registration.
type DisposeFn = Box<dyn FnOnce() -> Pin<Box<dyn Future<Output = Result<()>> + Send>> + Send>;
/// Reverses a single registration (route, init-script, exposed binding).
///
/// Mirrors Playwright's `DisposableStub`: the first `dispose()` runs the
/// underlying unbind closure; subsequent calls are no-ops (the closure is
/// consumed). `dispose()` and its `remove()` alias are interchangeable.
pub struct Disposable {
dispose_fn: Mutex<Option<DisposeFn>>,
}
impl std::fmt::Debug for Disposable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let consumed = self.dispose_fn.lock().map_or(true, |g| g.is_none());
f.debug_struct("Disposable").field("consumed", &consumed).finish()
}
}
impl Disposable {
/// Construct from a one-shot async unbind closure.
pub fn new<F, Fut>(dispose: F) -> Self
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<()>> + Send + 'static,
{
Self {
dispose_fn: Mutex::new(Some(Box::new(move || Box::pin(dispose())))),
}
}
/// Run the unbind closure exactly once. Repeat calls are no-ops and return
/// `Ok(())`, matching Playwright's `DisposableStub.dispose` semantics.
///
/// # Errors
///
/// Propagates any error returned by the underlying unbind closure.
pub async fn dispose(&self) -> Result<()> {
let taken = {
let mut guard = self
.dispose_fn
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
guard.take()
};
match taken {
Some(f) => f().await,
None => Ok(()),
}
}
/// Alias for [`Disposable::dispose`]. Playwright's TS surface exposes the
/// reverse-registration handle through `dispose()`; ferridriver also offers
/// `remove()` because the historical ferridriver API named these
/// `unroute` / `removeInitScript`.
///
/// # Errors
///
/// Propagates any error returned by the underlying unbind closure.
pub async fn remove(&self) -> Result<()> {
self.dispose().await
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
#[tokio::test]
async fn dispose_runs_once_then_is_noop() {
let calls = Arc::new(AtomicUsize::new(0));
let calls_for_closure = Arc::clone(&calls);
let d = Disposable::new(move || {
let calls = Arc::clone(&calls_for_closure);
async move {
calls.fetch_add(1, Ordering::SeqCst);
Ok(())
}
});
d.dispose().await.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 1);
// Repeat dispose() is a no-op.
d.dispose().await.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 1);
// remove() is an alias and also a no-op after consumption.
d.remove().await.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn remove_is_alias_for_dispose() {
let calls = Arc::new(AtomicUsize::new(0));
let calls_for_closure = Arc::clone(&calls);
let d = Disposable::new(move || {
let calls = Arc::clone(&calls_for_closure);
async move {
calls.fetch_add(1, Ordering::SeqCst);
Ok(())
}
});
d.remove().await.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn dispose_propagates_error() {
let d = Disposable::new(|| async { Err(crate::error::FerriError::invalid_argument("x", "boom")) });
assert!(d.dispose().await.is_err());
}
}