Skip to content

Commit

Permalink
feat(hiber): implement hibernation (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Sep 2, 2024
1 parent 5ff1896 commit ec71e0e
Show file tree
Hide file tree
Showing 38 changed files with 2,062 additions and 79 deletions.
4 changes: 4 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ tasks:
cmds:
- go test ./core

tf-hiber:
cmds:
- go test ./internal/feat/hiber

toc:
cmds:
- go test ./collections
Expand Down
5 changes: 3 additions & 2 deletions builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ func (bs *Builders) buildAll() (*buildArtefacts, error) {
order := manifest(active)
artefacts.Mediator.Arrange(active, order)
pi := &types.PluginInit{
Actions: actions,
O: o,
Actions: actions,
O: o,
Controls: artefacts.Mediator.Controls(),
}

for _, p := range plugins {
Expand Down
9 changes: 9 additions & 0 deletions core/hibernate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core

type Hibernate struct {
// Node denotes the filter object that represents the current file system item
// being visited.
//
StartAt *FilterDef
StopAt *FilterDef
}
27 changes: 24 additions & 3 deletions director-prime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ import (
"github.com/fortytw2/leaktest"
. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok
"github.com/snivilised/li18ngo"
tv "github.com/snivilised/traverse"
"github.com/snivilised/traverse/core"
"github.com/snivilised/traverse/cycle"
"github.com/snivilised/traverse/enums"
"github.com/snivilised/traverse/internal/services"
"github.com/snivilised/traverse/locale"
"github.com/snivilised/traverse/pref"
)

var _ = Describe("Director(Prime)", func() {
var _ = Describe("Director(Prime)", Ordered, func() {
BeforeAll(func() {
Expect(li18ngo.Use(
func(o *li18ngo.UseOptions) {
o.From.Sources = li18ngo.TranslationFiles{
locale.SourceID: li18ngo.TranslationSource{Name: "traverse"},
}
},
)).To(Succeed())
})

BeforeEach(func() {
services.Reset()
})
Expand Down Expand Up @@ -162,7 +175,11 @@ var _ = Describe("Director(Prime)", func() {
Subscription: tv.SubscribeFiles,
Handler: noOpHandler,
},
tv.WithHibernationWake(&core.FilterDef{}),
tv.WithHibernationWake(&core.FilterDef{
Description: "nonsense",
Type: enums.FilterTypeGlob,
Pattern: "*",
}),
tv.WithOnStop(func(_ string) {}),
)).Navigate(ctx)

Expand All @@ -183,7 +200,11 @@ var _ = Describe("Director(Prime)", func() {
Subscription: tv.SubscribeFiles,
Handler: noOpHandler,
},
tv.WithHibernationSleep(&core.FilterDef{}),
tv.WithHibernationSleep(&core.FilterDef{
Description: "nonsense",
Type: enums.FilterTypeGlob,
Pattern: "*",
}),
tv.WithOnStop(func(_ string) {}),
)).Navigate(ctx)

Expand Down
16 changes: 15 additions & 1 deletion director-resume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok

"github.com/snivilised/li18ngo"
tv "github.com/snivilised/traverse"
"github.com/snivilised/traverse/core"
"github.com/snivilised/traverse/cycle"
"github.com/snivilised/traverse/enums"
"github.com/snivilised/traverse/internal/services"
"github.com/snivilised/traverse/locale"
"github.com/snivilised/traverse/pref"
)

Expand All @@ -36,6 +38,14 @@ var _ = Describe("Director(Resume)", Ordered, func() {
Mode: os.ModeDir,
},
}

Expect(li18ngo.Use(
func(o *li18ngo.UseOptions) {
o.From.Sources = li18ngo.TranslationFiles{
locale.SourceID: li18ngo.TranslationSource{Name: "traverse"},
}
},
)).To(Succeed())
})

BeforeEach(func() {
Expand Down Expand Up @@ -151,7 +161,11 @@ var _ = Describe("Director(Resume)", Ordered, func() {
From: RestorePath,
Strategy: tv.ResumeStrategySpawn,
},
tv.WithHibernationWake(&core.FilterDef{}),
tv.WithHibernationWake(&core.FilterDef{
Description: "nonsense",
Type: enums.FilterTypeGlob,
Pattern: "*",
}),
restore,
)).Navigate(ctx)

Expand Down
12 changes: 5 additions & 7 deletions enums/hibernation-en-auto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions enums/hibernation-en.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ type Hibernation uint
const (
HibernationUndefined Hibernation = iota // undefined

// HibernationSleep listen not active, callback always invoked (subject to filtering)
//
HibernationSleep // sleep-hibernation

// HibernationFastward listen used to resume by fast-forwarding
//
HibernationFastward // fastward-hibernation

// HibernationPending conditional listening is awaiting activation
//
HibernationPending // pending-hibernation

// HibernationAwake conditional listening is active (callback is invoked)
// HibernationActive conditional listening is active (callback is invoked)
//
HibernationAwake // awake-hibernation
HibernationActive // active-hibernation

// HibernationRetired conditional listening is now deactivated
//
Expand Down
10 changes: 4 additions & 6 deletions enums/role-en-auto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions enums/role-en.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ package enums
type Role uint32

const (
RoleUndefined Role = iota // undefined-role
RoleAnchor // anchor-role
RoleClientFilter // client-filter-role
RoleClientHiberWake // client-hiber-wake-role
RoleClientHiberSleep // client-hiber-sleep-role
RoleFastward // fastward-role
RoleSampler // sampler-role
RoleUndefined Role = iota // undefined-role
RoleAnchor // anchor-role
RoleClientFilter // client-filter-role
RoleHibernate // hibernate-role
RoleSampler // sampler-role
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/nicksnyder/go-i18n/v2 v2.4.0
github.com/pkg/errors v0.9.1
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/snivilised/li18ngo v0.1.2
github.com/snivilised/li18ngo v0.1.3
github.com/snivilised/pants v0.1.2
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/snivilised/li18ngo v0.1.2 h1:xiOZnAcIkeHfEVuTy/FBwA1u9IrAfYSjO/DYMC77z/I=
github.com/snivilised/li18ngo v0.1.2/go.mod h1:Or3qUhpR6AM1X51i82RtyCvORWy2/hrxY9lg1i1gFTE=
github.com/snivilised/li18ngo v0.1.3 h1:wi5pa66vs2/gXyvZP64i9uwlCvXpedELKdFpn3B3SR8=
github.com/snivilised/li18ngo v0.1.3/go.mod h1:Or3qUhpR6AM1X51i82RtyCvORWy2/hrxY9lg1i1gFTE=
github.com/snivilised/pants v0.1.2 h1:6Abj02gV5rFYyKfCsmeEiOi1pLdRyITKUY5oDoRgYuU=
github.com/snivilised/pants v0.1.2/go.mod h1:BOZa24yLxVjjnTCFWQeCzUWL8eK4TLtXtkz3pMdEFQM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package hiber

import (
"github.com/snivilised/traverse/core"
"github.com/snivilised/traverse/enums"
"github.com/snivilised/traverse/pref"
)

const (
badge = "badge: hibernator"
)
Expand All @@ -19,3 +25,22 @@ func RestoreOptions() {
// called by resume to load options from json file and
// setup binder to reflect this
}

type (
nextFn func(node *core.Node, inspection core.Inspection) (bool, error)

state struct {
next nextFn
}
hibernateStates map[enums.Hibernation]state

hibernateOptions struct {
o *pref.HibernateOptions
beh *pref.HibernationBehaviour
}

triggers struct {
wake core.TraverseFilter
sleep core.TraverseFilter
}
)
23 changes: 17 additions & 6 deletions internal/feat/hiber/hibernate-plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ import (
)

func IfActive(o *pref.Options, mediator types.Mediator) types.Plugin {
if o.Hibernate.Wake != nil {
if o.Hibernate.IsHibernateActive() {
return &Plugin{
BasePlugin: kernel.BasePlugin{
Mediator: mediator,
ActivatedRole: enums.RoleClientHiberSleep, // TODO: or wake; to be resolved
ActivatedRole: enums.RoleHibernate,
},
profile: &simple{
common: common{
ho: &hibernateOptions{
o: &o.Hibernate,
beh: &o.Behaviours.Hibernation,
},
},
},
}
}
Expand All @@ -23,6 +31,7 @@ func IfActive(o *pref.Options, mediator types.Mediator) types.Plugin {

type Plugin struct {
kernel.BasePlugin
profile profile
}

func (p *Plugin) Name() string {
Expand All @@ -36,11 +45,13 @@ func (p *Plugin) Register(kc types.KernelController) error {
}

func (p *Plugin) Next(node *core.Node, inspection core.Inspection) (bool, error) {
_, _ = node, inspection

return true, nil
return p.profile.next(node, inspection)
}

func (p *Plugin) Init(_ *types.PluginInit) error {
func (p *Plugin) Init(pi *types.PluginInit) error {
if err := p.profile.init(pi.Controls); err != nil {
return err
}

return p.Mediator.Decorate(p)
}
Loading

0 comments on commit ec71e0e

Please sign in to comment.