Skip to content

Commit

Permalink
fix: reload and watcher
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Jan 25, 2024
1 parent aed0e81 commit efc5946
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
36 changes: 32 additions & 4 deletions contribs/gnodev/pkg/events/static/hotreload.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
(function() {
// Define the events that will trigger a page reload
var eventsReload = {{ .ReloadEvents | jsEventsArray }};

// Establish the WebSocket connection to the event server
var ws = new WebSocket('ws://{{- .Remote -}}');

// Flag to determine if the page is in the grace period after loading
var gracePeriod = true;
var graceTimeout = 1000; // ms

// Set a timer to end the grace period after `graceTimeout`
var debounceTimeout = setTimeout(function() {
gracePeriod = false;
}, graceTimeout);

// Handle incoming WebSocket messages
ws.onmessage = function(event) {
try {
var message = JSON.parse(event.data);
console.log('receiving message:', message);
if (eventsReload.includes(message.type)) {
console.log('Receiving message:', message);

// Ignore events not in the reload-triggering list
if (!eventsReload.includes(message.type)) {
return;
}

// Reload the page immediately if we're not in the grace period
if (!gracePeriod) {
window.location.reload();
return;
}

// If still in the grace period, debounce the reload
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(function() {
window.location.reload();
}, graceTimeout);

} catch (e) {
console.error('Error parsing message:', e);
console.error('Error handling message:', e);
}
};

// Handle ws errors and closure
ws.onerror = function(error) {
console.error('WebSocket Error:', error);
};

ws.onclose = function() {
console.log('WebSocket connection closed');
};
Expand Down
11 changes: 8 additions & 3 deletions contribs/gnodev/pkg/watcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,14 @@ func (p *PackageWatcher) AddPackages(pkgs ...gnomod.Pkg) error {
return len(p.pkgsDir[i]) <= len(dir) // Longest paths first
})

// Check for duplicates at the insertion point to avoid redundancy
// Check for duplicates
if index < len(p.pkgsDir) && p.pkgsDir[index] == dir {
continue // Skip
}

// Insert the package
p.pkgsDir = append(p.pkgsDir[:index], append([]string{abs}, p.pkgsDir[index:]...)...)

// Add the package to the watcher and handle any errors
if err := p.watcher.Add(abs); err != nil {
return fmt.Errorf("unable to watch %q: %w", pkg.Dir, err)
Expand All @@ -149,8 +152,10 @@ func (p *PackageWatcher) generatePackagesUpdateList(paths []string) PackageUpdat
mpkgs := map[string]*events.PackageUpdate{} // Pkg -> Update
for _, path := range paths {
for _, pkg := range p.pkgsDir {
// Check if the path is inside the package directory
if !strings.HasPrefix(pkg, path) {
dirPath := filepath.Dir(path)

// Check if a package directory contain our path directory
if !strings.HasPrefix(pkg, dirPath) {
continue
}

Expand Down

0 comments on commit efc5946

Please sign in to comment.