Skip to content

Commit 36be6bf

Browse files
committed
feat: make sure Viper compiles on WASM
fsnotify is not available on WASM, so config watching is not going to work. Signed-off-by: Mark Sagi-Kazar <[email protected]>
1 parent 727a41c commit 36be6bf

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

.github/workflows/wasm.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: WASM
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
jobs:
10+
build:
11+
name: Build
12+
runs-on: ubuntu-latest
13+
env:
14+
GOFLAGS: -mod=readonly
15+
16+
steps:
17+
- name: Set up Go
18+
uses: actions/setup-go@v2
19+
with:
20+
go-version: '1.16'
21+
22+
- name: Checkout code
23+
uses: actions/checkout@v2
24+
25+
- name: Ensure Viper compiles for WASM
26+
run: GOOS=js GOARCH=wasm go build .

viper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (v *Viper) WatchConfig() {
344344
initWG := sync.WaitGroup{}
345345
initWG.Add(1)
346346
go func() {
347-
watcher, err := fsnotify.NewWatcher()
347+
watcher, err := newWatcher()
348348
if err != nil {
349349
log.Fatal(err)
350350
}

watch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build !js
2+
3+
package viper
4+
5+
import "github.com/fsnotify/fsnotify"
6+
7+
type watcher = fsnotify.Watcher
8+
9+
func newWatcher() (*watcher, error) {
10+
return fsnotify.NewWatcher()
11+
}

watch_wasm.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// +build js,wasm
2+
3+
package viper
4+
5+
import (
6+
"errors"
7+
8+
"github.com/fsnotify/fsnotify"
9+
)
10+
11+
type watcher struct {
12+
Events chan fsnotify.Event
13+
Errors chan error
14+
}
15+
16+
func (*watcher) Close() error {
17+
return nil
18+
}
19+
20+
func (*watcher) Add(name string) error {
21+
return nil
22+
}
23+
24+
func (*watcher) Remove(name string) error {
25+
return nil
26+
}
27+
28+
func newWatcher() (*watcher, error) {
29+
return &watcher{}, errors.New("fsnotify is not supported on WASM")
30+
}

0 commit comments

Comments
 (0)