Skip to content

Commit

Permalink
feat(nav): intermediate check-in (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Nov 20, 2023
1 parent 9b6988b commit 1444327
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 44 deletions.
28 changes: 21 additions & 7 deletions xfs/nav/navigator-files.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package nav

import (
"github.com/samber/lo"
)

type filesNavigator struct {
navigator
}
Expand All @@ -15,6 +19,10 @@ func (n *filesNavigator) top(frame *navigationFrame, root string) (*TraverseResu
}

func (n *filesNavigator) preview(params *traverseParams) *inspection {
if s, found := n.cache[params.current.Path]; found {
return s
}

stash := n.inspect(params)
n.cache[params.current.Path] = stash

Expand All @@ -29,14 +37,14 @@ func (n *filesNavigator) inspect(params *traverseParams) *inspection {

if stash.isDir {
stash.contents, stash.readErr = n.agent.read(params.current.Path)

stash.contents.sort(stash.contents.Files)
stash.contents.sort(stash.contents.Folders)
} else {
stash.contents = newEmptyDirectoryEntries(n.o)
n.o.Hooks.Extend(params.navi, stash.contents)
}

n.o.Hooks.Extend(params.navi, stash.contents)

return stash
}

Expand All @@ -58,25 +66,31 @@ func (n *filesNavigator) traverse(params *traverseParams) (*TraverseItem, error)
params.navi = navi
n.descend(navi)

n.stash = n.inspect(params)
// >>> fmt.Printf("\n🥝🥝🥝 FILES-NAV: inspecting item: '%v'\n", params.current.Path)
stash := n.inspect(params)

if !n.stash.isDir {
if !stash.isDir {
// Effectively, this is the file only filter
//
return nil, params.frame.proxy(params.current, nil)
}

if n.samplingActive {
n.o.Sampler.Fn(n.stash.contents, n.samplingHelpers, nil)
n.broker.navigation = navi
n.broker.tp = params

n.o.Sampler.Fn(stash.contents, n.samplingHelpers,
lo.Ternary(n.samplingFilterActive, n.broker, nil),
)
}

entries := n.stash.contents.All()
entries := stash.contents.All()

if skip, err := n.agent.notify(&agentNotifyParams{
frame: params.frame,
current: params.current,
entries: entries,
readErr: n.stash.readErr,
readErr: stash.readErr,
}); skip == SkipTraversalAllEn || err != nil {
return nil, err
} else if skip == SkipTraversalDirEn {
Expand Down
4 changes: 4 additions & 0 deletions xfs/nav/navigator-universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (n *universalNavigator) top(frame *navigationFrame, root string) (*Traverse
}

func (n *universalNavigator) preview(params *traverseParams) *inspection {
if s, found := n.cache[params.current.Path]; found {
return s
}

stash := n.inspect(params)
n.cache[params.current.Path] = stash

Expand Down
8 changes: 0 additions & 8 deletions xfs/nav/sampling-broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ func (b *samplingBroker) Preview(child *TraverseItem) *inspection {
})
}

func (b *samplingBroker) navi() *NavigationInfo { // ???
return b.navigation
}

func (b *samplingBroker) params() *traverseParams { // ???
return b.tp
}

func (b *samplingBroker) sneak() inspector { // ???
return b.navigator
}
20 changes: 16 additions & 4 deletions xfs/nav/sampling-helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func createSamplingHelpers() SamplingHelpersCollection {
},
)
},
Terminator: func(filtered []fs.DirEntry, noOf *SampleNoOf) bool {
Filter: func(_ *BrokerParams) []fs.DirEntry {
panic("🔥 Filter func for SubscribeAny NOT-IMPLEMENTED")
},
IsFull: func(_ []fs.DirEntry, _ *SampleNoOf) bool {
panic("🔥 Terminator func for SubscribeAny NOT-IMPLEMENTED")
},
},
Expand All @@ -37,7 +40,10 @@ func createSamplingHelpers() SamplingHelpersCollection {
Slice: func(contents *DirectoryContents, noOf *SampleNoOf, fn SliceEntriesFn) {
contents.Folders = fn(contents.Folders, int(noOf.Folders))
},
Terminator: func(filtered []fs.DirEntry, noOf *SampleNoOf) bool {
Filter: func(_ *BrokerParams) []fs.DirEntry {
return []fs.DirEntry{}
},
IsFull: func(filtered []fs.DirEntry, noOf *SampleNoOf) bool {
return len(filtered) == int(noOf.Folders)
},
},
Expand All @@ -46,7 +52,10 @@ func createSamplingHelpers() SamplingHelpersCollection {
Slice: func(contents *DirectoryContents, noOf *SampleNoOf, fn SliceEntriesFn) {
contents.Folders = fn(contents.Folders, int(noOf.Folders))
},
Terminator: func(filtered []fs.DirEntry, noOf *SampleNoOf) bool {
Filter: func(_ *BrokerParams) []fs.DirEntry {
panic("🔥 Filter func for SubscribeFoldersWithFiles NOT-IMPLEMENTED")
},
IsFull: func(_ []fs.DirEntry, _ *SampleNoOf) bool {
panic("🔥 Terminator func for SubscribeFoldersWithFiles NOT-IMPLEMENTED")
},
},
Expand All @@ -55,7 +64,10 @@ func createSamplingHelpers() SamplingHelpersCollection {
Slice: func(contents *DirectoryContents, noOf *SampleNoOf, fn SliceEntriesFn) {
contents.Files = fn(contents.Files, int(noOf.Files))
},
Terminator: func(filtered []fs.DirEntry, noOf *SampleNoOf) bool {
Filter: func(_ *BrokerParams) []fs.DirEntry {
return []fs.DirEntry{}
},
IsFull: func(filtered []fs.DirEntry, noOf *SampleNoOf) bool {
return len(filtered) == int(noOf.Files)
},
},
Expand Down
20 changes: 12 additions & 8 deletions xfs/nav/traverse-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,11 @@ const (
type FilterBroker interface {
Filter() TraverseFilter
Preview(child *TraverseItem) *inspection
navi() *NavigationInfo
params() *traverseParams
sneak() inspector
}

// SampleCallback
type SampleCallback func(contents *DirectoryContents, helpers SamplingHelpersCollection, broker FilterBroker)
// SampleFunc
type SampleFunc func(contents *DirectoryContents, helpers SamplingHelpersCollection, broker FilterBroker)

type inspection struct {
current *TraverseItem
Expand All @@ -217,16 +215,22 @@ type inspection struct {
compoundCounts *compoundCounters
}

type inspectCache map[string]*inspection
type itemSubPath = string
type inspectCache map[itemSubPath]*inspection

type SliceSamplerFunc func(contents *DirectoryContents, noOf *SampleNoOf, fn SliceEntriesFn)

// (params *BrokerParams) []fs.DirEntry

type FilterSamplerFunc func(params *BrokerParams) []fs.DirEntry

type SamplingHelpers struct {
Slice SliceSamplerFunc
Terminator TerminatorFunc
Slice SliceSamplerFunc
Filter FilterSamplerFunc
IsFull SampleFullFunc
}

type (
TerminatorFunc func(filtered []fs.DirEntry, noOf *SampleNoOf) bool
SampleFullFunc func(filtered []fs.DirEntry, noOf *SampleNoOf) bool
SamplingHelpersCollection map[TraverseSubscription]SamplingHelpers
)
2 changes: 1 addition & 1 deletion xfs/nav/traverse-options.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type SamplingOptions struct {

// SamplerOptions
type SamplerOptions struct {
Fn SampleCallback
Fn SampleFunc
}

// OptionsStore represents that part of options that is directly
Expand Down
6 changes: 3 additions & 3 deletions xfs/nav/traverse-sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ var _ = Describe("Traverse With Sample", Ordered, func() {
},
}),

Entry(nil, &sampleTE{
Entry(nil, &sampleTE{ // !!!
naviTE: naviTE{
message: "filtered folders: default (first), with 2 folders that start with A",
should: "invoke for at most 2 folders per directory",
Expand Down Expand Up @@ -297,7 +297,7 @@ var _ = Describe("Traverse With Sample", Ordered, func() {
},
}),

XEntry(nil, &sampleTE{
XEntry(nil, &sampleTE{ // !!!
naviTE: naviTE{
message: "filtered files: default (first), 2 files",
should: "invoke for at most 2 files per directory",
Expand All @@ -308,7 +308,7 @@ var _ = Describe("Traverse With Sample", Ordered, func() {
filter: &filterTE{
name: "items with .flac suffix",
pattern: "*.flac",
scope: nav.ScopeAllEn,
scope: nav.ScopeLeafEn,
},
noOf: nav.SampleNoOf{
Files: 2,
Expand Down
58 changes: 45 additions & 13 deletions xfs/nav/traverse-samplers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func FilterFirst(params *BrokerParams) []fs.DirEntry {
filtered = append(filtered, entry)
}

if helpers[subscription].Terminator(filtered, params.NoOf) {
if helpers[subscription].IsFull(filtered, params.NoOf) {
break
}
}
Expand All @@ -66,22 +66,54 @@ func FilterLast(params *BrokerParams) []fs.DirEntry {
return params.Entries
}

func getSubSetSampler(noOf *SampleNoOf, fn SliceEntriesFn, first bool) SampleCallback {
// func getFirstOrLastSampler
func FirstOrLast(contents *DirectoryContents, helpers SamplingHelpersCollection, broker FilterBroker) {
_ = contents
_ = helpers
_ = broker
}

// this is incorrect design, we can have first bool flag, only
// know about 1 function, it should have to decide first or last.
// rather the default function if not explicitly set by the client
// is another function which decides between first or last and sends
// that function here., in options, wer could have a tri-state-bool

func getSubSetSampler(noOf *SampleNoOf, fn SliceEntriesFn, first bool) SampleFunc {
return func(contents *DirectoryContents, helpers SamplingHelpersCollection, broker FilterBroker) {
o := contents.Options
subscription := o.Store.Subscription

if utils.IsNil(broker) {
subscription := o.Store.Subscription
helpers[subscription].Slice(contents, noOf, fn)
} else {
contents.Folders = lo.Ternary(first, FilterFirst, FilterLast)(&BrokerParams{
Options: o,
Contents: contents,
Entries: contents.Folders, // | entries.Folders | both
NoOf: noOf,
Broker: broker,
Helpers: helpers,
})
// we still need to allow a client defined function, its not just a
// choice between first or last. In fact its only a choice between
// first or last, if there is not custom one.

switch subscription {
case SubscribeAny:
case SubscribeFiles:
contents.Folders = lo.Ternary(first, FilterFirst, FilterLast)(&BrokerParams{
Options: o,
Contents: contents,
Entries: contents.Files,
NoOf: noOf,
Broker: broker,
Helpers: helpers,
})
case SubscribeFolders:
contents.Folders = lo.Ternary(first, FilterFirst, FilterLast)(&BrokerParams{
Options: o,
Contents: contents,
Entries: contents.Folders,
NoOf: noOf,
Broker: broker,
Helpers: helpers,
})
case SubscribeFoldersWithFiles:
default:
}
}
}
}
Expand All @@ -93,7 +125,7 @@ func getSubSetSampler(noOf *SampleNoOf, fn SliceEntriesFn, first bool) SampleCal
// use the options push model so that the SampleNoOf instance required
// can be provided. The push model requires the use of ProvidedOptions
// instead of using the pull model via OptionsFn callback.
func GetFirstSampler(noOf *SampleNoOf) SampleCallback {
func GetFirstSampler(noOf *SampleNoOf) SampleFunc {
return getSubSetSampler(noOf, firstSampler, true)
}

Expand All @@ -104,6 +136,6 @@ func GetFirstSampler(noOf *SampleNoOf) SampleCallback {
// use the options push model so that the SampleNoOf instance required
// can be provided. The push model requires the use of ProvidedOptions
// instead of using the pull model via OptionsFn callback.
func GetLastSampler(noOf *SampleNoOf) SampleCallback {
func GetLastSampler(noOf *SampleNoOf) SampleFunc {
return getSubSetSampler(noOf, lastSampler, false)
}

0 comments on commit 1444327

Please sign in to comment.