-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handler/Sink refactor, added Pipe trait #91
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6c1de88
Handler/Sink refactor, added Pipe trait.
ddc89b9
Deprecate Handlers.create...Handler.
7c58cb4
Refactorings
c7bbad7
Ups, forgot to add file
2392bec
Mix in HandlerOps
565b318
Move transform... methods to HandlerOps trait
6cb14cb
Handler with only one Type parameter
fdietze 5e1ba29
Rename PipeOps
fdietze 4846f0f
Some Pipe/Handler renames, reorder PipeOps
fdietze d21fceb
HandlerOps with lens and imap
fdietze 669583c
PipeOps and HandlerOps as implicit classes
fdietze 279704b
Minor changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package outwatch | ||
|
||
import cats.effect.IO | ||
import outwatch.Sink.{ObservableSink, SubjectSink} | ||
|
||
object Handler { | ||
|
||
/** | ||
* This function also allows you to create initial values for your newly created Handler. | ||
* This is equivalent to calling `startWithMany` with the given values. | ||
* @param seeds a sequence of initial values that the Handler will emit. | ||
* @tparam T the type parameter of the elements | ||
* @return the newly created Handler. | ||
*/ | ||
def create[T](seeds: T*): IO[Pipe[T, T]] = Pipe.create(seeds:_*) | ||
|
||
def create[T]: IO[Pipe[T, T]] = Pipe.create[T] | ||
} | ||
|
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,67 @@ | ||
package outwatch | ||
|
||
import cats.effect.IO | ||
import outwatch.Sink.{ObservableSink, SubjectSink} | ||
import rxscalajs.Observable | ||
|
||
|
||
trait PipeOps[-I, +O] { self : Pipe[I, O] => | ||
|
||
def mapSink[I2](f: I2 => I): Pipe[I2, O] = Pipe(redirectMap(f), self) | ||
|
||
def mapSource[O2](f: O => O2): Pipe[I, O2] = Pipe(self, self.map(f)) | ||
|
||
def mapPipe[I2, O2](f: I2 => I)(g: O => O2): Pipe[I2, O2] = Pipe(self.redirectMap(f), self.map(g)) | ||
|
||
|
||
def collectSink[I2](f: PartialFunction[I2, I]): Pipe[I2, O] = Pipe(self.redirect(_.collect(f)), self) | ||
|
||
def collectSource[O2](f: PartialFunction[O, O2]): Pipe[I, O2] = Pipe(self, self.collect(f)) | ||
|
||
def collectPipe[I2, O2](f: PartialFunction[I2, I])(g: PartialFunction[O, O2]): Pipe[I2, O2] = Pipe( | ||
self.redirect(_.collect(f)), self.collect(g) | ||
) | ||
|
||
|
||
def filterSource(f: O => Boolean): Pipe[I, O] = Pipe(self, self.filter(f)) | ||
|
||
|
||
def transformSink[I2](f: Observable[I2] => Observable[I]): Pipe[I2, O] = Pipe(self.redirect(f), self) | ||
|
||
def transformSource[O2](f: Observable[O] => Observable[O2]): Pipe[I, O2] = Pipe(self, f(self)) | ||
|
||
def transformPipe[I2, O2](f: Observable[I2] => Observable[I])(g: Observable[O] => Observable[O2]): Pipe[I2, O2] = | ||
Pipe(self.redirect(f), g(self)) | ||
} | ||
|
||
|
||
object Pipe { | ||
private[outwatch] def apply[I, O](sink: Sink[I], source: Observable[O]): Pipe[I, O] = | ||
new ObservableSink[I, O](sink, source) with PipeOps[I, O] | ||
|
||
// This is not in PipeOps, because I is contravariant | ||
implicit class FilterSink[I, +O](pipe: Pipe[I, O]) { | ||
def filterSink(f: I => Boolean): Pipe[I, O] = Pipe(pipe.redirect(_.filter(f)), pipe) | ||
} | ||
|
||
/** | ||
* This function also allows you to create initial values for your newly created Pipe. | ||
* This is equivalent to calling `startWithMany` with the given values. | ||
* @param seeds a sequence of initial values that the Pipe will emit. | ||
* @tparam T the type parameter of the elements | ||
* @return the newly created Pipe. | ||
*/ | ||
def create[T](seeds: T*): IO[Pipe[T, T]] = create[T].map { pipe => | ||
if (seeds.nonEmpty) { | ||
pipe.transformSource(_.startWithMany(seeds: _*)) | ||
} | ||
else { | ||
pipe | ||
} | ||
} | ||
|
||
def create[T]: IO[Pipe[T, T]] = IO { | ||
val sink = SubjectSink[T]() | ||
Pipe(sink, sink) | ||
} | ||
} |
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,43 @@ | ||
package outwatch.dom | ||
|
||
import cats.effect.IO | ||
import org.scalajs.dom.{ClipboardEvent, DragEvent, KeyboardEvent, MouseEvent} | ||
import outwatch.dom.helpers.InputEvent | ||
|
||
/** | ||
* Trait containing event handlers, so they can be mixed in to other objects if needed. | ||
*/ | ||
trait HandlerFactories { | ||
|
||
@deprecated("Use Handler.inputEvents instead", "0.11.0") | ||
def createInputHandler() = Pipe.create[InputEvent] | ||
@deprecated("Use Handler.mouseEvents instead", "0.11.0") | ||
def createMouseHandler() = Pipe.create[MouseEvent] | ||
@deprecated("Use Handler.keyboardEvents instead", "0.11.0") | ||
def createKeyboardHandler() = Pipe.create[KeyboardEvent] | ||
@deprecated("Use Handler.dragEvents instead", "0.11.0") | ||
def createDragHandler() = Pipe.create[DragEvent] | ||
@deprecated("Use Handler.clipboardEvents instead", "0.11.0") | ||
def createClipboardHandler() = Pipe.create[ClipboardEvent] | ||
|
||
@deprecated("Use Handler.create[String] instead", "0.11.0") | ||
def createStringHandler(defaultValues: String*) = Pipe.create[String](defaultValues: _*) | ||
@deprecated("Use Handler.create[Boolean] instead", "0.11.0") | ||
def createBoolHandler(defaultValues: Boolean*) = Pipe.create[Boolean](defaultValues: _*) | ||
@deprecated("Use Handler.create[Double] instead", "0.11.0") | ||
def createNumberHandler(defaultValues: Double*) = Pipe.create[Double](defaultValues: _*) | ||
|
||
@deprecated("Use Handler.create[T] instead", "0.11.0") | ||
def createHandler[T](defaultValues: T*): IO[Pipe[T, T]] = Pipe.create[T](defaultValues: _*) | ||
|
||
|
||
implicit class HandlerCreateHelpers(handler: Handler.type) { | ||
lazy val inputEvents = Pipe.create[InputEvent] | ||
lazy val mouseEvents = Pipe.create[MouseEvent] | ||
lazy val keyboardEvents = Pipe.create[KeyboardEvent] | ||
lazy val dragEvents = Pipe.create[DragEvent] | ||
lazy val clipboardEvents = Pipe.create[ClipboardEvent] | ||
} | ||
|
||
} | ||
|
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package outwatch.dom.helpers | ||
|
||
import org.scalajs.dom.raw.{Event, HTMLInputElement} | ||
import org.scalajs.dom.Event | ||
import org.scalajs.dom.html | ||
|
||
import scala.scalajs.js.annotation.ScalaJSDefined | ||
|
||
@ScalaJSDefined | ||
class InputEvent() extends Event { | ||
override def target = { | ||
super.target.asInstanceOf[HTMLInputElement] | ||
super.target.asInstanceOf[html.Input] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,11 +5,23 @@ import cats.effect.IO | |
import scala.language.implicitConversions | ||
|
||
|
||
package object dom extends Attributes with Tags with Handlers { | ||
package object dom extends Attributes with Tags with HandlerFactories { | ||
|
||
type VNode = IO[VNode_] | ||
type VDomModifier = IO[VDomModifier_] | ||
|
||
type Observable[+A] = rxscalajs.Observable[A] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like the direction of outwatch becoming frp-backend agnostic. 👍 |
||
val Observable = rxscalajs.Observable | ||
|
||
type Sink[-A] = outwatch.Sink[A] | ||
val Sink = outwatch.Sink | ||
|
||
type Pipe[-I, +O] = outwatch.Pipe[I, O] | ||
val Pipe = outwatch.Pipe | ||
|
||
type Handler[T] = outwatch.Handler[T] | ||
val Handler = outwatch.Handler | ||
|
||
implicit def stringNode(string: String): VDomModifier = IO.pure(StringNode(string)) | ||
|
||
implicit def optionIsEmptyModifier(opt: Option[VDomModifier]): VDomModifier = opt getOrElse IO.pure(EmptyVDomModifier) | ||
|
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,6 @@ | ||
import rxscalajs.Observable | ||
|
||
package object outwatch { | ||
type Pipe[-I, +O] = Observable[O] with Sink[I] with PipeOps[I, O] | ||
type Handler[T] = Pipe[T,T] | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly, with all these methods we cannot overload and return a
Handler
ifI
andO
are the same type...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fdietze What do you think about having this:
and leaving
type Handler[T] = Pipe[T, T]
. This way aHandler
is aPipe
,transformHandlerSink
, andtransformHandlerSource
will not be needed any more (will be the same astransformSink
andtransformSource
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I'll give it a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I think it's much cleaner now.