This commit is contained in:
Michael Zhang 2020-02-11 01:13:00 -06:00
parent 8b0fdef461
commit 4865d5d284
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
7 changed files with 48 additions and 18 deletions

View file

@ -10,7 +10,11 @@ members = [
"examples/helloworld",
]
[features]
default = ["web"]
web = ["stdweb"]
[dependencies]
enterprise-compiler = { path = "enterprise-compiler" }
stdweb = "0.4.20"
stdweb = { version = "0.4.20", optional = true }
parking_lot = "0.10.0"

View file

@ -77,7 +77,7 @@ fn process(
}
impl<B: Backend> crate::Component<B> for #name<B> {
fn initialize(&self, el: &enterprise::stdweb::web::Element) {
fn create(&self, el: &enterprise::stdweb::web::Element) {
#init_el_code
}
}

View file

@ -14,7 +14,7 @@ fn main() {
let web = Web;
let app = HelloWorld::new(&web);
enterprise::render(&app, "app");
web.initialize(app, "app".into());
stdweb::event_loop();
}

View file

@ -1,5 +0,0 @@
pub trait Backend {}
pub struct Web;
impl Backend for Web {}

16
src/backend/mod.rs Normal file
View file

@ -0,0 +1,16 @@
//! Everything related to backends and backend-specific actions.
mod web;
pub use self::web::Web;
use crate::Component;
/// Describes a possible compile backend.
pub trait Backend: Sized {
/// The parameters that are passed into the init function.
type InitParams;
/// Initializes the backend with the given component.
fn initialize<C: Component<Self>>(&self, _: C, _: Self::InitParams);
}

18
src/backend/web.rs Normal file
View file

@ -0,0 +1,18 @@
use stdweb::web::{document, INonElementParentNode};
use crate::backend::Backend;
use crate::Component;
/// Compiling to a web application.
pub struct Web;
impl Backend for Web {
type InitParams = String;
fn initialize<C: Component<Self>>(&self, component: C, params: Self::InitParams) {
let id = params.as_ref();
if let Some(el) = document().get_element_by_id(id) {
component.create(&el);
}
}
}

View file

@ -1,3 +1,7 @@
//! Enterprise is a backend-agnostic framework for developing server-client GUI applications.
#![deny(missing_docs)]
extern crate enterprise_compiler;
// re-exports
@ -7,18 +11,11 @@ pub extern crate stdweb;
mod backend;
pub use enterprise_compiler::example;
use stdweb::web::{document, Element, INonElementParentNode};
pub use crate::backend::{Backend, Web};
/// Components are the building-blocks of enterprise applications.
pub trait Component<B: Backend> {
fn initialize(&self, element: &Element);
}
pub fn render<B: Backend, C: Component<B>>(component: &C, id: impl AsRef<str>) {
let id = id.as_ref();
if let Some(el) = document().get_element_by_id(id) {
println!("Rendering...");
component.initialize(&el);
}
/// TODO: replace this with a real init function.
fn create(&self, el: &crate::stdweb::web::Element);
}