Skip to content

Commit 8ae9f97

Browse files
committed
Merge pull request scala-js#108 from ochrons/ajaxupdate
Add support for sending ByteBuffers, typed arrays and Blobs in Ajax calls.
2 parents 407f033 + e6c0647 commit 8ae9f97

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

project/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.0")
1+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.2")
22

33
addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.2.1")

src/main/scala/org/scalajs/dom/ext/Extensions.scala

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package org.scalajs.dom.ext
22

3+
import java.nio.ByteBuffer
4+
35
import scala.language.implicitConversions
46
import scala.concurrent.{Promise, Future}
57

68
import scala.scalajs.js
9+
import scala.scalajs.js.typedarray._
10+
import scala.scalajs.js.typedarray.TypedArrayBufferOps._
711

812
import org.scalajs.dom
913
import org.scalajs.dom.{html, raw}
14+
import org.scalajs.dom.raw.Blob
15+
1016

1117
/**
1218
* Used to extend out javascript *Collections to make them usable as normal
@@ -236,42 +242,73 @@ case class AjaxException(xhr: dom.XMLHttpRequest) extends Exception {
236242
* Wraps an XMLHttpRequest to provide an easy one-line way of making
237243
* an Ajax call, returning a Future.
238244
*/
239-
object Ajax{
245+
object Ajax {
246+
/**
247+
* Supported data formats for Ajax are implicitly converted to InputData
248+
*/
249+
sealed trait InputData extends js.Any
250+
251+
object InputData {
252+
implicit def str2ajax(s: String): InputData = s.asInstanceOf[InputData]
253+
254+
implicit def arrayBufferView2ajax(b: ArrayBufferView): InputData = b.asInstanceOf[InputData]
255+
256+
implicit def blob2ajax(b: Blob): InputData = b.asInstanceOf[InputData]
257+
258+
implicit def byteBuffer2ajax(data: ByteBuffer): InputData = {
259+
if (data.hasTypedArray()) {
260+
// get relevant part of the underlying typed array
261+
data.typedArray().subarray(data.position, data.limit)
262+
} else {
263+
// fall back to copying the data
264+
val tempBuffer = ByteBuffer.allocateDirect(data.remaining)
265+
val origPosition = data.position
266+
tempBuffer.put(data)
267+
data.position(origPosition)
268+
tempBuffer.typedArray()
269+
}
270+
}
271+
}
272+
240273
def get(url: String,
241-
data: String = "",
274+
data: InputData = "",
242275
timeout: Int = 0,
243276
headers: Map[String, String] = Map.empty,
244-
withCredentials: Boolean = false,
277+
withCredentials: Boolean = false,
245278
responseType: String = "") = {
246279
apply("GET", url, data, timeout, headers, withCredentials, responseType)
247280
}
281+
248282
def post(url: String,
249-
data: String = "",
283+
data: InputData = "",
250284
timeout: Int = 0,
251285
headers: Map[String, String] = Map.empty,
252-
withCredentials: Boolean = false,
286+
withCredentials: Boolean = false,
253287
responseType: String = "") = {
254288
apply("POST", url, data, timeout, headers, withCredentials, responseType)
255289
}
290+
256291
def put(url: String,
257-
data: String = "",
258-
timeout: Int = 0,
259-
headers: Map[String, String] = Map.empty,
260-
withCredentials: Boolean = false,
261-
responseType: String = "") = {
292+
data: InputData = "",
293+
timeout: Int = 0,
294+
headers: Map[String, String] = Map.empty,
295+
withCredentials: Boolean = false,
296+
responseType: String = "") = {
262297
apply("PUT", url, data, timeout, headers, withCredentials, responseType)
263298
}
299+
264300
def delete(url: String,
265-
data: String = "",
301+
data: InputData = "",
266302
timeout: Int = 0,
267303
headers: Map[String, String] = Map.empty,
268304
withCredentials: Boolean = false,
269305
responseType: String = "") = {
270306
apply("DELETE", url, data, timeout, headers, withCredentials, responseType)
271307
}
308+
272309
def apply(method: String,
273310
url: String,
274-
data: String,
311+
data: InputData,
275312
timeout: Int,
276313
headers: Map[String, String],
277314
withCredentials: Boolean,

0 commit comments

Comments
 (0)