Skip to content
This repository was archived by the owner on Nov 25, 2022. It is now read-only.

Commit de633fa

Browse files
authored
Merge pull request #5 from aiham/dev
Release 0.4.0
2 parents a0699b3 + 65bb45d commit de633fa

28 files changed

+5594
-183
lines changed

.npmignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
src/
21
example/
2+
src/
3+
test/
4+
karma.conf.js
5+
.travis.yml
6+
.npmignore

.travis.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
sudo: required
2+
language: node_js
3+
node_js:
4+
- 6
5+
6+
before_install:
7+
- export DISPLAY=:99.0
8+
- sh -e /etc/init.d/xvfb start
9+
10+
script: ./node_modules/.bin/run-tests
11+
12+
after_failure:
13+
- for file in *.log; do echo $file; echo "======================"; cat $file; done || true
14+
15+
env:
16+
matrix:
17+
- BROWSER=chrome BVER=stable
18+
- BROWSER=firefox BVER=stable
19+
- UNIT_CMD='npm run lint'
20+
global:
21+
- TRAVIS=true
22+
- UNIT_CMD='npm run unit'
23+
24+
matrix:
25+
fast_finish: true
26+
27+
notifications:
28+
email:
29+

CONTRIBUTING.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Contributing Guidelines
2+
3+
For anyone looking to get involved to this project, we are glad to hear from you. Here are a few types of contributions that we would be interested in hearing about.
4+
5+
* Bug fixes
6+
- If you find a bug, please first report it using Github Issues.
7+
- Issues that have already been identified as a bug will be labelled `bug`.
8+
- If you'd like to submit a fix for a bug, send a Pull Request from your own fork and mention the Issue number.
9+
+ Include a test that isolates the bug and verifies that it was fixed.
10+
* New Features
11+
- If you'd like to accomplish something in the library that it doesn't already do, describe the problem in a new Github Issue.
12+
- Issues that have been identified as a feature request will be labelled `enhancement`.
13+
- If you'd like to implement the new feature, please wait for feedback from the project maintainers before spending too much time writing the code. In some cases, `enhancement`s may not align well with the project objectives at the time.
14+
* Tests, Documentation, Miscellaneous
15+
- If you think the test coverage could be improved, the documentation could be clearer, you've got an alternative implementation of something that may have more advantages, or any other change we would still be glad hear about it.
16+
- If its a trivial change, go ahead and send a Pull Request with the changes you have in mind
17+
- If not, open a Github Issue to discuss the idea first.
18+
19+
## Requirements
20+
21+
For a contribution to be accepted:
22+
23+
* The test suite must be complete and pass
24+
* Code must follow existing styling conventions
25+
* Commit messages must be descriptive. Related issues should be mentioned by number.
26+
27+
If the contribution doesn't meet these criteria, a maintainer will discuss it with you on the Issue. You can still continue to add more commits to the branch you have sent the Pull Request from.
28+
29+
## How To
30+
31+
1. Fork this repository on GitHub.
32+
1. Clone/fetch your fork to your local development machine.
33+
1. Create a new branch (e.g. `issue-12`, `feat/add_foo`, etc) and check it out.
34+
1. Make your changes and commit them. (Did the tests pass?)
35+
1. Push your new branch to your fork. (e.g. `git push myname issue-12`)
36+
1. Open a Pull Request from your new branch to the original fork's `dev` branch.
37+
1. Ensure the Pull Request's automated status checks have all passed and are ready for review.
38+
39+
## Code structure
40+
41+
Source code is to be placed in the [src](src/) directory and tests in the [test](test/) directory. Test files should be suffixed with `.spec.js` and follow a similar test suite structure to existing test files.
42+
43+
All source and test code should follow the same style as existing source and test code and will be enforced by an automated [eslint](http://eslint.org/) style check that runs on every Pull Request.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016-2017 Aiham Hammami
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# opentok-react
22

3-
[![npm version](https://badge.fury.io/js/opentok-react.svg)](https://badge.fury.io/js/opentok-react)
3+
[![npm version](https://badge.fury.io/js/opentok-react.svg)](https://badge.fury.io/js/opentok-react) [![Build Status](https://travis-ci.org/aiham/opentok-react.svg?branch=master)](https://travis-ci.org/aiham/opentok-react)
44

55
React components for OpenTok.js
66

@@ -19,7 +19,9 @@ React components for OpenTok.js
1919
- [OTStreams Component](#otstreams-component)
2020
- [OTSubscriber Component](#otsubscriber-component)
2121
- [createSession Helper](#createsession-helper)
22+
- [preloadScript Higher-Order Component](#preloadscript-higher-order-component)
2223
- [Custom Build](#custom-build)
24+
- [Contributing](#contributing)
2325
- [Tests](#tests)
2426

2527
## Pre-Requisites
@@ -31,16 +33,24 @@ React components for OpenTok.js
3133

3234
Add `opentok-react` as a dependency of your application:
3335

36+
```sh
37+
yarn add opentok-react
38+
```
39+
40+
Or if you're still using npm:
41+
3442
```sh
3543
npm install --save opentok-react
3644
```
3745

38-
Include `opentok.js` before your application:
46+
Then include `opentok.js` before your application:
3947

4048
```html
4149
<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>
4250
```
4351

52+
Alternatively, wrap your top-level component using OpenTok with the [`preloadScript`](#preloadscript-higher-order-component) HOC. The HOC will take care of loading `opentok.js` for you before rendering.
53+
4454
## Example App
4555

4656
There is an example application provided in `example/` and you can run it with the following steps:
@@ -66,19 +76,13 @@ The following sections explains how to import and use `opentok-react` in your Re
6676
Import the `opentok-react` components into your React application:
6777

6878
```js
69-
import { OTSession, OTPublisher, OTStreams, OTSubscriber, createSession } from 'opentok-react';
79+
import { OTSession, OTPublisher, OTStreams, OTSubscriber } from 'opentok-react';
7080
```
7181

7282
Or `require` it if you need to use CommonJS modules:
7383

7484
```js
75-
var otReact = require('opentok-react');
76-
77-
var OTSession = otReact.OTSession;
78-
var OTPublisher = otReact.OTPublisher;
79-
var OTStreams = otReact.OTStreams;
80-
var OTSubscriber = otReact.OTSubscriber;
81-
var createSession = otReact.createSession;
85+
const { OTSession, OTPublisher, OTStreams, OTSubscriber } = require('opentok-react');
8286
```
8387

8488
### Example with OTSession Component
@@ -142,20 +146,37 @@ class MyApp extends React.Component {
142146

143147
## API Reference
144148

145-
The `opentok-react` library is comprised of:
149+
The `opentok-react` library comprises of:
146150

147151
- `OTSession` Component
148152
- `OTPublisher` Component
149153
- `OTStreams` Component
150154
- `OTSubscriber` Component
151155
- `createSession` Helper
156+
- `preloadScript` Higher-Order Component
152157

153158
### OTSession Component
154159

155-
TODO
160+
| Prop | Type | Required | Description |
161+
| --- | --- | --- | --- |
162+
| apiKey | String | Yes | TokBox API Key
163+
| sessionId | String | Yes | TokBox Session ID
164+
| token | String | Yes | TokBox token
165+
| eventHandlers | Object&lt;Function&gt; | No | Event handlers passed into `session.on`
166+
| onConnect | Function() | No | Invoked when `session.connect` successfully completes
167+
| onError | Function(err) | No | Invoked when `session.connect` fails
156168

157169
### OTPublisher Component
158170

171+
| Prop | Type | Required | Description |
172+
| --- | --- | --- | --- |
173+
| session | [Session](https://tokbox.com/developer/sdks/js/reference/Session.html) | No | OpenTok Session instance
174+
| properties | Object | No | Properties passed into `OT.initPublisher`
175+
| eventHandlers | Object&lt;Function&gt; | No | Event handlers passed into `publisher.on`
176+
| onInit | Function() | No | Invoked when `OT.initPublisher` successfully completes
177+
| onPublish | Function() | No | Invoked when `session.publish` successfully completes
178+
| onError | Function(err) | No | Invoked when either `OT.initPublisher` or `session.publish` fail
179+
159180
The `OTPublisher` component will initialise a publisher and publish to a specified session upon mounting. You must specify a [Session](https://tokbox.com/developer/sdks/js/reference/Session.html) object using the `session` prop.
160181

161182
```js
@@ -209,10 +230,23 @@ class MyApp extends React.Component {
209230

210231
### OTStreams Component
211232

212-
TODO
233+
| Prop | Type | Required | Description |
234+
| --- | --- | --- | --- |
235+
| children | OTSubscriber | Yes | Must only have a single `OTSubscriber` component (or similar component that accepts `session` and `stream` props)
236+
| session | [Session](https://tokbox.com/developer/sdks/js/reference/Session.html) | Yes | OpenTok Session instance. This is auto populated by wrapping `OTStreams` with `OTSession`
237+
| streams | Array&lt;[Stream](https://tokbox.com/developer/sdks/js/reference/Stream.html)&gt; | No | Array of OpenTok Stream instances. This is auto populated by wrapping `OTStreams` with `OTSession`
213238

214239
### OTSubscriber Component
215240

241+
| Prop | Type | Required | Description |
242+
| --- | --- | --- | --- |
243+
| session | [Session](https://tokbox.com/developer/sdks/js/reference/Session.html) | No | OpenTok Session instance. This is auto populated by wrapping `OTSubscriber` with `OTStreams`
244+
| stream | [Stream](https://tokbox.com/developer/sdks/js/reference/Stream.html) | No | OpenTok Stream instance. This is auto populated by wrapping `OTSubscriber` with `OTStreams`
245+
| properties | Object | No | Properties passed into `session.subscribe`
246+
| eventHandlers | Object&lt;Function&gt; | No | Event handlers passed into `subscriber.on`
247+
| onSubscribe | Function() | No | Invoked when `session.subscribe` successfully completes
248+
| onError | Function(err) | No | Invoked when `session.subscribe` fails
249+
216250
The `OTSubscriber` component will subscribe to a specified stream from a specified session upon mounting. You must provide a [Stream](https://tokbox.com/developer/sdks/js/reference/Stream.html) object using the `stream` prop and a [Session](https://tokbox.com/developer/sdks/js/reference/Session.html) object using the `session` prop.
217251

218252
```js
@@ -273,6 +307,39 @@ The `createSession` helper returns an object with the following properties:
273307

274308
Use of this helper is optional and you can instead use the `OTSession` component or directly call [OT.initSession](https://tokbox.com/developer/sdks/js/reference/OT.html#initSession) and listen to [streamCreated](https://tokbox.com/developer/sdks/js/reference/Session.html#event:streamCreated) events if you prefer.
275309

310+
311+
### `preloadScript` Higher-Order Component
312+
313+
| Prop | Type | Required | Description |
314+
| --- | --- | --- | --- |
315+
| opentokClientUrl | String | No | The URL of the OpenTok client script to load. It defaults to `https://static.opentok.com/v2/js/opentok.min.js`.
316+
| loadingDelegate | Element | No | An element that will be displayed while the OpenTok client script is loading. It defaults to an empty `<div />`.
317+
318+
In larger applications, one might not want to load the `opentok.js` client with a `<script>` tag all the time. The `preloadScript` higher-order component will do this for you at the appropriate time.
319+
320+
For example, imagine you have a React Router application with the following route structure:
321+
322+
```javascript
323+
<Router>
324+
<Route path="/">
325+
<IndexRoute component="..." />
326+
<Route path="something" component="..." />
327+
<Route path="video" component={VideoChat} />
328+
<Route path="something-else" component="..." />
329+
</Route>
330+
</Router>
331+
```
332+
333+
What you'd like to do is delay the loading of `opentok.js` until the `VideoChat` component is being used. Here's how you can do this:
334+
335+
```javascript
336+
class VideoChat extends React.Component {
337+
// All the code of your component
338+
}
339+
340+
export default preloadScript(App);
341+
```
342+
276343
## Custom Build
277344

278345
1. `git clone https://github.com/aiham/opentok-react.git`
@@ -282,6 +349,28 @@ Use of this helper is optional and you can instead use the `OTSession` component
282349
1. `npm run build`
283350
1. Check that files in `dist/` have been updated.
284351

352+
## Contributing
353+
354+
If you make changes to the project that you would like to contribute back then please follow the [contributing guidelines](CONTRIBUTING.md). All contributions are greatly appreciated!
355+
285356
## Tests
286357

287-
TODO
358+
Run the unit tests locally with the following command:
359+
360+
```
361+
npm run unit
362+
```
363+
364+
By default this will launch the Chrome browser. To run tests in Firefox use:
365+
366+
```
367+
npm run unit -- --browsers Firefox
368+
```
369+
370+
Run the linter with:
371+
372+
```
373+
npm run lint
374+
```
375+
376+
The unit tests are automatically run on [Travis](https://travis-ci.org/aiham/opentok-react) on both Chrome and Firefox and the current build status is shown at the top of this document.

example/app.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
2+
import { render } from 'react-dom';
33

44
import config from './config';
55
import App from './components/App';
66

7-
OT.registerScreenSharingExtension('chrome', config.CHROME_EXTENSION_ID, 2);
8-
ReactDOM.render((
7+
render((
98
<App
109
apiKey={config.API_KEY}
1110
sessionId={config.SESSION_ID}
1211
token={config.TOKEN}
12+
loadingDelegate={<div>Loading...</div>}
13+
opentokClientUrl="https://static.opentok.com/v2/js/opentok.min.js"
1314
/>
1415
), document.getElementById('content'));

example/components/App.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import React, { Component } from 'react';
22

3-
import { OTSession, OTStreams } from '../../src'
3+
import { OTSession, OTStreams, preloadScript } from '../../src'
44
import ConnectionStatus from './ConnectionStatus';
55
import Publisher from './Publisher';
66
import Subscriber from './Subscriber';
7+
import config from '../config';
78

8-
export default class App extends Component {
9+
class App extends Component {
910
constructor(props) {
1011
super(props);
1112

1213
this.state = {
14+
error: null,
1315
connected: false
1416
};
1517

@@ -23,14 +25,24 @@ export default class App extends Component {
2325
};
2426
}
2527

28+
componentWillMount() {
29+
OT.registerScreenSharingExtension('chrome', config.CHROME_EXTENSION_ID, 2);
30+
}
31+
32+
onError = (err) => {
33+
this.setState({ error: `Failed to connect: ${err.message}` });
34+
}
35+
2636
render() {
2737
return (
2838
<OTSession
2939
apiKey={this.props.apiKey}
3040
sessionId={this.props.sessionId}
3141
token={this.props.token}
3242
eventHandlers={this.sessionEvents}
43+
onError={this.onError}
3344
>
45+
{this.state.error ? <div>{this.state.error}</div> : null}
3446
<ConnectionStatus connected={this.state.connected} />
3547
<Publisher />
3648
<OTStreams>
@@ -40,3 +52,5 @@ export default class App extends Component {
4052
);
4153
}
4254
}
55+
56+
export default preloadScript(App);

0 commit comments

Comments
 (0)