Skip to content

Commit 50217d4

Browse files
committed
fix: use discriminated union for duration
1 parent 0d0d074 commit 50217d4

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,9 @@ export function SomeScreen() {
179179

180180
## Types (inputs)
181181

182-
| Type | Values |
183-
| --------------------- | ----------------------------- |
184-
| `HapticEventType` | `'transient' \| 'continuous'` |
185-
| `HapticParameterType` | `'intensity' \| 'sharpness'` |
186-
| `HapticCurveType` | `'intensity' \| 'sharpness'` |
182+
| Type | Values |
183+
| --------------------- | ---------------------------- |
184+
| `HapticParameterType` | `'intensity' \| 'sharpness'` |
187185

188186
| `HapticEventParameter` | Type |
189187
| ---------------------- | --------------------- |

src/Ahap.nitro.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { HybridObject } from 'react-native-nitro-modules';
22

33
export type HapticEventType = 'transient' | 'continuous';
44
export type HapticParameterType = 'intensity' | 'sharpness';
5-
export type HapticCurveType = 'intensity' | 'sharpness';
5+
66
export type HapticEventParameter = {
77
type: HapticParameterType;
88
value: number;
@@ -12,21 +12,15 @@ export type HapticCurveControlPoint = {
1212
value: number;
1313
};
1414

15-
export type HapticEvent =
16-
| {
17-
type: 'transient';
18-
parameters: HapticEventParameter[];
19-
relativeTime: number;
20-
}
21-
| {
22-
type: 'continuous';
23-
parameters: HapticEventParameter[];
24-
relativeTime: number;
25-
duration: number;
26-
};
15+
export type HapticEvent = {
16+
type: HapticEventType;
17+
parameters: HapticEventParameter[];
18+
relativeTime: number;
19+
duration?: number; // Optional for transient events
20+
};
2721

2822
export type HapticCurve = {
29-
type: HapticCurveType;
23+
type: HapticParameterType;
3024
controlPoints: HapticCurveControlPoint[];
3125
relativeTime: number;
3226
};

src/index.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
import { useEffect } from 'react';
22
import { AppState } from 'react-native';
33
import { NitroModules } from 'react-native-nitro-modules';
4-
import type { Ahap, HapticCurve, HapticEvent } from './Ahap.nitro';
4+
import type {
5+
Ahap,
6+
HapticCurve,
7+
HapticEventParameter,
8+
HapticParameterType,
9+
} from './Ahap.nitro';
10+
11+
export type TransientHapticEvent = {
12+
type: 'transient';
13+
parameters: HapticEventParameter[];
14+
relativeTime: number;
15+
};
16+
17+
export type ContinuousHapticEvent = {
18+
type: 'continuous';
19+
parameters: HapticEventParameter[];
20+
relativeTime: number;
21+
duration: number;
22+
};
23+
24+
export type HapticEvent = TransientHapticEvent | ContinuousHapticEvent;
525

626
const AhapHybridObject = NitroModules.createHybridObject<Ahap>('Ahap');
727

@@ -93,4 +113,4 @@ export function HapticProvider({ children }: { children: React.ReactNode }) {
93113
}
94114

95115
export { AhapHybridObject };
96-
export type { HapticCurve, HapticEvent };
116+
export type { HapticCurve, HapticEventParameter, HapticParameterType };

0 commit comments

Comments
 (0)