-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ceed691
Initialize dummy package.
StevenXL c214559
Create dev script.
StevenXL 3047a80
Add testing via jest.
StevenXL aa0fb64
Add flow to package.
StevenXL 8f1c02f
Add CI artifacts.
StevenXL 2f19402
Add restyled configuration.
StevenXL c194e85
Use flow to type check test files.
StevenXL c14a7a9
Port `useExtraDeps`.
StevenXL d801d9f
Use flow-copy-source to generate .flow files.
StevenXL ad2f6d5
Remove type variable from PrimitiveDep.
StevenXL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
[include] | ||
|
||
[libs] | ||
|
||
[lints] | ||
|
||
[options] | ||
|
||
[strict] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @freckle/react-hooks | ||
|
||
Provides a collection of React hooks. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { | ||
unCallbackFn, | ||
unsafeMkCallbackFn, | ||
useExtraDeps, | ||
idExtraDep, | ||
isEqualExtraDep, | ||
} from "./use-extra-deps"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See: facebook/flow#2364