Skip to content

Commit 9e084c0

Browse files
authored
Merge branch 'master' into minor-fixes-and-ci
2 parents f377381 + 5eab256 commit 9e084c0

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

examples/main.go

+28
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,34 @@ func main() {
66
userStartsChatting()
77
userBIsNotPartOfChat()
88
userBIsPartOfChat()
9+
userJoinsChat()
10+
}
11+
12+
func userJoinsChat() {
13+
d := sequence.NewDiagram("new_chat")
14+
userA := "userA (name could be quite longer)"
15+
userB := "userB"
16+
b := "backend"
17+
18+
d.SetTitle("User starts a chat with someone (doesn't matter if new or not)")
19+
20+
d.AddParticipants(userA, userB, b)
21+
22+
_ = d.AddDirectionalEdge(userA, b, `POST /inbox {"recipient": userID, "project": projectID, "projectType": challenge|solution}"`)
23+
_ = d.AddDirectionalEdge(b, userA, "Response Status Code: 202, Body: Inbox{id, secret, recipient, ...}")
24+
_ = d.AddDirectionalEdge(userA, b, "WS connect")
25+
_ = d.AddDirectionalEdge(userA, b, `"{"action": "JOIN", "data": {"inboxId": str, "userId": "str", "inboxSecret": str}}"`)
26+
_ = d.AddDirectionalEdge(b, b, "STORE_CONNECTION_ID(userA)")
27+
_ = d.AddDirectionalEdge(b, b, "sets unread inbox userA = 0")
28+
_ = d.AddDirectionalEdge(b, userA, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`)
29+
_ = d.AddDirectionalEdge(userA, b, `"{"action": "SEND_MESSAGE", "data": {"inboxId": str, "userId": "str", "inboxSecret": str, "content": "hi"}}"`)
30+
_ = d.AddDirectionalEdge(b, b, "check connectionID userB")
31+
_ = d.AddUndirectionalEdge(b, b, "if connected: ")
32+
_ = d.AddDirectionalEdge(b, userB, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`)
33+
_ = d.AddUndirectionalEdge(b, b, "else: ")
34+
_ = d.AddUndirectionalEdge(b, b, "increment unread inbox userB")
935

36+
d.Render()
1037
}
1138

1239
func userBIsPartOfChat() {
@@ -71,6 +98,7 @@ func userStartsChatting() {
7198
d.AddParticipants(backend)
7299
d.AddParticipants(db)
73100

101+
d.AddDirectionalEdge(client, backend, "GET /inbox/<TO_ID>")
74102
d.AddDirectionalEdge(client, backend, "PUT /chat/user/<TO_ID>")
75103
d.AddDirectionalEdge(backend, db, "checks or create inbox for user")
76104
d.AddDirectionalEdge(backend, db, "set all unread messages to read if existing")

sequence/diagram.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ func (d *Diagram) renderParticipants() {
7878
}
7979
}
8080
spacePerBlock := float64(d.dc.Width() / len(d.participants))
81-
startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding
82-
// startX := float64(participantsPadding + (len(d.renderedParticipants) * (participantBoxWidth + 1000/(len(d.participants)))))
83-
endX := startX + participantBoxWidth
84-
startY := 1000 * 0.1 // 10% from the top
81+
strWidth, strHeight := d.dc.MeasureString(p.Name)
82+
startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding - strWidth/2
83+
84+
endX := startX + participantBoxWidth + strWidth
85+
startY := height * 0.1 // 10% from the top
8586
endY := startY + participantBoxHeight
8687
// draw the border
8788
d.dc.SetColor(color.Black)
@@ -104,11 +105,10 @@ func (d *Diagram) renderParticipants() {
104105
d.dc.DrawRectangle(
105106
startX,
106107
startY,
107-
participantBoxWidth,
108+
participantBoxWidth+strWidth,
108109
participantBoxHeight,
109110
)
110111
d.dc.SetColor(color.Black)
111-
strWidth, strHeight := d.dc.MeasureString(p.Name)
112112
centerStrWidth := startX + ((endX - startX) / 2) - strWidth/2
113113
centerStrHeight := (endY-startY)/2 + startY + (strHeight / 2)
114114

@@ -141,11 +141,13 @@ func (d *Diagram) renderEdges() {
141141

142142
for idx := range d.edges {
143143
e := &d.edges[idx]
144+
fromStrWidth, _ := d.dc.MeasureString(e.from.Name)
145+
toStrWidth, _ := d.dc.MeasureString(e.to.Name)
144146
fromCords := d.participantsCoordMap[e.from.Name]
145147
toCords := d.participantsCoordMap[e.to.Name]
146-
startX := fromCords.X + participantBoxWidth/2 - 2.5 // 2.5 = half of stroke width
148+
startX := fromCords.X + participantBoxWidth/2 - 2.5 + fromStrWidth/2 // 2.5 = half of stroke width
147149
startY := fromCords.Y + participantBoxHeight + 2.5 + float64((1+renderedEdges)*verticalSpaceBetweenEdges)
148-
endX := toCords.X + participantBoxWidth/2 - 2.5
150+
endX := toCords.X + participantBoxWidth/2 - 2.5 + toStrWidth/2
149151
isReverseEdge := endX < startX
150152

151153
d.dc.SetDash(6)

0 commit comments

Comments
 (0)