/reflex-ffi-examples

Reflex / GHCJS JS FFI Examples

Primary LanguageHaskellMIT LicenseMIT

Reflex / GHCJS JS FFI Examples

WARN: This project can't be built with GHC because it doesn't have jsaddle-based implementation (yet).

Use bin/build or bin/build-cabal for building and bin/open-page or bin/open-page-cabal for opening resulting HTML page. All the output goes to the browser console. Refresh page after opening the console to see all the delays in the real time.

WARN: You don't usually need to create your own stuff based on setTimeout since ghcjs-dom already has such functionality ready to use. Implementations in this project are provided just as examples. Use them to learn how to implement things that are missing in ghcjs-dom.

NOTE: For more info on bin/* commands, see reflex-template.

docs

libs to look at

  • ghcjs-base - primitives you'll need for GHCJS-specific code (JS FFI, etc.)
  • jsaddle - primitives that work with both GHCJS and GHC
  • ghcjs-dom - web browser API, includes these parts:

having code for both GHC and GHCJS

Usually, you want to have implementation of the needed API with both JS FFI (to use with GHCJS) and jsaddle (to use with GHC). You can use cabal conditional directives and C preprocessor to use proper implementation depending on compiler. Some examples of how this can be done:

  • ghcjs-dom uses:
    • #ifdef directives:
    • conditions on flag(jsffi) and ghcjs in cabal to use different packages (WARN: link to specific commit, can be outdated)
  • reflex-dom uses condition on ghcjs in cabal to use different source files and packages (WARN: link to specific commit, can be outdated)

misc

ghc-dom implementation of setTimeout is in WindowOrWorkerGlobalScope.hs (WARN: link to specific commit, can be outdated).