Skip to content

Commit

Permalink
Merge pull request #28 from outr/Simplification
Browse files Browse the repository at this point in the history
Simplification
  • Loading branch information
darkfrog26 authored Mar 19, 2020
2 parents a52ee9d + 3d8806f commit dbe47f4
Show file tree
Hide file tree
Showing 35 changed files with 289 additions and 546 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ language: scala
sudo: required
dist: trusty
scala:
- 2.12.8
- 2.13.1
jdk:
- oraclejdk8
before_script:
- curl https://raw.githubusercontent.com/scala-native/scala-native/master/scripts/travis_setup.sh | bash -x
script:
- sbt +clean +test
- sbt +clean +reactifyJVM/test +reactifyJS/test ++2.11.12 reactifyNative/test
- sbt coverage reactifyJVM/test
- sbt coverageReport
- sbt coverageAggregate
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[![Build Status](https://travis-ci.org/outr/reactify.svg?branch=master)](https://travis-ci.org/outr/reactify)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/759324d19db5496dbd9867b4a113c806)](https://www.codacy.com/app/matthicks/reactify?utm_source=github.com&utm_medium=referral&utm_content=outr/reactify&utm_campaign=Badge_Grade)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/759324d19db5496dbd9867b4a113c806)](https://www.codacy.com/app/matthicks/reactify?utm_source=github.com&utm_medium=referral&utm_content=outr/reactify&utm_campaign=Badge_Coverage)
[![Stories in Ready](https://badge.waffle.io/outr/reactify.png?label=ready&title=Ready)](https://waffle.io/outr/reactify)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/outr/reactify)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.outr/reactify_2.12/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.outr/reactify_2.12)
[![Latest version](https://index.scala-lang.org/outr/reactify/reactify/latest.svg)](https://index.scala-lang.org/outr/reactify)
Expand Down Expand Up @@ -34,13 +33,13 @@ reactify is published to Sonatype OSS and Maven Central currently supporting:
Configuring the dependency in SBT simply requires:

```
libraryDependencies += "com.outr" %% "reactify" % "3.0.3"
libraryDependencies += "com.outr" %% "reactify" % "4.0.0"
```

or, for Scala.js / Scala Native / cross-building:

```
libraryDependencies += "com.outr" %%% "reactify" % "3.0.3"
libraryDependencies += "com.outr" %%% "reactify" % "4.0.0"
```

## Concepts
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}

name in ThisBuild := "reactify"
organization in ThisBuild := "com.outr"
version in ThisBuild := "3.0.6"
version in ThisBuild := "4.0.0-SNAPSHOT"
scalaVersion in ThisBuild := "2.13.1"
crossScalaVersions in ThisBuild := List("2.13.1", "2.12.8", "2.11.12")

Expand Down
43 changes: 4 additions & 39 deletions reactify/src/main/scala/reactify/Channel.scala
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
package reactify

import reactify.group.ChannelGroup
import reactify.standard.StandardChannel

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

/**
* Channel is a stateless Reactive implementation exposing a public method to fire values.
*
* @tparam T the type of value this Reactive receives
*/
trait Channel[T] extends Reactive[T] {
/**
* Public method to fire a value against the Reactions attached to this Channel
*
* @param value the function value
*/
def set(value: => T): Unit

/**
* Convenience method to fire a value
*
* @see #set
* @param value the function value
*/
def :=(value: => T): Unit = set(value)

/**
* Convenience method for static (non-functional) invocation.
*
* @see #set
* @param value the value
*/
def @=(value: T): Unit = set(value)

/**
* Convenience functionality to assign the result of a future (upon completion) to this Channel
*/
def !(future: Future[T]): Future[Unit] = future.map { value =>
set(value)
}
class Channel[T] extends Reactive[T] with Mutable[T] {
override def set(f: => T): Unit = fire(f, None, reactions())

/**
* Group multiple channels together
Expand All @@ -50,7 +18,7 @@ trait Channel[T] extends Reactive[T] {
/**
* Group multiple channels together
*/
def and(that: Channel[T]): Channel[T] = ChannelGroup(None, List(this, that))
def and(that: Channel[T]): Channel[T] = ChannelGroup(List(this, that))

/**
* Functional mapping of this Channel into another Channel. All values received by this Channel will be mapped and
Expand Down Expand Up @@ -82,11 +50,8 @@ trait Channel[T] extends Reactive[T] {
}
channel
}

override def toString: String = name.getOrElse("Channel")
}

object Channel {
def apply[T]: Channel[T] = new StandardChannel[T](None)
def apply[T](name: Option[String]): Channel[T] = new StandardChannel[T](name)
def apply[T]: Channel[T] = new Channel[T]
}
24 changes: 15 additions & 9 deletions reactify/src/main/scala/reactify/Dep.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package reactify

import reactify.standard.StandardDep
import reactify.reaction.{Reaction, ReactionStatus}

/**
* Dep allows creation of a dependent `Var` on another `Var` allowing conversion between the two. This can be useful for
Expand All @@ -21,16 +21,22 @@ import reactify.standard.StandardDep
* @tparam T the type of value this Reactive receives
* @tparam R the type that this Dep receives
*/
trait Dep[T, R] extends Var[T] {
def owner: Var[R]
def t2R(t: T): R
def r2T(r: R): T
class Dep[T, R] protected(val owner: Var[R], t2R: T => R, r2T: R => T) extends Reactive[T] with Stateful[T] with Mutable[T] {
private val v: Val[T] = Val(r2T(owner))

v.reactions += new Reaction[T] {
override def apply(value: T, previous: Option[T]): ReactionStatus = {
fire(value, previous, reactions())
ReactionStatus.Continue
}
}

override def get: T = v.get

override def set(f: => T): Unit = owner := t2R(f)
}

object Dep {
def apply[T, R](owner: Var[R])
(implicit r2T: R => T, t2R: T => R): Dep[T, R] = new StandardDep[T, R](None, owner, r2T, t2R)
def apply[T, R](owner: Var[R],
name: String)
(implicit r2T: R => T, t2R: T => R): Dep[T, R] = new StandardDep[T, R](Option(name), owner, r2T, t2R)
(implicit r2T: R => T, t2R: T => R): Dep[T, R] = new Dep[T, R](owner, t2R, r2T)
}
18 changes: 18 additions & 0 deletions reactify/src/main/scala/reactify/Mutable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package reactify

import scala.concurrent.{ExecutionContext, Future}

trait Mutable[T] {
def set(f: => T): Unit
def static(f: T): Unit = set(f)

def :=(f: => T): Unit = set(f)
def @=(f: T): Unit = static(f)

/**
* Convenience functionality to assign the result of a future (upon completion) to this Channel
*/
def !(future: Future[T])(implicit ec: ExecutionContext): Future[Unit] = future.map { value =>
set(value)
}
}
2 changes: 1 addition & 1 deletion reactify/src/main/scala/reactify/Priority.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ object Priority {
val Lowest: Double = Double.MinValue
val Low: Double = -100.0
val Normal: Double = 0.0
val High: Double = 100.0D
val High: Double = 100.0
val Highest: Double = Double.MaxValue
}
15 changes: 2 additions & 13 deletions reactify/src/main/scala/reactify/Reactive.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package reactify

import reactify.reaction.{Reaction, ReactionStatus, Reactions}
import reactify.standard.StandardReactions

import scala.annotation.tailrec
import scala.concurrent.{Future, Promise}

/**
* Reactive is the core trait for Reactify. The basic premise is that a Reactive represents an instance that can attach
* Reactions and fire instances of `T` that are received by those Reactions.
* Reactions and fire `T` and are received by those Reactions.
*
* @tparam T the type of value this Reactive receives
*/
trait Reactive[T] {
/**
* An optional name associated. This is primarily used for distinguishing between instances as well as logging.
*/
def name: Option[String] = None

private lazy val _status = new ThreadLocal[Option[ReactionStatus]] {
override def initialValue(): Option[ReactionStatus] = None
}
Expand All @@ -40,7 +34,7 @@ trait Reactive[T] {
/**
* Reactions currently associated with this Reactive
*/
lazy val reactions: Reactions[T] = new StandardReactions[T]
lazy val reactions: Reactions[T] = new Reactions[T]

/**
* Convenience method to create a Reaction to attach to this Reactive
Expand Down Expand Up @@ -138,8 +132,3 @@ trait Reactive[T] {
}
}

object Reactive {
private[reactify] def fire[T](reactive: Reactive[T], value: T, previous: Option[T]): Unit = {
reactive.fire(value, previous, reactive.reactions())
}
}
145 changes: 0 additions & 145 deletions reactify/src/main/scala/reactify/State.scala

This file was deleted.

Loading

0 comments on commit dbe47f4

Please sign in to comment.