- Notifications
You must be signed in to change notification settings - Fork13
ClojureScript macros for convenient native Javascript object access.
License
binaryage/cljs-oops
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a ClojureScript library providing a few essential macros for operating with native Javascript objects ("oops" stands for "Object OPerationS"). Cljs-oops provides optimizer-safe property and method accessors, compact but efficient nested property accessors, and development-build-only safety checks that catch many common errors.
TOC|Object operations|Installation|Motivation|Benefits|FAQ
Boss: "Ship it!"You: "Let me compile it with :advanced optimizations..."Boss: "Sounds good!"...one coffee laterYou: "Oops! It just broke! And I don't know why." Boss: "Don't tell me that a random person on the Internet was wrong again."You: (sad face) "Yep, they provided slightly outdated externs!"Add these new power-macros to your tool belt:
ogetis a flexible, safe and guilt-free replacement foragetoset!isaseton steroidsocallis a replacement for(.call ...)built on top ofogetoapplyis a replacement for(.apply ...)built on top ofoget
Let's see some code examples first and then discuss the concepts:
Add oops dependency into your Leiningen'sproject.clj or boot file.
Require macros in your namespaces viaoops.core ClojureScript namespace:
(nsyour.project.namespace (:require [oops.core:refer [oget oset! ocall oapply ocall! oapply! oget+ oset!+ ocall+ oapply+ ocall!+ oapply!+]]))(oset! (js-obj):mood"a happy camper")
Please note that we are not using:refer-macros here. We rely onautomatic macro refer inference in latest ClojureScript.
Also please be aware that oops usesclojure.spec which is available since Clojure 1.9.If you cannot upgrade to Clojure 1.9, you may stick with Clojure 1.8 and add thisbackported version of clojure.spec.
Otherwise pretty standard stuff. If in doubts, look at thesample project.
I don't always do Javascript interops, but when I do, I call them by names.
-- Darwin (with sunglasses on)
ClojureScript developers should quickly learn how to inter-operate with native Javascript objects viathe dot operator.This was modelled to closely followClojure's Java interop story.
For example, the ClojureScript form(.-nativeProp obj) will compile toobj.nativeProp in Javascript.
It works pretty wellduring development but there is a catch! When you naively write code like that, it mightnot surviveadvanced optimizations. Closure Compiler needs some information about which property names are safe to renameand which cannot be renamed because they might be referenced externally or dynamically via strings.
Someone at Google had a quick and bad idea. We could provide a separate file which would describe this information.Let's call it an "externs file"!
I'm pretty opinionated about using externs. I hate it with passion. Here is the list of my reasons:
Development behaviour is disconnected from production behaviour - discovering breakages only after switching to :advanced mode.I know, I should continuously run tests against :advanced mode. But :advanced builds are pretty slow and it is no fun tofish for "Cannot read property 'j349s' of null"-kind of errors in minified raw Javascript files which could balloon to multi-MB sizes.Have to wait for quantum computers to provide our IDEs with enough computational power to parse and syntax-highlightmulti-megabyte one-line Javascript files ;)
Say, authors of a useful (native) library don't provide externs file (usually simply because they don't use Closure Compiler).So there must comesomeone else who is willing to maintain an externs file for their library by following changes in the library.You want to use the library so now you made yourself dependent on two sources of truth and they don't usually move in a lock-step.Also that someone will probably sooner or later lose interest in maintaining the externs file and you have no way of tellingif it is outdated/incomplete without doing a full code-review. And the worst part is that "someone" is very often you.
Incomplete (or outdated) externs files provide no feedback. Except that you suddenly discover that a new build is broken again andyou are back to "pseudo-names fishing".
Externs have to be configured. Paths must be specified. Externs are not co-located with the code they are describing.It is not always clear where individual externs are coming from. Some "default" externs for standard browser/DOM APIs are baked-inClosure Compiler by default which might give you false sense of security or confuse assumptions about how this whole thing works.
What if I told you to ditch your externs because there is a simpler way?
Simplyuse string names to access object properties in Javascript (in cases where you would rely on externs).Instead of(.-nativeProp obj) write(aget obj "nativeProp") which compiles toobj["nativeProp"]. String names are notsubject of renaming in advanced mode. And practically the same code runs in development and advanced mode.
I hear you. This looks dirty. We are abusingaget which wasexplicitly documented to be for native array only.Alternatively we could usegoog.object/get or the multi-aritygoog.object/getValueByKeys which looks a bit better,but kinda verbose.
Instead of investing your energy into maintaining externs you could as well incrementally write a lightweightClojure-style wrapper functions to access native APIs by string names directly. For example:
(defnget-element-by-id [id] (.call (aget js/document"getElementById") js/document id))
It is much more flexible than externs. You have full control and power of ClojureScript code here.And who knows, maybe later you will extract the code and publish it as a nice ClojureScript wrapper library.
Sounds good? With oops library the situation can be even better.What if we had something likeaget but safer and more flexible?I'm pleased to introduce you tooget...
The signature foroget is(oget obj & selector).
Selector is a data structure describing exact path for traversing into a native objectobj.Selectors can be plain strings, keywords or for conveniencearbitrarily nested collections of those.
Selectors are pretty flexible. The following selectors describe the same path:
(oget o"k3.?k31.k311")(oget o"k3""?k31":k311)(oget o ["k3""?k31""k311"])(oget o [["k3"]"?k31"]"k311")
Please note the ".?" is a modifier for "soft" access (inspired byCoffeeScript's existential operator).We expect that the key 'k31' might not be present and wantoget to stop and silently return nil in that case.
In case ofoset! you can use so-called "punching" for creation of missing keys on path. For example:
(oset! (js-obj)"!k1.!k2.!k3""val")
That will createk1 andk2 on the path to setting finalk3 key toval. If you didn't specify the exclamation modifiersoset! would complain about missing keys. This makes sense because if you know the path exists for sureyou don't want to use punching and that will ultimately lead to simpler code generated in :advanced mode (without any checks for missing keys).
Dynamic selector is a selector which is not fully known at compile-time. For example result of a function callis a dynamic selector:
(oget o (identity"key"))
At runtime the form result is the same but generated code is less effective. Dynamic selectors should be very rare.By default, oops assumes that you want to prefer static selectors and dynamic selectors are unintentional.Compiler will issue a compile-time warning about "Unexpected dynamic selector usage".To silence this warnings use "plus" version ofoget like this:
(oget+ o (identity"key"))
This way you express explicit consent with dynamic selector code-path.
By default, oops generates diagnostics code and does pretty intensive safe-checking in non-advanced builds.As you can see on the screenshots above you might get compile-time or run-time warnings and errors when unexpected things happen,like accessing missing keys or traversing non-objects.
By default, all diagnostics code is elided in :advanced builds and oops produces code similar to hand-writtenaget usage(without any safety-checks).
You can inspect our testcompilation transcripts to see what code is generated in different compiler modes.
I believe oops has sensible defaults and there should be no need to tweak it under normal circumstances.Anyways, look at possible configuration options indefaults.clj.
As you can see, you can provide your own config overrides in the ClojureScript compiler options mapvia:external-config > :oops/config.See example incljs-oops-sample project.
Isn't accessing properties by string names slower?
Well, only if the strings are computed dynamically at runtime. In case of string literals Javascript parser can see themand there should be no reason to treat them differently than dot properties. But you don't have to worry about this.Google Closure compiler rewrites string literals to dot property access whenever possible.
Should I use cljs-oops with Closure Library (e.g. goog.something namespace)?
No! Use oops only for interop with external code which is not part of your :advanced build.That means for all code where you would normally need to write externs.
Closure Library is compatible with advanced compilation and identifiers get properly minified during compilation.You don't have to write any externs for it, so you don't have to use oops with it.
Second area where you want to use string names is when you work with JSON data objects (e.g. data received over a network).String names explicitly prevent minification of key names which must stay intact.
For better understanding pleaseread this detailed article by Luke VanderHart.
How this approach compares toClojureScript externs inference?
Externs inference is very recent feature and looks promising. It was introduced after I put all the effort into developingthis library so my opinion is biased :-). I personally still prefer investing time into building light-weight ClojureScriptwrapper libraries using string-names than dealing with externs (even if they are auto-inferred).
I would recommend watchingNavigating ClojureScript's Fire Swamps byPeter Schuckwhere he compares both methods.
About
ClojureScript macros for convenient native Javascript object access.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.

