首页 \ 问答 \ 将Kotlin编译为JavaScript(Compile Kotlin to JavaScript)

将Kotlin编译为JavaScript(Compile Kotlin to JavaScript)

我已经开始研究Kotlin,我想用它代替TypeScript作为我的前端语言。 都编译为JavaScript,所以我想设置它以便创建一个Kotlin文件,我们称它为myFile.kt ,然后当我运行编译器时,它会生成一个myFile.js文件。

就像TypeScript如何使用myFile.ts并将其编译到myFile.js

我正在使用最新版本的IntelliJ 15与Kotlin的候选版本1。

我一直在搜索整个互联网的方式来做到这一点,但到目前为止,我发现设置IntelliJ的目的是让Kotlin从你的代码中创建一个JavaScript库到一个JAR文件中。 我也无法获得编译我的项目中的任何kt文件(他们以前是js文件)。

我现在想要做什么或者我是否以错误的方式思考这个问题?


I've started looking into Kotlin and I'd like to use it instead of TypeScript for my front end language. Both compile down to JavaScript so I'd like to set it up so that I make a Kotlin file, lets call it myFile.kt, then when I run the compiler it makes a myFile.js file.

Just like how TypeScript takes a myFile.ts and compiles it to a myFile.js.

I'm using the latest version of IntelliJ 15 with release candidate 1 of Kotlin.

I've been searching all over the internet for the way to do this but so far all I've found is setting up IntelliJ so that Kotlin creates a JavaScript library from your code in a JAR file. I also haven't been able to get that to compile any of the kt files in my project (they were previously js files).

Is what I'd like to do currently possible or am I thinking about this in the wrong way?


原文:https://stackoverflow.com/questions/35282237
更新时间:2022-11-10 09:11

最满意答案

Facebook的做法非常有趣。

执行此类通知的常见方法是在给定的间隔(可能每隔几秒钟)轮询服务器上的脚本(使用AJAX),以检查是否发生了某些事情。 然而,这可能是网络密集型的,你经常做出无意义的请求,因为没有发生任何事情。

Facebook的方式是使用彗星方法,而不是轮询一个间隔,一旦一个投票完成,它发出另一个。 但是,对服务器上的脚本的每个请求都有非常长的超时,服务器只有在发生事件时才响应该请求。 如果您在Facebook上启动了Firebug的“控制台”选项卡,则可能会发现这一点,请求脚本可能需要几分钟。 这是非常巧妙的,因为这种方法会立即减少请求的数量,以及发送数量的频率。 您现在有一个事件框架可以让服务器“触发”事件。

在这之后,根据这些民意测验返回的实际内容,这是一个JSON响应,似乎是一个事件列表,以及有关它们的信息。 尽管如此,还是有点难看。

在实际的技术方面,AJAX是通往这里的方式,因为您可以控制请求超时等诸多事情。 我建议(Stack overflow cliche here)使用jQuery来做AJAX,它会占用很多交叉可兼容性的问题。 在PHP方面,您可以简单地轮询您的PHP脚本中的事件日志数据库表,只有在发生事件时才返回客户端? 我期望有很多方法可以实现这一点。

实现:

服务器端:

在PHP中似乎有一些彗星库的实现,但是老实说,它真的很简单,这可能就像下面的伪代码一样:

while(!has_event_happened()) {
   sleep(5);
}

echo json_encode(get_events());
  • has_event_happened函数只会检查事件表或某些事件中是否发生过任何事情,然后get_events函数将返回表中新行的列表? 取决于问题的上下文。

  • 不要忘记更改您的PHP最大执行时间,否则会提前超时!

客户端:

看看jQuery插件进行Comet交互:

  • 项目主页: http : //plugins.jquery.com/project/Comet
  • Google代码: http : //code.google.com/p/jquerycomet/ - 看起来在subversion版本库中有一些示例用法。

也就是说,这个插件似乎增加了一些复杂性,在客户端上真的很简单,也许(和jQuery)一样:

function doPoll() {
   $.get("events.php", {}, function(result) {
      $.each(result.events, function(event) { //iterate over the events
          //do something with your event
      });
      doPoll(); 
      //this effectively causes the poll to run again as
      //soon as the response comes back
   }, 'json'); 
}

$(document).ready(function() {
    $.ajaxSetup({
       timeout: 1000*60//set a global AJAX timeout of a minute
    });
    doPoll(); // do the first poll
});

整个事情取决于你现有的架构如何组合在一起。


The way Facebook does this is pretty interesting.

A common method of doing such notifications is to poll a script on the server (using AJAX) on a given interval (perhaps every few seconds), to check if something has happened. However, this can be pretty network intensive, and you often make pointless requests, because nothing has happened.

The way Facebook does it is using the comet approach, rather than polling on an interval, as soon as one poll completes, it issues another one. However, each request to the script on the server has an extremely long timeout, and the server only responds to the request once something has happened. You can see this happening if you bring up Firebug's Console tab while on Facebook, with requests to a script possibly taking minutes. It is quite ingenious really, since this method cuts down immediately on both the number of requests, and how often you have to send them. You effectively now have an event framework that allows the server to 'fire' events.

Behind this, in terms of the actual content returned from those polls, it's a JSON response, with what appears to be a list of events, and info about them. It's minified though, so is a bit hard to read.

In terms of the actual technology, AJAX is the way to go here, because you can control request timeouts, and many other things. I'd recommend (Stack overflow cliche here) using jQuery to do the AJAX, it'll take a lot of the cross-compability problems away. In terms of PHP, you could simply poll an event log database table in your PHP script, and only return to the client when something happens? There are, I expect, many ways of implementing this.

