我正在使用jet作为异步环适配器。
Jet还带有异步http-client,它返回一个频道,其值@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_screen);
Context context = getApplicationContext();
settings = PreferenceManager.getDefaultSharedPreferences(context);
settings.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences settings, String key) {
Toast theToast;
theToast = Toast.makeText(this, "toast text", Toast.LENGTH_LONG);
if(key.equals("checkbox_key")){
theToast.show();
}
if (key.equals("checkbox_key")&& theToast != null){
theToast.cancel();
}
}
也是一个频道。
此外,异步服务器路由处理程序可以返回:body
密钥可以包含通道的映射。当此频道关闭时,响应将返回给客户端。
我正在编写以下:body
代码:
go
在我的 (defn- api-call-1 []
(go (-> (jet-client/get "api-url-1")
<!
:body ;; jet http client :body is also a channel.
<!
api-call-1-response-parse)))
(defn- api-call-2 []
(go (-> (jet-client/get "api-url-2")
<!
:body
<!
api-call-2-response-parse)))
(defn route-function []
(let [response-chan (chan)]
(go
(let [api-call-1-chan (api-call-1) ;; using channel returned by go
api-call-2-chan (api-call-2)]
(-> {:api-1 (<! api-call-1-chan)
:api-2 (<! api-call-2-chan)}
encode-response
(>! response-chan)))
(close! response-chan))
;; for not blocking server thread, return channel in body
{:body response-chan :status 200}))
中,我无法阻止。
虽然这段代码运行正常,但在api-call-1中使用route-function
是不是很糟糕?
我发现要在go
中使用<!
我需要将其放在api-call-1
块中。
现在我在go
中使用此go
广告块的频道。这看起来很单调吗?我担心不会将route-function
甚至api-call-1-response-parse
作为:body
的渠道公开。
构建route-function
块代码和函数的正确方法是什么?
我应该关心函数go
中的额外go
块吗?
答案 0 :(得分:0)
你看起来很像我在制作中的等效代码。这是非常惯用的,所以我认为你的代码结构正确。
core.async停放操作不能跨越函数边界的事实源于它被编写为宏并且需要一次处理整个代码块的事实(或者至少在它之后)词汇可用的)。这往往会使所有core.async代码以您正在使用的模式出现。