-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main 🧊 add use less, add use performance observer
- Loading branch information
Showing
20 changed files
with
233 additions
and
15 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useEffect } from 'react'; | ||
/** | ||
* @name useLess | ||
* @description - Hook that humorously suggests using it less not for production | ||
* @category Humor | ||
* | ||
* @template Value The type of the value | ||
* @param {Value} [value] The value to be returned | ||
* @returns {Value} The value passed to the hook | ||
* | ||
* @example | ||
* const value = useLess(state); | ||
*/ | ||
export const useLess = (value) => { | ||
useEffect(() => { | ||
console.warn("Warning: You forgot to delete the 'useLess' hook."); | ||
}, []); | ||
return value; | ||
}; |
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
46 changes: 46 additions & 0 deletions
46
packages/core/src/bundle/hooks/usePerformanceObserver/usePerformanceObserver.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,46 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
/** | ||
* @name usePerformanceObserver | ||
* @description - Hook that allows you to observe performance entries | ||
* @category Sensor | ||
* | ||
* @param {UsePerformanceObserverOptions} options The options for the performance observer | ||
* @param {PerformanceObserverCallback} callback The function to handle performance entries | ||
* @returns {object} An object containing the observer's support status and methods to start and stop the observer | ||
* | ||
* @example | ||
* const { supported, entries, start, stop } = usePerformanceObserver(); | ||
*/ | ||
export const usePerformanceObserver = (options, callback) => { | ||
const supported = typeof window !== 'undefined' && typeof PerformanceObserver !== 'undefined'; | ||
const [entries, setEntries] = useState([]); | ||
const observerRef = useRef(null); | ||
const internalCallback = useRef(); | ||
internalCallback.current = callback; | ||
const start = () => { | ||
if (!supported) | ||
return; | ||
const observer = new PerformanceObserver((entryList, observer) => { | ||
setEntries(entryList.getEntries()); | ||
internalCallback.current?.(entryList, observer); | ||
}); | ||
observer.observe(options); | ||
observerRef.current = observer; | ||
}; | ||
const stop = () => { | ||
if (!supported) | ||
return; | ||
observerRef.current?.disconnect(); | ||
observerRef.current = null; | ||
}; | ||
useEffect(() => { | ||
if (!supported) | ||
return; | ||
if (options.immediate) | ||
start(); | ||
return () => { | ||
stop(); | ||
}; | ||
}, []); | ||
return { supported, entries, start, stop }; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useCounter } from '../useCounter/useCounter'; | ||
import { useLess } from './useLess'; | ||
|
||
const Demo = () => { | ||
const counter = useCounter(); | ||
const value = useLess(counter.value); | ||
|
||
return ( | ||
<div> | ||
<div className='rounded-xl bg-amber-50 p-4 text-amber-500'> | ||
<strong>Warning:</strong> This hook is a joke. Please do not use it in production code! | ||
</div> | ||
|
||
<p> | ||
Useless value is <code>{value}</code> | ||
</p> | ||
|
||
<button className='button' type='button' onClick={() => counter.inc()}> | ||
Increment | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Demo; |
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,17 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
import { useLess } from './useLess'; | ||
|
||
it('Should use less', () => { | ||
const { result } = renderHook(() => useLess('value')); | ||
|
||
expect(result.current).toBe('value'); | ||
}); | ||
|
||
it('Should log a warning when useLess is used', () => { | ||
const mockConsoleWarn = vi.spyOn(console, 'warn'); | ||
renderHook(() => useLess()); | ||
|
||
expect(mockConsoleWarn).toHaveBeenCalledWith("Warning: You forgot to delete the 'useLess' hook."); | ||
mockConsoleWarn.mockRestore(); | ||
}); |
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 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* @name useLess | ||
* @description - Hook that humorously suggests using it less not for production | ||
* @category Humor | ||
* | ||
* @template Value The type of the value | ||
* @param {Value} [value] The value to be returned | ||
* @returns {Value} The value passed to the hook | ||
* | ||
* @example | ||
* const value = useLess(state); | ||
*/ | ||
export const useLess = (value?: any) => { | ||
useEffect(() => { | ||
console.warn("Warning: You forgot to delete the 'useLess' hook."); | ||
}, []); | ||
|
||
return value; | ||
}; |
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
28 changes: 28 additions & 0 deletions
28
packages/core/src/hooks/usePerformanceObserver/usePerformanceObserver.demo.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,28 @@ | ||
import { usePerformanceObserver } from './usePerformanceObserver'; | ||
|
||
const Demo = () => { | ||
const performance = usePerformanceObserver( | ||
{ entryTypes: ['paint'], immediate: true } | ||
); | ||
|
||
const refresh = () => window.location.reload(); | ||
|
||
return ( | ||
<div> | ||
<p>Performance entries:</p> | ||
<ul> | ||
{performance.entries.map((entry, index) => ( | ||
<li key={index}> | ||
{entry.name}:{' '} | ||
<code> | ||
{entry.startTime} - {entry.duration}ms | ||
</code> | ||
</li> | ||
))} | ||
</ul> | ||
<button onClick={refresh}>Refresh</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Demo; |
58 changes: 58 additions & 0 deletions
58
packages/core/src/hooks/usePerformanceObserver/usePerformanceObserver.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,58 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
/** The use performance observer options type */ | ||
export type UsePerformanceObserverOptions = PerformanceObserverInit & { | ||
/** Whether to start the observer immediately */ | ||
immediate?: boolean; | ||
}; | ||
|
||
/** | ||
* @name usePerformanceObserver | ||
* @description - Hook that allows you to observe performance entries | ||
* @category Sensor | ||
* | ||
* @param {UsePerformanceObserverOptions} options The options for the performance observer | ||
* @param {PerformanceObserverCallback} callback The function to handle performance entries | ||
* @returns {object} An object containing the observer's support status and methods to start and stop the observer | ||
* | ||
* @example | ||
* const { supported, entries, start, stop } = usePerformanceObserver(); | ||
*/ | ||
export const usePerformanceObserver = ( | ||
options: UsePerformanceObserverOptions, | ||
callback?: PerformanceObserverCallback | ||
) => { | ||
const supported = typeof window !== 'undefined' && typeof PerformanceObserver !== 'undefined'; | ||
const [entries, setEntries] = useState<PerformanceEntry[]>([]); | ||
|
||
const observerRef = useRef<PerformanceObserver | null>(null); | ||
const internalCallback = useRef<PerformanceObserverCallback | null>(); | ||
internalCallback.current = callback; | ||
|
||
const start = () => { | ||
if (!supported) return; | ||
const observer = new PerformanceObserver((entryList, observer) => { | ||
setEntries(entryList.getEntries()); | ||
internalCallback.current?.(entryList, observer); | ||
}); | ||
observer.observe(options); | ||
observerRef.current = observer; | ||
}; | ||
|
||
const stop = () => { | ||
if (!supported) return; | ||
observerRef.current?.disconnect(); | ||
observerRef.current = null; | ||
}; | ||
|
||
useEffect(() => { | ||
if (!supported) return; | ||
if (options.immediate) start(); | ||
|
||
return () => { | ||
stop(); | ||
}; | ||
}, []); | ||
|
||
return { supported, entries, start, stop }; | ||
}; |