-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support system info driven by pressure api #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cb851bd
feat: support system info driven by pressure api
k-wasniowski 0e3ee47
feat: different approach
k-wasniowski 63f1ca7
feat: add possibility to listen for changes
k-wasniowski 4331b9a
feat: add unit tests
k-wasniowski aeaa1c8
feat: add option to deregister listener
k-wasniowski dae4371
feat: better typing
k-wasniowski ea8716b
fix: deprecate cpu-info and add comment about types
k-wasniowski eba1002
fix: missed rename
k-wasniowski 92ed33d
fix: do not throw
k-wasniowski 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 hidden or 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 |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| "transcoding", | ||
| "transpiled", | ||
| "typedoc", | ||
| "Unregisters", | ||
| "untracked", | ||
| "videostateupdate", | ||
| "VITE", | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './browser-info'; | ||
| export * from './web-capabilities'; | ||
| export * from './cpu-info'; | ||
| export * from './system-info'; | ||
| export * from './web-capabilities'; | ||
This file contains hidden or 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,194 @@ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let pressureObserverCallback: any; | ||
|
|
||
| /** | ||
| * Mock implementation of PressureObserver to simulate CPU pressure states. | ||
| */ | ||
| class MockPressureObserver { | ||
| /** | ||
| * Mock implementation of PressureObserver to simulate CPU pressure states. | ||
| * | ||
| * @param callback - The callback to be called with pressure records. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| constructor(callback: (list: any) => void) { | ||
| pressureObserverCallback = callback; | ||
| } | ||
|
|
||
| /** | ||
| * Attaches the observer to a source to observe state changes. | ||
| */ | ||
| // eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-empty-function | ||
| observe() {} | ||
| } | ||
|
|
||
| Object.defineProperty(window, 'PressureObserver', { | ||
| writable: true, | ||
| configurable: true, | ||
| value: MockPressureObserver, | ||
| }); | ||
|
|
||
| // Import the SystemInfo class after defining the PressureObserver mock | ||
| // This ensures that the mock is available when SystemInfo is imported. | ||
| // eslint-disable-next-line import/first | ||
| import { SystemInfo } from './system-info'; | ||
|
|
||
| describe('SystemInfo', () => { | ||
| describe('isPressureObserverSupported', () => { | ||
| it('should return true when PressureObserver is supported', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| Object.defineProperty(window, 'PressureObserver', { | ||
| writable: true, | ||
| configurable: true, | ||
| value: {}, | ||
| }); | ||
|
|
||
| expect(SystemInfo.isPressureObserverSupported()).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false when PressureObserver is not supported', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| // Ensure that PressureObserver is not defined | ||
| delete window.PressureObserver; | ||
|
|
||
| expect(SystemInfo.isPressureObserverSupported()).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getCpuPressure', () => { | ||
| describe('when PressureObserver is supported', () => { | ||
| beforeEach(() => { | ||
| Object.defineProperty(window, 'PressureObserver', { | ||
| writable: true, | ||
| configurable: true, | ||
| value: MockPressureObserver, | ||
| }); | ||
| }); | ||
|
|
||
| it('should return undefined when information is not available', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| expect(SystemInfo.getCpuPressure()).toBeUndefined(); | ||
| }); | ||
|
|
||
| ['nominal', 'fair', 'serious', 'critical'].forEach((state) => { | ||
| it(`should return the last CPU pressure state as ${state}`, () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| pressureObserverCallback([{ source: 'cpu', state }]); | ||
|
|
||
| expect(SystemInfo.getCpuPressure()).toBe(state); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when PressureObserver is not supported', () => { | ||
| beforeEach(() => { | ||
| // Ensure that PressureObserver is not defined | ||
| delete window.PressureObserver; | ||
| }); | ||
|
|
||
| it('should return undefined', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| expect(SystemInfo.getCpuPressure()).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('onCpuPressureChange', () => { | ||
| describe('when PressureObserver is supported', () => { | ||
| beforeEach(() => { | ||
| Object.defineProperty(window, 'PressureObserver', { | ||
| writable: true, | ||
| configurable: true, | ||
| value: MockPressureObserver, | ||
| }); | ||
| }); | ||
|
|
||
| it('should call the callback when CPU pressure state changes', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| const callback = jest.fn(); | ||
| SystemInfo.onCpuPressureChange(callback); | ||
|
|
||
| pressureObserverCallback([{ source: 'cpu', state: 'nominal' }]); | ||
| expect(callback).toHaveBeenCalledWith('nominal'); | ||
| }); | ||
|
|
||
| it('should not call the callback if the CPU pressure state does not change', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| const callback = jest.fn(); | ||
| SystemInfo.onCpuPressureChange(callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith('nominal'); | ||
|
|
||
| // Call with the same state | ||
| pressureObserverCallback([{ source: 'cpu', state: 'nominal' }]); | ||
| expect(callback).toHaveBeenCalledTimes(1); // Should not be called again | ||
|
|
||
| // Call with a different state | ||
| pressureObserverCallback([{ source: 'cpu', state: 'fair' }]); | ||
| expect(callback).toHaveBeenCalledWith('fair'); | ||
| }); | ||
|
|
||
| it('should not emit if callback was deregistered', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| const callback = jest.fn(); | ||
| SystemInfo.onCpuPressureChange(callback); | ||
| expect(callback).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Call with the same state | ||
| pressureObserverCallback([{ source: 'cpu', state: 'nominal' }]); | ||
| expect(callback).toHaveBeenCalledWith('nominal'); | ||
| expect(callback).toHaveBeenCalledTimes(2); | ||
|
|
||
| // Deregister the callback | ||
| SystemInfo.offCpuPressureChange(callback); | ||
|
|
||
| // Call with a different state | ||
| pressureObserverCallback([{ source: 'cpu', state: 'fair' }]); | ||
| expect(callback).toHaveBeenCalledTimes(2); // Should not be called again | ||
| }); | ||
| }); | ||
|
|
||
| describe('when PressureObserver is not supported', () => { | ||
| beforeEach(() => { | ||
| // Ensure that PressureObserver is not defined | ||
| delete window.PressureObserver; | ||
| }); | ||
|
|
||
| it('should not attach listener', () => { | ||
| expect.hasAssertions(); | ||
|
|
||
| const callback = jest.fn(); | ||
|
|
||
| SystemInfo.onCpuPressureChange(callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledTimes(0); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getNumLogicalCores', () => { | ||
| it('should return the number of logical CPU cores when the information is available', () => { | ||
| expect.assertions(1); | ||
|
|
||
| jest.spyOn(Navigator.prototype, 'hardwareConcurrency', 'get').mockReturnValue(1); | ||
|
|
||
| expect(SystemInfo.getNumLogicalCores()).toBe(1); | ||
| }); | ||
|
|
||
| it('should return undefined when the logical CPU cores information is not available', () => { | ||
| expect.assertions(1); | ||
|
|
||
| jest.spyOn(Navigator.prototype, 'hardwareConcurrency', 'get').mockImplementation(); | ||
|
|
||
| expect(SystemInfo.getNumLogicalCores()).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.