Implementing:

Server Side:

There appear to be a few implementations of comet libraries in PHP, but to be honest, it really is very simple, something perhaps like the following pseudocode:

while(!has_event_happened()) {
   sleep(5);
}

echo json_encode(get_events());
  • The has_event_happened function would just check if anything had happened in an events table or something, and then the get_events function would return a list of the new rows in the table? Depends on the context of the problem really.

  • Don't forget to change your PHP max execution time, otherwise it will timeout early!

Client Side:

Take a look at the jQuery plugin for doing Comet interaction:

That said, the plugin seems to add a fair bit of complexity, it really is very simple on the client, perhaps (with jQuery) something like:

function doPoll() {
   $.get("events.php", {}, function(result) {
      $.each(result.events, function(event) { //iterate over the events
          //do something with your event
      });
      doPoll(); 
      //this effectively causes the poll to run again as
      //soon as the response comes back
   }, 'json'); 
}

$(document).ready(function() {
    $.ajaxSetup({
       timeout: 1000*60//set a global AJAX timeout of a minute
    });
    doPoll(); // do the first poll
});

The whole thing depends a lot on how your existing architecture is put together.

相关问答

更多
  • Facebook的做法非常有趣。 执行此类通知的常见方法是在给定的间隔(可能每隔几秒钟)轮询服务器上的脚本(使用AJAX),以检查是否发生了某些事情。 然而,这可能是网络密集型的,你经常做出无意义的请求,因为没有发生任何事情。 Facebook的方式是使用彗星方法,而不是轮询一个间隔,一旦一个投票完成,它发出另一个。 但是,对服务器上的脚本的每个请求都有非常长的超时,服务器只有在发生事件时才响应该请求。 如果您在Facebook上启动了Firebug的“控制台”选项卡,则可能会发现这一点,请求脚本可能需要几 ...
  • MailMessage类正在为正文查找单个字符串。 你可以在这里阅读关于这个类的文档 。 您当前的$Body变量是一个字符串数组。 public MailMessage( string from, string to, string subject, string body ) 将$Body变量转换为单个字符串,方法是将其传递给Out-String如评论中提到的@JosefZ: $EmailBody = $myTable | ConvertTo-Csv -NoTyp ...
  • 我有完全相同的问题,我可以通过在https://developers.facebook.com/apps应用程序基本信息的网站部分中输入一个url来解决这个问题,添加一个应用程序域和一个画布url ...我因为这是通知链接的网址,所以怀疑它只能使用画布网址。 希望能帮助到你, 干杯! I have had the exact same problem, I was able to fix this by entering a url in the Website part of https://develo ...
  • 问题已解决。 我无法从facebook获得POST回调,因为我托管了我的rails app n heroku。 使用Heroku它限制了单个进程,因此我只从Facebook获得GET而不是POST。 Heroku使用单个dyno阻止其他进程。 为了支持我们需要让两个dynos从facebook获得回调。 Problem fixed. I am not able to get POST callback from facebook because i hosted my rails app n heroku. ...
  • 你应该有一个NodeJS和socket.io ,将它安装在你的服务器上 当有事件时,使用Redis保存变量并将其发送到NodeJs或使用此PHP库,您可以将变量从PHP发送到NodeJS服务器: http : //elephant.io/ NodeJs上的数据处理 返回PHP页面使用Socket.io (client javascript)来捕获此事件并显示给最终用户(使用emit和on ) 您可以使用elephant.io来执行此指令: 将PHP与Socket.io一起使用 或者使用Redis : htt ...
  • 对于这种情况,Pubsub将是一个合适的解决方案。 它可以从我所看到的内容中处理您的所有要求。 如果您希望用户为每种消息类型订阅的方式不同,则需要为每种事件类型设置一个节点(或者,可以在单个节点上使用过滤器,但我认为多个节点更容易)。 节点所有者可以允许任何人发布到节点,因此这不是问题。 提到了PEP,但如果您想要发布给不在您名单中的用户,则不是正确的解决方案。 如果消息传输的简单行为足够丰富,Pubsub还允许您定义所需的消息内容类型或根本不定义消息。 Pubsub would be an appropr ...
  • 根据文件 - 目前,只有Facebook.com上的应用才能使用应用通知。 通知仅出现在Facebook.com的桌面版本上。 所以你必须在facebook内有一个画布应用程序来发送通知。 另外,还有其他不同的分享方式,看看这篇文章: 在Android中分享 编辑: 实施/feed API- private void publishStory() { Session session = Session.getActiveSession(); if (session != null){ // Ch ...
  • 您可以在设备上获取已安装应用程序的列表,并检查是否已安装您要查找的应用程序。 然后为用户提供选择他想要分享的应用程序的选项 List packages = getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo appInfo : packages) { if ("com.facebook.katana".equals(appI ...
  • 没有任何解决方案可以让Facebook的服务器通过webhooks通知访问您的代码,而无法通过可用的公共IP地址访问您的代码,而该IP地址正在收听Facebook的通知 如果托管代码的机器只有一个私有IP,那么您需要某种具有公共IP的代理来将相关请求转发给该机器 There is no solution that will let Facebook's servers reach your code with webhooks notifications without your code being re ...
  • 使用新的subscribed_apps端点 - 无需将应用程序添加到该页面的页面: https : //developers.facebook.com/docs/graph-api/reference/v2.3/page/subscribed_apps 为了使Realtime Updates正常工作,您需要使用subscribed_apps和订阅端点。 Use the new subscribed_apps endpoint - no need to add the App to a Page for th ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)