我一直在慢慢研究F#带来的所有功能.一个特别引起我兴趣的是MailboxProcessor.
MailboxProcessor视为锁的替代品吗?module Tcp =
open System
open System.Collections.Generic
open System.Net
open System.Net.Sockets
open System.Threading
type SocketAsyncMessage =
| Get of AsyncReplyChannel<SocketAsyncEventArgs>
| Put of SocketAsyncEventArgs
| Dispose of AsyncReplyChannel<MailboxProcessor<SocketAsyncMessage>>
type SocketAsyncEventArgsPool(size:int) =
let agent =
lazy(MailboxProcessor.Start(
(fun inbox ->
let references = lazy(new List<SocketAsyncEventArgs>(size))
let idleReferences = lazy(new Queue<SocketAsyncEventArgs>(size))
let rec loop () =
async {
let! message = inbox.Receive()
match message with
| Get channel ->
if idleReferences.Value.Count > 0 then
channel.Reply(idleReferences.Value.Dequeue())
else …Run Code Online (Sandbox Code Playgroud) 我正在开发Chrome扩展程序,我遇到了以下问题.当某个事件发生时,我的后台页面会创建一个带有chrome.tabs.createAPI 的新选项卡(页面)并发送一个Object.
发送的Object(称为items)是一个对象列表,具有一个名为Item的特定类(原型).
这里有一些代码:
// in background.html
chrome.tabs.create({index: 0, url: "results.html"}, function(tab) {
chrome.tabs.sendRequest(tab.id, {'items': itemsList}, function(response) {
console.log(response.farewall);
});
});
Run Code Online (Sandbox Code Playgroud)
另一方面,在新创建的页面中,我收到发送的对象
// newpage.html
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
console.dir(request.items);
sendResponse({});
}
);
Run Code Online (Sandbox Code Playgroud)
问题是,当我收到对象列表时,newpage.html对象类型丢失了.确实console.dir()在新的中使用了background page,itemsList中的对象类型被正确报告,但是没有在收到的项目列表对象中报告newpage.html.
我可以手动background.html通过字符串手动序列化数据,并手动反序列化newpage.html但我想知道是否有更好的方法来支付这一点并防止列表中的对象类型(即Item)丢失.
我想知道,如何使用erlang发送消息进行处理.我确实启动了一个进程,输出显示pid <0.39.0>.我的问题是如何手动向此过程(<0.39.0>)发送消息.
任何帮助,将不胜感激
了解这些关于Erlang消息传递行为的时间顺序的事实:
如果进程A向进程B发送两条消息,则保证两条消息按它们发送的顺序到达.
如果进程A向进程B发送消息,然后向进程C发送消息,则无法保证它们的接收顺序.
同样,如果进程A和B向C发送消息,则无法保证接收消息的顺序.
我的问题是:
如果进程A和B向进程C发送消息,当A和B在相同的(微)时间内发送消息时,具有相同的内部功能,在同一节点和同一台机器中可以发送消息,接收顺序是什么?运行并行进程?
我有从Chrome扩展程序的后台脚本调用的以下方法.目标是将消息发送到特定选项卡,然后使用结果调用提供的回调方法.重要的是callbackDone必须始终在某个时间点调用.所以它是这样的:
function sendToTab(nTabID, callbackDone)
{
(function()
{
chrome.tabs.sendMessage(nTabID, {
action: "update01"
},
function(response)
{
if(chrome.runtime.lastError)
{
//Failed to send message to the page
if(callbackDone)
callbackDone(nTabID, null); //Page never received the message
}
else
{
//Sent message OK
if(response.result === true)
{
if(callbackDone)
callbackDone(nTabID, true); //Success!
}
else
{
if(callbackDone)
callbackDone(nTabID, false); //Page returns failure
}
}
});
}());
}
Run Code Online (Sandbox Code Playgroud)
然后从处理消息的页面内(可以注入content script)我处理它:
chrome.runtime.onMessage.addListener(onMessageProc);
function onMessageProc(request, sender, sendResponse)
{
if(request.action == "update01")
{
//Do …Run Code Online (Sandbox Code Playgroud) javascript google-chrome message-passing google-chrome-extension
我正在构建一个 Chrome 扩展,它使用针对特定 Web 域定制的 JavaScript 库。这是对该库的限制,只能从该特定域调用(实际上是许可证问题)。
我想将该 js 库加载到我的 Chrome 扩展中,但该扩展给出了内容安全策略错误,因为要使用的库来自 HTTP(不是 HTTPS)URL。我在 StackOverflow 中搜索了此内容,我看到的唯一选项是本地加载该文件或通过消息传递加载该文件。
有人可以告诉我在 my 中应该有什么代码background.js以及content.js在 my 中添加什么权限吗manifest.json?
获取该 js 库的网站是,例如http://library.com/js/xy.js。我知道应该还有其他东西,比如 content.js 中的一些代码。如何从该域加载代码?
清单.json:
{
"manifest_version": 2,
"name": "abc",
"version": "0.2",
"description": "abc",
"content_security_policy": "script-src 'self'; object-src 'self'",
"browser_action": {
"default_icon": "MM_logo_2009.png",
"default_popup": "tab/popup.html"
},
"background": {
"scripts": ["event.js"],
"persistent": false
},
"permissions": [
"http://library.com/js/xy.js",
"bookmarks",
"tabs",
"storage",
"http://*/*",
"https://*/*"
]
}
Run Code Online (Sandbox Code Playgroud)
事件.js:
var xhr = new XMLHttpRequest();
xhr.open("GET", …Run Code Online (Sandbox Code Playgroud) html javascript xmlhttprequest message-passing google-chrome-extension
根据维基百科,消息传递和调用方法之间的区别是"在消息传递中,每个参数必须有足够的可用额外内存,用于将现有参数复制到新消息的一部分",而在方法调用中只有地址参数的传递.
消息传递与调用常规方法的不同之处在于所有参数都是结构或值类型,即它们都必须完全在堆栈中推送,以便被调用者能够使用它们?
language-agnostic smalltalk language-design objective-c message-passing
我正在开发一个扩展,我不想使用选项页面.我使用浏览器操作(图标显示在右上角),并通过该页面创建一些首选项,然后将它们存储在localStorage中.
但是,我的内容脚本需要读取localStorage,但我知道它无法访问它.我看着传递的信息,但无法实现我想要的.
在这里我尝试过:
popup.js
$(document).ready(function(){
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getList")
sendResponse({status: localStorage['list']});
else
sendResponse({}); // snub them.
});
});
Run Code Online (Sandbox Code Playgroud)
content.js
$(document).ready(function(){
var p;
chrome.extension.sendRequest({method: "getList"}, function(response) {
p = response.status;
alert(response.status);
});
});
Run Code Online (Sandbox Code Playgroud) 我的总体目标是通过后台页面截取屏幕截图:
http://developer.chrome.com/extensions/tabs.html#method-captureVisibleTab
并将其传递给内容脚本,以便我可以使用页面的HTML DOM来分析屏幕截图并按照我想要的方式进行切割.
但是,我似乎无法通过Message Passing将dataUrl传递回内容脚本:
http://developer.chrome.com/extensions/messaging.html
我试过JSON.stringify(),但无济于事.
这非常好用:
background.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
sendResponse({imgSrc:'hello'});
}
);
Run Code Online (Sandbox Code Playgroud)
我将代码切换到此,没有任何通过:
background.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
chrome.tabs.captureVisibleTab(
null,
{},
function(dataUrl)
{
sendResponse({imgSrc:dataUrl});
}
)
}
);
Run Code Online (Sandbox Code Playgroud)
我唯一证明背景页面实际上是截屏的是我可以做到的
chrome.tabs.captureVisibleTab(null,{},function(dataUrl){console.log(dataUrl);});
Run Code Online (Sandbox Code Playgroud)
我明白了
"数据:图像/ JPEG; BASE64,/ 9J/4AAQSkZJRgABAQAAA ....等..."
登录background.html,这是有效的
我的问题是:如何将此URL发送到内容脚本?
我不希望在后台页面上执行所有逻辑,这些逻辑无法控制实际可见页面上的任何内容.
javascript json message-passing google-chrome-extension content-script
有了像Otto和EventBus这样的库,我想知道使用Handler是否仍然有意义:
Handler允许您发送和处理与线程的MessageQueue关联的Message和Runnable对象.每个Handler实例都与一个线程和该线程的消息队列相关联.
除事件总线库外,如何使用处理程序?我认为使用vanilla线程直接通过事件总线发送消息就足够了,或者我在这里遗漏了什么?
message-passing ×10
javascript ×4
concurrency ×2
erlang ×2
android ×1
erl ×1
f# ×1
html ×1
java ×1
json ×1
objective-c ×1
otto ×1
pid ×1
smalltalk ×1