Browse Source

a bit of renaming things

pull/8/head
Stephen Searles 2 years ago
parent
commit
da848ae87f
7 changed files with 28 additions and 29 deletions
  1. 2
    2
      caddyhugo.go
  2. 9
    10
      client.go
  3. 3
    3
      deltas.go
  4. 11
    11
      doc_test.go
  5. 1
    1
      http.go
  6. 1
    1
      setup.go
  7. 1
    1
      templates.go

+ 2
- 2
caddyhugo.go View File

@@ -41,7 +41,7 @@ type CaddyHugo struct {

Media *MediaSource

docs map[string]*docref
docs map[string]*editSession
mtx sync.Mutex

authorTmpl, adminTmpl, editTmpl *template.Template
@@ -86,7 +86,7 @@ func (ch *CaddyHugo) Publish() error {
}

// TmplData collects data for template execution
func (ch *CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} {
func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{} {
var doc *acedoc.Document
if docref != nil {
doc = docref.doc

+ 9
- 10
client.go View File

@@ -13,14 +13,14 @@ import (
"git.stephensearles.com/stephen/acedoc"
)

type docref struct {
type editSession struct {
clients uint
name string
doc *acedoc.Document
tmpdir string
}

func (ch *CaddyHugo) newClient(docName string) (*docref, error) {
func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
filename := ch.docFilename(docName)

fmt.Println("opening", filename)
@@ -32,7 +32,7 @@ func (ch *CaddyHugo) newClient(docName string) (*docref, error) {
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName)))
tmpdir := path.Join(os.TempDir(), draftPrefix)

ref := &docref{
ref := &editSession{
name: docName,
doc: acedoc.NewString(string(contents)),
tmpdir: tmpdir,
@@ -103,26 +103,25 @@ func (ch *CaddyHugo) newClient(docName string) (*docref, error) {
return ref, nil
}

func (ch *CaddyHugo) hasdocref(docName string) (*docref, bool) {
func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) {
dr, ok := ch.docs[ch.docFilename(docName)]
return dr, ok
}

func (ch *CaddyHugo) client(docName string) (*docref, error) {
func (ch *CaddyHugo) editSession(docName string) (*editSession, error) {
ch.mtx.Lock()
defer ch.mtx.Unlock()

var err error

dr, ok := ch.hasdocref(docName)
dr, ok := ch.hasEditSession(docName)
if !ok {
dr, err = ch.newClient(docName)
dr, err = ch.newEditSession(docName)
}

return dr, err
}

func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) {
name := r.URL.Path[len("/hugo/edit/"):]
return ch.client(name)
func docNameFromEditRequest(r *http.Request) string {
return r.URL.Path[len("/hugo/edit/"):]
}

+ 3
- 3
deltas.go View File

@@ -53,13 +53,13 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int

conn.SetReadDeadline(time.Time{})

doc, err := ch.doc(r)
doc, err := ch.editSession(docNameFromEditRequest(r))
if err != nil {
fmt.Println(err)
return http.StatusBadRequest, err
}

return ch.handleConn(conn, doc)
return ch.handleDeltaConn(conn, doc)
}

func (ch *CaddyHugo) Message(deltas ...acedoc.Delta) Message {
@@ -69,7 +69,7 @@ func (ch *CaddyHugo) Message(deltas ...acedoc.Delta) Message {
}
}

func (ch *CaddyHugo) handleConn(conn DeltaConn, doc *docref) (int, error) {
func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, error) {
const idlePing = 15 * time.Second
const idlePingShort = 1 * time.Millisecond


+ 11
- 11
doc_test.go View File

@@ -62,7 +62,7 @@ func TestEdits(t *testing.T) {
var mtx sync.Mutex
received := []acedoc.Delta{}

doc, err := w.CH.client(contentPath)
doc, err := w.CH.editSession(contentPath)
if err != nil {
t.Fatal("error creating document client:", err)
}
@@ -75,7 +75,7 @@ func TestEdits(t *testing.T) {
return nil
}))

_, ok := w.CH.hasdocref(contentPath)
_, ok := w.CH.hasEditSession(contentPath)
if !ok {
t.Fatal("expected there to be an established client")
}
@@ -158,12 +158,12 @@ func TestDeltasSingle(t *testing.T) {

client := new(WebsocketTester)

doc, err := w.CH.client("content/" + title + ".md")
doc, err := w.CH.editSession("content/" + title + ".md")
if err != nil {
t.Fatal("couldn't establish docref for client 0:", err)
}

go w.CH.handleConn(client, doc)
go w.CH.handleDeltaConn(client, doc)

a := acedoc.Insert(0, 0, "a")

@@ -199,13 +199,13 @@ func TestDeltasDouble(t *testing.T) {
clientA := new(WebsocketTester)
clientB := new(WebsocketTester)

doc, err := w.CH.client("content/" + title + ".md")
doc, err := w.CH.editSession("content/" + title + ".md")
if err != nil {
t.Fatal("couldn't establish docref for client 0:", err)
}

go w.CH.handleConn(clientA, doc)
go w.CH.handleConn(clientB, doc)
go w.CH.handleDeltaConn(clientA, doc)
go w.CH.handleDeltaConn(clientB, doc)

// send the first message, simulating the browser on clientA
clientA.ReceiveJSON(w.CH.Message(acedoc.Insert(0, 0, "a")))
@@ -266,14 +266,14 @@ func TestDeltasMulti(t *testing.T) {

clients := []*WebsocketTester{{}, {}, {}}

doc, err := w.CH.client("content/" + title + ".md")
doc, err := w.CH.editSession("content/" + title + ".md")
if err != nil {
t.Fatal("couldn't establish docref:", err)
}

go w.CH.handleConn(clients[0], doc)
go w.CH.handleConn(clients[1], doc)
go w.CH.handleConn(clients[2], doc)
go w.CH.handleDeltaConn(clients[0], doc)
go w.CH.handleDeltaConn(clients[1], doc)
go w.CH.handleDeltaConn(clients[2], doc)

a := acedoc.Insert(0, 0, "a")
b := acedoc.Insert(0, 0, "b")

+ 1
- 1
http.go View File

@@ -155,7 +155,7 @@ func (ch *CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler {
return ch.DeltaWebsocket(w, r)
}

doc, err := ch.doc(r)
doc, err := ch.editSession(docNameFromEditRequest(r))
if err != nil {
fmt.Println(err)
return http.StatusNotFound, err

+ 1
- 1
setup.go View File

@@ -25,7 +25,7 @@ func (ch *CaddyHugo) Setup(dir string) error {
var err error

ch.Dir = dir
ch.docs = make(map[string]*docref)
ch.docs = make(map[string]*editSession)

ch.HugoCfg = &deps.DepsCfg{}


+ 1
- 1
templates.go View File

@@ -67,7 +67,7 @@ type tmplData struct {
R *http.Request
*CaddyHugo
Doc *acedoc.Document
docref *docref
docref *editSession
}

func (t *tmplData) LoadContent() (string, error) {

Loading…
Cancel
Save