asm-dom

A minimal WebAssembly virtual DOM to build C++ SPA (Single page application...

README

:rage4: asm-dom :rage4: experimental Build Status npm version npm downloads Join the chat at https://gitter.im/mbasso/asm-dom

A minimal WebAssembly virtual DOM to build C++ SPA (Single page applications)

Table of Contents

Motivation

asm-dom is a minimal WebAssembly virtual DOM to build C++ SPA (Single page applications). You can write an entire SPA in C++ and compile it to WebAssembly (or asmjs as fallback) using Emscripten, asm-dom will call DOM APIs for you. This will produce an app thataims to execute at native speed by taking advantage of common hardware capabilities, also, you can use your C/C++ code without any change, you haven't to create a binding layer to use it (as we have to do if we want to use a C++ lib from JS). Basically we are creating an app in C++ that call javascript if needed instead of the opposite. You can write only once in C++ and share as much code as possible with desktop/mobile apps and web site. If you want to learn more about performance, please see this.
How can I structure my application with asm-dom?
asm-dom is a low-level virtual DOM library. It is unopinionated with regards to how you should structure your application.
How did you come up with the concept of asm-dom?
At the beginning asm-dom is born from the idea to test the powerful of WebAssembly in a common use case that is not gaming, VR, AR or Image / video editing. Unfortunately, at the moment, GC/DOM Integration is a future feature 🦄, so, asm-dom isn't totally developed in wasm. All interactions with the DOM are written in Javascript. This is a big disadvantage because of the overhead of the binding between JS and WASM, in the future asm-dom will be even more powerful, anyway results are satisfying.

Inline Example

  1. ``` js
  2. #include "asm-dom.hpp"
  3. using namespace asmdom;
  4. int main() {
  5.   Config config = Config();
  6.   init(config);
  7.   // asm-dom can be used with a JSX like syntax thanks to gccx
  8.   VNode* vnode = (
  9.     <div
  10.       onclick={[](emscripten::val e) -> bool {
  11.         emscripten::val::global("console").call<void>("log", emscripten::val("clicked"));
  12.         return true;
  13.       }}
  14.     >
  15.       <span style="font-weight: bold">This is bold</span>
  16.       and this is just normal text
  17.       <a href="/foo">I'll take you places!</a>
  18.     </div>
  19.   );
  20.   // Patch into empty DOM element – this modifies the DOM as a side effect
  21.   patch(
  22.     emscripten::val::global("document").call<emscripten::val>(
  23.       "getElementById",
  24.       std::string("root")
  25.     ),
  26.     vnode
  27.   );
  28.   // without gccx
  29.   VNode* newVnode = h("div",
  30.     Data(
  31.       Callbacks {
  32.         {"onclick", [](emscripten::val e) -> bool {
  33.           emscripten::val::global("console").call<void>("log", emscripten::val("another click"));
  34.           return true;
  35.         }}
  36.       }
  37.     ),
  38.     Children {
  39.       h("span",
  40.         Data(
  41.           Attrs {
  42.             {"style", "font-weight: normal; font-style: italic"}
  43.           }
  44.         ),
  45.         std::string("This is now italic type")
  46.       ),
  47.       h(" and this is just normal text", true),
  48.       h("a",
  49.         Data(
  50.           Attrs {
  51.             {"href", "/bar"}
  52.           }
  53.         ),
  54.         std::string("I'll take you places!")
  55.       )
  56.     }
  57.   );
  58.   // Second `patch` invocation
  59.   patch(vnode, newVnode); // asm-dom efficiently updates the old view to the new state
  60.   return 0;
  61. };
  62. ```

Getting started

asm-dom aims to be used from C++, however it can be used also from javascript, here you can find the doc of both:

Ecosystem

Here you can find a list of related projects:
- gccx - CPX (JSX like syntax) support.
- asm-dom-boilerplate - A simple boilerplate to start using asm-dom without configuration.

Examples

Examples are available in the examples folder.
Also, here is the list of third-party examples:
- asm-dom-cmake - Build using cmake only.
and online Demos:

Roadmap

- [ ] Thunks support
- [ ] asm-dom aims to be even more powerful with GC/DOM Integration. Unfortunately this is a future feature 🦄, so, we have to be patient and wait a bit.

Change Log

This project adheres to Semantic Versioning.
Every release, along with the migration instructions, is documented on the Github Releases page.

Authors

Matteo Basso

Copyright and License

Copyright for portions of project asm-dom are held by:
- Simon Friis Vindum, 2015 as part of project snabbdom
- project snabbdom-to-html
All other copyright for project asm-dom are held by Matteo Basso.
Copyright (c) 2016, Matteo Basso.
asm-dom source code is licensed under the MIT License.