watchjs

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultIgnore = watch.DefaultIgnore

DefaultIgnore contains a list of files that you usually want to ignore. Such as temporary files, hidden files, log files and binaries.

View Source
var Script string

Script is reloading script for server.

Functions

func DisableCache added in v0.5.1

func DisableCache(w http.ResponseWriter)

DisableCache sets headers to disable response caching.

func FileToURL

func FileToURL(filename, basedir, urlprefix string) (url string, ok bool)

FileToURL converts filename in basedir to a url in urlprefix.

Types

type Action

type Action string

Action defines how browser reacts to a specific file changing.

const (
	// IgnoreChanges ignores the file change.
	IgnoreChanges Action = "ignore"
	// ReloadBrowser reloads the whole page.
	ReloadBrowser Action = "reload"
	// LiveInject deletes old reference and reinjects the code.
	LiveInject Action = "inject"
)

func DefaultOnChange

func DefaultOnChange(change watch.Change) (string, Action)

DefaultOnChange assumes that your folder structure matches your URL structure. It live injects css and reloads browser otherwise.

type Change

type Change struct {
	// Kind is one of "create", "modify", "delete"
	Kind string `json:"kind"`
	// Path rewriting TODO:
	Path string `json:"path"`
	// Modified returns the modified time of the file.
	Modified time.Time `json:"modified"`
	// Action is the action browser should take with this file.
	Action Action `json:"action"`

	// Package finds match based on `package("<pkgname>", function(){`
	Package string `json:"package"`
	// Depends finds text based on `depends("<pkgname>")`
	Depends []string `json:"depends"`
}

Change defines a list of changes.

type Changes

type Changes []Change

Changes is a list of Change.

type Config

type Config struct {
	// Interval defines how often to poll the disk.
	Interval time.Duration
	// Monitor these globs for changes.
	Monitor []string
	// Ignore these globs to avoid unnecessary updates.
	Ignore []string
	// Care only monitor files that match these globs.
	Care []string

	// URL where the watchjs server is serving on.
	// Code defaults to using the request.URL otherwise.
	URL string
	// ManualScriptSetup allows to disable automatic setup of js reloading script.
	ManualScriptSetup bool
	// ReconnectInterval defines how fast watchjs tries to reconnect after losing connection to the server.
	ReconnectInterval time.Duration

	// OnChange should return the URL path for a particular file and the reaction for javascript.
	OnChange func(change watch.Change) (path string, reaction Action)
}

Config is configures server for modifications.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub dispatches Message to multiple listeners.

func NewHub

func NewHub() *Hub

NewHub creates a new Hub.

func (*Hub) Dispatch

func (hub *Hub) Dispatch(message Message)

Dispatch message to all registered connections.

func (*Hub) Register

func (hub *Hub) Register(conn Listener)

Register adds connections to hub.

func (*Hub) Unregister

func (hub *Hub) Unregister(conn Listener)

Unregister removes connection from hub.

type Listener

type Listener interface {
	// Dispatch must not block
	Dispatch(Message)
}

Listener is a connection that cares about the changes.

type Message

type Message struct {
	Type string  `json:"type"`
	Data Changes `json:"data"`
}

Message is json message that is sent on changes.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server responds to regular requests with watchjs.Script and handles incoming websockets.

func NewServer

func NewServer(config Config) *Server

NewServer creates a new server using the specified config.

func (*Server) ReloadBrowser

func (server *Server) ReloadBrowser()

ReloadBrowser sends a reload message to all connected browsers.

func (*Server) ServeHTTP

func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP reponds to:

GET with watchjs.Script.
WebSocket Upgrade with serving update messages.

func (*Server) Stop

func (server *Server) Stop()

Stop stops changes monitoring.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL