This repository was archived by the owner on Dec 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv.cljc
128 lines (99 loc) · 3.52 KB
/
env.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(ns env
(:require
["create-react-class" :as crc]
#?@(:test []
:default [[react-native-navigation-bridge :as rnn-bridge]])
[reagent.core :as r]))
(declare register-component)
(declare bind-component)
(defonce id-seq-ref (atom 0))
(defonce mounted-ref (atom {}))
(defonce screens-ref (atom {}))
(defn register
([key]
(register key nil))
([key options]
(let [get-props
(fn [this]
{::key key
::id (-> this .-state .-id)
:component-id (-> this .-props .-componentId)})
wrapper
(crc #js
{:displayName
(str key "Wrapper")
:getInitialState
(let [id (swap! id-seq-ref inc)]
(fn [] #js {:key key
:id id}))
:componentDidMount
(fn []
(this-as
^js this
(bind-component this)
(swap! mounted-ref
assoc-in [key (-> this .-state .-id)] this)))
:componentWillUnmount
(fn []
(this-as
^js this
(swap! mounted-ref update key dissoc (-> this .-state .-id))))
;; FIXME: forward other lifecycles the same way
:navigationButtonPressed
(fn [params]
(this-as
^js this
(let [{:keys [navigation-button-pressed]}
(get @screens-ref key)
props
(get-props this)]
(js/console.log "navigationButtonPressed"
key
(boolean navigation-button-pressed)
(pr-str props))
(when navigation-button-pressed
(navigation-button-pressed
(js->clj params :keywordize-keys true)
props)))))
:componentDidAppear
(fn []
(this-as
^js this
(js/console.log "componentDidAppear" key)))
:componentDidDisappear
(fn []
(this-as
^js this
(js/console.log "componentDidDisappear" key)))
:render
(fn []
(this-as
^js this
(let [{:keys [render]}
(get @screens-ref key)
props
(get-props this)]
(js/console.log "render" key (pr-str props))
(-> (render props)
(r/as-element)))))})]
(when (not (nil? options))
(set! (.-options wrapper)
(fn [passProps] (clj->js options))))
(register-component key (fn [] wrapper)))))
(defn reload {:dev/after-load true} []
(doseq [[key instances] @mounted-ref
[id inst] instances]
(js/console.log "forceUpdate" key id)
(.forceUpdate ^js inst)))
(defn add-screen [key screen-def]
(swap! screens-ref assoc key screen-def))
#?(:test
(defn register-component [key component] [])
:default
(defn register-component [key component]
(rnn-bridge/register-component key component)))
#?(:test
(defn bind-component [component] [])
:default
(defn bind-component [component]
(rnn-bridge/bind-component component)))