Fork me on GitHub
Oliver George07:10:32

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


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)


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.


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


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


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


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


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


@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.


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.


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


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

  {: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
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?


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


jest seems to be required and able to handle that?


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.


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


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


I think jest runs everything through babel or metro first


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


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


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.


it would but your code clearly imported something


.../.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


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


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


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


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.


@thheller thanks validating it was me doing something dumb 🙂


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?


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


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


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