mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
DevTools: Add resource_available
as a common shared util (#36632)
These PR adds `resource_available` as a common shared util - [x] ./mach build -d does not report any errors - [x] ./mach test-tidy does not report any errors - [x] These changes partially implement https://github.com/servo/servo/issues/36027 --------- Signed-off-by: atbrakhi <atbrakhi@igalia.com>
This commit is contained in:
parent
e9e103b46c
commit
7c1e5918a8
6 changed files with 68 additions and 28 deletions
42
components/devtools/resource.rs
Normal file
42
components/devtools/resource.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::net::TcpStream;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::StreamId;
|
||||
use crate::protocol::JsonPacketStream;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct ResourceAvailableReply<T: Serialize> {
|
||||
pub from: String,
|
||||
#[serde(rename = "type")]
|
||||
pub type_: String,
|
||||
pub array: Vec<(String, Vec<T>)>,
|
||||
}
|
||||
|
||||
pub(crate) trait ResourceAvailable {
|
||||
fn actor_name(&self) -> String;
|
||||
|
||||
fn get_streams(&self) -> &RefCell<HashMap<StreamId, TcpStream>>;
|
||||
|
||||
fn resource_available<T: Serialize>(&self, resource: T, resource_type: String) {
|
||||
self.resources_available(vec![resource], resource_type);
|
||||
}
|
||||
|
||||
fn resources_available<T: Serialize>(&self, resources: Vec<T>, resource_type: String) {
|
||||
let msg = ResourceAvailableReply::<T> {
|
||||
from: self.actor_name(),
|
||||
type_: "resources-available-array".into(),
|
||||
array: vec![(resource_type, resources)],
|
||||
};
|
||||
|
||||
for stream in self.get_streams().borrow_mut().values_mut() {
|
||||
let _ = stream.write_json_packet(&msg);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue