@flare-engine/haptics 0.4.17 → 0.4.19

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.
@@ -0,0 +1,63 @@
1
+ // src/provider.ts
2
+ /**
3
+ * Wrap cue implementations with a real enabled gate. When disabled, cue methods
4
+ * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the
5
+ * live state (held in this closure, so each call is an independent instance) so a
6
+ * consumer's haptics toggle is observable — and unit-testable. Not part of the
7
+ * public barrel; the shipped providers (expo / web / noop factory) build on it.
8
+ */
9
+ function createGatedProvider(cues, initialEnabled) {
10
+ let enabled = initialEnabled;
11
+ return {
12
+ async impact(style) {
13
+ if (enabled) await cues.impact(style);
14
+ },
15
+ async notification(type) {
16
+ if (enabled) await cues.notification(type);
17
+ },
18
+ async selection() {
19
+ if (enabled) await cues.selection();
20
+ },
21
+ isEnabled() {
22
+ return enabled;
23
+ },
24
+ setEnabled(next) {
25
+ enabled = next;
26
+ }
27
+ };
28
+ }
29
+ /** Stateless no-op cues, shared by both no-op providers below. */
30
+ const NOOP_CUES = {
31
+ async impact() {},
32
+ async notification() {},
33
+ async selection() {}
34
+ };
35
+ /**
36
+ * Immutable no-op provider. Reports disabled and fires nothing — the safe default
37
+ * where there is no haptics backend (web without vibration, CI, tests) and the
38
+ * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
39
+ * hard "off" default, so wiring it into a context can never accidentally report
40
+ * "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
41
+ * consumer's haptics setting), use {@link createNoopHapticsProvider}.
42
+ */
43
+ const NoopHapticsProvider = {
44
+ ...NOOP_CUES,
45
+ isEnabled() {
46
+ return false;
47
+ },
48
+ setEnabled() {}
49
+ };
50
+ /**
51
+ * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
52
+ * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
53
+ * without a native backend. Each call is an independent instance; defaults to
54
+ * disabled (matching {@link NoopHapticsProvider}).
55
+ */
56
+ function createNoopHapticsProvider(initialEnabled = false) {
57
+ return createGatedProvider(NOOP_CUES, initialEnabled);
58
+ }
59
+ exports.NoopHapticsProvider = NoopHapticsProvider;
60
+ exports.createGatedProvider = createGatedProvider;
61
+ exports.createNoopHapticsProvider = createNoopHapticsProvider;
62
+
63
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","names":[],"sources":["../src/provider.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n"],"mappings":";;;;;;;;AAgDA,SAAgB,oBAAoB,MAAkB,gBAA0C;CAC9F,IAAI,UAAU;CACd,OAAO;EACL,MAAM,OAAO,OAA0C;GACrD,IAAI,SAAS,MAAM,KAAK,OAAO,KAAK;EACtC;EACA,MAAM,aAAa,MAA8C;GAC/D,IAAI,SAAS,MAAM,KAAK,aAAa,IAAI;EAC3C;EACA,MAAM,YAA2B;GAC/B,IAAI,SAAS,MAAM,KAAK,UAAU;EACpC;EACA,YAAqB;GACnB,OAAO;EACT;EACA,WAAW,MAAqB;GAC9B,UAAU;EACZ;CACF;AACF;;AAGA,MAAM,YAAwB;CAC5B,MAAM,SAAwB,CAE9B;CACA,MAAM,eAA8B,CAEpC;CACA,MAAM,YAA2B,CAEjC;AACF;;;;;;;;;AAUA,MAAa,sBAAuC;CAClD,GAAG;CACH,YAAqB;EACnB,OAAO;CACT;CACA,aAAmB,CAEnB;AACF;;;;;;;AAQA,SAAgB,0BAA0B,iBAAiB,OAAwB;CACjF,OAAO,oBAAoB,WAAW,cAAc;AACtD"}
@@ -0,0 +1,61 @@
1
+ // src/provider.ts
2
+ /**
3
+ * Wrap cue implementations with a real enabled gate. When disabled, cue methods
4
+ * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the
5
+ * live state (held in this closure, so each call is an independent instance) so a
6
+ * consumer's haptics toggle is observable — and unit-testable. Not part of the
7
+ * public barrel; the shipped providers (expo / web / noop factory) build on it.
8
+ */
9
+ function createGatedProvider(cues, initialEnabled) {
10
+ let enabled = initialEnabled;
11
+ return {
12
+ async impact(style) {
13
+ if (enabled) await cues.impact(style);
14
+ },
15
+ async notification(type) {
16
+ if (enabled) await cues.notification(type);
17
+ },
18
+ async selection() {
19
+ if (enabled) await cues.selection();
20
+ },
21
+ isEnabled() {
22
+ return enabled;
23
+ },
24
+ setEnabled(next) {
25
+ enabled = next;
26
+ }
27
+ };
28
+ }
29
+ /** Stateless no-op cues, shared by both no-op providers below. */
30
+ const NOOP_CUES = {
31
+ async impact() {},
32
+ async notification() {},
33
+ async selection() {}
34
+ };
35
+ /**
36
+ * Immutable no-op provider. Reports disabled and fires nothing — the safe default
37
+ * where there is no haptics backend (web without vibration, CI, tests) and the
38
+ * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
39
+ * hard "off" default, so wiring it into a context can never accidentally report
40
+ * "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
41
+ * consumer's haptics setting), use {@link createNoopHapticsProvider}.
42
+ */
43
+ const NoopHapticsProvider = {
44
+ ...NOOP_CUES,
45
+ isEnabled() {
46
+ return false;
47
+ },
48
+ setEnabled() {}
49
+ };
50
+ /**
51
+ * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
52
+ * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
53
+ * without a native backend. Each call is an independent instance; defaults to
54
+ * disabled (matching {@link NoopHapticsProvider}).
55
+ */
56
+ function createNoopHapticsProvider(initialEnabled = false) {
57
+ return createGatedProvider(NOOP_CUES, initialEnabled);
58
+ }
59
+ export { NoopHapticsProvider, createGatedProvider, createNoopHapticsProvider };
60
+
61
+ //# sourceMappingURL=provider.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.mjs","names":[],"sources":["../src/provider.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n"],"mappings":";;;;;;;;AAgDA,SAAgB,oBAAoB,MAAkB,gBAA0C;CAC9F,IAAI,UAAU;CACd,OAAO;EACL,MAAM,OAAO,OAA0C;GACrD,IAAI,SAAS,MAAM,KAAK,OAAO,KAAK;EACtC;EACA,MAAM,aAAa,MAA8C;GAC/D,IAAI,SAAS,MAAM,KAAK,aAAa,IAAI;EAC3C;EACA,MAAM,YAA2B;GAC/B,IAAI,SAAS,MAAM,KAAK,UAAU;EACpC;EACA,YAAqB;GACnB,OAAO;EACT;EACA,WAAW,MAAqB;GAC9B,UAAU;EACZ;CACF;AACF;;AAGA,MAAM,YAAwB;CAC5B,MAAM,SAAwB,CAE9B;CACA,MAAM,eAA8B,CAEpC;CACA,MAAM,YAA2B,CAEjC;AACF;;;;;;;;;AAUA,MAAa,sBAAuC;CAClD,GAAG;CACH,YAAqB;EACnB,OAAO;CACT;CACA,aAAmB,CAEnB;AACF;;;;;;;AAQA,SAAgB,0BAA0B,iBAAiB,OAAwB;CACjF,OAAO,oBAAoB,WAAW,cAAc;AACtD"}
package/dist/web.d.mts ADDED
@@ -0,0 +1,19 @@
1
+ import { HapticsProvider } from "./provider.mjs";
2
+ /** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */
3
+ export type WebVibrate = (pattern: number | number[]) => boolean;
4
+ /**
5
+ * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
6
+ * `navigator.vibrate` bound to `navigator` (e.g.
7
+ * `navigator.vibrate?.bind(navigator)`).
8
+ *
9
+ * When `vibrate` is absent — the API is unsupported on iOS Safari and most
10
+ * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
11
+ * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
12
+ * and the provider never claims to be enabled when there is no backend). When
13
+ * present, starts enabled; gate via `setEnabled`.
14
+ *
15
+ * Cue durations: impact scales with intensity, notification uses a distinct pulse
16
+ * pattern per category, selection is a single short tick.
17
+ */
18
+ export declare function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider;
19
+ //# sourceMappingURL=web.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.d.mts","names":[],"sources":["../src/web.ts"],"mappings":";;YASY,cAAc;;;;;;;;;;;;;;;wBAsCV,2BAA2B,UAAU,aAAa"}
package/dist/web.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { HapticsProvider } from "./provider.js";
2
+ /** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */
3
+ export type WebVibrate = (pattern: number | number[]) => boolean;
4
+ /**
5
+ * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
6
+ * `navigator.vibrate` bound to `navigator` (e.g.
7
+ * `navigator.vibrate?.bind(navigator)`).
8
+ *
9
+ * When `vibrate` is absent — the API is unsupported on iOS Safari and most
10
+ * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
11
+ * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
12
+ * and the provider never claims to be enabled when there is no backend). When
13
+ * present, starts enabled; gate via `setEnabled`.
14
+ *
15
+ * Cue durations: impact scales with intensity, notification uses a distinct pulse
16
+ * pattern per category, selection is a single short tick.
17
+ */
18
+ export declare function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider;
19
+ //# sourceMappingURL=web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.d.ts","names":[],"sources":["../src/web.ts"],"mappings":";;YASY,cAAc;;;;;;;;;;;;;;;wBAsCV,2BAA2B,UAAU,aAAa"}
package/dist/web.js ADDED
@@ -0,0 +1,66 @@
1
+ const require_provider = require("./provider.js");
2
+ // src/web.ts
3
+ /** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
4
+ const IMPACT_MS = {
5
+ light: 10,
6
+ medium: 20,
7
+ heavy: 40,
8
+ soft: 15,
9
+ rigid: 30
10
+ };
11
+ /** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */
12
+ const NOTIFICATION_PATTERN = {
13
+ success: [
14
+ 10,
15
+ 40,
16
+ 20
17
+ ],
18
+ warning: [
19
+ 20,
20
+ 60,
21
+ 20
22
+ ],
23
+ error: [
24
+ 30,
25
+ 40,
26
+ 30,
27
+ 40,
28
+ 30
29
+ ]
30
+ };
31
+ /** Selection tick duration (ms) — a single light buzz. */
32
+ const SELECTION_MS = 5;
33
+ const DEFAULT_IMPACT = "medium";
34
+ const DEFAULT_NOTIFICATION = "success";
35
+ /**
36
+ * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
37
+ * `navigator.vibrate` bound to `navigator` (e.g.
38
+ * `navigator.vibrate?.bind(navigator)`).
39
+ *
40
+ * When `vibrate` is absent — the API is unsupported on iOS Safari and most
41
+ * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
42
+ * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
43
+ * and the provider never claims to be enabled when there is no backend). When
44
+ * present, starts enabled; gate via `setEnabled`.
45
+ *
46
+ * Cue durations: impact scales with intensity, notification uses a distinct pulse
47
+ * pattern per category, selection is a single short tick.
48
+ */
49
+ function createWebVibrationProvider(vibrate) {
50
+ if (typeof vibrate !== "function") return require_provider.NoopHapticsProvider;
51
+ const buzz = vibrate;
52
+ return require_provider.createGatedProvider({
53
+ async impact(style) {
54
+ buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);
55
+ },
56
+ async notification(type) {
57
+ buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);
58
+ },
59
+ async selection() {
60
+ buzz(SELECTION_MS);
61
+ }
62
+ }, true);
63
+ }
64
+ exports.createWebVibrationProvider = createWebVibrationProvider;
65
+
66
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","names":["NoopHapticsProvider","createGatedProvider"],"sources":["../src/web.ts"],"sourcesContent":["import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":";;;AAYA,MAAM,YAA+C;CACnD,OAAO;CACP,QAAQ;CACR,OAAO;CACP,MAAM;CACN,OAAO;AACT;;AAGA,MAAM,uBAAiE;CACrE,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,OAAO;EAAC;EAAI;EAAI;EAAI;EAAI;CAAE;AAC5B;;AAGA,MAAM,eAAe;AAErB,MAAM,iBAAoC;AAC1C,MAAM,uBAA+C;;;;;;;;;;;;;;;AAgBrD,SAAgB,2BAA2B,SAAuC;CAChF,IAAI,OAAO,YAAY,YACrB,OAAOA,iBAAAA;CAET,MAAM,OAAO;CACb,OAAOC,iBAAAA,oBACL;EACE,MAAM,OAAO,OAAsB;GACjC,KAAK,UAAU,SAAS,eAAe;EACzC;EACA,MAAM,aAAa,MAAqB;GACtC,KAAK,qBAAqB,QAAQ,qBAAqB;EACzD;EACA,MAAM,YAA2B;GAC/B,KAAK,YAAY;EACnB;CACF,GACA,IACF;AACF"}
package/dist/web.mjs ADDED
@@ -0,0 +1,66 @@
1
+ import { NoopHapticsProvider, createGatedProvider } from "./provider.mjs";
2
+ // src/web.ts
3
+ /** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
4
+ const IMPACT_MS = {
5
+ light: 10,
6
+ medium: 20,
7
+ heavy: 40,
8
+ soft: 15,
9
+ rigid: 30
10
+ };
11
+ /** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */
12
+ const NOTIFICATION_PATTERN = {
13
+ success: [
14
+ 10,
15
+ 40,
16
+ 20
17
+ ],
18
+ warning: [
19
+ 20,
20
+ 60,
21
+ 20
22
+ ],
23
+ error: [
24
+ 30,
25
+ 40,
26
+ 30,
27
+ 40,
28
+ 30
29
+ ]
30
+ };
31
+ /** Selection tick duration (ms) — a single light buzz. */
32
+ const SELECTION_MS = 5;
33
+ const DEFAULT_IMPACT = "medium";
34
+ const DEFAULT_NOTIFICATION = "success";
35
+ /**
36
+ * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
37
+ * `navigator.vibrate` bound to `navigator` (e.g.
38
+ * `navigator.vibrate?.bind(navigator)`).
39
+ *
40
+ * When `vibrate` is absent — the API is unsupported on iOS Safari and most
41
+ * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
42
+ * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
43
+ * and the provider never claims to be enabled when there is no backend). When
44
+ * present, starts enabled; gate via `setEnabled`.
45
+ *
46
+ * Cue durations: impact scales with intensity, notification uses a distinct pulse
47
+ * pattern per category, selection is a single short tick.
48
+ */
49
+ function createWebVibrationProvider(vibrate) {
50
+ if (typeof vibrate !== "function") return NoopHapticsProvider;
51
+ const buzz = vibrate;
52
+ return createGatedProvider({
53
+ async impact(style) {
54
+ buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);
55
+ },
56
+ async notification(type) {
57
+ buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);
58
+ },
59
+ async selection() {
60
+ buzz(SELECTION_MS);
61
+ }
62
+ }, true);
63
+ }
64
+ export { createWebVibrationProvider };
65
+
66
+ //# sourceMappingURL=web.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.mjs","names":[],"sources":["../src/web.ts"],"sourcesContent":["import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":";;;AAYA,MAAM,YAA+C;CACnD,OAAO;CACP,QAAQ;CACR,OAAO;CACP,MAAM;CACN,OAAO;AACT;;AAGA,MAAM,uBAAiE;CACrE,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,OAAO;EAAC;EAAI;EAAI;EAAI;EAAI;CAAE;AAC5B;;AAGA,MAAM,eAAe;AAErB,MAAM,iBAAoC;AAC1C,MAAM,uBAA+C;;;;;;;;;;;;;;;AAgBrD,SAAgB,2BAA2B,SAAuC;CAChF,IAAI,OAAO,YAAY,YACrB,OAAO;CAET,MAAM,OAAO;CACb,OAAO,oBACL;EACE,MAAM,OAAO,OAAsB;GACjC,KAAK,UAAU,SAAS,eAAe;EACzC;EACA,MAAM,aAAa,MAAqB;GACtC,KAAK,qBAAqB,QAAQ,qBAAqB;EACzD;EACA,MAAM,YAA2B;GAC/B,KAAK,YAAY;EACnB;CACF,GACA,IACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flare-engine/haptics",
3
- "version": "0.4.17",
3
+ "version": "0.4.19",
4
4
  "description": "Haptics (tactile feedback) provider interface (consumer-supplied adapter)",
