Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from elmerbulthuis/paralell
Browse files Browse the repository at this point in the history
Paralell
  • Loading branch information
elmerbulthuis authored Apr 27, 2020
2 parents f470b19 + de847da commit 8dbe7b4
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 71 deletions.
22 changes: 14 additions & 8 deletions example/counter/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sync"
"testing"

"github.com/LuvDaSun/redux-go/redux"
"github.com/stretchr/testify/assert"
)

Expand All @@ -12,18 +13,22 @@ func Test(test *testing.T) {
const count2 = 1000

store := CreateApplicationStore()
store.Dispatch(nil)
stateChannel := make(chan *ApplicationState)
unsubscribe := store.Subscribe(func(state redux.State) {
stateChannel <- state.(*ApplicationState)
})
defer unsubscribe()

state1 := store.GetState().(*ApplicationState)

store.Dispatch(&IncrementAction{})
state2 := store.GetState().(*ApplicationState)
store.DispatchChannel <- &IncrementAction{}
state2 := <-stateChannel

store.Dispatch(&IncrementAction{})
state3 := store.GetState().(*ApplicationState)
store.DispatchChannel <- &IncrementAction{}
state3 := <-stateChannel

store.Dispatch(&DecrementAction{})
state4 := store.GetState().(*ApplicationState)
store.DispatchChannel <- &DecrementAction{}
state4 := <-stateChannel

assert.Equal(test, 0, state1.SelectCounter())
assert.Equal(test, 1, state2.SelectCounter())
Expand All @@ -34,7 +39,8 @@ func Test(test *testing.T) {

job := func() {
for range [count1]int{} {
store.Dispatch(&IncrementAction{})
store.DispatchChannel <- &IncrementAction{}
<-stateChannel
}
wg.Done()
}
Expand Down
8 changes: 3 additions & 5 deletions example/light/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ CreateToggleMiddleware transforms the toggle action
func CreateToggleMiddleware() redux.MiddlewareFactory {

return func(store redux.StoreInterface) redux.Middleware {
return func(next redux.Dispatcher) redux.Dispatcher {

return func(next redux.Dispatch) redux.Dispatch {
return func(action redux.Action) {
next(action)

switch action.(type) {
case *ToggleAction:
state := store.GetState().(*ApplicationState)
if state.SelectLightIsOn() {
next(&SwitchOffAction{})
store.Dispatch(&SwitchOffAction{})
} else {
next(&SwitchOnAction{})
store.Dispatch(&SwitchOnAction{})
}
}

}
}
}
Expand Down
30 changes: 19 additions & 11 deletions example/light/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ import (
"sync"
"testing"

"github.com/LuvDaSun/redux-go/redux"
"github.com/stretchr/testify/assert"
)

func Test(test *testing.T) {
const count1 = 10
const count2 = 100
const count3 = 1000
const count1 = 8
const count2 = 16
const count3 = 32

store := CreateApplicationStore()
store.Dispatch(nil)
stateChannel := make(chan *ApplicationState)
unsubscribe := store.Subscribe(func(state redux.State) {
stateChannel <- state.(*ApplicationState)
})
defer unsubscribe()

state1 := store.GetState().(*ApplicationState)

store.Dispatch(&SwitchOnAction{})
state2 := store.GetState().(*ApplicationState)
store.DispatchChannel <- &SwitchOnAction{}
state2 := <-stateChannel

store.Dispatch(&SwitchOffAction{})
state3 := store.GetState().(*ApplicationState)
store.DispatchChannel <- &SwitchOffAction{}
state3 := <-stateChannel

store.Dispatch(&ToggleAction{})
state4 := store.GetState().(*ApplicationState)
store.DispatchChannel <- &ToggleAction{}
<-stateChannel
state4 := <-stateChannel

assert.Equal(test, false, state1.SelectLightIsOn())
assert.Equal(test, true, state2.SelectLightIsOn())
Expand All @@ -36,7 +42,9 @@ func Test(test *testing.T) {

job := func() {
for range [count2]int{} {
store.Dispatch(&ToggleAction{})
store.DispatchChannel <- &ToggleAction{}
<-stateChannel
<-stateChannel
}
wg.Done()
}
Expand Down
22 changes: 22 additions & 0 deletions example/subscription/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package subscription

/*
StartAction starts events
*/
type StartAction struct {
ID int
}

/*
StopAction stops events
*/
type StopAction struct {
ID int
}

/*
EventAction an event
*/
type EventAction struct {
ID int
}
65 changes: 65 additions & 0 deletions example/subscription/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package subscription

import (
"context"
"time"

"github.com/LuvDaSun/redux-go/redux"
)

/*
CreateSubscriptionMiddleware emits events!
*/
func CreateSubscriptionMiddleware(ctx context.Context, interval time.Duration) redux.MiddlewareFactory {

return func(store redux.StoreInterface) redux.Middleware {
cancels := make(map[int]context.CancelFunc)

subscriptionLoop := func(subscriptionCtx context.Context, id int) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

for {
select {
case <-subscriptionCtx.Done():
return

case <-ticker.C:
store.DispatchChannel <- &EventAction{
ID: id,
}
}
}
}
handleStartAction := func(id int) {
subscriptionCtx, cancel := context.WithCancel(ctx)
cancels[id] = cancel
go subscriptionLoop(subscriptionCtx, id)

}
handleStopAction := func(id int) {
cancel := cancels[id]
delete(cancels, id)
cancel()
}

return func(next redux.Dispatch) redux.Dispatch {

return func(action redux.Action) {
switch action := action.(type) {
case *StartAction:
handleStartAction(action.ID)
next(action)

case *StopAction:
next(action)
handleStopAction(action.ID)

default:
next(action)
}

}
}
}
}
32 changes: 32 additions & 0 deletions example/subscription/reducers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package subscription

import "github.com/LuvDaSun/redux-go/redux"

/*
ApplicationState state
*/
type ApplicationState struct {
counter int
}

/*
InitialApplicationState initial application state
*/
var InitialApplicationState = &ApplicationState{
counter: 0,
}

/*
ReduceApplicationState reduces application state
*/
func ReduceApplicationState(state *ApplicationState, action redux.Action) *ApplicationState {
switch action.(type) {

case *EventAction:
return &ApplicationState{
counter: state.counter + 1,
}
}

return state
}
8 changes: 8 additions & 0 deletions example/subscription/selectors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package subscription

/*
SelectCounter selects counter value
*/
func (state *ApplicationState) SelectCounter() int {
return state.counter
}
24 changes: 24 additions & 0 deletions example/subscription/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package subscription

import (
"context"
"time"

"github.com/LuvDaSun/redux-go/redux"
)

/*
CreateApplicationStore does exactly that!
*/
func CreateApplicationStore(ctx context.Context, interval time.Duration) *redux.Store {
reducer := func(state redux.State, action redux.Action) redux.State {
return ReduceApplicationState(state.(*ApplicationState), action)
}

store := redux.CreateStore(InitialApplicationState, reducer).
ApplyMiddleware(
CreateSubscriptionMiddleware(ctx, interval),
)

return store
}
52 changes: 52 additions & 0 deletions example/subscription/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package subscription

import (
"context"
"testing"
"time"

"github.com/LuvDaSun/redux-go/redux"
"github.com/stretchr/testify/assert"
)

func Test(test *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

const interval = time.Millisecond * 100
const count = 100

store := CreateApplicationStore(ctx, interval)
stateChannel := make(chan *ApplicationState)
unsubscribe := store.Subscribe(func(state redux.State) {
stateChannel <- state.(*ApplicationState)
})
defer unsubscribe()

for index := 0; index < count; index++ {
store.DispatchChannel <- &StartAction{ID: index}
<-stateChannel
}
state1 := store.GetState().(*ApplicationState)

for index := 0; index < count; index++ {
<-stateChannel
}
state2 := store.GetState().(*ApplicationState)

for index := 0; index < count; index++ {
<-stateChannel
}
state3 := store.GetState().(*ApplicationState)

for index := 0; index < count; index++ {
store.DispatchChannel <- &StopAction{ID: index}
<-stateChannel
}
state4 := store.GetState().(*ApplicationState)

assert.Equal(test, 0*count, state1.SelectCounter())
assert.Equal(test, 1*count, state2.SelectCounter())
assert.Equal(test, 2*count, state3.SelectCounter())
assert.Equal(test, 2*count, state4.SelectCounter())
}
22 changes: 15 additions & 7 deletions example/todo/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@ import (
"sync"
"testing"

"github.com/LuvDaSun/redux-go/redux"
"github.com/stretchr/testify/assert"
)

func Test(test *testing.T) {
const count = 1000

store := CreateApplicationStore()
store.Dispatch(nil)
stateChannel := make(chan *ApplicationState)
unsubscribe := store.Subscribe(func(state redux.State) {
stateChannel <- state.(*ApplicationState)
})
defer unsubscribe()

state1 := store.GetState().(*ApplicationState)

{
var wg sync.WaitGroup
job := func(index int) {
store.Dispatch(&AddTaskItemAction{
store.DispatchChannel <- &AddTaskItemAction{
id: fmt.Sprintf("%d", index),
name: string(index),
})
}
<-stateChannel
wg.Done()
}
wg.Add(count)
Expand All @@ -36,9 +42,10 @@ func Test(test *testing.T) {
{
var wg sync.WaitGroup
job := func(index int) {
store.Dispatch(&CompleteTaskItemAction{
store.DispatchChannel <- &CompleteTaskItemAction{
id: fmt.Sprintf("%d", index),
})
}
<-stateChannel
wg.Done()
}
wg.Add(count)
Expand All @@ -52,9 +59,10 @@ func Test(test *testing.T) {
{
var wg sync.WaitGroup
job := func(index int) {
store.Dispatch(&RemoveTaskItemAction{
store.DispatchChannel <- &RemoveTaskItemAction{
id: fmt.Sprintf("%d", index),
})
}
<-stateChannel
wg.Done()
}
wg.Add(count)
Expand Down
Loading

0 comments on commit 8dbe7b4

Please sign in to comment.