<|start_filename|>dispatch/dispatcher.go<|end_filename|>
package dispatch

import (
	"github.com/go-humble/detect"
	"github.com/influx6/gu/dispatch/mque"
)

//==============================================================================

// history provides a central URL coordinator for gu.
var history *HistoryProvider

// dispatch provides a default dispatcher for listening to events.
var dispatch = mque.New()

// Subscribe adds a new listener to the dispatcher.
func Subscribe(q interface{}, end ...func()) mque.End {
	return dispatch.Q(q, end...)
}

// Dispatch emits a event into the dispatch callback listeners.
func Dispatch(q interface{}) {
	dispatch.Run(q)
}

//==============================================================================

// init intializes the internal state management variables used in dispatch.
func init() {
	if detect.IsBrowser() {
		history = History(HashSequencer)

		// Initiate to the current path.
		history.Follow(GetLocation())
	}
}


<|start_filename|>dispatch/resolver_test.go<|end_filename|>
package dispatch_test

import (
	"testing"

	"github.com/influx6/gu/dispatch"
	"github.com/influx6/gu/tests"
)

func TestResolver(t *testing.T) {
	rx := dispatch.NewResolver("/:id")
	params, _, state := rx.Test("12")

	if !state {
		tests.Failed(t, "Should have matched giving path")
	}
	tests.Passed(t, "Should have matched giving path")

	val, ok := params["id"]
	if !ok {
		tests.Failed(t, "Should have retrieve parameter :id => %s", val)
	}
	tests.Passed(t, "Should have retrieve parameter :id => %s", val)

	rx.ResolvedPassed(func(px dispatch.Path) {
		tests.Passed(t, "Should have notified with Path %#v", px)
	})

	rx.ResolvedFailed(func(px dispatch.Path) {
		tests.Failed(t, "Should have notified with Path %#v", px)
	})

	rx.Resolve(dispatch.UseLocation("/12"))
}

func TestResolverLevels(t *testing.T) {
	home := dispatch.NewResolver("/home/*")
	rx := dispatch.NewResolver("/:id")

	home.Register(rx)

	rx.ResolvedPassed(func(px dispatch.Path) {
		tests.Passed(t, "Should have notified with Path %#v", px)
	})

	rx.ResolvedFailed(func(px dispatch.Path) {
		tests.Failed(t, "Should have notified with Path %#v", px)
	})

	home.Resolve(dispatch.UseLocation("home/12"))
}

func TestResolverFailed(t *testing.T) {
	rx := dispatch.NewResolver("/:id")
	rx.ResolvedPassed(func(px dispatch.Path) {
		tests.Failed(t, "Should have notified with failed Path %#v", px)
	})

	rx.ResolvedFailed(func(px dispatch.Path) {
		tests.Passed(t, "Should have notified with failed Path %#v", px)
	})

	rx.Resolve(dispatch.UseLocation("/home/12"))
}


<|start_filename|>tests/routes/route_test.go<|end_filename|>
package routes

import (
	"testing"

	"github.com/influx6/gu"
	"github.com/influx6/gu/dispatch"
	"github.com/influx6/gu/tests"
)

func TestRoute(t *testing.T) {
	rm := gu.NewRouteManager()

	home := rm.L("/home/*")
	if _, _, pass := home.Test("/home/models/12"); !pass {
		tests.Failed(t, "Should have validated path /home/models/12")
	}
	tests.Passed(t, "Should have validated path /home/models/12")

	index := rm.L("/index/*")
	if _, _, pass := index.Test("/index"); !pass {
		tests.Failed(t, "Should have validated path /index")
	}
	tests.Passed(t, "Should have validated path /index")

	getModel := home.N("/models/*")
	if _, _, pass := getModel.Test("/models"); !pass {
		tests.Failed(t, "Should have validated path /models")
	}
	tests.Passed(t, "Should have validated path /models")

	if _, _, pass := getModel.Test("/models/12"); !pass {
		tests.Failed(t, "Should have validated path /models/12")
	}
	tests.Passed(t, "Should have validated path /models/12")

	id := getModel.N("/:id")
	param, _, pass := id.Test("/12")
	if !pass {
		tests.Failed(t, "Should have validated path /12")
	}
	tests.Passed(t, "Should have validated path /12: %#v", param)

	home.ResolvedPassed(func(px dispatch.Path) {
		tests.Passed(t, "Should have validated path /home/models/12:  /home")
	}).ResolvedFailed(func(px dispatch.Path) {
		tests.Failed(t, "Should have validated path /home/models/12:  /home")
	})

	getModel.ResolvedPassed(func(px dispatch.Path) {
		tests.Passed(t, "Should have validated path /home/models/12:  /models")
	}).ResolvedFailed(func(px dispatch.Path) {
		tests.Failed(t, "Should have validated path /home/models/12:  /models")
	})

	id.ResolvedPassed(func(px dispatch.Path) {
		tests.Passed(t, "Should have validated path /home/models/12:  /id")
	}).ResolvedFailed(func(px dispatch.Path) {
		tests.Failed(t, "Should have validated path /home/models/12:  /id")
	})

	home.Resolve(dispatch.UseLocation("/home/models/12"))
	home.Resolve(dispatch.UseLocationHash("http://thunderhouse.com/#home/models/12"))
}
