lilactown / helix

A simple, easy to use library for React development in ClojureScript.
Eclipse Public License 2.0
627 stars 52 forks source link

Metadata annotations for optimizations #58

Closed lilactown closed 4 years ago

lilactown commented 4 years ago

This PR implements a new feature in the Helix component analyzer/compiler: the ability to annotate expressions with :memo and :callback metadata and have them wrapped in use-memo or use-callback.

By default, annotating an expression with ^:memo or ^:callback will automatically infer the dependencies from the local context. E.g.:

(let [foo "foo"
      foobar ^:memo (str foo "bar)]
  ,,,)

Will be emitted as

(let [foo "foo"
      foobar (helix.hooks/use-memo [foo] (str foo "bar"))]
  ,,,)

You can override this by passing in a vector of dependencies into the annotating:

(let [foo "foo"
      foobar ^{:memo :once} (str foo "bar)]
  ,,,)

Will emit

```clojure
(let [foo "foo"
      foobar (helix.hooks/use-memo :once (str foo "bar"))]
  ,,,)