Open deontologician opened 8 years ago
Eventually, we'll add stuff like React Native here, but for now this should be mostly everything
If I may sugesst a way to work with Typescript
horizon/index.d.ts
declare module '@horizon/client' {
const Horizon
export default Horizon
}
typescript-file.ts
/// <reference path="horizon/index.d.ts" />
import {default as Horizon} from '@horizon/client';
There are a few different ways you can integrate Horizon client into your frontend app.
Main Horizon.js file
This is a UMD (universal module definition) built with:
Array.from
Array.findIndex
Array.keys
Object.assign
This is the package that is served by Horizon server at
/horizon/horizon.js
. It's located atrequire('@horizon/client/dist/horizon.js')
.Use this if you are not using a bundler like Webpack or Browserify, and you'd like to get up and running quickly without worrying about polyfills and Horizon dependencies.
Core Horizon.js file
This is a UMD without RxJS or any polyfills. It's a bit more work to get started with, but if you don't mind bringing your own RxJS and polyfills it is a much smaller bundle than the main horizon.js.
It's located at
require('@horizon/client/dist/horizon-core.js')
Use this if you are not using a bundler like Webpack or Browserify, and you have already included RxJS, and you're handling your own polyfills.
Using Webpack or Browserify with ES5 sources
If you use a bundler like Webpack or Browserify, you can import the es5 sources like:
RxJS is a dependency, but we only patch in the operators we need in the Horizon library itself. Often you'll want other operators, so you'll have to require the patching modules yourself. For example, to get the
toPromise
andswitchMap
operators, you would add these lines somewhere in your source code:This allows you to control the size of your bundle by not adding operators you don't need, but it may be very tedious adding everything individually. If you'd like to add all operators at once:
Using a bundler with ES6 sources
Horizon is written in es6 currently, so if you'd prefer to do something like to babelify our source along with your own in webpack or browserify, import it like:
If you're using Rollup, we already have
jsmain:next
set, so you can just use:Similar to es5 above, you'll need to patch in exactly which RxJS operators you want to use. So:
Or for the kitchen sink:
Node.js
If you're requiring the horizon client from Node.js, you'll also get the es5 sources, so everything mentioned there applies here as well. But in addition you'll need to provide a WebSocket polyfill, since that is not part of Node.js. If you install the
ws
package, you can add such a polyfill like: