All Projects → albrow → Vdom

albrow / Vdom

A virtual dom implementation written in go which is compatible with gopherjs

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to Vdom

Lute
🎼 一款结构化的 Markdown 引擎,支持 Go 和 JavaScript。
Stars: ✭ 211 (+154.22%)
Mutual labels:  gopherjs
react
Super lightweight Go bindings for react.js
Stars: ✭ 130 (+56.63%)
Mutual labels:  gopherjs
Goplayspace
Advanced Go Playground frontend written in Go, with syntax highlighting, turtle graphics mode, and more
Stars: ✭ 765 (+821.69%)
Mutual labels:  gopherjs
Golymer
Web components with golang (gopherjs) moved to gitlab.com/microo8/golymer
Stars: ✭ 220 (+165.06%)
Mutual labels:  gopherjs
qlql
No description or website provided.
Stars: ✭ 26 (-68.67%)
Mutual labels:  gopherjs
go-pouchdb
GopherJS bindings for PouchDB ⚠️ NOTICE ⚠️ this package has been superseded by https://github.com/go-kivik/kivik
Stars: ✭ 12 (-85.54%)
Mutual labels:  gopherjs
Gr
Aka Go React: GopherJS bindings for Facebook React. NOTE: Still early and not production ready.
Stars: ✭ 162 (+95.18%)
Mutual labels:  gopherjs
Instago
Download/access photos, videos, stories, story highlights, postlives, following and followers of Instagram
Stars: ✭ 59 (-28.92%)
Mutual labels:  gopherjs
iris
The interpreter of ISLisp
Stars: ✭ 58 (-30.12%)
Mutual labels:  gopherjs
vecty-chatapp
Vecty Samples
Stars: ✭ 13 (-84.34%)
Mutual labels:  gopherjs
Lute
🎼 一款对中文语境优化的 Markdown 引擎,支持 Go 和 JavaScript。A structured Markdown engine that supports Go and JavaScript.
Stars: ✭ 222 (+167.47%)
Mutual labels:  gopherjs
gox
JSX for Go
Stars: ✭ 165 (+98.8%)
Mutual labels:  gopherjs
gopherjs-frappe-charts
[experimental/outdated; frappe v0.07] GopherJS bindings for frappe/charts - simple Go charts for your frontend
Stars: ✭ 20 (-75.9%)
Mutual labels:  gopherjs
Vue
The progressive framework for WebAssembly applications.
Stars: ✭ 211 (+154.22%)
Mutual labels:  gopherjs
Vecty Router
A declarative client-side router for Vecty applications.
Stars: ✭ 24 (-71.08%)
Mutual labels:  gopherjs
Vecty
Vecty lets you build responsive and dynamic web frontends in Go using WebAssembly, competing with modern web frameworks like React & VueJS.
Stars: ✭ 2,161 (+2503.61%)
Mutual labels:  gopherjs
grpcweb-boilerplate
A minimal repo containing all the boilerplate for getting started with GopherJS using gRPC-Web
Stars: ✭ 45 (-45.78%)
Mutual labels:  gopherjs
Userpages
my blog for sharing (source code for siongui.github.io)
Stars: ✭ 64 (-22.89%)
Mutual labels:  gopherjs
Gopherjs Electron
Gopherjs bindings for Electron with an API translator.
Stars: ✭ 26 (-68.67%)
Mutual labels:  gopherjs
golang-101
🍺 In-depth internals, my personal notes, example codes and projects. Includes - Thousands of codes, OOP, Concurrency, Parallelism, Goroutines, Mutexes & Wait Groups, Testing in Go, Go tool chain, Backend web development, Some projects including Log file parser using bufio.Scanner, Spam Masker, Retro led clock, Console animations, Dictionary pro…
Stars: ✭ 61 (-26.51%)
Mutual labels:  gopherjs

vdom

GoDoc

vdom is a virtual dom implementation written in go which is compatible with gopherjs and inspired by react.js. The primary purpose of vdom is to improve the performance of view rendering in humble, a framework that lets you write frontend web apps in pure go and compile them to js to be run in the browser. However, vdom is framework agnostic, and generally will work whenever you can render html for your views as a slice of bytes.

Progress

vdom is now very close to being feature-complete, and it is pretty rigourously tested. Version 0.0.1 will be released soon. All that's left to do is benchmark and, if necessary, improve performance. Ad hoc testing suggests that it might currently be slower than setInnerHTML in at least some cases. I plan to fix this if possible.

Browser Compatibility

vdom has been tested and works with IE9+ and the latest versions of Chrome, Safari, and Firefox.

Javascript code generated with gopherjs uses typed arrays, so in order to work with IE9, you will need a polyfill. There is one in karma/js/support/polyfill/typedarray.js which is used for the karma tests.

Installing

Assuming you have already installed go and set up your go workspace, you can install vdom like you would any other package:

go get github.com/albrow/vdom

Import in your go code:

import "github.com/albrow/vdom"

Install the latest version of gopherjs, which compiles your go code to javascript:

