-
Notifications
You must be signed in to change notification settings - Fork 9
/
hotreloadmodule.go
59 lines (48 loc) · 1.72 KB
/
hotreloadmodule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package hotreload
import (
"context"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnfs/workspace/wsremote"
"namespacelabs.dev/foundation/internal/integrations"
"namespacelabs.dev/foundation/internal/wscontents"
)
type hotReloadModule struct {
module build.Workspace
sink wsremote.Sink
}
func NewHotReloadModule(module build.Workspace, opts *integrations.HotReloadOpts) build.Workspace {
return hotReloadModule{
module: module,
sink: observerSink{opts.Sink, opts.EventProcessor},
}
}
func (w hotReloadModule) ModuleName() string { return w.module.ModuleName() }
func (w hotReloadModule) Abs() string { return w.module.Abs() }
func (w hotReloadModule) VersionedFS(rel string, observeChanges bool) compute.Computable[wscontents.Versioned] {
if observeChanges {
return wsremote.ObserveAndPush(w.module.Abs(), rel, w.sink)
}
return w.module.VersionedFS(rel, observeChanges)
}
type observerSink struct {
sink wsremote.Sink
eventProcessor func(*wscontents.FileEvent) *wscontents.FileEvent
}
func (obs observerSink) Deposit(ctx context.Context, events []*wscontents.FileEvent) (bool, error) {
processedEvents := []*wscontents.FileEvent{}
for _, ev := range events {
processedEvent := ev
if obs.eventProcessor != nil {
processedEvent = obs.eventProcessor(ev)
if processedEvent == nil {
return false, nil
}
}
processedEvents = append(processedEvents, processedEvent)
}
return obs.sink.Deposit(ctx, processedEvents)
}