-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntrypointInvoker.scala
46 lines (36 loc) · 1.23 KB
/
EntrypointInvoker.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package japgolly.webapputil.entrypoint
import japgolly.microlibs.stdlib_ext.EscapeUtils
import java.lang.{StringBuilder => JStringBuilder}
object EntrypointInvoker {
def apply[I](defn: EntrypointDef[I]): EntrypointInvoker[I] =
new EntrypointInvoker(defn)
// TODO Potential optimisation: have this estimate a good initial SB size for itself by observing past results
private[EntrypointInvoker] final val ExpectedJsLength = 256
}
final class EntrypointInvoker[Input](defn: EntrypointDef[Input]) {
import EntrypointInvoker.ExpectedJsLength
private val runCmdHead =
defn.objectAndMethod + "(\""
private val appendEncoded: (JStringBuilder, String) => Unit =
if (defn.codec.escapeEncodedString)
EscapeUtils.appendEscaped
else
_.append(_)
private def call(sb: JStringBuilder, i: Input): Unit = {
sb.append(runCmdHead)
appendEncoded(sb, defn.codec.encode(i))
sb.append("\")")
}
def apply(i: Input): Js = {
val sb = new JStringBuilder(ExpectedJsLength)
call(sb, i)
Js(sb.toString)
}
def apply(w: Js.Wrapper, i: Input): Js = {
val sb = new JStringBuilder(ExpectedJsLength + w.totalLength)
sb.append(w.before)
call(sb, i)
sb.append(w.after)
Js(sb.toString)
}
}