Skip to content

Commit

Permalink
ref(core): move node to core (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed May 7, 2024
1 parent 1e124d2 commit 4a9967e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 79 deletions.
26 changes: 2 additions & 24 deletions event/node.go → core/node.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package event
package core

import (
"io/fs"
Expand All @@ -7,33 +7,11 @@ import (
"github.com/snivilised/traverse/enums"
)

// package: node represents a single file system entity and in the world
// of observables is like an event. The observable fluency chain will be
// based upon the node.

// Envelope is a wrapper around the Node
type Envelope struct {
N *Node
}

// Seal wraps a Node inside an Envelope
func Seal(n *Node) *Envelope {
// When using rx, we must instantiate it with a struct, not a pointer
// to struct. So we create a distinction between what is the unit of
// transfer (Envelope) and it's payload, the Node. This means that the
// Envelope can only have non pointer receivers, but the Node can
// be defined with pointer receivers.
//
return &Envelope{
N: n,
}
}

// Node represents a file system node event and represents each file system
// entity encountered during traversal. The event representing the root node
// does not have a DirEntry because it is not created as a result of a readDir
// invoke. Therefore, the client has to know that when its function is called back,
// they will be no DirEntry for the root node.
// there will be no DirEntry for the root node.
type Node struct {
Path string
Entry fs.DirEntry // contains a FileInfo via Info() function
Expand Down
3 changes: 1 addition & 2 deletions cycle/cycle-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cycle

import (
"github.com/snivilised/traverse/core"
"github.com/snivilised/traverse/event"
)

// cycle represents life cycle events; can't use prefs
Expand Down Expand Up @@ -35,5 +34,5 @@ type (

// NodeHandler is a generic handler that is for any notification that contains
// the traversal node, such as directory ascend or descend.
NodeHandler func(node *event.Node)
NodeHandler func(node *core.Node)
)
7 changes: 3 additions & 4 deletions cycle/notify.go → cycle/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cycle

import (
"github.com/snivilised/traverse/core"
"github.com/snivilised/traverse/event"
)

type (
Expand Down Expand Up @@ -51,7 +50,7 @@ var (

func init() {
AscendDispatcher = Dispatch[NodeHandler]{
Invoke: func(_ *event.Node) {},
Invoke: func(_ *core.Node) {},
broadcaster: BroadcastNode,
}

Expand All @@ -61,7 +60,7 @@ func init() {
}

DescendDispatcher = Dispatch[NodeHandler]{
Invoke: func(_ *event.Node) {},
Invoke: func(_ *core.Node) {},
broadcaster: BroadcastNode,
}

Expand Down Expand Up @@ -132,7 +131,7 @@ func BroadcastEnd(listeners []EndHandler) EndHandler {
}

func BroadcastNode(listeners []NodeHandler) NodeHandler {
return func(node *event.Node) {
return func(node *core.Node) {
for _, listener := range listeners {
listener(node)
}
Expand Down
47 changes: 47 additions & 0 deletions cycle/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cycle_test

import (
. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok

"github.com/snivilised/traverse/core"
"github.com/snivilised/traverse/cycle"
)

var _ = Describe("controls", func() {
When("bind", func() {
It("should: dispatch notification to event handler", func() {
const path = "/traversal-root"

var (
controls cycle.Controls
events cycle.Events
begun bool
ended bool
)

// init(registry->options):
//
events.Bind(&controls)

// client:
//
events.Begin.On(func(root string) {
begun = true
Expect(root).To(Equal(path))
})

events.End.On(func(_ core.TraverseResult) {
ended = true
})

// component side:
//
controls.Begin.Dispatch.Invoke(path)
controls.End.Dispatch.Invoke(nil)

Expect(begun).To(BeTrue(), "begin notification handler not invoked")
Expect(ended).To(BeTrue(), "end notification handler not invoked")
})
})
})
47 changes: 0 additions & 47 deletions cycle/notify_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions pref/sampler-options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pref

import (
"github.com/snivilised/traverse/event"
"github.com/snivilised/traverse/core"
)

type (
Expand All @@ -24,7 +24,7 @@ type (
}

// EachDirectoryEntryPredicate callback to invoke for each child node event
EachDirectoryEntryPredicate func(node *event.Node) bool
EachDirectoryEntryPredicate func(node *core.Node) bool

// WhileDirectoryPredicate determines when to terminate the loop
WhileDirectoryPredicate func(fi *FilteredInfo) bool
Expand Down

0 comments on commit 4a9967e

Please sign in to comment.