Sitelet https://github.com/shogowada/scala-json-rpc/commit/fef32e87edf92f64bbce1d32d6d8e1e8d73adb6d
Skip to content

Commit fef32e8

Browse files
authored
Support disposable function inside either (#33)
* Refactor * Refactor * Refactor * Refactor * Refactor * Refactor * Extract client parameter factory * Refactor * Refactor * Combine result factory of client and server * Combine parameter factory of client and server * Refactor * Refactor * Share the code between parameter and result factory * Support DisposableFunction inside Either * Eliminate duplication * Refactor * Install node via NVM * Fix version number * Update README.md
1 parent cc7c427 commit fef32e8

20 files changed

Lines changed: 436 additions & 226 deletions

‎.travis.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ before_script:
2626
- "export DISPLAY=:99"
2727
- "sh -e /etc/init.d/xvfb start"
2828
- sleep 3 # give xvfb some time to start
29+
- ". $HOME/.nvm/nvm.sh"
30+
- "nvm install node"
31+
- "nvm use node"
2932

3033
script:
3134
- sbt ++$TRAVIS_SCALA_VERSION test

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ def onResponseJSONReceived(responseJSON: String): Unit = {
108108

109109
|Platform|SBT|Scala Version|Scala JS Version|
110110
|---|---|---|---|
111-
|JVM|```"io.github.shogowada" %% "scala-json-rpc" % "0.9.0"```|2.12||
112-
|JS|```"io.github.shogowada" %%% "scala-json-rpc" % "0.9.0"```|2.12|0.6.15+|
111+
|JVM|```"io.github.shogowada" %% "scala-json-rpc" % "0.9.1"```|2.12||
112+
|JS|```"io.github.shogowada" %%% "scala-json-rpc" % "0.9.1"```|2.12|0.6.15+|
113113

114114
scala-json-rpc has **no external dependency**, so it should fit into any of your Scala JVM & JS applications.
115115

‎build.sbt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ publishArtifact := false
1212
val commonSettings = Seq(
1313
organization := "io.github.shogowada",
1414
name := "scala-json-rpc",
15-
version := "0.9.0",
15+
version := "0.9.1",
1616
scalaVersion := "2.12.2",
1717
logBuffered in Test := false,
1818
licenses := Seq("MIT" -> url(/sitelet?url=https%3A%2F%2Fgithub.com%2Fshogowada%2Fscala-json-rpc%2Fcommit%2F%253Cspan%2520class%3D%2522pl-s%2522%253E%253Cspan%2520class%3D%2522pl-pds%2522%253E%26quot%3B%253C%2Fspan%253Ehttps%3A%2F%2Fopensource.org%2Flicenses%2FMIT%253Cspan%2520class%3D%2522pl-pds%2522%253E%26quot%3B%253C%2Fspan%253E%253C%2Fspan%253E)),
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.github.shogowada.scala.jsonrpc
22

33
object Constants {
4-
final val JSONRPC = "2.0"
4+
final val JSONRPC: String = "2.0"
55

6-
final val ReservedMethodNamePrefix = "io.github.shogowada.scala.jsonrpc."
6+
final val ReservedMethodNamePrefix: String = "io.github.shogowada.scala.jsonrpc."
77

8-
final val FunctionMethodNamePrefix = ReservedMethodNamePrefix + "function."
8+
final val FunctionMethodNamePrefix: String = ReservedMethodNamePrefix + "function."
99

10-
final val DisposeMethodName = ReservedMethodNamePrefix + "dispose"
10+
final val DisposeMethodName: String = ReservedMethodNamePrefix + "dispose"
1111
}

‎shared/src/main/scala/io/github/shogowada/scala/jsonrpc/JSONRPCServerAndClient.scala‎

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.github.shogowada.scala.jsonrpc
33
import io.github.shogowada.scala.jsonrpc.client.{JSONRPCClient, JSONRPCClientMacro}
44
import io.github.shogowada.scala.jsonrpc.serializers.JSONSerializer
55
import io.github.shogowada.scala.jsonrpc.server.{JSONRPCServer, JSONRPCServerMacro}
6-
import io.github.shogowada.scala.jsonrpc.utils.JSONRPCMacroUtils
6+
import io.github.shogowada.scala.jsonrpc.common.JSONRPCMacroUtils
77

88
import scala.concurrent.{ExecutionContext, Future}
99
import scala.language.experimental.macros
@@ -39,8 +39,10 @@ object JSONRPCServerAndClientMacro {
3939
val bindAPI = JSONRPCServerMacro.bindAPIImpl[c.type, API](c)(server, Some(client), api)
4040
c.Expr[Unit](
4141
q"""
42-
$serverAndClientDefinition
43-
$bindAPI
42+
{
43+
$serverAndClientDefinition
44+
$bindAPI
45+
}
4446
"""
4547
)
4648
}
@@ -54,8 +56,10 @@ object JSONRPCServerAndClientMacro {
5456
val createAPI = JSONRPCClientMacro.createAPIImpl[c.type, API](c)(client, Some(server))
5557
c.Expr[API](
5658
q"""
57-
$serverAndClientDefinition
58-
$createAPI
59+
{
60+
$serverAndClientDefinition
61+
$createAPI
62+
}
5963
"""
6064
)
6165
}
@@ -69,30 +73,32 @@ object JSONRPCServerAndClientMacro {
6973
val executionContext: c.Expr[ExecutionContext] = c.Expr(q"$server.executionContext")
7074
c.Expr[Future[Unit]](
7175
q"""
72-
..${macroUtils.imports}
73-
$serverAndClientDefinition
74-
def receiveAndSend(json: String): Future[Unit] = {
75-
val wasJSONRPCResponse: Boolean = $client.receive(json)
76-
if (!wasJSONRPCResponse) {
77-
$server.receive(json)
78-
.flatMap((maybeResponseJSONFromUs: Option[String]) => {
79-
maybeResponseJSONFromUs match {
80-
case Some(responseJSONFromUs) => $client.send(responseJSONFromUs)
81-
case None => Future(None)($executionContext)
82-
}
83-
})($executionContext)
84-
.flatMap((maybeResponseJSONFromThem: Option[String]) => {
85-
maybeResponseJSONFromThem match {
86-
case Some(responseJSONFromThem) => receiveAndSend(responseJSONFromThem)
87-
case None => Future(None)($executionContext)
88-
}
89-
})($executionContext)
90-
.map(_ => ())
91-
} else {
92-
Future()($executionContext)
76+
{
77+
..${macroUtils.imports}
78+
$serverAndClientDefinition
79+
def receiveAndSend(json: String): Future[Unit] = {
80+
val wasJSONRPCResponse: Boolean = $client.receive(json)
81+
if (!wasJSONRPCResponse) {
82+
$server.receive(json)
83+
.flatMap((maybeResponseJSONFromUs: Option[String]) => {
84+
maybeResponseJSONFromUs match {
85+
case Some(responseJSONFromUs) => $client.send(responseJSONFromUs)
86+
case None => Future(None)($executionContext)
87+
}
88+
})($executionContext)
89+
.flatMap((maybeResponseJSONFromThem: Option[String]) => {
90+
maybeResponseJSONFromThem match {
91+
case Some(responseJSONFromThem) => receiveAndSend(responseJSONFromThem)
92+
case None => Future(None)($executionContext)
93+
}
94+
})($executionContext)
95+
.map(_ => ())
96+
} else {
97+
Future()($executionContext)
98+
}
9399
}
100+
receiveAndSend($json)
94101
}
95-
receiveAndSend($json)
96102
"""
97103
)
98104
}

‎shared/src/main/scala/io/github/shogowada/scala/jsonrpc/client/DisposableFunctionClientFactoryMacro.scala‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.shogowada.scala.jsonrpc.client
22

3-
import io.github.shogowada.scala.jsonrpc.utils.JSONRPCMacroUtils
3+
import io.github.shogowada.scala.jsonrpc.common.JSONRPCMacroUtils
44

55
import scala.concurrent.Future
66
import scala.reflect.macros.blackbox
@@ -9,18 +9,18 @@ class DisposableFunctionClientFactoryMacro[CONTEXT <: blackbox.Context](val c: C
99

1010
import c.universe._
1111

12-
lazy val macroUtils = JSONRPCMacroUtils[c.type](c)
13-
lazy val methodClientFactoryMacro = new JSONRPCMethodClientFactoryMacro[c.type](c)
12+
private lazy val macroUtils = JSONRPCMacroUtils[c.type](c)
13+
private lazy val methodClientFactoryMacro = new JSONRPCMethodClientFactoryMacro[c.type](c)
1414

1515
def getOrCreate(
16-
server: c.Tree,
1716
client: c.Tree,
18-
disposableFunctionType: c.Type,
19-
disposableFunctionMethodName: c.Tree
17+
server: c.Tree,
18+
disposableFunctionMethodName: c.Tree,
19+
disposableFunctionType: c.Type
2020
): c.Tree = {
2121
val disposableFunctionRepository = macroUtils.getDisposableFunctionRepository(server)
2222

23-
val newDisposableFunction = create(server, client, disposableFunctionType, disposableFunctionMethodName)
23+
val newDisposableFunction = create(client, server, disposableFunctionType, disposableFunctionMethodName)
2424

2525
q"""
2626
$disposableFunctionRepository
@@ -30,8 +30,8 @@ class DisposableFunctionClientFactoryMacro[CONTEXT <: blackbox.Context](val c: C
3030
}
3131

3232
private def create(
33-
server: c.Tree,
3433
client: c.Tree,
34+
server: c.Tree,
3535
disposableFunctionType: c.Type,
3636
disposableFunctionMethodName: c.Tree
3737
): c.Tree = {

‎shared/src/main/scala/io/github/shogowada/scala/jsonrpc/client/DisposableFunctionMethodNameRepository.scala‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import io.github.shogowada.scala.jsonrpc.{Constants, DisposableFunction}
66

77
class DisposableFunctionMethodNameRepository {
88

9-
var identifierToMethodNameMap: Map[Any, String] = Map()
10-
var methodNameToIdentifierMap: Map[String, Any] = Map()
9+
private var identifierToMethodNameMap: Map[Any, String] = Map.empty
10+
private var methodNameToIdentifierMap: Map[String, Any] = Map.empty
1111

1212
def getOrAddAndNotify(disposableFunction: DisposableFunction, notify: (String) => Unit): String = this.synchronized {
1313
val identifier = disposableFunction.identifier

‎shared/src/main/scala/io/github/shogowada/scala/jsonrpc/client/JSONRPCClient.scala‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package io.github.shogowada.scala.jsonrpc.client
22

33
import io.github.shogowada.scala.jsonrpc.Types.{Id, JSONSender}
44
import io.github.shogowada.scala.jsonrpc.serializers.JSONSerializer
5-
import io.github.shogowada.scala.jsonrpc.utils.JSONRPCMacroUtils
5+
import io.github.shogowada.scala.jsonrpc.common.JSONRPCMacroUtils
66

77
import scala.concurrent.{ExecutionContext, Future, Promise}
88
import scala.language.experimental.macros
99
import scala.reflect.macros.blackbox
1010

11-
class JSONRPCClient[JSONSerializerInUse <: JSONSerializer]
12-
(
11+
class JSONRPCClient[JSONSerializerInUse <: JSONSerializer](
1312
val jsonSerializer: JSONSerializerInUse,
1413
val jsonSender: JSONSender,
1514
val executionContext: ExecutionContext
@@ -45,8 +44,10 @@ object JSONRPCClientMacro {
4544
val api = createAPIImpl[c.type, API](c)(client, None)
4645
c.Expr[API](
4746
q"""
48-
$clientDefinition
49-
$api
47+
{
48+
$clientDefinition
49+
$api
50+
}
5051
"""
5152
)
5253
}
@@ -142,14 +143,16 @@ object JSONRPCClientMacro {
142143

143144
c.Expr[Boolean](
144145
q"""
145-
..${macroUtils.imports}
146-
$clientDefinition
147-
$maybePromisedResponse
148-
.map(promisedResponse => {
149-
promisedResponse.success($json)
150-
true
151-
})
152-
.getOrElse(false)
146+
{
147+
..${macroUtils.imports}
148+
$clientDefinition
149+
$maybePromisedResponse
150+
.map(promisedResponse => {
151+
promisedResponse.success($json)
152+
true
153+
})
154+
.getOrElse(false)
155+
}
153156
"""
154157
)
155158
}

0 commit comments

Comments
 (0)