Skip to content

Commit 8b262be

Browse files
Introduce polling mode!
1 parent f980f05 commit 8b262be

File tree

5 files changed

+90
-21
lines changed

5 files changed

+90
-21
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Debugbar
2+
class PollingController < ApplicationController
3+
skip_before_action :verify_authenticity_token, only: [:confirm]
4+
before_action :cors_set_access_control_headers
5+
6+
def poll
7+
render json: RequestBuffer.all.map(&:to_h)
8+
end
9+
10+
def confirm
11+
RequestBuffer.remove params[:ids]
12+
head :ok
13+
end
14+
15+
private
16+
17+
def cors_set_access_control_headers
18+
response.headers['Access-Control-Allow-Origin'] = '*'
19+
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
20+
response.headers['Access-Control-Allow-Methods'] = 'POST, GET'
21+
end
22+
end
23+
end

client/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ <h2 class="text-2xl font-bold tracking-tight text-gray-900">Everything you need
6868

6969
<div id="__debugbar"></div>
7070
<script type="text/javascript">
71-
window._debugbarConfigOptions = {
72-
height: 600,
73-
}
71+
window._debugbarConfigOptions = {
72+
mode: "poll",
73+
height: 600,
74+
// poll: {
75+
// interval: 1000,
76+
// },
77+
}
7478
</script>
7579
<script type="module" src="/src/dev.ts"></script>
7680

client/src/Debugbar.vue

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ const routeAlias = computed(() => {
4141
return requestsStore.currentRequest?.meta.params.controller + "#" + requestsStore.currentRequest?.meta.params.action
4242
})
4343
44-
let debugbarChannel
44+
let debugbarChannel = null
4545
46-
if (configStore.config.mode == "ws") {
46+
if (configStore.config.mode === "ws") {
4747
debugbarChannel = createConsumer(configStore.config.actionCableUrl).subscriptions.create(
48-
{ channel: configStore.config.channelName },
48+
{ channel: configStore.config.cable.channelName },
4949
{
5050
connected() {
5151
console.log("🟢 Connected to channel")
@@ -72,21 +72,45 @@ if (configStore.config.mode == "ws") {
7272
},
7373
}
7474
)
75+
} else if (configStore.config.mode === "poll") {
76+
console.log(
77+
`Using debugbar in "polling mode". Consider using "ws" mode for better performance (requires ActionCable).`
78+
)
79+
setInterval(() => {
80+
fetch(configStore.config.pollUrl)
81+
.then((response) => response.json())
82+
.then((data) => {
83+
if (data.length == 0) {
84+
return
85+
}
86+
87+
console.log(data)
88+
89+
const ids = requestsStore.addRequests(data)
90+
91+
if (!isActive.value) {
92+
requestsStore.setCurrentRequestById(ids[ids.length - 1])
93+
}
94+
95+
fetch(configStore.config.pollUrl + "/confirm", {
96+
// mode: "no-cors",
97+
method: "POST",
98+
headers: {
99+
"Content-Type": "application/json",
100+
},
101+
body: JSON.stringify({ ids: ids }),
102+
})
103+
})
104+
}, configStore.config.poll.interval)
75105
} else {
76-
console.log(`Using debugbar in "offline mode", ideal for demoing with fixtures.`)
77-
debugbarChannel = {
78-
send: (data) => {
79-
// No-op
80-
console.log("Ignoring `send` call", data)
81-
},
82-
}
106+
console.log(`Using debugbar in "offline mode", ideal for demos using fixtures.`)
83107
}
84108
85109
const clearRequests = () => {
86110
console.log("Clearing requests")
87111
state.activeTab = ""
88112
requestsStore.clearRequests()
89-
debugbarChannel.send({ clear: true })
113+
debugbarChannel?.send({ clear: true })
90114
}
91115
92116
// Resizing the debugbar

client/src/models/Config.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
import defaultsDeep from "lodash/defaultsDeep"
22

33
export type DebugbarConfigOptions = {
4-
mode: "ws" | "off"
4+
mode: "ws" | "poll" | "off"
5+
prefix: string
56
cable: {
67
url: string
7-
prefix: string
8+
channelName: string
9+
}
10+
poll: {
11+
url: string
12+
interval: number
813
}
9-
channelName: string
1014
height: number
1115
}
1216

17+
export type DebugbarConfig = DebugbarConfigOptions & {
18+
actionCableUrl: string
19+
pollUrl: string
20+
}
21+
1322
export function newDebugbarConfig(options: DebugbarConfigOptions) {
14-
const obj = defaultsDeep(options, {
23+
const obj: DebugbarConfig = defaultsDeep(options, {
1524
mode: "ws",
25+
prefix: "/_debugbar",
1626
cable: {
1727
url: "ws://127.0.0.1:3000",
18-
prefix: "/_debugbar",
28+
channelName: "Debugbar::DebugbarChannel",
29+
},
30+
poll: {
31+
url: "http://127.0.0.1:3000",
32+
interval: 500,
1933
},
20-
channelName: "Debugbar::DebugbarChannel",
2134
height: 360,
2235
} as DebugbarConfigOptions)
2336

24-
obj.actionCableUrl = `${obj.cable.url}${obj.cable.prefix}/cable`
37+
obj.actionCableUrl = `${obj.cable.url}${obj.prefix}/cable`
38+
obj.pollUrl = `${obj.poll.url}${obj.prefix}/poll`
2539

2640
return obj
2741
}

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Debugbar::Engine.routes.draw do
22
mount ActionCable.server => '/cable'
33

4+
get 'poll' => "polling#poll"
5+
options 'poll/confirm' => "polling#confirm"
6+
post 'poll/confirm' => "polling#confirm"
7+
48
# TODO: Silence logs for this route if `::Rails.application.config.assets.quiet` is true
59
get 'assets/script' => "assets#js"
610
end

0 commit comments

Comments
 (0)