Skip to content
This repository has been archived by the owner on Apr 19, 2021. It is now read-only.

changed debounce out for bindRaf #14

Merged
merged 3 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
node_modules
build
npm-debug.log
bundle.js
bundle.js

# ide folders
.idea

# for now removing package-lock as it's not my decision to include
package-lock.json
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, we should pin this somehow.

4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import debounce from './lib/debounce';
import { bindRaf } from "./lib/bindRaf";
Copy link
Owner

@dazld dazld Oct 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use kebab case for file names?


class OnVisible extends Component {
constructor() {
super(...arguments);
this.onScroll = debounce(this.onScroll.bind(this), 10);
this.onScroll = bindRaf(this.onScroll.bind(this));
this.state = {
visible: false,
bottom: 0,
Expand Down
22 changes: 22 additions & 0 deletions src/lib/bindRaf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const bindRaf = (fn) => {
let isRunning = null;
let that = null;
let args = null;

const run = () => {
isRunning = false;
fn.apply(that, args);
};

return () => {
that = this;
args = arguments;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not using these variables, so could probably make this simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try to get around to fixin that tomorrow, sorry I tried a few solution before I was happy, that’s the left over mess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dazld Just read over the code. both variables are used.

  1. self is used so that on callback the context of the function we are wrapping has the same context as the place we bound it (I think)
  2. args is used to grab the arguments that were passed in upon creation of the callback.

I have made another commit to rename that to the more standard self name and removed debounce as it's unused.

The only improvement this could use would be a cancellation token, but I think that can wait as the not binding to null fix previously is good enough.

Any more issues?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should learn to read, sorry @falconmick - no, this all looks ok.


if (isRunning) {
return;
}

isRunning = true;
requestAnimationFrame(run);
};
};