Skip to content

Commit d21cd89

Browse files
authored
Improve docs (#51)
* fix typos * fix typos in ts * Update README.md typos readme * add instructions for hook api * fix context doc * improve docs * improve component doc
1 parent ac9dda3 commit d21cd89

File tree

11 files changed

+57
-28
lines changed

11 files changed

+57
-28
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,9 @@ See the [chat example](https://github.com/jfyne/live-examples/tree/main/chat) fo
335335

336336
### Hooks
337337

338-
Hooks take the following form. They allow additional javscript to be during a
339-
page lifecycle.
338+
Hooks take the following form. They allow additional javascript to hook into the live lifecycle.
339+
These should be used to implement custom behavior and bind additional events which are not supported
340+
out of the box.
340341

341342
[embedmd]:# (web/src/interop.ts)
342343
```ts
@@ -422,10 +423,25 @@ See the [chat example](https://github.com/jfyne/live-examples/tree/main/chat) fo
422423

423424
### Integrating with your app
424425

425-
There are two ways to inegrate javascript into your applications. The first is the simplest, using the built
426+
There are two ways to integrate javascript into your applications. The first is the simplest, using the built
426427
in javascript handler. This includes client side code to initialise the live handler and automatically looks for
427428
hooks at `window.Hooks`. All of the examples use this method.
428429

430+
To add a custom hook register it before including the `live.js` file.
431+
```javascript
432+
window.Hooks = window.Hooks || {};
433+
window.Hooks['my-hook'] = {
434+
mount: function() {
435+
// ...
436+
}
437+
};
438+
```
439+
440+
Use the `live-hook` attribute to wire the hook with live.
441+
```html
442+
<div live-hook="my-hook"></div>
443+
```
444+
429445
See the [chat example](https://github.com/jfyne/live-examples/tree/main/chat) for usage.
430446

431447
The second method is suited for more complex apps, there is a companion package published on npm. The version
@@ -520,7 +536,7 @@ selects file(s), the file metadata can be validated with a helper function.
520536
Reactive entries - Uploads are populated in the `.Uploads` template context. Entries automatically respond
521537
to progress and errors.
522538

523-
### Entry validataion
539+
### Entry validation
524540

525541
File selection triggers the usual form change event and there is a helper function to validate the uploads.
526542
Use `live.ValidateUploads` to validate the incoming files. Any validation errors will be available in the `.Uploads`

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func contextWithWriter(ctx context.Context, w http.ResponseWriter) context.Conte
3232
return context.WithValue(ctx, writerKey, w)
3333
}
3434

35-
// Request pulls out an initiating request from a context.
35+
// Writer pulls out a response writer from a context.
3636
func Writer(ctx context.Context) http.ResponseWriter {
3737
data := ctx.Value(writerKey)
3838
w, ok := data.(http.ResponseWriter)

engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (e *BaseEngine) sockets() []Socket {
260260
}
261261

262262
// hasSocket check a socket is there error if it isn't connected or
263-
// doensn't exist.
263+
// doesn't exist.
264264
func (e *BaseEngine) hasSocket(s Socket) error {
265265
e.socketsMu.Lock()
266266
defer e.socketsMu.Unlock()

event.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
type EventConfig func(e *Event) error
99

1010
const (
11-
// EventError indicates an error has occured.
11+
// EventError indicates an error has occurred.
1212
EventError = "err"
1313
// EventPatch a patch event containing a diff.
1414
EventPatch = "patch"
15-
// EventAck sent when an event is ackknowledged.
15+
// EventAck sent when an event is acknowledged.
1616
EventAck = "ack"
1717
// EventConnect sent as soon as the server accepts the
1818
// WS connection.

http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (h *HttpEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5656
}
5757
}
5858

59-
// Check if we are going to upgrade to a webscoket.
59+
// Check if we are going to upgrade to a websocket.
6060
upgrade := false
6161
for _, header := range r.Header["Upgrade"] {
6262
if header == "websocket" {
@@ -77,7 +77,7 @@ func (h *HttpEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7777
return
7878
}
7979

80-
// Upgrade to the webscoket version.
80+
// Upgrade to the websocket version.
8181
h.serveWS(ctx, w, r)
8282
}
8383

page/component.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ type SelfHandler func(ctx context.Context, data interface{}) (interface{}, error
3030
// ComponentConstructor a func for creating a new component.
3131
type ComponentConstructor func(ctx context.Context, h live.Handler, s live.Socket) (*Component, error)
3232

33-
// Component a self contained component on the page.
33+
// Component is a self contained component on the page. Components can be reused accross the application
34+
// or used to compose complex interfaces by splitting events handlers and render logic into
35+
// smaller pieces.
36+
//
37+
// Remember to use a unique ID and use the Event function which scopes the event-name
38+
// to trigger the event in the right component.
3439
type Component struct {
3540
// ID identifies the component on the page. This should be something stable, so that during the mount
3641
// it can be found again by the socket.
42+
// When reusing the same component this ID should be unique to avoid conflicts.
3743
ID string
3844

3945
// Handler a reference to the host handler.

page/configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func WithRegister(fn RegisterHandler) ComponentConfig {
2020
}
2121
}
2222

23-
// WithMount set a mounnt handler on the component.
23+
// WithMount set a mount handler on the component.
2424
func WithMount(fn MountHandler) ComponentConfig {
2525
return func(c *Component) error {
2626
c.Mount = fn

pubsub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ func (p *PubSub) Subscribe(topic string, h Engine) {
5454
})
5555
}
5656

57-
// Receice a message from the transport.
57+
// Recieve a message from the transport.
5858
func (p *PubSub) Recieve(topic string, msg Event) {
5959
ctx := context.Background()
6060
for _, node := range p.handlers[topic] {
6161
node.self(ctx, nil, msg)
6262
}
6363
}
6464

65-
// TransportMessage a userful container to send live events.
65+
// TransportMessage a useful container to send live events.
6666
type TransportMessage struct {
6767
Topic string
6868
Msg Event

socket.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Socket interface {
2828
// socket.
2929
Assigns() interface{}
3030
// Assign set data to this socket. This will happen automatically
31-
// if you return data from and `EventHander`.
31+
// if you return data from an `EventHander`.
3232
Assign(data interface{})
3333
// Connected returns true if this socket is connected via the websocket.
3434
Connected() bool
@@ -96,7 +96,7 @@ func NewBaseSocket(s Session, e Engine, connected bool) *BaseSocket {
9696
}
9797
}
9898

99-
// ID generate a unique ID for this socket.
99+
// ID generates a unique ID for this socket.
100100
func (s *BaseSocket) ID() SocketID {
101101
if s.id == "" {
102102
s.id = SocketID(NewID())
@@ -112,8 +112,8 @@ func (s *BaseSocket) Assigns() interface{} {
112112
return s.data
113113
}
114114

115-
// Assign set data to this socket. This will happen automatically
116-
// if you return data from and `EventHander`.
115+
// Assign sets data to this socket. This will happen automatically
116+
// if you return data from an `EventHander`.
117117
func (s *BaseSocket) Assign(data interface{}) {
118118
s.dataMu.Lock()
119119
defer s.dataMu.Unlock()
@@ -125,15 +125,15 @@ func (s *BaseSocket) Connected() bool {
125125
return s.connected
126126
}
127127

128-
// Self send an event to this socket itself. Will be handled in the
128+
// Self sends an event to this socket itself. Will be handled in the
129129
// handlers HandleSelf function.
130130
func (s *BaseSocket) Self(ctx context.Context, event string, data interface{}) error {
131131
msg := Event{T: event, SelfData: data}
132132
s.engine.self(ctx, s, msg)
133133
return nil
134134
}
135135

136-
// Broadcast send an event to all sockets on this same engine.
136+
// Broadcast sends an event to all sockets on this same engine.
137137
func (s *BaseSocket) Broadcast(event string, data interface{}) error {
138138
return s.engine.Broadcast(event, data)
139139
}
@@ -175,17 +175,17 @@ func (s *BaseSocket) AllowUploads(config *UploadConfig) {
175175
s.uploadConfigs = append(s.uploadConfigs, config)
176176
}
177177

178-
// UploadConfigs return the configs for this socket.
178+
// UploadConfigs returns the configs for this socket.
179179
func (s *BaseSocket) UploadConfigs() []*UploadConfig {
180180
return s.uploadConfigs
181181
}
182182

183-
// Uploads return the sockets uploads.
183+
// Uploads returns the sockets uploads.
184184
func (s *BaseSocket) Uploads() UploadContext {
185185
return s.uploads
186186
}
187187

188-
// AssignUpload set uploads to this socket.
188+
// AssignUpload sets uploads to this socket.
189189
func (s *BaseSocket) AssignUpload(config string, upload *Upload) {
190190
if s.uploads == nil {
191191
s.uploads = map[string][]*Upload{}
@@ -202,12 +202,12 @@ func (s *BaseSocket) AssignUpload(config string, upload *Upload) {
202202
s.uploads[config] = append(s.uploads[config], upload)
203203
}
204204

205-
// ClearUploads clear this sockets upload map.
205+
// ClearUploads clears this sockets upload map.
206206
func (s *BaseSocket) ClearUploads() {
207207
s.uploads = map[string][]*Upload{}
208208
}
209209

210-
// ClearUpload clear a specific upload from this socket.
210+
// ClearUpload clears a specific upload from this socket.
211211
func (s *BaseSocket) ClearUpload(config string, upload *Upload) {
212212
if s.uploads == nil {
213213
s.uploads = map[string][]*Upload{}
@@ -223,15 +223,22 @@ func (s *BaseSocket) ClearUpload(config string, upload *Upload) {
223223
}
224224
}
225225

226+
// LastRender returns the last render result of this socket.
226227
func (s *BaseSocket) LatestRender() *html.Node {
227228
return s.currentRender
228229
}
230+
231+
// UpdateRender replaces the last render result of this socket.
229232
func (s *BaseSocket) UpdateRender(render *html.Node) {
230233
s.currentRender = render
231234
}
235+
236+
// Session returns the session of this socket.
232237
func (s *BaseSocket) Session() Session {
233238
return s.session
234239
}
240+
241+
// Messages returns a channel of event messages sent and received by this socket.
235242
func (s *BaseSocket) Messages() chan Event {
236243
return s.msgs
237244
}

web/src/forms.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ export class Forms {
118118
values[this.upKey][key].push(fi);
119119
break;
120120
default:
121-
// If the key doesn exist set it.
121+
// If the key doesn't exist set it.
122122
if (!Reflect.has(values, key)) {
123123
values[key] = value;
124124
return;
125125
}
126126
// If it already exists that means this needs to become
127-
// and array.
127+
// an array.
128128
if (!Array.isArray(values[key])) {
129129
values[key] = [values[key]];
130130
}

web/src/interop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface Hook {
5454
}
5555

5656
/**
57-
* The DOM management interace. This allows external JS libraries to
57+
* The DOM management interface. This allows external JS libraries to
5858
* interop with Live.
5959
*/
6060
export interface DOM {

0 commit comments

Comments
 (0)