Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize Package #1

Merged
merged 10 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[ignore]
.*/node_modules/resolve/test/resolver/malformed_package_json/package.json
Copy link
Collaborator Author

Choose a reason for hiding this comment

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



[include]

[libs]

[lints]

[options]

[strict]
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CI

on:
pull_request:
push:
branches: main

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
cache: yarn
- run: yarn install
- run: yarn build
- run: yarn test
- run: yarn run check-git-clean
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
15 changes: 15 additions & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exclude:
- "**/dist/**/*"
StevenXL marked this conversation as resolved.
Show resolved Hide resolved
- "**/*.patch"
- "**/node_modules/**/*"
- "**/vendor/**/*"
- ".github/workflows/**/*" # https://github.com/restyled-io/restyler/issues/73

restylers:
- clang-format:
enabled: false
- jq:
enabled: false # prefer prettier-json
- prettier:
include:
- 'src/**/*.js'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 Renaissance Learning Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @freckle/react-hooks

Provides a collection of React hooks.
Copy link
Member

Choose a reason for hiding this comment

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

useExtraDeps is only used internally, but I see that type CallbackFn and unCallbackFn is imported across the Freckle frontends. It's exported in this extracted module to avoid cyclical dependencies. After use-safe-callback is extracted, these should be documented

13 changes: 13 additions & 0 deletions check-git-clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash -e

if ! git diff --quiet; then echo "

The CI build resulted in additional changed files.
Typically this is due to not running yarn build locally before
submitting a pull request.

The following changes were found:
";

git diff --exit-code;
fi;
37 changes: 37 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "idExtraDep", {
enumerable: true,
get: function get() {
return _useExtraDeps.idExtraDep;
}
});
Object.defineProperty(exports, "isEqualExtraDep", {
enumerable: true,
get: function get() {
return _useExtraDeps.isEqualExtraDep;
}
});
Object.defineProperty(exports, "unCallbackFn", {
enumerable: true,
get: function get() {
return _useExtraDeps.unCallbackFn;
}
});
Object.defineProperty(exports, "unsafeMkCallbackFn", {
enumerable: true,
get: function get() {
return _useExtraDeps.unsafeMkCallbackFn;
}
});
Object.defineProperty(exports, "useExtraDeps", {
enumerable: true,
get: function get() {
return _useExtraDeps.useExtraDeps;
}
});

var _useExtraDeps = require("./use-extra-deps");
7 changes: 7 additions & 0 deletions dist/index.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {
unCallbackFn,
unsafeMkCallbackFn,
useExtraDeps,
idExtraDep,
isEqualExtraDep,
} from "./use-extra-deps";
125 changes: 125 additions & 0 deletions dist/use-extra-deps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.unCallbackFn = exports.isEqualExtraDep = exports.idExtraDep = void 0;
exports.unsafeMkCallbackFn = unsafeMkCallbackFn;
exports.useExtraDeps = useExtraDeps;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var React = _interopRequireWildcard(require("react"));

var _pickBy = _interopRequireDefault(require("lodash/pickBy"));

var _omitBy = _interopRequireDefault(require("lodash/omitBy"));

var _isFunction = _interopRequireDefault(require("lodash/isFunction"));

var _mapValues = _interopRequireDefault(require("lodash/mapValues"));

