<commit_before>package main

import (
	"log"
	"net/http"
	"os"
	"time"

	"github.com/jpillora/velox"
)

type Foo struct {
	velox.State //adds sync state and an Update() method
	A, B        int
	C           []int
	D           string
	E           Bar
}

type Bar struct {
	X, Y int
}

func main() {
	//state we wish to sync
	foo := &Foo{A: 21, B: 42, D: "0"}
	go func() {
		i := 0
		for {
			//change foo
			foo.A++
			if i%2 == 0 {
				foo.B--
			}
			i++
			if i > 10 {
				foo.C = foo.C[1:]
			}
			foo.C = append(foo.C, i)
			if i%5 == 0 {
				foo.E.Y++
			}
			//push to observers
			foo.Push()
			//do other stuff...
			time.Sleep(250 * time.Millisecond)
		}
	}()
	//sync handlers
	http.Handle("/velox.js", velox.JS)
	http.Handle("/sync", velox.SyncHandler(foo))
	//index handler
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		w.Write(indexhtml)
	})
	//listen!
	port := os.Getenv("PORT")
	if port == "" {
		port = "7070"
	}
	log.Printf("Listening on :%s...", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}

var indexhtml = []byte(`
<pre id="out"></pre>
<script src="/velox.js"></script>
<script>
	var foo = {};
	var v = velox.sse("/sync", foo);
	v.onupdate = function() {
		out.innerHTML = JSON.stringify(foo, null, 2);
	};
</script>
`)

//NOTE: deltas are not sent in the example since the target object is too small
<commit_msg>added example code and link back to repo in example server<commit_after>package main

import (
	"log"
	"net/http"
	"os"
	"time"

	"github.com/jpillora/velox"
)

type Foo struct {
	velox.State //adds sync state and an Update() method
	A, B        int
	C           []int
	D           string
	E           Bar
}

type Bar struct {
	X, Y int
}

func main() {
	//state we wish to sync
	foo := &Foo{A: 21, B: 42, D: "0"}
	go func() {
		i := 0
		for {
			//change foo
			foo.A++
			if i%2 == 0 {
				foo.B--
			}
			i++
			if i > 10 {
				foo.C = foo.C[1:]
			}
			foo.C = append(foo.C, i)
			if i%5 == 0 {
				foo.E.Y++
			}
			//push to observers
			foo.Push()
			//do other stuff...
			time.Sleep(250 * time.Millisecond)
		}
	}()
	//sync handlers
	http.Handle("/velox.js", velox.JS)
	http.Handle("/sync", velox.SyncHandler(foo))
	//index handler
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		w.Write(indexhtml)
	})
	//listen!
	port := os.Getenv("PORT")
	if port == "" {
		port = "7070"
	}
	log.Printf("Listening on :%s...", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}

var indexhtml = []byte(`

<!-- documentation -->
<pre id="code">
&lt;pre id="example">&lt;/pre>
&lt;script src="/velox.js">&lt;/script>
&lt;script>
var foo = {};
var v = velox.sse("/sync", foo);
v.onupdate = function() {
	example.innerHTML = JSON.stringify(foo, null, 2);
};
&lt;/script>
</pre>
<a href="https://github.com/jpillora/velox"><img style="position: absolute; z-index: 2; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
<hr>

<!-- example -->
<pre id="example"></pre>
<script src="/velox.js"></script>
<script>
var foo = {};
var v = velox.sse("/sync", foo);
v.onupdate = function() {
	example.innerHTML = JSON.stringify(foo, null, 2);
};
</script>
`)

//NOTE: deltas are not sent in the example since the target object is too small

{'fraction_non_alphanumeric': 0.19640224122677677, 'fraction_numerical': 0.015924506045414333, 'mean_word_length': 2.7480662983425415, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4693163', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1338, 'n_words': 345}