[JS-Interface] How to make (js-closure ...) lambdas take callback arguments

19 views
Skip to first unread message

Aeron Sioson

unread,
Dec 30, 2016, 1:29:43 PM12/30/16
to BiwaScheme
Hi everyone!

I'm building a chat app and I'm wanting to write is mostly using scheme. For the UI, I've used the built-in (element-new ..) syntax and the jquery functions to make changes to it. Using the jquery functions, I learned that the callback for things such as .click() events had to be wrapped in a (js-closure ...) sexp. Since they don't take arguments, I've written them like so:

(define on-click
 
(js-invoke ($ "#foo") 'click
   
(js-closure (lambda ()
      (foo bar baz)))))

Then, I used localforage for offline storage of message info. It was an async system that invoked callbacks once the storage operation was completed, like so:

localforage.setItem("key", "value", function(arg) {
  console
.log(arg);
})

Seeing as how I made callback with the jquery functions, I tried creating an interface to the library like this:

(define localforage
 
(js-eval "localforage"))

; Param 'store' could be either the global
; localforage store or a particular instance
; of it stored in a variable.
(define set-item!
 
(lambda (store key value callback)
   
(js-invoke store 'setItem key value
      (js-closure callback))))

Testing it out with:

(set-item! some-storage "key" "value"
 
(lambda (value)
   
(console-log value)))

The above just returns 'null' in the Firefox console. Am I doing it wrong? Am I going about this in the right direction? I didn't raise an issue on the Github page because I don't know if this is expected behavior or not.

Please help.






















Yutaka Hara

unread,
Jan 8, 2017, 5:44:20 AM1/8/17
to biwas...@googlegroups.com
Hi,

I've tried to reproduce the issue with this code, but it worked both
on Chrome and
Firefox (i.e. "some data" is shown in the developer console.)

<script>
window.localforage = {
setItem: function(key, value, cb) {
cb("some data")
}
}
</script>
<script src="../../release/biwascheme.js">
(define localforage (js-eval "localforage"))
(define set-item!
(lambda (store key value callback)
(js-invoke store 'setItem key value
(js-closure callback))))
(set-item! localforage "key" "value"
(lambda (value)
(console-log value)))
</script>


2016-12-31 3:29 GMT+09:00 'Aeron Sioson' via BiwaScheme
<biwas...@googlegroups.com>:
> Testing it out with:
>
> (set-item! some-storage "key" "value"
> (lambda (value)
> (console-log value)))
>
> The above just returns 'null' in the Firefox console. Am I doing it wrong?
> Am I going about this in the right direction? I didn't raise an issue on the
> Github page because I don't know if this is expected behavior or not.
>
> Please help.
>
--
Yutaka HARA
Reply all
Reply to author
Forward
0 new messages