var _values = _interopRequireDefault(require("lodash/values"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var unCallbackFn = function unCallbackFn(fn) {
return fn;
}; // Used only by `useSafeCallback`


exports.unCallbackFn = unCallbackFn;

function unsafeMkCallbackFn(f) {
return f;
}

// Hook used to help avoid pitfalls surrounding misuse of objects and arrays in the deps of
// `useEffect` et. al.
//
// By only allowing `PrimitiveDep`s in the `deps` array and forcing functions and non-primitives
// through `extraDeps`, we can ensure that we are not doing naive reference equality like React
// does for the `deps` array.
//
// See `useSafeEffect` for usage of this hook
//
// Returns an object based upon deps and extraDeps:
// { allDeps: An array that is suitable to use as a deps array for things like `useEffect`
// , extraDepValues: An object that has the same keys as extraDeps but contains their plain values
// }
//
function useExtraDeps(deps, extraDeps) {
var _React$useState = React.useState(Symbol()),
_React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2),
run = _React$useState2[0],
setRun = _React$useState2[1];

var nonFnsRef = React.useRef(null);
var fns = (0, _pickBy["default"])(extraDeps, _isFunction["default"]);
var nonFns = (0, _omitBy["default"])(extraDeps, _isFunction["default"]);

var hasChange = function hasChange() {
if (nonFnsRef.current === null || nonFnsRef.current === undefined) {
return true;
}

for (var _key in nonFns) {
if (!nonFns[_key].comparator(nonFns[_key].value, nonFnsRef.current[_key].value)) {
return true;
}
}

return false;
};

if (hasChange()) {
setRun(Symbol());
nonFnsRef.current = nonFns;
}

return {
allDeps: [].concat((0, _toConsumableArray2["default"])(deps), (0, _toConsumableArray2["default"])((0, _values["default"])(fns)), [run]),
extraDepValues: _objectSpread(_objectSpread({}, (0, _mapValues["default"])(nonFns, function (_ref) {
var value = _ref.value;
return value;
})), fns)
};
}

var idExtraDep = function idExtraDep(value) {
return {
value: value,
comparator: function comparator(a, b) {
return a.id === b.id;
}
};
};

exports.idExtraDep = idExtraDep;

var isEqualExtraDep = function isEqualExtraDep(value) {
return {
value: value,
comparator: function comparator(a, b) {
return (0, _isEqual["default"])(a, b);
}
};
};

exports.isEqualExtraDep = isEqualExtraDep;
98 changes: 98 additions & 0 deletions dist/use-extra-deps/index.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// @flow

import * as React from "react";
import pickBy from "lodash/pickBy";
import omitBy from "lodash/omitBy";
import isFunction from "lodash/isFunction";
import mapValues from "lodash/mapValues";
import values from "lodash/values";
import isEqual from "lodash/isEqual";

export type PrimitiveLangDep = boolean | string | number | null | void | symbol;

// Dependencies that are safe to use in the normal `useEffect` deps array
//
// `PrimitiveDep` is a generic type. Typically usage is to bind the type
// variable `PrimitiveDomainDep` to opaque types that wrap primitives.
export type PrimitiveDep<PrimitiveDomainDep> =
| PrimitiveLangDep
| PrimitiveDomainDep;

// Wrapper around a function that has been wrapped in `useSafeCallback`. This type is here to avoid
// cyclical dependencies.
export opaque type CallbackFn<F> = F;

export const unCallbackFn = <F>(fn: CallbackFn<F>): F => fn;

// Used only by `useSafeCallback`
export function unsafeMkCallbackFn<F: (...Array<any>) => any>(
f: F
): CallbackFn<F> {
return f;
}

export type ExtraDeps<V> =
| {| value: V, comparator: (a: V, b: V) => boolean |}
| CallbackFn<V>;

// Hook used to help avoid pitfalls surrounding misuse of objects and arrays in the deps of
// `useEffect` et. al.
//
// By only allowing `PrimitiveDep`s in the `deps` array and forcing functions and non-primitives
// through `extraDeps`, we can ensure that we are not doing naive reference equality like React
// does for the `deps` array.
//
// See `useSafeEffect` for usage of this hook
//
// Returns an object based upon deps and extraDeps:
// { allDeps: An array that is suitable to use as a deps array for things like `useEffect`
// , extraDepValues: An object that has the same keys as extraDeps but contains their plain values
// }
//
export function useExtraDeps<S: { [key: string]: any }, PrimitiveDomainDep>(
deps: $ReadOnlyArray<PrimitiveDep<PrimitiveDomainDep>>,
extraDeps: S
): {|
allDeps: $ReadOnlyArray<any>,
extraDepValues: $ObjMap<S, <V>(ExtraDeps<V>) => V>,
|} {
const [run, setRun] = React.useState<symbol>(Symbol());
const nonFnsRef = React.useRef(null);

const fns = pickBy(extraDeps, isFunction);
const nonFns = omitBy(extraDeps, isFunction);

const hasChange = () => {
if (nonFnsRef.current === null || nonFnsRef.current === undefined) {
return true;
}
for (const key in nonFns) {
if (
!nonFns[key].comparator(nonFns[key].value, nonFnsRef.current[key].value)
) {
return true;
}
}
return false;
};

if (hasChange()) {
setRun(Symbol());
nonFnsRef.current = nonFns;
}

return {
allDeps: [...deps, ...values(fns), run],
extraDepValues: { ...mapValues(nonFns, ({ value }) => value), ...fns },
};
}

export const idExtraDep = <V: { id: string }>(value: V): ExtraDeps<V> => ({
value,
comparator: (a, b) => a.id === b.id,
});

export const isEqualExtraDep = <V>(value: V): ExtraDeps<V> => ({
value,
comparator: (a: V, b: V): boolean => isEqual(a, b),
});
Loading