-
Notifications
You must be signed in to change notification settings - Fork 18
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
Describing and handling sharedFacet errors #125
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a821dbc
Change the facet factory type to be a more normal object
c7fcf1c
Add task to run typecheck only
a8ecad5
Updates to fix type inconsistencies
2fd52bd
wip
b41462d
Added new way of caching functions with more than one argument, and i…
Shenato 8a0a9b7
Added test for SharedFacetsAvailable which acts as an errorboundary
Shenato 93334a3
WIP
Shenato 580b102
We finally found a way to delegate error handling type to the user la…
Shenato d94f024
Renamed SharedFacetsAvailable to SharedFacetsErrorBoundary
Shenato 754e786
Added a test for checking that providing a local error handler is use…
Shenato bf6f5d1
Small organization improvements in the tests for error handling
Shenato 70ae890
Little re-org in test file
Shenato 32c76e0
fix lint issues
Shenato 6c51fc8
Merge branch 'main' into on-error-shared-facet
Shenato 28f7a71
Merge branch 'main' into on-error-shared-facet
Shenato 4600e59
Merge branch 'main' into on-error-shared-facet
Shenato 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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"publish": "yarn workspaces foreach --topological --no-private npm publish --tolerate-republish --access public", | ||
"test": "cross-env NODE_ENV=test jest --coverage", | ||
"lint": "eslint .", | ||
"types": "tsc --noEmit", | ||
"package": "yarn workspaces foreach --topological --verbose pack --out=%s-%v.tgz && ts-node ./scripts/moveAllPackagedToArtifacts.ts" | ||
}, | ||
"devDependencies": { | ||
|
@@ -43,7 +44,8 @@ | |
"jest-junit-reporter": "^1.1.0", | ||
"jest-sonar-reporter": "2.0.0", | ||
"prettier": "^2.3.2", | ||
"ts-node": "^10.9.1" | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.2" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
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
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
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
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
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
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
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
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
14 changes: 14 additions & 0 deletions
14
packages/@react-facet/shared-facet/src/components/SharedFacetsErrorBoundary.tsx
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,14 @@ | ||
import React, { ReactNode } from 'react' | ||
import { sharedFacetsErrorBoundaryContext } from '../context/sharedFacetAvailable' | ||
import { SharedFacetError } from '../types' | ||
|
||
export type SharedFacetsErrorBoundaryProps = { | ||
children: ReactNode | ||
onError: (args: SharedFacetError) => void | ||
} | ||
|
||
export const SharedFacetsErrorBoundary = ({ onError, children }: SharedFacetsErrorBoundaryProps) => { | ||
return ( | ||
<sharedFacetsErrorBoundaryContext.Provider value={onError}>{children}</sharedFacetsErrorBoundaryContext.Provider> | ||
) | ||
} |
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 @@ | ||
export * from './SharedFacetsErrorBoundary' |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
export * from './sharedFacetAvailable' | ||
export * from './sharedFacetDriver' |
8 changes: 8 additions & 0 deletions
8
packages/@react-facet/shared-facet/src/context/sharedFacetAvailable.tsx
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,8 @@ | ||
import { createContext } from 'react' | ||
import { SharedFacetError } from '../types' | ||
|
||
export type SharedFacetErrorBoundaryCallback = (error: SharedFacetError) => void | ||
|
||
const noop = () => {} | ||
|
||
export const sharedFacetsErrorBoundaryContext = createContext<SharedFacetErrorBoundaryCallback>(noop) |
14 changes: 14 additions & 0 deletions
14
packages/@react-facet/shared-facet/src/context/sharedFacetDriver.tsx
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,14 @@ | ||
import React, { ReactNode, createContext } from 'react' | ||
import { SharedFacetDriver } from '../types' | ||
|
||
const dummyConstructor = () => () => {} | ||
|
||
export type SharedFacetDriverProviderProps = { | ||
children: ReactNode | ||
driver: SharedFacetDriver | ||
} | ||
export const sharedFacetDriverContext = createContext<SharedFacetDriver>(dummyConstructor) | ||
|
||
export const SharedFacetDriverProvider = ({ children, driver }: SharedFacetDriverProviderProps) => { | ||
return <sharedFacetDriverContext.Provider value={driver}>{children}</sharedFacetDriverContext.Provider> | ||
} |
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,53 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
type ArgsToValueMapType<T extends Array<any>, V> = Map<T, V> | ||
|
||
function areArraysMatchingRef<T extends Array<any>>(key1: T, key2: T): boolean { | ||
if (key1.length !== key2.length) { | ||
return false | ||
} | ||
|
||
// This assumes we will never change the order of the array key in the set, otherwise the loops will be | ||
// exponential in relation to how many arguments are in the keys | ||
for (let i = 0; i < key1.length; i++) { | ||
if (key1[i] !== key2[i]) return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
export function functionCaching<T extends Array<any>, V>() { | ||
const refs: ArgsToValueMapType<T, V> = new Map() | ||
|
||
function removeFromRef(argsKey: T) { | ||
for (const [key] of refs) { | ||
if (areArraysMatchingRef(key, argsKey)) { | ||
refs.delete(key) | ||
return | ||
} | ||
} | ||
} | ||
|
||
function getFromRef(argsKey: T): V | undefined { | ||
for (const [key, value] of refs) { | ||
if (areArraysMatchingRef(key, argsKey)) { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
function addToRef(argsKey: T, facet: V) { | ||
const existingRecord = getFromRef(argsKey) | ||
if (existingRecord) { | ||
return | ||
} | ||
refs.set(argsKey, facet) | ||
} | ||
|
||
return { | ||
refs, | ||
addToRef, | ||
removeFromRef, | ||
getFromRef, | ||
} | ||
} |
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 @@ | ||
export * from './useSharedFacet' |
14 changes: 14 additions & 0 deletions
14
packages/@react-facet/shared-facet/src/hooks/useSharedFacet.ts
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,14 @@ | ||
import { useContext } from 'react' | ||
import { Facet } from '@react-facet/core' | ||
import { sharedFacetDriverContext } from '../context/sharedFacetDriver' | ||
import { SharedFacet, SharedFacetError } from '../types' | ||
import { sharedFacetsErrorBoundaryContext } from '../context' | ||
|
||
export const useSharedFacet = <T>( | ||
sharedFacet: SharedFacet<T>, | ||
onErrorCallback?: (error: SharedFacetError) => void, | ||
): Facet<T> => { | ||
const sharedFacetDriver = useContext(sharedFacetDriverContext) | ||
const onErrorWrapperCallback = useContext(sharedFacetsErrorBoundaryContext) | ||
return sharedFacet.initializer(sharedFacetDriver, onErrorCallback ?? onErrorWrapperCallback) | ||
} |
Oops, something went wrong.
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.
It looks like this was added at one point, but its no longer needed?
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.
Because of this needed change to cleanup the new cache for SharedFacets in
useSharedSelector
I had to change alll functions similar tomapFacetsCached
to include an optional onCleanup callback function to allow the cleaning up the cache when the facets are no longer there.