Skip to content

Commit

Permalink
Merge pull request #113 from brianhuster/dev
Browse files Browse the repository at this point in the history
Fix bug
  • Loading branch information
brianhuster authored Oct 11, 2024
2 parents 14961e4 + 1cfd16e commit 392b114
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require("lazy").setup({
'brianhuster/live-preview.nvim',
dependencies = {'brianhuster/autosave.nvim'}, -- Not required, but recomended for autosaving
opts = {},
}
}
})
```

Expand Down
13 changes: 8 additions & 5 deletions doc/livepreview.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Server class for live-preview.nvim To call this class, do >lua
• {watch_dir} (`fun(self: Server, func: function)`) Watch a directory
for changes and send a message "reload" to a WebSocket
client
{start} (`fun(self: Server, ip: string, port: number)`) Start the
server
{start} (`fun(self: Server, ip: string, port: number, func: function, client: uv_tcp_t)`)
Start the server
{stop} (`fun(self: Server)`) Stop the server


Expand All @@ -68,12 +68,15 @@ Server:routes({path}) *livepreview.server.Server:routes()*
Return: ~
(`string`) path to the file

Server:start({ip}, {port}) *livepreview.server.Server:start()*
*livepreview.server.Server:start()*
Server:start({ip}, {port}, {func}, {client})
Start the server

Parameters: ~
{ip} (`string`) IP address to bind to
{port} (`number`) port to bind to
{ip} (`string`) IP address to bind to
{port} (`number`) port to bind to
{func} (`function`)
{client} (`uv_tcp_t`) uv_tcp client

Server:stop() *livepreview.server.Server:stop()*
Stop the server
Expand Down
9 changes: 8 additions & 1 deletion lua/livepreview/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ function M.preview_file(filepath, port)
M.utils.kill_port(port)
server = M.server.Server:new(vim.fs.dirname(filepath))
vim.wait(50, function()
server:start("127.0.0.1", port)
server:start("127.0.0.1", port, function(client)
if M.utils.supported_filetype(filepath) == 'html' then
M.server.websocket.send_json(client, { type = "reload" })
else
local content = M.utils.uv_read_file(filepath)
M.server.websocket.send_json(client, { type = "update", content = content })
end
end)
end)
end

Expand Down
12 changes: 5 additions & 7 deletions lua/livepreview/server/Server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ function Server:routes(path)
file_path = vim.fs.joinpath(self.webroot, path)
end


return file_path
end

Expand All @@ -96,7 +95,9 @@ end
--- Start the server
--- @param ip string: IP address to bind to
--- @param port number: port to bind to
function Server:start(ip, port)
--- @param func function(client)|nil: function to call when when there is a change in the watched directory
--- @param client uv_tcp_t: uv_tcp client
function Server:start(ip, port, func)
self.server:bind(ip, port)
self.server:listen(128, function(err)
if err then
Expand Down Expand Up @@ -124,11 +125,8 @@ function Server:start(ip, port)
end)
ws_client = client
self:watch_dir(function()
if supported_filetype(filepath) == 'html' then
websocket.send_json(client, { type = "reload" })
else
local content = utils.uv_read_file(filepath)
websocket.send_json(client, { type = "update", content = content })
if func then
func(client)
end
end)
end)
Expand Down
1 change: 1 addition & 0 deletions lua/livepreview/server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ local M = {}
M.handler = require('livepreview.server.handler')
M.utils = require('livepreview.server.utils')
M.Server = require('livepreview.server.Server')
M.websocket = require('livepreview.server.websocket')
return M
1 change: 0 additions & 1 deletion static/ws-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async function connectWebSocket() {
};

socket.onmessage = (event) => {
console.log("Message received: ", event.data);
const message = JSON.parse(event.data);

if (message.type === "reload") {
Expand Down

0 comments on commit 392b114

Please sign in to comment.