5
5
  "keywords": ["flare-engine", "haptics", "provider", "interface", "react-native", "typescript"],
6
6
  "license": "Apache-2.0",
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"mappings":";KACY;;KAGA;;;;;;;;;UAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB;;;;;qBCnGtD,gCAAc,QAAA;;wBAGX,cAAc;;;;;;;;;;;;;UCSb;EACf,YAAY,iBAAiB;EAC7B,kBAAkB,gBAAgB;EAClC,kBAAkB;;;;;;;;EAQlB,4BAA4B,eAAe;;;UAI5B;;;;;;;;EAQf;;;;;;;;;;;;;;;;;;;;;;;;wBAyCc,0BACd,MAAM,mBACN,UAAU,qBACT;;KC7ES,cAAc;;;;;;;;;;;;;;;wBAsCV,2BAA2B,UAAU,aAAa"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"mappings":";KACY;;KAGA;;;;;;;;;UAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB;;;;;qBCnGtD,gCAAc,QAAA;;wBAGX,cAAc;;;;;;;;;;;;;UCSb;EACf,YAAY,iBAAiB;EAC7B,kBAAkB,gBAAgB;EAClC,kBAAkB;;;;;;;;EAQlB,4BAA4B,eAAe;;;UAI5B;;;;;;;;EAQf;;;;;;;;;;;;;;;;;;;;;;;;wBAyCc,0BACd,MAAM,mBACN,UAAU,qBACT;;KC7ES,cAAc;;;;;;;;;;;;;;;wBAsCV,2BAA2B,UAAU,aAAa"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","names":["createContext","useContext"],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n","import { createContext, useContext } from 'react';\nimport { type HapticsProvider, NoopHapticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `HapticsProvider`. Defaults to\n * `NoopHapticsProvider`.\n */\nexport const HapticsContext = createContext<HapticsProvider>(NoopHapticsProvider);\n\n/** Hook returning the current `HapticsProvider`. */\nexport function useHaptics(): HapticsProvider {\n return useContext(HapticsContext);\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n} from './provider.js';\n\n/**\n * Structural shape of the `expo-haptics` module surface this adapter needs — the\n * `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared\n * here so the package never depends on `expo-haptics`; the app injects the real\n * module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).\n *\n * Style / type params are `string` because expo types them as its own string\n * enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal\n * this package's `HapticImpactStyle` / `HapticNotificationType` literals\n * (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through\n * unchanged and the real `Haptics` module stays structurally assignable.\n */\nexport interface ExpoHapticsModule {\n impactAsync(style?: string): Promise<void>;\n notificationAsync(type?: string): Promise<void>;\n selectionAsync(): Promise<void>;\n /**\n * Android system haptic feedback. Optional, so a module without it still fits.\n * `type` is `string` for the same reason as above: expo types it as its\n * `AndroidHaptics` string enum, and method syntax keeps the real module\n * structurally assignable. The adapter passes that enum's values\n * (`'clock-tick'`, `'confirm'`, …).\n */\n performAndroidHapticsAsync?(type: string): Promise<void>;\n}\n\n/** Options for {@link createExpoHapticsProvider}. */\nexport interface ExpoHapticsOptions {\n /**\n * The running platform — pass `Platform.OS` from `react-native`. With\n * `'android'`, cues use the system haptic effects when the injected module has\n * `performAndroidHapticsAsync`. The hint is needed because expo's function\n * resolves without doing anything on other platforms, so its presence alone\n * cannot pick the route.\n */\n platform?: string;\n}\n\nconst ANDROID_SELECTION = 'clock-tick';\n\nconst ANDROID_IMPACT: Readonly<Record<HapticImpactStyle, string>> = {\n light: 'keyboard-tap',\n soft: 'keyboard-tap',\n medium: 'virtual-key',\n heavy: 'long-press',\n rigid: 'long-press',\n};\n\nconst ANDROID_NOTIFICATION: Readonly<Record<HapticNotificationType, string>> = {\n success: 'confirm',\n warning: 'long-press',\n error: 'reject',\n};\n\n/**\n * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.\n * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,\n * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an\n * omitted style / type forwards `undefined`, letting expo apply its own default.\n * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the\n * `@flare-engine/a11y` reduced-motion signal, or a user setting).\n *\n * With `options.platform === 'android'` and a module that has\n * `performAndroidHapticsAsync`, cues play Android system haptic effects instead:\n * `selection` → `'clock-tick'`; `impact` `light` / `soft` → `'keyboard-tap'`,\n * `medium` (and omitted) → `'virtual-key'`, `heavy` / `rigid` → `'long-press'`;\n * `notification` `success` (and omitted) → `'confirm'`, `warning` →\n * `'long-press'`, `error` → `'reject'`. If the module rejects an effect\n * (`'confirm'` / `'reject'` need Android 11), that cue falls back to the expo\n * call above, and the provider stops trying that effect.\n *\n * @example\n * import * as Haptics from 'expo-haptics';\n * import { Platform } from 'react-native';\n * const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });\n */\nexport function createExpoHapticsProvider(\n expo: ExpoHapticsModule,\n options?: ExpoHapticsOptions,\n): HapticsProvider {\n if (options?.platform !== 'android' || typeof expo.performAndroidHapticsAsync !== 'function') {\n return createGatedProvider(\n {\n impact: (style) => expo.impactAsync(style),\n notification: (type) => expo.notificationAsync(type),\n selection: () => expo.selectionAsync(),\n },\n true,\n );\n }\n\n const perform = expo.performAndroidHapticsAsync.bind(expo);\n const unsupported = new Set<string>();\n\n // Resolves `true` when the system effect played, `false` when the caller must\n // fall back. Any rejection marks the type: the module's error class is not\n // visible from here, and a type that failed once is not worth a retry per cue.\n const playSystemEffect = async (type: string): Promise<boolean> => {\n if (unsupported.has(type)) return false;\n try {\n await perform(type);\n return true;\n } catch {\n unsupported.add(type);\n return false;\n }\n };\n\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n if (!(await playSystemEffect(ANDROID_IMPACT[style ?? 'medium']))) {\n await expo.impactAsync(style);\n }\n },\n async notification(type): Promise<void> {\n if (!(await playSystemEffect(ANDROID_NOTIFICATION[type ?? 'success']))) {\n await expo.notificationAsync(type);\n }\n },\n async selection(): Promise<void> {\n if (!(await playSystemEffect(ANDROID_SELECTION))) await expo.selectionAsync();\n },\n },\n true,\n );\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":"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"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n","import { createContext, useContext } from 'react';\nimport { type HapticsProvider, NoopHapticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `HapticsProvider`. Defaults to\n * `NoopHapticsProvider`.\n */\nexport const HapticsContext = createContext<HapticsProvider>(NoopHapticsProvider);\n\n/** Hook returning the current `HapticsProvider`. */\nexport function useHaptics(): HapticsProvider {\n return useContext(HapticsContext);\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n} from './provider.js';\n\n/**\n * Structural shape of the `expo-haptics` module surface this adapter needs — the\n * `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared\n * here so the package never depends on `expo-haptics`; the app injects the real\n * module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).\n *\n * Style / type params are `string` because expo types them as its own string\n * enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal\n * this package's `HapticImpactStyle` / `HapticNotificationType` literals\n * (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through\n * unchanged and the real `Haptics` module stays structurally assignable.\n */\nexport interface ExpoHapticsModule {\n impactAsync(style?: string): Promise<void>;\n notificationAsync(type?: string): Promise<void>;\n selectionAsync(): Promise<void>;\n /**\n * Android system haptic feedback. Optional, so a module without it still fits.\n * `type` is `string` for the same reason as above: expo types it as its\n * `AndroidHaptics` string enum, and method syntax keeps the real module\n * structurally assignable. The adapter passes that enum's values\n * (`'clock-tick'`, `'confirm'`, …).\n */\n performAndroidHapticsAsync?(type: string): Promise<void>;\n}\n\n/** Options for {@link createExpoHapticsProvider}. */\nexport interface ExpoHapticsOptions {\n /**\n * The running platform — pass `Platform.OS` from `react-native`. With\n * `'android'`, cues use the system haptic effects when the injected module has\n * `performAndroidHapticsAsync`. The hint is needed because expo's function\n * resolves without doing anything on other platforms, so its presence alone\n * cannot pick the route.\n */\n platform?: string;\n}\n\nconst ANDROID_SELECTION = 'clock-tick';\n\nconst ANDROID_IMPACT: Readonly<Record<HapticImpactStyle, string>> = {\n light: 'keyboard-tap',\n soft: 'keyboard-tap',\n medium: 'virtual-key',\n heavy: 'long-press',\n rigid: 'long-press',\n};\n\nconst ANDROID_NOTIFICATION: Readonly<Record<HapticNotificationType, string>> = {\n success: 'confirm',\n warning: 'long-press',\n error: 'reject',\n};\n\n/**\n * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.\n * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,\n * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an\n * omitted style / type forwards `undefined`, letting expo apply its own default.\n * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the\n * `@flare-engine/a11y` reduced-motion signal, or a user setting).\n *\n * With `options.platform === 'android'` and a module that has\n * `performAndroidHapticsAsync`, cues play Android system haptic effects instead:\n * `selection` → `'clock-tick'`; `impact` `light` / `soft` → `'keyboard-tap'`,\n * `medium` (and omitted) → `'virtual-key'`, `heavy` / `rigid` → `'long-press'`;\n * `notification` `success` (and omitted) → `'confirm'`, `warning` →\n * `'long-press'`, `error` → `'reject'`. If the module rejects an effect\n * (`'confirm'` / `'reject'` need Android 11), that cue falls back to the expo\n * call above, and the provider stops trying that effect.\n *\n * @example\n * import * as Haptics from 'expo-haptics';\n * import { Platform } from 'react-native';\n * const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });\n */\nexport function createExpoHapticsProvider(\n expo: ExpoHapticsModule,\n options?: ExpoHapticsOptions,\n): HapticsProvider {\n if (options?.platform !== 'android' || typeof expo.performAndroidHapticsAsync !== 'function') {\n return createGatedProvider(\n {\n impact: (style) => expo.impactAsync(style),\n notification: (type) => expo.notificationAsync(type),\n selection: () => expo.selectionAsync(),\n },\n true,\n );\n }\n\n const perform = expo.performAndroidHapticsAsync.bind(expo);\n const unsupported = new Set<string>();\n\n // Resolves `true` when the system effect played, `false` when the caller must\n // fall back. Any rejection marks the type: the module's error class is not\n // visible from here, and a type that failed once is not worth a retry per cue.\n const playSystemEffect = async (type: string): Promise<boolean> => {\n if (unsupported.has(type)) return false;\n try {\n await perform(type);\n return true;\n } catch {\n unsupported.add(type);\n return false;\n }\n };\n\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n if (!(await playSystemEffect(ANDROID_IMPACT[style ?? 'medium']))) {\n await expo.impactAsync(style);\n }\n },\n async notification(type): Promise<void> {\n if (!(await playSystemEffect(ANDROID_NOTIFICATION[type ?? 'success']))) {\n await expo.notificationAsync(type);\n }\n },\n async selection(): Promise<void> {\n if (!(await playSystemEffect(ANDROID_SELECTION))) await expo.selectionAsync();\n },\n },\n true,\n );\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":"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"}