Skip to content

Instantly share code, notes, and snippets.

@mdhaney
Created February 20, 2019 21:09
Show Gist options
  • Save mdhaney/61aa1a44bfc48837d6fb7051737043a9 to your computer and use it in GitHub Desktop.
Save mdhaney/61aa1a44bfc48837d6fb7051737043a9 to your computer and use it in GitHub Desktop.
Fulcro - Expo bootstrapping
(ns core
(:require [fulcro.client.primitives :as prim]
[fulcro.client :as fc]
[fulcro.client.data-fetch :as df]
[fulcro.client.network :as net]
[expo :as expo]
[myapp.root :refer [MyRealFulcroRoot]
[support :as sup]
[oops.core :refer [ocall]]))
(defonce app (atom (fc/new-fulcro-client
;:networking {:remote (net/fulcro-http-remote {:url ion-url})}
:reconciler-options {:render-mode :keyframe
:root-render sup/root-render
:root-unmount sup/root-unmount})))
(defonce RootNode (sup/root-node! 1))
(defonce app-root (prim/factory RootNode))
(defn reload []
(swap! app fc/mount MyRealFulcroRoot 1))
(defn init []
(reload)
(expo/register-root app-root))
(ns expo
(:require [oops.core :refer [ocall]]))
(set! js/window.Expo (js/require "expo"))
(defn register-root [^js/React.Component app-root]
(ocall js/Expo "registerRootComponent" (fn [props & children]
(apply app-root props children))))
(ns support
(:require [fulcro.client.primitives :refer-macros [ui]]))
(defonce root-nodes (atom {}))
(defn root-node!
"A substitute for a real root node (1) for mounting om-next component.
You have to call function :on-render and :on-unmount in reconciler :root-render :root-unmount function."
[id]
(let [content (atom nil)
instance (atom nil)
class (ui Object
(componentWillMount [this] (reset! instance this))
(render [_] @content))]
(swap! root-nodes assoc id {:on-render (fn [el]
(reset! content el)
(when @instance
(.forceUpdate @instance)))
:on-unmount (fn [])
:class class})
class))
(defn root-render
"Use this as reconciler :root-render function."
[el id]
(let [node (get @root-nodes id)
on-render (:on-render node)]
(when on-render (on-render el))))
(defn root-unmount
"Use this as reconciler :root-unmount function."
[id]
(let [node (get @root-nodes id)
unmount-fn (:on-unmount node)]
(when unmount-fn (unmount-fn))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment