Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
26 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,38 @@ | ||
module App.Command.CommandGroup where | ||
|
||
import Prelude | ||
|
||
import App.Command.Command (Command) | ||
import App.Command.Command as Command | ||
import Data.Array as Array | ||
import Data.Maybe (fromMaybe) | ||
import Effect (Effect) | ||
import Effect.Ref (Ref) | ||
import Effect.Ref as Ref | ||
|
||
type CommandGroup = | ||
{ extrinsic :: Boolean | ||
, commands :: Ref (Array Command) | ||
} | ||
|
||
create :: Effect CommandGroup | ||
create = do | ||
commands <- Ref.new [] | ||
pure { extrinsic: false, commands } | ||
|
||
createExtrinsic :: Effect CommandGroup | ||
createExtrinsic = do | ||
commands <- Ref.new [] | ||
pure { extrinsic: true, commands } | ||
|
||
squash :: Command -> CommandGroup -> Effect Unit | ||
squash cmd group = | ||
group.commands # Ref.modify_ \cmds -> | ||
fromMaybe (Array.snoc cmds cmd) do | ||
{ init, last } <- Array.unsnoc cmds | ||
cmd' <- Command.squash cmd last | ||
pure $ Array.snoc init cmd' | ||
|
||
any :: (Command -> Boolean) -> CommandGroup -> Effect Boolean | ||
any pred group = | ||
Ref.read group.commands <#> Array.any pred |
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,63 @@ | ||
module App.Command.CommandManager where | ||
|
||
import Prelude | ||
|
||
import App.Command.Command (Command) | ||
import App.Command.Command as Command | ||
import App.Command.CommandGroup (CommandGroup) | ||
import App.Command.CommandGroup as CommandGroup | ||
import App.Model.Puzzle (Puzzle) | ||
import Data.Array as Array | ||
import Data.Traversable (traverse_) | ||
import Effect (Effect) | ||
import Effect.Ref (Ref) | ||
import Effect.Ref as Ref | ||
import Effect.Unsafe (unsafePerformEffect) | ||
|
||
type CommandManager = | ||
{ current :: CommandGroup | ||
, postHandlers :: Array (Command -> Effect Unit) | ||
, commitHandlers :: Array (CommandGroup -> Effect Unit) | ||
} | ||
|
||
self :: Ref CommandManager | ||
self = unsafePerformEffect do | ||
current <- CommandGroup.create | ||
Ref.new | ||
{ current | ||
, postHandlers: [] | ||
, commitHandlers: [] | ||
} | ||
|
||
onPost :: (Command -> Effect Unit) -> Effect Unit | ||
onPost f = self # Ref.modify_ \obj -> | ||
obj { postHandlers = Array.snoc obj.postHandlers f } | ||
|
||
onCommit :: (CommandGroup -> Effect Unit) -> Effect Unit | ||
onCommit f = self # Ref.modify_ \obj -> | ||
obj { commitHandlers = Array.snoc obj.commitHandlers f } | ||
|
||
commit :: Effect Unit | ||
commit = do | ||
obj <- self # Ref.read | ||
let group = obj.current | ||
next <- CommandGroup.create | ||
self # Ref.write obj { current = next } | ||
traverse_ (_ $ group) obj.commitHandlers | ||
|
||
post :: Puzzle -> Command -> Effect Unit | ||
post puzzle cmd = do | ||
b <- Command.isValid puzzle cmd | ||
when b do | ||
obj <- self # Ref.read | ||
CommandGroup.squash cmd obj.current | ||
traverse_ (_ $ cmd) obj.postHandlers | ||
|
||
receive :: Puzzle -> Array Command -> Effect Unit | ||
receive puzzle cmds = do | ||
traverse_ (Command.execute puzzle) cmds | ||
group <- CommandGroup.createExtrinsic | ||
traverse_ (\cmd -> CommandGroup.squash cmd group) cmds | ||
obj <- self # Ref.read | ||
traverse_ (_ $ group) obj.commitHandlers | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.