Skip to content

Commit 0718ede

Browse files
Serializing Archieves
1 parent c2773f3 commit 0718ede

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

build/build.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sort"
2222
"strconv"
2323
"strings"
24+
"sync"
2425
"time"
2526

2627
"github.com/fsnotify/fsnotify"
@@ -938,23 +939,40 @@ func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*Packag
938939
return pkg, archive, nil
939940
}
940941

942+
// getExeModTime will determine the mod time of the GopherJS binary
943+
// the first time this is called and cache the result for subsequent calls.
944+
var getExeModTime = func() func() time.Time {
945+
var (
946+
once sync.Once
947+
result time.Time
948+
)
949+
getTime := func() {
950+
var fileInfo os.FileInfo
951+
gopherjsBinary, err := os.Executable()
952+
if err == nil {
953+
fileInfo, err = os.Stat(gopherjsBinary)
954+
if err == nil {
955+
result = fileInfo.ModTime()
956+
return
957+
}
958+
}
959+
os.Stderr.WriteString("Could not get GopherJS binary's modification timestamp. Please report issue.\n")
960+
result = time.Now()
961+
}
962+
return func() time.Time {
963+
once.Do(getTime)
964+
return result
965+
}
966+
}()
967+
941968
// BuildPackage compiles an already loaded package.
942969
func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
943970
if archive, ok := s.UpToDateArchives[pkg.ImportPath]; ok {
944971
return archive, nil
945972
}
946973

947-
var fileInfo os.FileInfo
948-
gopherjsBinary, err := os.Executable()
949-
if err == nil {
950-
fileInfo, err = os.Stat(gopherjsBinary)
951-
if err == nil && fileInfo.ModTime().After(pkg.SrcModTime) {
952-
pkg.SrcModTime = fileInfo.ModTime()
953-
}
954-
}
955-
if err != nil {
956-
os.Stderr.WriteString("Could not get GopherJS binary's modification timestamp. Please report issue.\n")
957-
pkg.SrcModTime = time.Now()
974+
if exeModTime := getExeModTime(); exeModTime.After(pkg.SrcModTime) {
975+
pkg.SrcModTime = exeModTime
958976
}
959977

960978
for _, importedPkgPath := range pkg.Imports {
@@ -966,14 +984,13 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
966984
return nil, err
967985
}
968986

969-
impModTime := importedPkg.SrcModTime
970-
if impModTime.After(pkg.SrcModTime) {
987+
if impModTime := importedPkg.SrcModTime; impModTime.After(pkg.SrcModTime) {
971988
pkg.SrcModTime = impModTime
972989
}
973990
}
974991

975-
if pkg.FileModTime().After(pkg.SrcModTime) {
976-
pkg.SrcModTime = pkg.FileModTime()
992+
if fileModTime := pkg.FileModTime(); fileModTime.After(pkg.SrcModTime) {
993+
pkg.SrcModTime = fileModTime
977994
}
978995

979996
if !s.options.NoCache {
@@ -1018,7 +1035,8 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
10181035
fmt.Println(pkg.ImportPath)
10191036
}
10201037

1021-
s.buildCache.StoreArchive(archive, time.Now())
1038+
archiveBuiltTime := time.Now()
1039+
s.buildCache.StoreArchive(archive, archiveBuiltTime)
10221040
s.UpToDateArchives[pkg.ImportPath] = archive
10231041

10241042
return archive, nil

0 commit comments

Comments
 (0)