Fork me on GitHub
#cljsrn
<
2019-10-02
>
Oliver George07:10:32

Let me rephrase. Anyone using requestIdleCallback to schedule cpu intensive tasks?

thheller07:10:19

I'd say no. only if you can break that work up into many small pieces that each take no longer than 4ms or so (it still runs in the main thread)

danielneal07:10:14

second @thheller yeah the key is splitting up things into very tiny bits. Looking forward to React 17 etc, the thing to use might be the react scheduler, which has a bunch of different priorities, and will fit the work around rendering.

thheller07:10:40

cpu intensive work should probably be done in workers but no clue how react-native handles that

danielneal07:10:57

there’s no support for workers in vanilla react native. There’s some ugly hacks involving webviews, and a couple of npm libs, but none of which I’d feel comfortable relying on

danielneal07:10:59

I’ve been using the reframe event queue to dispatch background events, suitably batched

vikeri07:10:27

@olivergeorge I know people have used web workers to offload cpu intensive tasks in RN.

danielneal07:10:56

I guess if the work is hard but the deserialization / serialization is a small cost in comparison to the work done

danielneal08:10:54

@vikeri have you got any examples?

Oliver George09:10:52

Yeah I don’t think web workers helps me in this case because of the serialization cost. Happily I can do small steps with this one.

Oliver George09:10:05

Thanks for the replies though. I am reassured that I’m on the right path.

danielneal09:10:06

ah yeah, I remember seeing that, great idea putting all the subs in a worker

Oliver George10:10:33

Web workers "structured cloning" sounds interesting. Does it generally work with CLJS data types?

Oliver George10:10:10

Ah, well. Can't have nice things I guess.

jgood16:10:10

@thheller I'm trying to set up some node tests for my react-native project. shadow-cljs.edn

...

 :builds
 {:app
  {:target     :react-native
   :init-fn    
   :output-dir "app"
   :devtools   {:autoload true}}

  :test
  {:target    :node-test
   :output-to "out/node-tests.js"
   :autorun   true}}}

test file
(ns time-align-mobile.handlers-test
  (:require [cljs.test :as t :refer-macros [deftest is]]
            [time-align-mobile.handlers :as handlers]
            [time-align-mobile.db :as db :refer [app-db]]))

(deftest initialize-db
  (is (= app-db (handlers/initialize-db [] []))))

...
The tests are on pure clojure functions that I use in re-frame handlers. No react-native specific things in them. But when I try to run the tests I get
>> node out/node-tests.js

SHADOW import error /home/justin/projects/time-align-mobile/.shadow-cljs/builds/test/dev/out/cljs-runtime/shadow.js.shim.module$react_native.js
/home/justin/projects/time-align-mobile/node_modules/react-native/Libraries/Utilities/warnOnce.js:15
const warnedKeys: {[string]: boolean} = {};
      ^^^^^^^^^^

SyntaxError: Missing initializer in const declaration
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/justin/projects/time-align-mobile/node_modules/react-native/Libraries/react-native/react-native-implementation.js:14:18)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)

Am I doing something dumb or is there something wrong with how shadow is including other project dependencies?

thheller17:10:40

@jgoodhcg this isn't shadow-cljs at all. this happens if you require("react-native") in node.

thheller17:10:59

jest seems to be required and able to handle that?

jgood17:10:53

Hmm so a node module build config that uses jest to run tests. I'll give this a try. What I was going for was the specific node-test build configuration that could run while I'm developing. It was only supposed to test pure cljs functions that should be able to run on node.

thheller17:10:08

the problem is that you can't load most react-native code directly in node

thheller17:10:28

they are written in typescript or flow or some other JS flavor that can't be loaded directly

thheller17:10:37

I think jest runs everything through babel or metro first

thheller17:10:19

if you don't require any of the react-native packages then :node-test should be fine

thheller17:10:31

but even a simple (:require ["react-native" :refer (View)]) will fail

jgood17:10:49

I assumed that if everything up the dependency tree from the functions I imported didn't touch any react native libraries it would just work.

thheller17:10:02

it would but your code clearly imported something

thheller17:10:36

.../.shadow-cljs/builds/test/dev/out/cljs-runtime/shadow.js.shim.module$react_native.js this file is generated if you did (:require ["react-native" ...]) somewhere

jgood17:10:38

I'll remove any imports to the test ns and see if it still fails.

jgood17:10:10

I thought I was clear of any react-native deps which lead me to think that shadow did something i wasn't aware of.

jgood17:10:03

Works when I keep the ns clear of any imports to my project code. :thumbsup:

jgood17:10:12

I'll probably try to isolate the functions I want to test this way and then come back around to a jest implementation when I go to test view components and more react-native integrated code.

jgood17:10:29

@thheller thanks validating it was me doing something dumb 🙂

Maksym17:10:26

Guys, sorry for bothering but I'm sick off warnings in my console window, they all have same text

Cannot infer target type in expression ...
, here is an example:
------ WARNING #17 - :infer-warning --------------------------------------------
 File: D:\Dev\clojure\rn-rf-shadow\src\main\example\views\warehouse.cljs:43:43
--------------------------------------------------------------------------------
  40 |
  41 | (defn render-fn [item index separator]
  42 |
  43 |   [c/text (str (.-name item) ", amount: " (.-amount item))])
-------------------------------------------------^------------------------------
 Cannot infer target type in expression (. item -amount)
Can you please explain me what does it mean in other words? And how to get rid of it?

thheller17:10:43

you turned those warnings on via :infer-externs :auto

thheller17:10:20

you get rid of them by applying the ^js typehints. in this case (defn render-rn [^js item index separator] ...)

Maksym17:10:13

Thank you very much for the reference, I'll read about it :thumbsup: