ClojureScript on node.js, code

I am trying to run ClojureScript on node.js

app1.js target code: working:

var rx = require("./lib/rx/rx.node.js"); var moment = require("./lib/moment/moment.js"); var timeStream = new rx.Observable.interval(300) .subscribe(function(index) { console.log(moment().format("dddd, MMMM Do YYYY, h:mm:ss a")); }); 

core.cljs is my attempt:

 (ns rxcljs.core (:use [cljs.nodejs :only [require]]) ) (def rx (require "./lib/rx/rx.node.js")) (def moment (require "./lib/moment/moment.js")) (-> rx (.Observable) (.interval 300) (.subscribe #(->> (->(moment) (.format "dddd, MMMM Do YYYY, h:mm:ss a" ) ) (.log js/console) ) ) ) 

app.js actual compilation output: not working

 ..... ..... cljs.nodejs = {}; cljs.nodejs.require = require; cljs.nodejs.process = process; cljs.core.string_print = cljs.nodejs.require.call(null, "util").print; var rxcljs = {core:{}}; rxcljs.core.rx = cljs.nodejs.require.call(null, "./lib/rx/rx.node.js"); rxcljs.core.moment = cljs.nodejs.require.call(null, "./lib/moment/moment.js"); rxcljs.core.rx.Observable().interval(300).subscribe(function() { return console.log(rxcljs.core.moment.call(null).format("dddd, MMMM Do YYYY, h:mm:ss a")) }); 

Error:

 /...../rxcljs/app.js:12726 rxcljs.core.rx.Observable().interval(300).subscribe(function() { ^ TypeError: Cannot call method 'interval' of undefined 

Please advice.


Answer EDIT

Thanks to Michal:

 (ns rxcljs.core (:use [cljs.nodejs :only [require]]) ) (def log #(.log js/console %)) (def rx (require "./lib/rx/rx.node.js")) (def moment (require "./lib/moment/moment.js")) (-> rx .-Observable (.interval 300) (.subscribe #(->> (-> (moment) (.format "dddd, MMMM Do YYYY, h:mm:ss a") ) (log) ) ) ) 

rx interval is working properly, torque format output signal is still carried.

+6
source share
1 answer

(.Observable rx) always a method call in ClojureScript; you must use (.-Observable rx) or possibly (aget rx "Observable") to access the properties. 1

With this in mind, your definition of timeStream can be rewritten in ClojureScript as follows:

 (def time-stream (.. rx -Observable ;; property access (interval 300) ;; method call (subscribe (fn [index] ...)))) 

You can also use js/rxcljs.core.rx.Observable if you find it prettier (the magic js namespace invokes the JavaScript alphabetic identifier specified as part of the character name that will be used on the compiled output, in particular, this means that you need to provide a namespace prefix, as shown here).


1 Note that in Clojure (.foo x) may be a method call or access to properties depending on x , so there is a difference between the dialects.

+5
source

Source: https://habr.com/ru/post/948729/


All Articles