Skip to content

Commit 105422b

Browse files
authored
Make more service's methods static (yewstack#1382)
1 parent 6798054 commit 105422b

File tree

5 files changed

+8
-33
lines changed

5 files changed

+8
-33
lines changed

examples/file_upload/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use yew::{html, ChangeData, Component, ComponentLink, Html, ShouldRender};
55

66
pub struct Model {
77
link: ComponentLink<Model>,
8-
reader: ReaderService,
98
tasks: Vec<ReaderTask>,
109
files: Vec<String>,
1110
by_chunks: bool,
@@ -26,7 +25,6 @@ impl Component for Model {
2625

2726
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
2827
Model {
29-
reader: ReaderService::new(),
3028
link,
3129
tasks: vec![],
3230
files: vec![],
@@ -49,10 +47,10 @@ impl Component for Model {
4947
let task = {
5048
if chunks {
5149
let callback = self.link.callback(Msg::Chunk);
52-
self.reader.read_file_by_chunks(file, callback, 10).unwrap()
50+
ReaderService::read_file_by_chunks(file, callback, 10).unwrap()
5351
} else {
5452
let callback = self.link.callback(Msg::Loaded);
55-
self.reader.read_file(file, callback).unwrap()
53+
ReaderService::read_file(file, callback).unwrap()
5654
}
5755
};
5856
self.tasks.push(task);

yew-stdweb/examples/file_upload/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use yew::{html, ChangeData, Component, ComponentLink, Html, ShouldRender};
55

66
pub struct Model {
77
link: ComponentLink<Model>,
8-
reader: ReaderService,
98
tasks: Vec<ReaderTask>,
109
files: Vec<String>,
1110
by_chunks: bool,
@@ -26,7 +25,6 @@ impl Component for Model {
2625

2726
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
2827
Model {
29-
reader: ReaderService::new(),
3028
link,
3129
tasks: vec![],
3230
files: vec![],
@@ -53,10 +51,10 @@ impl Component for Model {
5351
let task = {
5452
if chunks {
5553
let callback = self.link.callback(Msg::Chunk);
56-
self.reader.read_file_by_chunks(file, callback, 10).unwrap()
54+
ReaderService::read_file_by_chunks(file, callback, 10).unwrap()
5755
} else {
5856
let callback = self.link.callback(Msg::Loaded);
59-
self.reader.read_file(file, callback).unwrap()
57+
ReaderService::read_file(file, callback).unwrap()
6058
}
6159
};
6260
self.tasks.push(task);

yew/src/services/reader/std_web.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,8 @@ fn new_file_reader() -> Result<FileReader, &'static str> {
2525
}
2626

2727
impl ReaderService {
28-
/// Creates a new service instance connected to `App` by provided `sender`.
29-
pub fn new() -> Self {
30-
Self {}
31-
}
32-
3328
/// Reads all bytes from a file and returns them with a callback.
34-
pub fn read_file(
35-
&mut self,
36-
file: File,
37-
callback: Callback<FileData>,
38-
) -> Result<ReaderTask, &str> {
29+
pub fn read_file(file: File, callback: Callback<FileData>) -> Result<ReaderTask, &'static str> {
3930
let file_reader = new_file_reader()?;
4031
let reader = file_reader.clone();
4132
let name = file.name();
@@ -59,11 +50,10 @@ impl ReaderService {
5950

6051
/// Reads data chunks from a file and returns them with a callback.
6152
pub fn read_file_by_chunks(
62-
&mut self,
6353
file: File,
6454
callback: Callback<Option<FileChunk>>,
6555
chunk_size: usize,
66-
) -> Result<ReaderTask, &str> {
56+
) -> Result<ReaderTask, &'static str> {
6757
let file_reader = new_file_reader()?;
6858
let name = file.name();
6959
let mut position = 0;

yew/src/services/reader/web_sys.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ use js_sys::Uint8Array;
1212
use std::cmp;
1313

1414
impl ReaderService {
15-
/// Creates a new service instance connected to `App` by provided `sender`.
16-
pub fn new() -> Self {
17-
Self {}
18-
}
19-
2015
/// Reads all bytes from a file and returns them with a callback.
21-
pub fn read_file(&mut self, file: File, callback: Callback<FileData>) -> Result<ReaderTask> {
16+
pub fn read_file(file: File, callback: Callback<FileData>) -> Result<ReaderTask> {
2217
let file_reader = FileReader::new().map_err(|_| anyhow!("couldn't acquire file reader"))?;
2318
let reader = file_reader.clone();
2419
let name = file.name();
@@ -42,7 +37,6 @@ impl ReaderService {
4237

4338
/// Reads data chunks from a file and returns them with a callback.
4439
pub fn read_file_by_chunks(
45-
&mut self,
4640
file: File,
4741
callback: Callback<Option<FileChunk>>,
4842
chunk_size: usize,

yew/src/services/resize.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,8 @@ impl WindowDimensions {
5757
}
5858

5959
impl ResizeService {
60-
/// Creates a new ResizeService.
61-
pub fn new() -> ResizeService {
62-
ResizeService {}
63-
}
64-
6560
/// Register a callback that will be called when the browser window resizes.
66-
pub fn register(&mut self, callback: Callback<WindowDimensions>) -> ResizeTask {
61+
pub fn register(callback: Callback<WindowDimensions>) -> ResizeTask {
6762
let callback =
6863
move |#[cfg(feature = "web_sys")] _event: &Event,
6964
#[cfg(feature = "std_web")] _event: ResizeEvent| {

0 commit comments

Comments
 (0)