Skip to content

Commit

Permalink
[Refactor] Utility: Extract noop methods to util
Browse files Browse the repository at this point in the history
Create two utility functions one returning empty Promise object and one returning void to use as default props for the component and HoC.

Signed-off-by: Progyan Bhattacharya <[email protected]>
  • Loading branch information
Progyan-APAC committed Nov 22, 2019
1 parent 6f4fcc2 commit cdb32cf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-optimistic-toggle",
"version": "0.5.2",
"version": "0.5.3",
"description": "React component to build Optimistic UIs.",
"main": "lib/OptimisticToggle.js",
"repository": "https://github.com/interviewstreet/react-optimistic-toggle.git",
Expand Down
4 changes: 2 additions & 2 deletions src/OptimisticToggle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React, { Component, SyntheticEvent } from 'react';

const noop = () => {};
import { noop, noopPromise } from './util';

type Props = {
/** Initial Value of the Checkbox */
Expand All @@ -19,7 +19,7 @@ type State = {
class OptimisticToggle extends Component<Props, State> {
static defaultProps: Props = {
initialValue: false,
action: noop,
action: noopPromise,
children: noop,
};

Expand Down
4 changes: 2 additions & 2 deletions src/useOptimisticToggle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useRef, SyntheticEvent } from 'react';

const noop = () => {};
import { noopPromise } from './util';

type Props = {
/** Initial Value of the Checkbox */
Expand All @@ -9,7 +9,7 @@ type Props = {
action?: (toggleState: boolean, event: SyntheticEvent) => Promise<any>,
};

function useOptimisticToggle({ initialValue = false, action = noop }: Props) {
function useOptimisticToggle({ initialValue = false, action = noopPromise }: Props) {
const [stateOptimistic, setStateOptimistic] = useState(initialValue);
const refCurrentPromise = useRef();
const refFailedCount = useRef(0);
Expand Down
10 changes: 10 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore next */
export function noop() {
/** No Operation */
}

/* istanbul ignore next */
export function noopPromise() {
/** No Operation that Returns a Promise */
return Promise.resolve();
}

0 comments on commit cdb32cf

Please sign in to comment.