feat(napi): add support for weak references

This commit is contained in:
Devon Govett 2022-05-07 21:48:10 -04:00 committed by LongYinan
parent 5ecf16e5ad
commit 27402aee81
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
9 changed files with 96 additions and 15 deletions

View file

@ -278,10 +278,12 @@ Generated by [AVA](https://avajs.dev).
export type CSSRuleList = CssRuleList␊
export class CssRuleList {␊
getRules(): Array<string>
get parentStyleSheet(): CSSStyleSheet␊
get name(): string | null␊
}␊
export type CSSStyleSheet = CssStyleSheet␊
export class CssStyleSheet {␊
constructor(rules: Array<string>)␊
constructor(name: string, rules: Array<string>)␊
get rules(): CssRuleList␊
anotherCssStyleSheet(): AnotherCssStyleSheet␊
}␊

View file

@ -202,9 +202,11 @@ test('should be able to create object reference and shared reference', (t) => {
test('should be able to into_reference', (t) => {
const rules = ['body: { color: red }', 'div: { color: blue }']
const sheet = new CssStyleSheet(rules)
const sheet = new CssStyleSheet('test.css', rules)
t.is(sheet.rules, sheet.rules)
t.deepEqual(sheet.rules.getRules(), rules)
t.is(sheet.rules.parentStyleSheet, sheet)
t.is(sheet.rules.name, 'test.css')
const anotherStyleSheet = sheet.anotherCssStyleSheet()
t.is(anotherStyleSheet.rules, sheet.rules)
})

View file

@ -268,10 +268,12 @@ export class JsRemote {
export type CSSRuleList = CssRuleList
export class CssRuleList {
getRules(): Array<string>
get parentStyleSheet(): CSSStyleSheet
get name(): string | null
}
export type CSSStyleSheet = CssStyleSheet
export class CssStyleSheet {
constructor(rules: Array<string>)
constructor(name: string, rules: Array<string>)
get rules(): CssRuleList
anotherCssStyleSheet(): AnotherCssStyleSheet
}

View file

@ -64,6 +64,7 @@ struct OwnedStyleSheet {
#[napi]
pub struct CSSRuleList {
owned: Rc<RefCell<OwnedStyleSheet>>,
parent: WeakReference<CSSStyleSheet>,
}
#[napi]
@ -72,10 +73,26 @@ impl CSSRuleList {
pub fn get_rules(&self) -> Vec<String> {
self.owned.borrow().rules.to_vec()
}
#[napi(getter)]
pub fn parent_style_sheet(&self) -> WeakReference<CSSStyleSheet> {
self.parent.clone()
}
#[napi(getter)]
pub fn name(&self, env: Env) -> Result<Option<String>> {
Ok(
self
.parent
.upgrade(env)?
.map(|stylesheet| stylesheet.name.clone()),
)
}
}
#[napi]
pub struct CSSStyleSheet {
name: String,
inner: Rc<RefCell<OwnedStyleSheet>>,
rules: Option<Reference<CSSRuleList>>,
}
@ -97,13 +114,21 @@ impl AnotherCSSStyleSheet {
#[napi]
impl CSSStyleSheet {
#[napi(constructor)]
pub fn new(rules: Vec<String>) -> Result<Self> {
pub fn new(name: String, rules: Vec<String>) -> Result<Self> {
let inner = Rc::new(RefCell::new(OwnedStyleSheet { rules }));
Ok(CSSStyleSheet { inner, rules: None })
Ok(CSSStyleSheet {
name,
inner,
rules: None,
})
}
#[napi(getter)]
pub fn rules(&mut self, env: Env) -> Result<Reference<CSSRuleList>> {
pub fn rules(
&mut self,
env: Env,
reference: Reference<CSSStyleSheet>,
) -> Result<Reference<CSSRuleList>> {
if let Some(rules) = &self.rules {
return rules.clone(env);
}
@ -111,6 +136,7 @@ impl CSSStyleSheet {
let rules = CSSRuleList::into_reference(
CSSRuleList {
owned: self.inner.clone(),
parent: reference.downgrade(),
},
env,
)?;