|
| 1 | +package dotty.tools.scaladoc |
| 2 | + |
| 3 | +import scala.io.Source |
| 4 | +import dotty.tools.scaladoc.PageEntry |
| 5 | +import org.scalajs.dom.webworkers.Worker |
| 6 | +import org.scalajs.dom._ |
| 7 | +import scala.scalajs.js.{ JSON, Dynamic } |
| 8 | +import scala.collection.mutable.ListBuffer |
| 9 | +import scala.scalajs.js |
| 10 | +import scala.scalajs.js.timers._ |
| 11 | +import org.scalajs.dom.ext.Ajax |
| 12 | +import scala.scalajs.js.URIUtils |
| 13 | + |
| 14 | +import scala.concurrent.ExecutionContext.global |
| 15 | +import scala.concurrent.ExecutionContext |
| 16 | +import scala.concurrent.Future |
| 17 | + |
| 18 | +case class Match( |
| 19 | + prettifiedSignature: String, |
| 20 | + functionName: String, |
| 21 | + packageLocation: String, |
| 22 | + pageLocation: String |
| 23 | +) |
| 24 | + |
| 25 | +case class OutputFormat( |
| 26 | + query: String, |
| 27 | + matches: List[Match] |
| 28 | +) |
| 29 | + |
| 30 | +//TODO CORS problems |
| 31 | +class InkuireDelegateSearchEngine { |
| 32 | + |
| 33 | + given ec: ExecutionContext = global |
| 34 | + |
| 35 | + val ec2 = "http://ec2-52-59-255-148.eu-central-1.compute.amazonaws.com:8080" //TODO configure |
| 36 | + |
| 37 | + private def getURLContent(url: String): Future[String] = Ajax.get(url).map(_.responseText).fallbackTo(Future("[]")) |
| 38 | + |
| 39 | + def dynamicToPageEntry(d: Dynamic): PageEntry = |
| 40 | + PageEntry( |
| 41 | + d.functionName.asInstanceOf[String], |
| 42 | + d.prettifiedSignature.asInstanceOf[String], |
| 43 | + d.pageLocation.asInstanceOf[String], |
| 44 | + d.functionName.asInstanceOf[String], |
| 45 | + List.empty |
| 46 | + ) |
| 47 | + |
| 48 | + def query(s: String)(callback: PageEntry => Node): Unit = { |
| 49 | + val signature = URIUtils.encodeURIComponent(s) |
| 50 | + getURLContent(ec2 + "/forSignature?signature=" + signature).map(JSON.parse(_)).foreach { (d: Dynamic) => |
| 51 | + d.matches.asInstanceOf[js.Array[Dynamic]].map(dynamicToPageEntry).foreach(callback) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +class InkuireJSSearchEngine { |
| 58 | + |
| 59 | + val scriptPath = Globals.pathToRoot + "scripts/" |
| 60 | + val worker = new Worker(s"${scriptPath}inkuire-worker.js") |
| 61 | + |
| 62 | + def dynamicToPageEntry(d: Dynamic): PageEntry = { |
| 63 | + PageEntry( |
| 64 | + d.functionName.asInstanceOf[String], |
| 65 | + d.prettifiedSignature.asInstanceOf[String], |
| 66 | + d.pageLocation.asInstanceOf[String], |
| 67 | + d.functionName.asInstanceOf[String], |
| 68 | + List.empty |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + def query(s: String)(callback: PageEntry => Node): List[PageEntry] = { |
| 73 | + worker.onmessage = _ => () |
| 74 | + val res = ListBuffer[PageEntry]() |
| 75 | + val func = (msg: MessageEvent) => { |
| 76 | + msg.data.asInstanceOf[String] match { |
| 77 | + case "engine_ready" => |
| 78 | + case "new_query" => |
| 79 | + case q => |
| 80 | + val matches = JSON.parse(q).matches |
| 81 | + val actualMatches = matches.asInstanceOf[js.Array[Dynamic]].map(dynamicToPageEntry) |
| 82 | + actualMatches.foreach(callback) |
| 83 | + } |
| 84 | + } |
| 85 | + worker.onmessage = func |
| 86 | + worker.postMessage(s) |
| 87 | + res.toList |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments