Fork me on GitHub
#clojurescript
<
2020-08-12
>
kasey01:08:15

Ported, still have to test usage.

(deftype BatchingBuffer [items n]
  cljs.core.async.impl.protocols/Buffer
  
  (full? [this]
    (>= (count @items) n))
  
  (remove! [this]
    (let [[old new] (reset-vals! items [])]
      old))
  
  (add!* [this item]
    (swap! items conj item))
  
  (close-buf! [this])
  
  cljs.core/ICounted
  (-count [this]
    (if (empty? @items)
      0
      1)))

(defn batching-buffer [n]
  (BatchingBuffer. (atom []) n))

kasey06:08:15

I take a file, copy (or move and rename, tried both) it into a subfolder under main, change the require appropriately (folder.file instead of just file). And then I get Cannot read property 'cljs$core$IFn$_invoke$arity$2' of null in the ClojureScript app. using shadow-cljs. Any common thing that trips up newcomers that results in that?

kasey06:08:42

Got it working. Something to do with namespaced keys

artem.wide09:08:05

Hi guys, I have wrapper around require :

(defn load [path]
  (try
    (js/require (str path))
    (catch :default _ nil)))
and :optimizations :advanced So I get an error: Invalid call at line 1478: require(q.a(a)) May anyone help, please?)

artem.wide09:08:01

If I don’t use str path , I would have Invalid module: path`. js/require can only process strings`

artem.wide09:08:36

I tried ^string , but no luck

thheller09:08:04

in which context do you use this? require is often a compile time constant and cannot be used dynamically. eg. webpack, react-native, etc.

thheller09:08:22

only place you can use it dynamically is node pretty much

artem.wide09:08:13

I use it with Expo (ReactNative) to load images from assets…

artem.wide09:08:14

I have a crash, when asset cannot be downloaded, so decided to go with a wrapper

thheller09:08:19

yeah you can't. the JS tools need to be able to read the require calls at compile time

thheller09:08:56

so only (js/require "../assets/as-a-string.png") or so works

artem.wide09:08:12

Thanks @. Any ideas how not to crash on Could not download from ?)

thheller09:08:06

I don't know. I don't do react-native dev myself. maybe it can only use local files? check the expo/metro docs.

artem.wide12:08:22

@ The interesting thing is - wrapper works locally, when I don’t do cljs optimizations

allaboutthatmace178912:08:53

I'm trying to put a CodeMirror editor in a reagent app, but am falling at an early hurdle. I've required codemirror with

(:require [reagent.core :as r]
            ["codemirror" :as codemirror])
following https://stackoverflow.com/questions/38255446/clojurescript-how-to-change-codemirror-reactively-with-reagent example (and a couple of others) I'm trying to do something like
(reset! cm (js/CodeMirror. (.createElement js/document "div")
                           (clj->js {})))
but I get a 'CodeMirror is not defined' I also tried codemirror/CodeMirror. in place of js/CodeMirror. but there I got CodeMirror is not a constructor - so it at least seems to be finding the class in that case. Is there some missing require thing I need to do before I can use js/CodeMirror.?

thheller13:08:26

@allaboutthatmace1789 it is just (codemirror. ...), so just the :as alias you used. no js/.

artem.wide15:08:20

May anyone help with the macros , please? I want to have a macros - wrapper on js/require , so I can’t have myrequire.clj and use it in myrequire.cljs . And if I put defmacro in myrequire.cljs - compiler can’t find it on classpath

artem.wide15:08:07

Yeah, I read it, but it defines foo in .clj, but I need a js/require ..

thheller16:08:03

what does that have to do with anything? you can't actually call js/require in the macro? you can emit it only

artem.wide19:08:55

yeah, I was able, my bad. Problem is in try/catch there… I want to have js try/catch there..

artem.wide21:08:52

in case anyone needs: loader.clj: (ns static-assets.loader) (defmacro load-asset [path] (try` (js/require (str ~path)) (catch js/Object e# nil))) loader.cljs: (ns static-assets.loader (:require-macros [static-assets.loader])) usage: (:require [static-assets.loader :refer [load-asset]] (load-asset "./assets/ic.png")

worlds-endless19:08:22

What's the best way to truncate a string in Clojurescript? Easy would be (apply str (take my-limit s)), but this feels wasteful

dpassen119:08:49

(subs s 0 my-limit)

worlds-endless19:08:48

That needs safety-checks, unfortunately, since it breaks if my-limit is greater than string.length

worlds-endless19:08:12

Just wondering if those checks are teh best way to go, or if there's a better built-in somewhere

alexmiller19:08:37

not in core that I'm aware of

dpassen119:08:51

I don’t think it breaks in cljs land, it definitely does in clj (jvm) land

austin02119:08:25

Does anyone here have any libraries or tips on doing web client only p2p projects in clojurescript? I see there are some options in the javascript space for this (http://www.freedomjs.org/), but want to see if there are any clojurescript oriented options available before reaching for interop.

sritchie0920:08:12

Hey all! I'm looking for an equals method that does what Clojurescript's does... but can compare js/BigInt , number, goog.math.Long and goog.math.Integer

sritchie0920:08:45

soft equality == will work between js/BigInt and js/Number,

sritchie0920:08:33

I can extend IEquiv, but that will only take if the first argument is a type other than js/Number

sritchie0920:08:05

one way to do this would be to re-extend IEquiv to number to override the default behavior. Is this very-not-recommended?