@nordicsemiconductor/pc-nrfconnect-shared 60.0.0-pre1 → 60.0.0-pre2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bootstrap.css +8951 -0
- package/dist/index.css +1451 -0
- package/dist/index.js +122119 -0
- package/{src/Logo/nordic-logo-blue-icon-only.png → dist/nordic-logo-blue-icon-only-H7BABN6D.png} +0 -0
- package/{src/Logo/nordic-logo-gray-icon-only.png → dist/nordic-logo-gray-icon-only-GATOTR3L.png} +0 -0
- package/package.json +12 -3
- package/typings/generated/src/Device/deviceInfo/deviceInfo.d.ts +1 -2
- package/.github/ISSUE_TEMPLATE/custom.md +0 -10
- package/.husky/pre-push +0 -3
- package/.renovaterc.json +0 -10
- package/Changelog.md +0 -2973
- package/azure-pipelines.yml +0 -34
- package/config/eslintrc.js +0 -190
- package/config/jest.config.js +0 -41
- package/config/prettier.config.js +0 -13
- package/config/tsconfig.json +0 -15
- package/fw/bootloader/graviton_bootloader_v1.0.1-[nRF5_SDK_15.0.1-1.alpha_f76d012].zip +0 -0
- package/jest.config.js +0 -7
- package/main/index.ts +0 -32
- package/mocks/deviceLibMock.ts +0 -21
- package/mocks/electronMock.ts +0 -15
- package/mocks/electronStoreMock.ts +0 -19
- package/mocks/emptyMock.ts +0 -7
- package/mocks/fileMock.ts +0 -13
- package/mocks/gaMock.ts +0 -9
- package/mocks/mockTests.test.ts +0 -15
- package/mocks/packageJsonMock.ts +0 -10
- package/mocks/remoteMock.ts +0 -17
- package/scripts/esbuild-bootstrap.js +0 -15
- package/scripts/esbuild-renderer.js +0 -131
- package/scripts/installHusky.ts +0 -17
- package/scripts/prepare-shared-release.ts +0 -122
- package/scripts/release-shared.test.ts +0 -132
- package/scripts/release-shared.ts +0 -207
- package/src/About/About.tsx +0 -25
- package/src/About/AboutButton.tsx +0 -28
- package/src/About/ApplicationCard.tsx +0 -83
- package/src/About/DeviceCard.tsx +0 -74
- package/src/About/DocumentationCard.tsx +0 -18
- package/src/About/DocumentationSection.tsx +0 -24
- package/src/About/Section.tsx +0 -21
- package/src/About/ShortcutButton.tsx +0 -41
- package/src/About/SupportCard.tsx +0 -97
- package/src/About/about.scss +0 -40
- package/src/About/documentationSlice.ts +0 -41
- package/src/About/section.scss +0 -15
- package/src/About/shortcutSlice.ts +0 -52
- package/src/Alert/Alert.module.scss +0 -29
- package/src/Alert/Alert.tsx +0 -37
- package/src/App/App.test.tsx +0 -47
- package/src/App/App.tsx +0 -227
- package/src/App/ConnectedToStore.tsx +0 -19
- package/src/App/VisibilityBar.tsx +0 -103
- package/src/App/app.scss +0 -83
- package/src/App/appLayout.ts +0 -95
- package/src/App/shared.scss +0 -49
- package/src/App/visibility-bar.scss +0 -55
- package/src/Button/Button.tsx +0 -66
- package/src/Button/button.module.scss +0 -154
- package/src/Card/Card.tsx +0 -26
- package/src/Card/card.module.scss +0 -28
- package/src/Device/BrokenDeviceDialog/BrokenDeviceDialog.tsx +0 -58
- package/src/Device/BrokenDeviceDialog/broken-device-dialog.scss +0 -18
- package/src/Device/BrokenDeviceDialog/brokenDeviceDialogSlice.tsx +0 -48
- package/src/Device/DeviceSelector/BasicDeviceInfo.tsx +0 -115
- package/src/Device/DeviceSelector/DeviceIcon.tsx +0 -27
- package/src/Device/DeviceSelector/DeviceList/AnimatedList.tsx +0 -62
- package/src/Device/DeviceSelector/DeviceList/BrokenDevice.tsx +0 -34
- package/src/Device/DeviceSelector/DeviceList/Device.tsx +0 -84
- package/src/Device/DeviceSelector/DeviceList/DeviceList.tsx +0 -113
- package/src/Device/DeviceSelector/DeviceList/EditDeviceButtons.tsx +0 -28
- package/src/Device/DeviceSelector/DeviceList/MoreDeviceInfo.tsx +0 -65
- package/src/Device/DeviceSelector/DeviceList/RenameDevice.tsx +0 -22
- package/src/Device/DeviceSelector/DeviceList/broken-device.scss +0 -32
- package/src/Device/DeviceSelector/DeviceList/device-list.scss +0 -64
- package/src/Device/DeviceSelector/DeviceList/device.scss +0 -28
- package/src/Device/DeviceSelector/DeviceList/edit-device-buttons.scss +0 -31
- package/src/Device/DeviceSelector/DeviceList/more-device-info.scss +0 -33
- package/src/Device/DeviceSelector/DeviceList/rename-device.scss +0 -13
- package/src/Device/DeviceSelector/DeviceSelector.test.tsx +0 -342
- package/src/Device/DeviceSelector/DeviceSelector.tsx +0 -173
- package/src/Device/DeviceSelector/Favorite.tsx +0 -49
- package/src/Device/DeviceSelector/SelectDevice.tsx +0 -36
- package/src/Device/DeviceSelector/SelectedDevice.tsx +0 -65
- package/src/Device/DeviceSelector/arrow-down.svg +0 -1
- package/src/Device/DeviceSelector/basic-device-info.scss +0 -62
- package/src/Device/DeviceSelector/device-icon.scss +0 -27
- package/src/Device/DeviceSelector/favorite.scss +0 -17
- package/src/Device/DeviceSelector/select-device.scss +0 -38
- package/src/Device/DeviceSelector/selected-device.scss +0 -57
- package/src/Device/DeviceSetup/DeviceSetupView.tsx +0 -102
- package/src/Device/deviceAutoSelectSlice.ts +0 -144
- package/src/Device/deviceInfo/deviceInfo.ts +0 -254
- package/src/Device/deviceInfo/nRF51-Series-logo.svg +0 -1
- package/src/Device/deviceInfo/nRF52-Series-logo.svg +0 -1
- package/src/Device/deviceInfo/nRF53-Series-logo.svg +0 -1
- package/src/Device/deviceInfo/nRF70-Series_no-background_RGB.svg +0 -36
- package/src/Device/deviceInfo/nRF91-Series-logo.svg +0 -1
- package/src/Device/deviceInfo/ppk-logo.svg +0 -1
- package/src/Device/deviceInfo/unknown-logo.svg +0 -30
- package/src/Device/deviceInfo/unknown-nordic-logo.svg +0 -25
- package/src/Device/deviceLibWrapper.test.ts +0 -97
- package/src/Device/deviceLibWrapper.ts +0 -133
- package/src/Device/deviceLister.test.ts +0 -89
- package/src/Device/deviceLister.ts +0 -396
- package/src/Device/deviceSetup.ts +0 -264
- package/src/Device/deviceSetupSlice.ts +0 -94
- package/src/Device/deviceSlice.ts +0 -248
- package/src/Device/dfu-cc.ts +0 -162
- package/src/Device/initPacket.ts +0 -402
- package/src/Device/jprogOperations.ts +0 -206
- package/src/Device/sdfuOperations.ts +0 -601
- package/src/Dialog/Dialog.test.tsx +0 -134
- package/src/Dialog/Dialog.tsx +0 -236
- package/src/Dialog/dialog.scss +0 -60
- package/src/Dropdown/Dropdown.module.scss +0 -83
- package/src/Dropdown/Dropdown.test.tsx +0 -62
- package/src/Dropdown/Dropdown.tsx +0 -106
- package/src/ErrorBoundary/ErrorBoundary.test.tsx +0 -105
- package/src/ErrorBoundary/ErrorBoundary.tsx +0 -186
- package/src/ErrorBoundary/bug.svg +0 -1
- package/src/ErrorBoundary/error-boundary.scss +0 -102
- package/src/ErrorDialog/ErrorDialog.test.tsx +0 -70
- package/src/ErrorDialog/ErrorDialog.tsx +0 -94
- package/src/ErrorDialog/error.scss +0 -14
- package/src/ErrorDialog/errorDialogSlice.test.ts +0 -64
- package/src/ErrorDialog/errorDialogSlice.ts +0 -80
- package/src/FactoryReset/FactoryResetButton.test.tsx +0 -46
- package/src/FactoryReset/FactoryResetButton.tsx +0 -81
- package/src/InlineInput/InlineInput.tsx +0 -170
- package/src/InlineInput/NumberInlineInput.tsx +0 -107
- package/src/InlineInput/inline-input.scss +0 -38
- package/src/InlineInput/number-inline-input.scss +0 -15
- package/src/Log/LogEntry.tsx +0 -62
- package/src/Log/LogViewer.tsx +0 -54
- package/src/Log/log-entry.scss +0 -30
- package/src/Log/log-viewer.scss +0 -24
- package/src/Log/logSlice.ts +0 -73
- package/src/Log/syncLogToStore.ts +0 -36
- package/src/Logo/Logo.tsx +0 -48
- package/src/Logo/logo.scss +0 -11
- package/src/Main/Main.tsx +0 -13
- package/src/Main/main.scss +0 -13
- package/src/MasonryLayout/MasonryLayout.tsx +0 -234
- package/src/MasonryLayout/masonryLayout.module.scss +0 -46
- package/src/NavBar/NavBar.tsx +0 -22
- package/src/NavBar/NavMenu.test.tsx +0 -41
- package/src/NavBar/NavMenu.tsx +0 -53
- package/src/NavBar/NavMenuItem.tsx +0 -41
- package/src/NavBar/nav-bar.scss +0 -26
- package/src/NavBar/nav-menu-item.scss +0 -34
- package/src/OpenApp/openApp.ts +0 -18
- package/src/Panes/FeedbackPane.tsx +0 -149
- package/src/PseudoButton/PseudoButton.tsx +0 -56
- package/src/PseudoButton/pseudo-button.scss +0 -11
- package/src/SerialPort/ConflictingSettingsDialog.tsx +0 -242
- package/src/SerialPort/SerialPort.test.ts +0 -54
- package/src/SerialPort/SerialPort.ts +0 -202
- package/src/Shortcuts/Shortcut-item.scss +0 -33
- package/src/Shortcuts/Shortcut-modal.scss +0 -20
- package/src/Shortcuts/ShortcutItem.tsx +0 -48
- package/src/Shortcuts/ShortcutModal.tsx +0 -67
- package/src/SidePanel/Group.tsx +0 -106
- package/src/SidePanel/SidePanel.tsx +0 -19
- package/src/SidePanel/group.scss +0 -42
- package/src/SidePanel/sidepanel.scss +0 -20
- package/src/Slider/Bar.tsx +0 -24
- package/src/Slider/Factor.test.ts +0 -48
- package/src/Slider/Handle.tsx +0 -121
- package/src/Slider/Slider.test.tsx +0 -25
- package/src/Slider/Slider.tsx +0 -91
- package/src/Slider/Ticks.tsx +0 -54
- package/src/Slider/__snapshots__/Slider.test.tsx.snap +0 -50
- package/src/Slider/bar.scss +0 -24
- package/src/Slider/factor.ts +0 -24
- package/src/Slider/handle.scss +0 -32
- package/src/Slider/percentage.ts +0 -67
- package/src/Slider/range.ts +0 -92
- package/src/Slider/slider.scss +0 -14
- package/src/Slider/ticks.scss +0 -28
- package/src/Slider/variables.scss +0 -16
- package/src/StartStopButton/StartStopButton.tsx +0 -60
- package/src/StartStopButton/play-circle.svg +0 -1
- package/src/StartStopButton/start-stop-button.scss +0 -47
- package/src/StartStopButton/stop-circle.svg +0 -1
- package/src/StateSelector/StateSelector.tsx +0 -64
- package/src/StateSelector/state-selector.scss +0 -38
- package/src/Stepper/Stepper.tsx +0 -139
- package/src/Stepper/stepper.scss +0 -127
- package/src/Toggle/Toggle.tsx +0 -120
- package/src/Toggle/toggle.scss +0 -109
- package/src/bootstrap.scss +0 -14
- package/src/index.ts +0 -146
- package/src/logging/appTransport.test.ts +0 -78
- package/src/logging/appTransport.ts +0 -41
- package/src/logging/describeError.test.ts +0 -38
- package/src/logging/describeError.ts +0 -56
- package/src/logging/index.ts +0 -103
- package/src/logging/logBuffer.test.ts +0 -41
- package/src/logging/logBuffer.ts +0 -24
- package/src/logging/sendInitialLogMessages.ts +0 -64
- package/src/store.ts +0 -60
- package/src/svg.d.ts +0 -15
- package/src/utils/AppTypes.ts +0 -60
- package/src/utils/appDirs.ts +0 -78
- package/src/utils/bleChannels.ts +0 -16
- package/src/utils/classNames.test.ts +0 -29
- package/src/utils/classNames.ts +0 -25
- package/src/utils/colors.scss +0 -46
- package/src/utils/colors.ts +0 -38
- package/src/utils/describeVersion.ts +0 -21
- package/src/utils/environment.ts +0 -6
- package/src/utils/logLibVersions.ts +0 -103
- package/src/utils/open.ts +0 -46
- package/src/utils/packageJson.ts +0 -60
- package/src/utils/persistentStore.ts +0 -144
- package/src/utils/systemReport.ts +0 -160
- package/src/utils/truncateMiddle.test.ts +0 -21
- package/src/utils/truncateMiddle.ts +0 -20
- package/src/utils/usageData.ts +0 -189
- package/src/utils/useHotKey.ts +0 -62
- package/src/utils/useStopwatch.test.tsx +0 -311
- package/src/utils/useStopwatch.ts +0 -140
- package/src/variables.scss +0 -820
- package/styles.scss +0 -88
- package/test/dispatchTo.ts +0 -23
- package/test/index.ts +0 -7
- package/test/jestResolver.js +0 -28
- package/test/setupTests.ts +0 -27
- package/test/testUtil.ts +0 -20
- package/test/testrenderer.tsx +0 -39
- /package/{src/Logo/nordic-logo-white-icon-only.png → dist/nordic-logo-white-icon-only-XIZBW537.png} +0 -0
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2015 Nordic Semiconductor ASA
|
|
3
|
-
*
|
|
4
|
-
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import React from 'react';
|
|
8
|
-
import { act, render } from '@testing-library/react';
|
|
9
|
-
|
|
10
|
-
import useStopWatch, { ITimer, Stopwatch } from './useStopwatch';
|
|
11
|
-
|
|
12
|
-
let appCallback = () => {};
|
|
13
|
-
|
|
14
|
-
const setup = (stopwatch: Stopwatch) => {
|
|
15
|
-
const returnVal = {};
|
|
16
|
-
const TestComponent = () => {
|
|
17
|
-
Object.assign(returnVal, useStopWatch(stopwatch));
|
|
18
|
-
return null;
|
|
19
|
-
};
|
|
20
|
-
render(<TestComponent />);
|
|
21
|
-
return returnVal as ReturnType<typeof useStopWatch>;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
describe('Stop Watch', () => {
|
|
25
|
-
const mockNow = jest.fn(() => 0);
|
|
26
|
-
const mockSetTimeout = jest.fn((callback: () => void) => {
|
|
27
|
-
appCallback = callback;
|
|
28
|
-
return () => {};
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const timerMock: ITimer = {
|
|
32
|
-
now: mockNow,
|
|
33
|
-
setTimeout: mockSetTimeout,
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const expectZeroElapsedTime = (
|
|
37
|
-
stopwatch: ReturnType<typeof useStopWatch>
|
|
38
|
-
) => {
|
|
39
|
-
expect(stopwatch.time).toBe(0);
|
|
40
|
-
expect(stopwatch.seconds).toBe(0);
|
|
41
|
-
expect(stopwatch.minutes).toBe(0);
|
|
42
|
-
expect(stopwatch.hours).toBe(0);
|
|
43
|
-
expect(stopwatch.days).toBe(0);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
beforeEach(() => {
|
|
47
|
-
mockNow.mockReturnValue(0);
|
|
48
|
-
jest.clearAllMocks();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('Auto Start will init timer', () => {
|
|
52
|
-
const { isRunning } = setup({
|
|
53
|
-
autoStart: true,
|
|
54
|
-
resolution: 1000,
|
|
55
|
-
timer: timerMock,
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
expect(timerMock.setTimeout).toBeCalledTimes(1);
|
|
59
|
-
expect(timerMock.setTimeout).toBeCalledWith(expect.anything(), 1000);
|
|
60
|
-
expect(isRunning).toBeTruthy();
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('Pause will stop the stopwatch and report elapsed time', () => {
|
|
64
|
-
const stopwatch = setup({
|
|
65
|
-
autoStart: true,
|
|
66
|
-
resolution: 1000,
|
|
67
|
-
timer: timerMock,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
expect(timerMock.setTimeout).toBeCalledTimes(1);
|
|
71
|
-
expect(timerMock.setTimeout).toBeCalledWith(expect.anything(), 1000);
|
|
72
|
-
expect(stopwatch.isRunning).toBeTruthy();
|
|
73
|
-
|
|
74
|
-
expectZeroElapsedTime(stopwatch);
|
|
75
|
-
|
|
76
|
-
mockNow.mockReturnValue(433);
|
|
77
|
-
|
|
78
|
-
act(() => {
|
|
79
|
-
stopwatch.pause();
|
|
80
|
-
stopwatch;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
expect(stopwatch.time).toBe(433);
|
|
84
|
-
expect(stopwatch.seconds).toBe(0);
|
|
85
|
-
expect(stopwatch.minutes).toBe(0);
|
|
86
|
-
expect(stopwatch.hours).toBe(0);
|
|
87
|
-
expect(stopwatch.days).toBe(0);
|
|
88
|
-
expect(stopwatch.isRunning).toBeFalsy();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test('Start a paused timer will continue timer', () => {
|
|
92
|
-
const stopwatch = setup({
|
|
93
|
-
autoStart: true,
|
|
94
|
-
resolution: 1000,
|
|
95
|
-
timer: timerMock,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
expect(timerMock.setTimeout).toBeCalledTimes(1);
|
|
99
|
-
expect(timerMock.setTimeout).toBeCalledWith(expect.anything(), 1000);
|
|
100
|
-
expect(stopwatch.isRunning).toBeTruthy();
|
|
101
|
-
|
|
102
|
-
expectZeroElapsedTime(stopwatch);
|
|
103
|
-
|
|
104
|
-
expect(timerMock.setTimeout).nthCalledWith(1, expect.anything(), 1000);
|
|
105
|
-
|
|
106
|
-
mockNow.mockReturnValue(500);
|
|
107
|
-
|
|
108
|
-
act(() => {
|
|
109
|
-
stopwatch.pause();
|
|
110
|
-
stopwatch;
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
expect(stopwatch.time).toBe(500);
|
|
114
|
-
expect(stopwatch.seconds).toBe(0);
|
|
115
|
-
expect(stopwatch.minutes).toBe(0);
|
|
116
|
-
expect(stopwatch.hours).toBe(0);
|
|
117
|
-
expect(stopwatch.days).toBe(0);
|
|
118
|
-
expect(stopwatch.isRunning).toBeFalsy();
|
|
119
|
-
|
|
120
|
-
mockNow.mockReturnValue(5000);
|
|
121
|
-
|
|
122
|
-
act(() => {
|
|
123
|
-
stopwatch.start();
|
|
124
|
-
stopwatch;
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
expect(timerMock.setTimeout).toBeCalledTimes(2);
|
|
128
|
-
expect(timerMock.setTimeout).nthCalledWith(2, expect.anything(), 500);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test('Timer will not automatically start', () => {
|
|
132
|
-
const { isRunning } = setup({
|
|
133
|
-
autoStart: false,
|
|
134
|
-
resolution: 1000,
|
|
135
|
-
timer: timerMock,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
expect(timerMock.setTimeout).toBeCalledTimes(0);
|
|
139
|
-
expect(isRunning).toBeFalsy();
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
test('Start call will start the timer', () => {
|
|
143
|
-
const stopwatch = setup({
|
|
144
|
-
autoStart: false,
|
|
145
|
-
resolution: 1000,
|
|
146
|
-
timer: timerMock,
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
expect(timerMock.setTimeout).toBeCalledTimes(0);
|
|
150
|
-
expect(stopwatch.isRunning).toBeFalsy();
|
|
151
|
-
|
|
152
|
-
mockNow.mockReturnValue(5000);
|
|
153
|
-
|
|
154
|
-
act(() => {
|
|
155
|
-
stopwatch.start();
|
|
156
|
-
stopwatch;
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
expect(timerMock.setTimeout).toBeCalledTimes(1);
|
|
160
|
-
expect(timerMock.setTimeout).toBeCalledWith(expect.anything(), 1000);
|
|
161
|
-
expect(stopwatch.isRunning).toBeTruthy();
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
test('Reset will reset all time the stopwatch and report elapsed time', () => {
|
|
165
|
-
const stopwatch = setup({
|
|
166
|
-
autoStart: true,
|
|
167
|
-
resolution: 1000,
|
|
168
|
-
timer: timerMock,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
expect(timerMock.setTimeout).toBeCalledTimes(1);
|
|
172
|
-
expect(timerMock.setTimeout).nthCalledWith(1, expect.anything(), 1000);
|
|
173
|
-
expect(stopwatch.isRunning).toBeTruthy();
|
|
174
|
-
|
|
175
|
-
expectZeroElapsedTime(stopwatch);
|
|
176
|
-
|
|
177
|
-
mockNow.mockReturnValue(1200);
|
|
178
|
-
|
|
179
|
-
act(() => {
|
|
180
|
-
appCallback();
|
|
181
|
-
stopwatch;
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
expect(timerMock.setTimeout).nthCalledWith(2, expect.anything(), 800);
|
|
185
|
-
|
|
186
|
-
expect(stopwatch.time).toBe(1200);
|
|
187
|
-
expect(stopwatch.seconds).toBe(1);
|
|
188
|
-
expect(stopwatch.minutes).toBe(0);
|
|
189
|
-
expect(stopwatch.hours).toBe(0);
|
|
190
|
-
expect(stopwatch.days).toBe(0);
|
|
191
|
-
expect(stopwatch.isRunning).toBeTruthy();
|
|
192
|
-
|
|
193
|
-
mockNow.mockReturnValue(6800);
|
|
194
|
-
act(() => {
|
|
195
|
-
stopwatch.reset();
|
|
196
|
-
stopwatch;
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
expect(timerMock.setTimeout).nthCalledWith(3, expect.anything(), 1000);
|
|
200
|
-
|
|
201
|
-
expectZeroElapsedTime(stopwatch);
|
|
202
|
-
expect(stopwatch.isRunning).toBeTruthy();
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
test('Time will update output after precise interval elapsed trigger a new timeout', () => {
|
|
206
|
-
const stopwatch = setup({
|
|
207
|
-
autoStart: true,
|
|
208
|
-
resolution: 1000,
|
|
209
|
-
timer: timerMock,
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
expectZeroElapsedTime(stopwatch);
|
|
213
|
-
|
|
214
|
-
// trigger timeout with precise time elapsed
|
|
215
|
-
mockNow.mockReturnValue(1000);
|
|
216
|
-
|
|
217
|
-
act(() => {
|
|
218
|
-
appCallback();
|
|
219
|
-
stopwatch;
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
expect(timerMock.setTimeout).toBeCalledTimes(2);
|
|
223
|
-
expect(timerMock.setTimeout).toBeCalledWith(expect.anything(), 1000);
|
|
224
|
-
|
|
225
|
-
expect(stopwatch.time).toBe(1000);
|
|
226
|
-
expect(stopwatch.seconds).toBe(1);
|
|
227
|
-
expect(stopwatch.minutes).toBe(0);
|
|
228
|
-
expect(stopwatch.hours).toBe(0);
|
|
229
|
-
expect(stopwatch.days).toBe(0);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
test('Time will update trigger a new timeout and add positive correction', () => {
|
|
233
|
-
const stopwatch = setup({
|
|
234
|
-
autoStart: true,
|
|
235
|
-
resolution: 1000,
|
|
236
|
-
timer: timerMock,
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
expectZeroElapsedTime(stopwatch);
|
|
240
|
-
|
|
241
|
-
// trigger timeout with error of -200ms
|
|
242
|
-
mockNow.mockReturnValue(800);
|
|
243
|
-
|
|
244
|
-
act(() => {
|
|
245
|
-
appCallback();
|
|
246
|
-
stopwatch;
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
expect(timerMock.setTimeout).toBeCalledTimes(2);
|
|
250
|
-
expect(timerMock.setTimeout).nthCalledWith(1, expect.anything(), 1000);
|
|
251
|
-
expect(timerMock.setTimeout).nthCalledWith(2, expect.anything(), 200);
|
|
252
|
-
|
|
253
|
-
expect(stopwatch.time).toBe(800);
|
|
254
|
-
expect(stopwatch.seconds).toBe(0);
|
|
255
|
-
expect(stopwatch.minutes).toBe(0);
|
|
256
|
-
expect(stopwatch.hours).toBe(0);
|
|
257
|
-
expect(stopwatch.days).toBe(0);
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
test('Time will update trigger a new timeout and add negative correction', () => {
|
|
261
|
-
const stopwatch = setup({
|
|
262
|
-
autoStart: true,
|
|
263
|
-
resolution: 1000,
|
|
264
|
-
timer: timerMock,
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
expectZeroElapsedTime(stopwatch);
|
|
268
|
-
|
|
269
|
-
// trigger timeout with error of +200ms
|
|
270
|
-
mockNow.mockReturnValue(1200);
|
|
271
|
-
|
|
272
|
-
act(() => {
|
|
273
|
-
appCallback();
|
|
274
|
-
stopwatch;
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
expect(timerMock.setTimeout).toBeCalledTimes(2);
|
|
278
|
-
expect(timerMock.setTimeout).nthCalledWith(1, expect.anything(), 1000);
|
|
279
|
-
expect(timerMock.setTimeout).nthCalledWith(2, expect.anything(), 800);
|
|
280
|
-
|
|
281
|
-
expect(stopwatch.time).toBe(1200);
|
|
282
|
-
expect(stopwatch.seconds).toBe(1);
|
|
283
|
-
expect(stopwatch.minutes).toBe(0);
|
|
284
|
-
expect(stopwatch.hours).toBe(0);
|
|
285
|
-
expect(stopwatch.days).toBe(0);
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
test('Test time part splitting', () => {
|
|
289
|
-
const stopwatch = setup({
|
|
290
|
-
autoStart: true,
|
|
291
|
-
resolution: 1000,
|
|
292
|
-
timer: timerMock,
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
expectZeroElapsedTime(stopwatch);
|
|
296
|
-
|
|
297
|
-
// 2days 1hr 1 min 3 seconds 500ms
|
|
298
|
-
mockNow.mockReturnValue(176588500);
|
|
299
|
-
|
|
300
|
-
act(() => {
|
|
301
|
-
appCallback();
|
|
302
|
-
stopwatch;
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
expect(stopwatch.time).toBe(176588500);
|
|
306
|
-
expect(stopwatch.seconds).toBe(8);
|
|
307
|
-
expect(stopwatch.minutes).toBe(3);
|
|
308
|
-
expect(stopwatch.hours).toBe(1);
|
|
309
|
-
expect(stopwatch.days).toBe(2);
|
|
310
|
-
});
|
|
311
|
-
});
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2023 Nordic Semiconductor ASA
|
|
3
|
-
*
|
|
4
|
-
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
8
|
-
|
|
9
|
-
export interface ITimer {
|
|
10
|
-
now: () => number;
|
|
11
|
-
setTimeout: (callback: () => void, ms: number) => () => void;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type Stopwatch = {
|
|
15
|
-
autoStart: boolean;
|
|
16
|
-
resolution?: number;
|
|
17
|
-
timer?: ITimer;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const defaultTimer: ITimer = {
|
|
21
|
-
now: () => Date.now(),
|
|
22
|
-
setTimeout: (callback: () => void, ms: number) => {
|
|
23
|
-
const t = setTimeout(callback, ms);
|
|
24
|
-
return () => clearTimeout(t);
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const splitMS = (ms: number) => {
|
|
29
|
-
const time = ms;
|
|
30
|
-
const days = Math.floor(ms / (24 * 60 * 60 * 1000));
|
|
31
|
-
ms -= days * 24 * 60 * 60 * 1000;
|
|
32
|
-
|
|
33
|
-
const hours = Math.floor(ms / (60 * 60 * 1000));
|
|
34
|
-
ms -= hours * 60 * 60 * 1000;
|
|
35
|
-
|
|
36
|
-
const minutes = Math.floor(ms / (60 * 1000));
|
|
37
|
-
ms -= minutes * 60 * 1000;
|
|
38
|
-
|
|
39
|
-
const seconds = Math.floor(ms / 1000);
|
|
40
|
-
const millisecond = ms - seconds * 60 * 1000;
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
time,
|
|
44
|
-
days,
|
|
45
|
-
hours,
|
|
46
|
-
minutes,
|
|
47
|
-
seconds,
|
|
48
|
-
millisecond,
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export default ({
|
|
53
|
-
autoStart = false,
|
|
54
|
-
timer = defaultTimer,
|
|
55
|
-
resolution = 1000,
|
|
56
|
-
}: Stopwatch) => {
|
|
57
|
-
const previousTickTime = useRef(timer.now());
|
|
58
|
-
const expectedTickTime = useRef(-1);
|
|
59
|
-
const autoStarted = useRef(false);
|
|
60
|
-
const pauseTimeout = useRef<(() => void) | null>(null);
|
|
61
|
-
const [isRunning, setIsRunning] = useState(false);
|
|
62
|
-
const [time, setTime] = useState(0);
|
|
63
|
-
|
|
64
|
-
const nextTick = useCallback(
|
|
65
|
-
(elapsedTime: number) => {
|
|
66
|
-
expectedTickTime.current =
|
|
67
|
-
previousTickTime.current +
|
|
68
|
-
(resolution - (elapsedTime % resolution));
|
|
69
|
-
|
|
70
|
-
const nextInterval = expectedTickTime.current - timer.now();
|
|
71
|
-
|
|
72
|
-
const handler = (delta: number, shouldContinue = true) => {
|
|
73
|
-
setTime(elapsedTime + delta);
|
|
74
|
-
previousTickTime.current = timer.now();
|
|
75
|
-
if (shouldContinue)
|
|
76
|
-
pauseTimeout.current = nextTick(elapsedTime + delta);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const release = timer.setTimeout(() => {
|
|
80
|
-
handler(timer.now() - previousTickTime.current);
|
|
81
|
-
}, nextInterval);
|
|
82
|
-
|
|
83
|
-
return () => {
|
|
84
|
-
handler(timer.now() - previousTickTime.current, false);
|
|
85
|
-
release();
|
|
86
|
-
};
|
|
87
|
-
},
|
|
88
|
-
[resolution, timer]
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
const start = useCallback(
|
|
92
|
-
(elapsedTime = time) => {
|
|
93
|
-
if (pauseTimeout.current === null) {
|
|
94
|
-
previousTickTime.current = timer.now();
|
|
95
|
-
expectedTickTime.current =
|
|
96
|
-
timer.now() + (resolution - (elapsedTime % resolution));
|
|
97
|
-
setIsRunning(true);
|
|
98
|
-
pauseTimeout.current = nextTick(elapsedTime);
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
[nextTick, resolution, time, timer]
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
const pause = useCallback(() => {
|
|
105
|
-
setIsRunning(false);
|
|
106
|
-
if (pauseTimeout.current) {
|
|
107
|
-
pauseTimeout.current();
|
|
108
|
-
pauseTimeout.current = null;
|
|
109
|
-
}
|
|
110
|
-
}, []);
|
|
111
|
-
|
|
112
|
-
const reset = useCallback(() => {
|
|
113
|
-
pause();
|
|
114
|
-
setTime(0);
|
|
115
|
-
start(0);
|
|
116
|
-
}, [pause, start]);
|
|
117
|
-
|
|
118
|
-
useEffect(
|
|
119
|
-
() => () => {
|
|
120
|
-
if (pauseTimeout.current) {
|
|
121
|
-
pauseTimeout.current();
|
|
122
|
-
pauseTimeout.current = null;
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
[]
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
if (autoStart && !autoStarted.current) {
|
|
129
|
-
autoStarted.current = true;
|
|
130
|
-
start(0);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return {
|
|
134
|
-
...splitMS(time),
|
|
135
|
-
start,
|
|
136
|
-
pause,
|
|
137
|
-
reset,
|
|
138
|
-
isRunning,
|
|
139
|
-
};
|
|
140
|
-
};
|