Skip to content

Commit 08e919d

Browse files
committed
Refactor based on feedback
1 parent fcd3c2c commit 08e919d

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ config :my_app, MyAppWeb.Endpoint,
3737
]
3838
```
3939

40-
Next, you'll need to listen for the `"phx:live_reload:loaded"` event and enable client logging by calling the reloader's `enableServerLogs()` function, for example:
40+
Next, you'll need to listen for the `"phx:live_reload:connected"` event and enable client logging by calling the reloader's `enableServerLogs()` function, for example:
4141

4242
```javascript
43-
window.addEventListener("phx:live_reload:loaded", ({detail: {reloader}}) => {
43+
window.addEventListener("phx:live_reload:connected", ({detail: reloader}) => {
4444
// enable server log streaming to client.
4545
// disable with reloader.disableServerLogs()
4646
reloader.enableServerLogs()
@@ -61,10 +61,10 @@ config :my_app, MyAppWeb.Endpoint,
6161
]
6262
```
6363

64-
We passed `vscode://` protocol URL to open vscode with placeholders of `__FILE__:__LINE__`, which will be substited at runtime. Check your editor's documentation on protocol URL support. To open your configured editor URL when an element is clicked, say with alt-click, you can wire up an event listener within your `"phx:live_reload:loaded"` callback and make use of the reloader's `openEditor` function, passing the event target as the DOM node to reference for HEEx file:line annotation information. For example:
64+
We passed `vscode://` protocol URL to open vscode with placeholders of `__FILE__:__LINE__`, which will be substited at runtime. Check your editor's documentation on protocol URL support. To open your configured editor URL when an element is clicked, say with alt-click, you can wire up an event listener within your `"phx:live_reload:connected"` callback and make use of the reloader's `openEditor` function, passing the event target as the DOM node to reference for HEEx file:line annotation information. For example:
6565

6666
```javascript
67-
window.addEventListener("phx:live_reload:loaded", ({detail: {reloader}}) => {
67+
window.addEventListener("phx:live_reload:connected", ({detail: reloader}) => {
6868
// enable server log streaming to client.
6969
reloader.enableServerLogs() // disable with reloader.disableServerLogs()
7070
// open configured ELIXIR_EDITOR_URL at file:line of the alt-clicked element's HEEx component

priv/static/phoenix_live_reload.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,23 @@ let reloadStrategies = {
4949

5050
class LiveReloader {
5151
constructor(socket){
52+
this.socket = socket
5253
this.logsEnabled = false
5354
this.enabledOnce = false
5455
this.editorURL = null
5556
this.relativePath = null
5657
}
5758
enable(){
58-
if(!this.enabledOnce){
59-
if(parent.document.readyState === "loaded"){
60-
this.dispatchReady()
59+
this.socket.onOpen(() => {
60+
if(this.enabledOnce){ return }
61+
this.enabledOnce = true
62+
if(["complete", "loaded", "interactive"].indexOf(parent.document.readyState) >= 0){
63+
this.dispatchConnected()
6164
} else {
62-
parent.addEventListener("load", () => this.dispatchReady())
65+
parent.addEventListener("load", () => this.dispatchConnected())
6366
}
64-
}
65-
this.enabledOnce = true
67+
})
68+
6669
this.channel = socket.channel("phoenix:live_reload", {})
6770
this.channel.on("assets_change", msg => {
6871
let reloadStrategy = reloadStrategies[msg.asset_type] || reloadStrategies.page
@@ -73,7 +76,7 @@ class LiveReloader {
7376
this.editorURL = editor_url
7477
this.relativePath = relative_path
7578
})
76-
socket.connect()
79+
this.socket.connect()
7780
}
7881

7982
disable(){
@@ -96,8 +99,8 @@ class LiveReloader {
9699

97100
// private
98101

99-
dispatchReady(){
100-
parent.dispatchEvent(new CustomEvent("phx:live_reload:loaded", {detail: {reloader: this}}))
102+
dispatchConnected(){
103+
parent.dispatchEvent(new CustomEvent("phx:live_reload:connected", {detail: this}))
101104
}
102105

103106
log(level, str){

0 commit comments

Comments
 (0)