This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from elmerbulthuis/paralell
Paralell
- Loading branch information
Showing
13 changed files
with
303 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.