go get -u github.com/gopherjs/gopherjs

When you are ready, compile your go code to javascript using the gopherjs command line tool. Then include the resulting js file in your application.

Quickstart Guide

I'll update this section when all functionality is completed. For now, here's a preview of what usage will probably look like.

Assuming you have a go html template called todo.tmpl:

<li class="todo-list-item {{ if .Completed }} completed {{ end }}">
	<input class="toggle" type="checkbox" {{ if .Completed }} checked {{ end }}>
	<label class="todo-label">{{ .Title }}</label>
	<button class="destroy"></button>
	<input class="edit" onfocus="this.value = this.value;" value="{{ .Title }}">
</li>

And a Todo view/model type that looks like this:

type Todo struct {
	Title     string
	Completed bool
	Root    dom.Element
	tree      *vdom.Tree
}

You could do the following:

var todoTmpl = template.Must(template.ParseFiles("todo.tmpl"))

func (todo *Todo) Render() error {
	// Execute the template with the given todo and write to a buffer
	buf := bytes.NewBuffer([]byte{})
	if err := tmpl.Execute(buf, todo); err != nil {
		return err
	}
	// Parse the resulting html into a virtual tree
	newTree, err := vdom.Parse(buf.Bytes())
	if err != nil {
		return err
	}
	// Calculate the diff between this render and the last render
	patches, err := vdom.Diff(todo.tree, newTree)
	if err != nil {
		return err
	}
	// Effeciently apply changes to the actual DOM
	if err := patches.Patch(todo.Root); err != nil {
		return err
	}
	// Remember the virtual DOM state for the next render to diff against
	todo.tree = newTree
}

Testing

vdom uses three sets of tests. If you're on a unix system, you can run all the tests in one go with scripts/test.sh. The script also compiles the go files to javsacript each time it runs. You will still need to install the dependencies for the script to work correctly.

Go Tests

Traditional go tests can be run with go test .. These tests are for code which does not interact with the DOM or depend on js-specific features.

Gopherjs Tests

You can run gopherjs test github.com/albrow/vdom to compile the same tests from above to javascript and tests them with node.js. This will also test some code which might depend on js-specific features (but not the DOM) and can't be tested with pure go. You will need to install node.js to run these tests.

Karma Tests

vdom uses karma and the jasmine test framework to test code that interacts with the DOM in real browsers. You will need to install these dependencies:

Don't forget to also install the karma command line tools with npm install -g karma-cli.

You will also need to install a launcher for each browser you want to test with, as well as the browsers themselves. Typically you install a karma launcher with npm install -g karma-chrome-launcher. You can edit the config files karma/test-mac.conf.js and karma/test-windows.conf.js if you want to change the browsers that are tested on. The Mac OS config specifies Chrome, Firefox, and Safari, and the Windows config specifies IE9-11, Chrome, and Firefox. You only need to install IE11, since the older versions can be tested via emulation.

Once you have installed all the dependencies, start karma with karma start karma/test-mac.conf.js or karma start karma/test-windows.conf.js depending on your operating system. If you are using a unix machine, simply copy one of the config files and edit the browsers section as needed. Once karma is running, you can keep it running in between tests.

Next you need to compile the test.go file to javascript so it can run in the browsers:

gopherjs build karma/go/test.go -o karma/js/test.js

Finally run the tests:

karma run

Benchmarking

vdom uses three sets of benchmarks. If you're on a unix system, you can run all the benchmarks in one go with scripts/bench.sh. The script also compiles the go files to javsacript each time it runs. You will still need to install the dependencies for the script to work correctly.

NOTE: There are some additional dependencies for benchmarking that are not needed for testing.

Go Benchmarks

Traditional go benchmarks can be run with go test -bench . -run none. I don't expect you to be using vdom in a pure go context (but there's nothing stopping you from doing so!), so these tests mainly serve as a comparison to the gopherjs benchmarks. It also helps with catching obvious performance problems early.

Gopherjs Benchmarks

To compile the library to javascript and benchmark it with node.js, you can run gopherjs test github.com/albrow/vdom --bench=. --run=none. These benchmarks are only for code that doesn't interact directly with the DOM. You will need to install node.js to run these benchmarks.

Karma Benchmarks

vdom uses karma and benchmark.js to test code that interacts with the DOM in real browsers. You will need to install these dependencies:

Don't forget to also install the karma command line tools with npm install -g karma-cli.

Just like with the tests, you will need to install a launcher for each browser you want to test with.

Once you have installed all the dependencies, start karma with karma start karma/bench-mac.conf.js or karma start karma/bench-windows.conf.js depending on your operating system. We have to use different config files because of a limitation of karma-benchmark. You will probably want to kill karma and restart it if you were running it with the test configuration. If you are using a unix machine, simply copy one of the config files and edit the browsers section as needed. Once karma is running, you can keep it running in between benchmarks.

Next you need to compile the bench.go file to javascript so it can run in the browsers:

gopherjs build karma/go/bench.go -o karma/js/bench.js

Finally run the benchmarks:

karma run
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].