@apple-pie/slice 0.1.2 → 0.1.4

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.
Files changed (149) hide show
  1. package/README.md +89 -27
  2. package/dist/cjs/chunks/{Button-TfMwGVI5.js → Button-krf4dQeL.js} +4 -2
  3. package/dist/cjs/chunks/{ButtonBar-BqGC6sad.js → ButtonBar-C9Nm9faU.js} +1 -1
  4. package/dist/cjs/chunks/{Camera-om1AA2D0.js → Camera-Dx_PT3pS.js} +63 -46
  5. package/dist/cjs/chunks/{Card-Ctwbhu26.js → Card-AgEMKJ4O.js} +1 -1
  6. package/dist/cjs/chunks/{CheckBox-CeDIiSvi.js → CheckBox-CLMxlUff.js} +1 -1
  7. package/dist/cjs/chunks/{Chip-DdTtB57x.js → Chip-CIy_Xe3i.js} +1 -1
  8. package/dist/cjs/chunks/{DropDown-CC90XfyD.js → DropDown-FZb52YxR.js} +1 -1
  9. package/dist/cjs/chunks/{FileList-ClTfGKIL.js → FileList-CkpMj-os.js} +1 -1
  10. package/dist/cjs/chunks/{Grouper-jiYhVC82.js → Grouper-Bgc1wrQM.js} +1 -1
  11. package/dist/cjs/chunks/{IconButton-BiUfqoPb.js → IconButton-CBWmUZxR.js} +1 -1
  12. package/dist/cjs/chunks/{ProgressIndicator-B3-OxGYF.js → ProgressIndicator-DVD-gD3T.js} +1 -1
  13. package/dist/cjs/chunks/{PromptInput-Dsziepzs.js → PromptInput-DULf5ock.js} +1 -1
  14. package/dist/cjs/chunks/{RadioButton-i5n_nNV5.js → RadioButton-Ce9uf54a.js} +1 -1
  15. package/dist/cjs/chunks/{RadioButtonList-BCdk-5aD.js → RadioButtonList-dhPr-Uhs.js} +1 -1
  16. package/dist/cjs/chunks/SSEConnection-B_yE8cXz.js +140 -0
  17. package/dist/cjs/chunks/{TabBar-BF5CvZ-v.js → TabBar-DuwypMB6.js} +1 -1
  18. package/dist/cjs/chunks/{TextArea-BmOomEKp.js → TextArea-BJCjRUEG.js} +1 -1
  19. package/dist/cjs/chunks/{TextField-CXMyCClp.js → TextField-CjT26H_6.js} +18 -16
  20. package/dist/cjs/chunks/{Toast-CQ0-yuzl.js → Toast-DMUM1wuL.js} +1 -1
  21. package/dist/cjs/chunks/{UploadArea-B5Xvp0H4.js → UploadArea-BE6but3t.js} +1 -1
  22. package/dist/cjs/chunks/WSConnection-DCBuJGmb.js +206 -0
  23. package/dist/cjs/chunks/{_types-Cxk9tRiB.js → _types-C_zOsCbB.js} +57 -50
  24. package/dist/cjs/chunks/colors-uej0anrN.js +27 -0
  25. package/dist/cjs/chunks/sseStore-CIIeshjm.js +59 -0
  26. package/dist/cjs/chunks/windowStore-CCxCOUqa.js +156 -0
  27. package/dist/cjs/components/Button/index.js +3 -3
  28. package/dist/cjs/components/ButtonBar/index.js +3 -3
  29. package/dist/cjs/components/Camera/index.js +4 -4
  30. package/dist/cjs/components/Card/index.js +2 -2
  31. package/dist/cjs/components/CheckBox/index.js +2 -2
  32. package/dist/cjs/components/Chip/index.js +2 -2
  33. package/dist/cjs/components/DropDown/index.js +2 -2
  34. package/dist/cjs/components/FileList/index.js +3 -3
  35. package/dist/cjs/components/Grouper/index.js +2 -2
  36. package/dist/cjs/components/Icon/index.js +1 -1
  37. package/dist/cjs/components/IconButton/index.js +2 -2
  38. package/dist/cjs/components/Progress/index.js +2 -2
  39. package/dist/cjs/components/PromptInput/index.js +5 -5
  40. package/dist/cjs/components/RadioButton/index.js +2 -2
  41. package/dist/cjs/components/RadioButtonList/index.js +3 -3
  42. package/dist/cjs/components/TabBar/index.js +3 -3
  43. package/dist/cjs/components/TextArea/index.js +4 -4
  44. package/dist/cjs/components/Textfield/index.js +5 -5
  45. package/dist/cjs/components/Toast/index.js +2 -2
  46. package/dist/cjs/components/UploadArea/index.js +4 -4
  47. package/dist/cjs/hooks/useWindow.js +46 -11
  48. package/dist/cjs/index.js +2 -2
  49. package/dist/cjs/stores/SSE.js +9 -0
  50. package/dist/cjs/stores/WS.js +62 -0
  51. package/dist/cjs/stores/window.js +14 -3
  52. package/dist/cjs/stores.js +20 -5
  53. package/dist/cjs/utils/index.js +4 -26
  54. package/dist/cjs/utils/objects/index.js +6 -0
  55. package/dist/esm/chunks/{Avatar-BQT1r-Ge.mjs → Avatar-CDMV6f9w.mjs} +1 -1
  56. package/dist/esm/chunks/{AvatarGroup-B787aNtD.mjs → AvatarGroup-BZ18GwhK.mjs} +1 -1
  57. package/dist/esm/chunks/{Button-1MgE-kl9.mjs → Button-kXFJmcKv.mjs} +5 -3
  58. package/dist/esm/chunks/{ButtonBar-Eahfd-40.mjs → ButtonBar-DIsqJmrS.mjs} +1 -1
  59. package/dist/esm/chunks/{Camera-Bvoug1KF.mjs → Camera-BvJJ5cd-.mjs} +65 -48
  60. package/dist/esm/chunks/{Card-RSZhXncV.mjs → Card-DjHdCav5.mjs} +1 -1
  61. package/dist/esm/chunks/{CheckBox-CfLedK0e.mjs → CheckBox-C1LPehvl.mjs} +1 -1
  62. package/dist/esm/chunks/{Chip-Db4N0WVH.mjs → Chip-ByaaWpFx.mjs} +1 -1
  63. package/dist/esm/chunks/{DivInput-BEpjdfu4.mjs → DivInput-B7BliU1p.mjs} +1 -1
  64. package/dist/esm/chunks/{DrggablePanel-DJm1Mx5u.mjs → DrggablePanel-DvactFf5.mjs} +1 -1
  65. package/dist/esm/chunks/{DropDown-ryz3GK81.mjs → DropDown-CM01mO_W.mjs} +1 -1
  66. package/dist/esm/chunks/{FileIcon-_mK2EcB2.mjs → FileIcon-CC_k5dUW.mjs} +1 -1
  67. package/dist/esm/chunks/{FileList-hCMTALyN.mjs → FileList-Bon537-t.mjs} +4 -4
  68. package/dist/esm/chunks/{Grouper-B1gjd3Sx.mjs → Grouper-DNmAYWdR.mjs} +1 -1
  69. package/dist/esm/chunks/{IconButton-SyRu990z.mjs → IconButton-C8RBNFzh.mjs} +1 -1
  70. package/dist/esm/chunks/{Label-DGOkB_Px.mjs → Label-DOvquc82.mjs} +1 -1
  71. package/dist/esm/chunks/{Pager-BA0lD9mI.mjs → Pager-BX4TMwD_.mjs} +1 -1
  72. package/dist/esm/chunks/{ProgressIndicator-BayyosKr.mjs → ProgressIndicator-D2eairZl.mjs} +1 -1
  73. package/dist/esm/chunks/{PromptInput-CUXCuYVA.mjs → PromptInput-DXFAZspz.mjs} +2 -2
  74. package/dist/esm/chunks/{RadioButton-BRFSVtmm.mjs → RadioButton-_lo_FJ8g.mjs} +2 -2
  75. package/dist/esm/chunks/{RadioButtonList-D6a_EcB6.mjs → RadioButtonList-CLHJxrI_.mjs} +1 -1
  76. package/dist/esm/chunks/SSEConnection-Dj9P0SWE.mjs +140 -0
  77. package/dist/esm/chunks/{Slider-D2XVyi2g.mjs → Slider-Bih21QiT.mjs} +1 -1
  78. package/dist/esm/chunks/{TabBar-BqajqueL.mjs → TabBar-CDDzA_m1.mjs} +2 -2
  79. package/dist/esm/chunks/{TextArea-C5EbLAN1.mjs → TextArea-DBmM6aQm.mjs} +1 -1
  80. package/dist/esm/chunks/{TextField-DnQxDxus.mjs → TextField-DyUTMegV.mjs} +20 -18
  81. package/dist/esm/chunks/{Tip-B64Pa9eq.mjs → Tip-B-cDOFvV.mjs} +1 -1
  82. package/dist/esm/chunks/{Toast-B3GgzcTW.mjs → Toast-ovxR12_-.mjs} +2 -2
  83. package/dist/esm/chunks/{UploadArea-DpWT-5DA.mjs → UploadArea-tT5V8q23.mjs} +4 -4
  84. package/dist/esm/chunks/WSConnection-DlYSitff.mjs +206 -0
  85. package/dist/esm/chunks/{_types-DGDKmYIh.mjs → _types-CTmvSEX0.mjs} +36 -29
  86. package/dist/esm/chunks/colors-DR9fLI5X.mjs +30 -0
  87. package/dist/esm/chunks/{misc-Osk7tIah.mjs → misc-cg9I7drO.mjs} +1 -1
  88. package/dist/esm/chunks/sseStore-DdqmnAAf.mjs +56 -0
  89. package/dist/esm/chunks/windowStore-BOTh1g37.mjs +148 -0
  90. package/dist/esm/components/Avatar/index.mjs +2 -2
  91. package/dist/esm/components/AvatarGroup/index.mjs +3 -3
  92. package/dist/esm/components/Button/index.mjs +4 -4
  93. package/dist/esm/components/ButtonBar/index.mjs +4 -4
  94. package/dist/esm/components/Camera/index.mjs +5 -5
  95. package/dist/esm/components/Card/index.mjs +3 -3
  96. package/dist/esm/components/CheckBox/index.mjs +3 -3
  97. package/dist/esm/components/Chip/index.mjs +3 -3
  98. package/dist/esm/components/DivInput/index.mjs +2 -2
  99. package/dist/esm/components/DraggablePanel/index.mjs +2 -2
  100. package/dist/esm/components/DropDown/index.mjs +3 -3
  101. package/dist/esm/components/FileIcon/index.mjs +2 -2
  102. package/dist/esm/components/FileList/index.mjs +5 -5
  103. package/dist/esm/components/Grouper/index.mjs +3 -3
  104. package/dist/esm/components/Icon/index.mjs +2 -2
  105. package/dist/esm/components/IconButton/index.mjs +3 -3
  106. package/dist/esm/components/Label/index.mjs +2 -2
  107. package/dist/esm/components/Pager/index.mjs +2 -2
  108. package/dist/esm/components/Progress/index.mjs +3 -3
  109. package/dist/esm/components/PromptInput/index.mjs +7 -7
  110. package/dist/esm/components/RadioButton/index.mjs +3 -3
  111. package/dist/esm/components/RadioButtonList/index.mjs +4 -4
  112. package/dist/esm/components/Slider/index.mjs +2 -2
  113. package/dist/esm/components/TabBar/index.mjs +4 -4
  114. package/dist/esm/components/TextArea/index.mjs +5 -5
  115. package/dist/esm/components/Textfield/index.mjs +6 -6
  116. package/dist/esm/components/Tip/index.mjs +2 -2
  117. package/dist/esm/components/Toast/index.mjs +3 -3
  118. package/dist/esm/components/UploadArea/index.mjs +6 -6
  119. package/dist/esm/hooks/useTrackRenders.mjs +1 -1
  120. package/dist/esm/hooks/useWindow.mjs +47 -11
  121. package/dist/esm/index.mjs +31 -31
  122. package/dist/esm/stores/SSE.mjs +7 -0
  123. package/dist/esm/stores/WS.mjs +59 -0
  124. package/dist/esm/stores/window.mjs +3 -1
  125. package/dist/esm/stores.mjs +6 -2
  126. package/dist/esm/utils/index.mjs +6 -30
  127. package/dist/esm/utils/objects/index.mjs +5 -0
  128. package/dist/types/components/Camera/_types.d.ts +1 -0
  129. package/dist/types/components/Icon/_types.d.ts +1 -0
  130. package/dist/types/hooks/index.d.ts +1 -1
  131. package/dist/types/hooks/useWindow/useWindow.d.ts +19 -0
  132. package/dist/types/index.d.ts +1 -1
  133. package/dist/types/stores/SSE/_types.d.ts +17 -0
  134. package/dist/types/stores/SSE/index.d.ts +2 -0
  135. package/dist/types/stores/SSE/sseStore.d.ts +28 -0
  136. package/dist/types/stores/WS/_types.d.ts +17 -0
  137. package/dist/types/stores/WS/index.d.ts +2 -0
  138. package/dist/types/stores/WS/wsStore.d.ts +19 -0
  139. package/dist/types/stores/index.d.ts +1 -0
  140. package/dist/types/stores/window/_types.d.ts +5 -0
  141. package/dist/types/stores/window/index.d.ts +3 -2
  142. package/dist/types/stores/window/windowStore.d.ts +8 -0
  143. package/dist/types/utils/index.d.ts +2 -0
  144. package/dist/types/utils/objects/SSEConnection/SSEConnection.d.ts +166 -0
  145. package/dist/types/utils/objects/WSConnection/WSConnection.d.ts +147 -0
  146. package/dist/types/utils/objects/index.d.ts +4 -0
  147. package/package.json +6 -1
  148. package/dist/cjs/chunks/windowStore-RaJswiHM.js +0 -70
  149. package/dist/esm/chunks/windowStore-CwW0i_le.mjs +0 -71
@@ -2,7 +2,7 @@ export type { ToolTip, ToolTipInfo } from './components/sharedTypes';
2
2
  export { ToolTipType } from './components/sharedTypes';
3
3
  export { light, dark, lightTheme, darkTheme, motion, elevations, Elevation, } from './theme';
4
4
  export type { SliceTheme, Colors, Type, Corners, Elevations, } from './theme';
5
- export { type KeyboardShortcut, type KeyboardShortcuts, type BreakPoints, useKeyboardShortcuts, useTheme, useObserveTheme, useToolTip, useLastUpdated, useLocalStore, useWindow, useDoubleClick, useObserveResize, } from './hooks';
5
+ export { type KeyboardShortcut, type KeyboardShortcuts, type BreakPoints, type WindowGeolocation, type WindowGeolocationError, useKeyboardShortcuts, useTheme, useObserveTheme, useToolTip, useLastUpdated, useLocalStore, useWindow, useDoubleClick, useObserveResize, } from './hooks';
6
6
  export { Avatar } from './components/Avatar/index';
7
7
  export type { AvatarProps } from './components/Avatar/index';
8
8
  export { AvatarGroup } from './components/AvatarGroup';
@@ -0,0 +1,17 @@
1
+ import type { SSEConnection, SSEConnectionOptions, SSEEventMap, SSEUnifiedMessage } from '../../utils';
2
+ export type SSEStoreConnectionOptions = Omit<SSEConnectionOptions<unknown, SSEEventMap>, 'unifiedOnMessage'> & {
3
+ onMessageCallback?: (message: SSEUnifiedMessage<unknown, SSEEventMap>) => void;
4
+ };
5
+ export interface SSEStoreConnection {
6
+ name: string;
7
+ connection: SSEConnection<unknown, SSEEventMap>;
8
+ }
9
+ export interface SSEStore {
10
+ connections: SSEStoreConnection[];
11
+ message: SSEUnifiedMessage<unknown, SSEEventMap> | null;
12
+ closedConnection: string | null;
13
+ actions: {
14
+ addConnection: (name: string, options: SSEStoreConnectionOptions) => SSEStoreConnection;
15
+ removeConnection: (name: string) => void;
16
+ };
17
+ }
@@ -0,0 +1,2 @@
1
+ export { useSSEStore, useSSE, useConnectionClose, useConnectionMessage, useIsConnected, useMessage, useSSEActions, useLastSSEMessage, } from './sseStore';
2
+ export type { SSEStore, SSEStoreConnection, SSEStoreConnectionOptions, } from './_types';
@@ -0,0 +1,28 @@
1
+ import { type SSEEventMap, type SSEUnifiedMessage } from '../../utils';
2
+ import type { SSEStore, SSEStoreConnectionOptions } from './_types';
3
+ export declare const useSSEStore: import("zustand").UseBoundStore<import("zustand").StoreApi<SSEStore>>;
4
+ export declare const useSSE: () => {
5
+ addConnection: (name: string, options: SSEStoreConnectionOptions) => import("./_types").SSEStoreConnection;
6
+ removeConnection: (name: string) => void;
7
+ };
8
+ export declare const useConnectionClose: () => string | null;
9
+ export declare const useConnectionMessage: (connection: string) => {
10
+ type: "error";
11
+ event: Event;
12
+ } | {
13
+ type: "open";
14
+ event: Event;
15
+ } | {
16
+ type: "close";
17
+ event: Event;
18
+ } | import("../../utils").SSEUnifiedCustomMessage<SSEEventMap, string> | null;
19
+ export declare const useIsConnected: (connection?: string) => boolean;
20
+ export declare function useMessage(): SSEUnifiedMessage<unknown, SSEEventMap> | null;
21
+ export declare function useMessage<T = unknown>(type: 'message', connection?: string): T | string | null;
22
+ export declare function useMessage(type: 'open' | 'error' | 'close', connection?: string): Event | null;
23
+ export declare function useMessage<T = unknown>(type: string, connection?: string): T | string | null;
24
+ export declare const useSSEActions: {
25
+ addConnection: (name: string, options: SSEStoreConnectionOptions) => import("./_types").SSEStoreConnection;
26
+ removeConnection: (name: string) => void;
27
+ };
28
+ export declare const useLastSSEMessage: () => SSEUnifiedMessage<unknown, SSEEventMap> | null;
@@ -0,0 +1,17 @@
1
+ import type { UnifiedMessageEvent, WSConnection, WSConnectionOptions } from '../../utils';
2
+ export type WSStoreConnectionOptions = Omit<WSConnectionOptions<unknown>, 'unifiedMessages'> & {
3
+ onMessageCallback?: (message: UnifiedMessageEvent<unknown>) => void;
4
+ };
5
+ export interface WSStoreConnection {
6
+ name: string;
7
+ connection: WSConnection<unknown>;
8
+ }
9
+ export interface WSStore {
10
+ connections: WSStoreConnection[];
11
+ message: UnifiedMessageEvent<unknown> | null;
12
+ closedConnection: string | null;
13
+ actions: {
14
+ addConnection: (name: string, options: WSStoreConnectionOptions) => WSStoreConnection;
15
+ removeConnection: (name: string) => void;
16
+ };
17
+ }
@@ -0,0 +1,2 @@
1
+ export { useWSStore, useWS, useConnectionClose, useConnectionMessage, useIsConnected, useMessage, useWSActions, useLastWSMessage, } from './wsStore';
2
+ export type { WSStore, WSStoreConnection, WSStoreConnectionOptions, } from './_types';
@@ -0,0 +1,19 @@
1
+ import { type UnifiedMessageEvent } from '../../utils';
2
+ import type { WSStore, WSStoreConnectionOptions } from './_types';
3
+ export declare const useWSStore: import("zustand").UseBoundStore<import("zustand").StoreApi<WSStore>>;
4
+ export declare const useWS: () => {
5
+ addConnection: (name: string, options: WSStoreConnectionOptions) => import("./_types").WSStoreConnection;
6
+ removeConnection: (name: string) => void;
7
+ };
8
+ export declare const useConnectionClose: () => string | null;
9
+ export declare const useConnectionMessage: (connection: string) => UnifiedMessageEvent<unknown> | null;
10
+ export declare const useIsConnected: (connection?: string) => boolean;
11
+ export declare function useMessage(): UnifiedMessageEvent<unknown> | null;
12
+ export declare function useMessage<T = unknown>(type: 'message', connection?: string): T | string | Blob | ArrayBuffer | null;
13
+ export declare function useMessage(type: 'open' | 'error' | 'close', connection?: string): Event | ErrorEvent | CloseEvent | null;
14
+ export declare function useMessage<T = unknown>(type: string, connection?: string): T | string | Blob | ArrayBuffer | null;
15
+ export declare const useWSActions: {
16
+ addConnection: (name: string, options: WSStoreConnectionOptions) => import("./_types").WSStoreConnection;
17
+ removeConnection: (name: string) => void;
18
+ };
19
+ export declare const useLastWSMessage: () => UnifiedMessageEvent<unknown> | null;
@@ -1,3 +1,4 @@
1
+ export * from './SSE';
1
2
  export * from './toast';
2
3
  export * from './tip';
3
4
  export * from './uploads';
@@ -1,3 +1,4 @@
1
+ import type { WindowGeolocation } from '../../hooks/useWindow/useWindow';
1
2
  export type WindowStore = {
2
3
  formFactor: FormFactor;
3
4
  dpr: 1 | 2 | 3;
@@ -7,8 +8,12 @@ export type WindowStore = {
7
8
  height: string;
8
9
  viewportWidth: number;
9
10
  viewportHeight: number;
11
+ location: WindowGeolocation | null;
12
+ locationError: Error | null;
13
+ gettingLocation: boolean;
10
14
  actions: {
11
15
  initialize: () => () => void;
16
+ getLocation: () => Promise<WindowGeolocation>;
12
17
  };
13
18
  };
14
19
  /**
@@ -1,2 +1,3 @@
1
- export { useWindowStore } from './windowStore';
2
- export { FormFactor, type BreakPoints, type WindowStore, } from './_types';
1
+ export { useWindowStore, useFormFactor, useViewportWidth, useViewportHeight, useIsAppleDevice, useIsTouchDevice, useIsElectron, useDpr, useLocation, useLocationError, useGettingLocation, useInitializeWindow, useGetLocation, formFactor, viewportWidth, viewportHeight, isAppleDevice, isTouchDevice, isElectron, dpr, location, locationError, gettingLocation, } from './windowStore';
2
+ export { FormFactor, type BreakPoints, type WindowStore } from './_types';
3
+ export type { WindowGeolocation, WindowGeolocationError, } from '../../hooks/useWindow/useWindow';
@@ -1,3 +1,4 @@
1
+ import type { WindowGeolocation } from '../../hooks/useWindow/useWindow';
1
2
  import { FormFactor, type WindowStore } from './_types';
2
3
  export declare const useWindowStore: import("zustand").UseBoundStore<import("zustand").StoreApi<WindowStore>>;
3
4
  export declare const useFormFactor: () => FormFactor;
@@ -7,7 +8,11 @@ export declare const useIsAppleDevice: () => boolean;
7
8
  export declare const useIsTouchDevice: () => boolean;
8
9
  export declare const useIsElectron: () => boolean;
9
10
  export declare const useDpr: () => 2 | 1 | 3;
11
+ export declare const useLocation: () => WindowGeolocation | null;
12
+ export declare const useLocationError: () => Error | null;
13
+ export declare const useGettingLocation: () => boolean;
10
14
  export declare const useInitializeWindow: () => () => () => void;
15
+ export declare const useGetLocation: () => () => Promise<WindowGeolocation>;
11
16
  export declare const formFactor: () => FormFactor;
12
17
  export declare const viewportWidth: () => number;
13
18
  export declare const viewportHeight: () => number;
@@ -15,3 +20,6 @@ export declare const isAppleDevice: () => boolean;
15
20
  export declare const isTouchDevice: () => boolean;
16
21
  export declare const isElectron: () => boolean;
17
22
  export declare const dpr: () => 2 | 1 | 3;
23
+ export declare const location: () => WindowGeolocation | null;
24
+ export declare const locationError: () => Error | null;
25
+ export declare const gettingLocation: () => boolean;
@@ -1,2 +1,4 @@
1
1
  export { copyToClipboard } from './functions/misc';
2
2
  export { tintFromColor, addOpacity } from './functions/colors';
3
+ export { SSEConnection, WSConnection } from './objects';
4
+ export type { SSECustomEvent, SSEConnectionCloseOption, SSEConnectionOptions, SSEEventMap, SSEUnifiedBuiltInMessage, SSEUnifiedCustomMessage, SSEUnifiedMessage, UnifiedMessageEvent, WSConnectionOptions, } from './objects';
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Maps each custom SSE event name to the payload type that event emits.
3
+ */
4
+ export type SSEEventMap = Record<string, unknown>;
5
+ /**
6
+ * Parsed SSE payloads can still be raw strings when the event data is not JSON.
7
+ */
8
+ type SSEParsedData<T> = T | string;
9
+ /**
10
+ * Restricts custom event names to string keys so they can be used with
11
+ * `addEventListener`.
12
+ */
13
+ type SSEEventName<TCustomEvents extends SSEEventMap> = Extract<keyof TCustomEvents, string>;
14
+ /**
15
+ * Built-in EventSource events represented in unified callback mode.
16
+ */
17
+ export type SSEUnifiedBuiltInMessage<TMessage> = {
18
+ type: 'message';
19
+ data: SSEParsedData<TMessage>;
20
+ event: MessageEvent;
21
+ } | {
22
+ type: 'error';
23
+ event: Event;
24
+ } | {
25
+ type: 'open';
26
+ event: Event;
27
+ } | {
28
+ type: 'close';
29
+ event: Event;
30
+ };
31
+ export interface SSEConnectionCloseOption<TCustomEvents extends SSEEventMap = Record<string, never>> {
32
+ event?: SSEEventName<TCustomEvents>;
33
+ message?: string;
34
+ }
35
+ /**
36
+ * A custom event in unified callback mode. The `type` determines the shape of
37
+ * `data`.
38
+ */
39
+ export type SSEUnifiedCustomMessage<TCustomEvents extends SSEEventMap, TName extends SSEEventName<TCustomEvents> = SSEEventName<TCustomEvents>> = {
40
+ type: TName;
41
+ data: SSEParsedData<TCustomEvents[TName]>;
42
+ event: MessageEvent;
43
+ };
44
+ /**
45
+ * All messages that can flow through the single unified callback.
46
+ */
47
+ export type SSEUnifiedMessage<TMessage, TCustomEvents extends SSEEventMap> = SSEUnifiedBuiltInMessage<TMessage> | SSEUnifiedCustomMessage<TCustomEvents>;
48
+ /**
49
+ * Callback signature for standard mode, where only the default `message`
50
+ * event is routed to `onMessageCallback`.
51
+ */
52
+ type SSEStandardMessageCallback<TMessage> = (data: SSEParsedData<TMessage>) => void;
53
+ /**
54
+ * Callback signature for unified mode, where built-in and custom events are
55
+ * all routed through a discriminated object.
56
+ */
57
+ type SSEUnifiedMessageCallback<TMessage, TCustomEvents extends SSEEventMap> = (message: SSEUnifiedMessage<TMessage, TCustomEvents>) => void;
58
+ /**
59
+ * Declares one custom SSE event. `name` selects a key from the event map, and
60
+ * `handler` is typed to that event's payload when provided.
61
+ */
62
+ export interface SSECustomEvent<TCustomEvents extends SSEEventMap, TName extends SSEEventName<TCustomEvents> = SSEEventName<TCustomEvents>> {
63
+ name: TName;
64
+ handler?: (data: TCustomEvents[TName]) => void;
65
+ }
66
+ /**
67
+ * Options shared by both standard and unified message modes.
68
+ */
69
+ interface SSEConnectionOptionsBase<TCustomEvents extends SSEEventMap = Record<string, never>> {
70
+ url?: string;
71
+ options?: EventSourceInit;
72
+ connectionClose?: SSEConnectionCloseOption<TCustomEvents>;
73
+ onErrorCallback?: (event: Event) => void;
74
+ onOpenCallback?: (event: Event) => void;
75
+ onCloseCallback?: (event: Event) => void;
76
+ customEvents?: SSECustomEvent<TCustomEvents> | SSECustomEvent<TCustomEvents>[];
77
+ }
78
+ /**
79
+ * Options for unified mode. When enabled, every built-in and custom event is
80
+ * emitted through `onMessageCallback`.
81
+ */
82
+ interface SSEConnectionUnifiedOptions<TMessage = unknown, TCustomEvents extends SSEEventMap = Record<string, never>> extends SSEConnectionOptionsBase<TCustomEvents> {
83
+ unifiedOnMessage: true;
84
+ onMessageCallback?: SSEUnifiedMessageCallback<TMessage, TCustomEvents>;
85
+ }
86
+ /**
87
+ * Options for standard mode. Only the default `message` event payload is sent
88
+ * to `onMessageCallback`.
89
+ */
90
+ interface SSEConnectionStandardOptions<TMessage = unknown, TCustomEvents extends SSEEventMap = Record<string, never>> extends SSEConnectionOptionsBase<TCustomEvents> {
91
+ unifiedOnMessage?: false;
92
+ onMessageCallback?: SSEStandardMessageCallback<TMessage>;
93
+ }
94
+ /**
95
+ * Public connection options. The `unifiedOnMessage` flag selects which
96
+ * callback signature is valid.
97
+ */
98
+ export type SSEConnectionOptions<TMessage = unknown, TCustomEvents extends SSEEventMap = Record<string, never>> = SSEConnectionStandardOptions<TMessage, TCustomEvents> | SSEConnectionUnifiedOptions<TMessage, TCustomEvents>;
99
+ export declare class SSEConnection<TMessage = unknown, TCustomEvents extends SSEEventMap = Record<string, never>> {
100
+ private readonly url;
101
+ private readonly options;
102
+ private sseConnection;
103
+ private readonly onMessageCallback?;
104
+ private readonly onErrorCallback?;
105
+ private readonly onOpenCallback?;
106
+ private readonly onCloseCallback?;
107
+ private readonly connectionClose?;
108
+ private readonly customEvents;
109
+ private readonly customEventHandlers;
110
+ private readonly unifiedOnMessage;
111
+ /**
112
+ * Whether the underlying EventSource is currently open.
113
+ */
114
+ get connected(): boolean;
115
+ /**
116
+ * Direct access to the underlying EventSource instance.
117
+ */
118
+ get connection(): EventSource | null;
119
+ /**
120
+ * Create an SEE connection instance
121
+ */
122
+ constructor(options?: SSEConnectionOptions<TMessage, TCustomEvents>);
123
+ /**
124
+ * Safely parse SSE event data. Falls back to the raw string when the payload is not JSON.
125
+ */
126
+ private parseEventData;
127
+ /**
128
+ * Attach default SEE events
129
+ */
130
+ private attachDefaultEventListeners;
131
+ private emitParsedMessage;
132
+ private emitUnifiedMessage;
133
+ private emitCloseEvent;
134
+ private closeFromConnectionEvent;
135
+ private shouldCloseFromMessage;
136
+ private emitUnifiedCustomMessage;
137
+ /**
138
+ * Attach custom event listeners to the SSE connection
139
+ */
140
+ private attachCustomEventListener;
141
+ /**
142
+ * Attach custom event listeners to the SSE connection
143
+ */
144
+ private attachCustomEventListeners;
145
+ /**
146
+ * Clean up listeners
147
+ */
148
+ private detachAllEventListeners;
149
+ /**
150
+ * One message received
151
+ */
152
+ onMessage: (event: MessageEvent) => void;
153
+ /**
154
+ * On error event
155
+ */
156
+ onError: (event: Event) => void;
157
+ /**
158
+ * On open event
159
+ */
160
+ onOpen: (event: Event) => void;
161
+ /**
162
+ * Close the SSE connection and release resources.
163
+ */
164
+ close: () => void;
165
+ }
166
+ export {};
@@ -0,0 +1,147 @@
1
+ type WSRawData = string | Blob | ArrayBuffer;
2
+ type WSParsedData<TMessage> = TMessage | WSRawData;
3
+ export type UnifiedMessageEvent<TMessage> = {
4
+ type: 'message';
5
+ data: WSParsedData<TMessage>;
6
+ event: MessageEvent;
7
+ } | {
8
+ type: 'open';
9
+ event: Event;
10
+ } | {
11
+ type: 'close';
12
+ event: CloseEvent;
13
+ } | {
14
+ type: 'error';
15
+ event: ErrorEvent;
16
+ };
17
+ type WSStandardMessageCallback<TMessage> = (data: WSParsedData<TMessage>) => void;
18
+ type WSUnifiedMessageCallback<TMessage> = (message: UnifiedMessageEvent<TMessage>) => void;
19
+ interface WSConnectionOptionsBase {
20
+ url: string;
21
+ autoReconnect?: boolean;
22
+ reconnectInterval?: number;
23
+ reconnectAttempts?: number;
24
+ reconnectFalloff?: boolean;
25
+ keepAlive?: boolean;
26
+ keepAliveInterval?: number;
27
+ token?: string | (() => Promise<string>);
28
+ onOpenCallback?: (event: Event) => void;
29
+ onCloseCallback?: (event: CloseEvent) => void;
30
+ onErrorCallback?: (error: ErrorEvent) => void;
31
+ }
32
+ interface WSConnectionUnifiedOptions<TMessage = unknown> extends WSConnectionOptionsBase {
33
+ unifiedMessages: true;
34
+ onMessageCallback?: WSUnifiedMessageCallback<TMessage>;
35
+ }
36
+ interface WSConnectionStandardOptions<TMessage = unknown> extends WSConnectionOptionsBase {
37
+ unifiedMessages?: false;
38
+ onMessageCallback?: WSStandardMessageCallback<TMessage>;
39
+ }
40
+ export type WSConnectionOptions<TMessage = unknown> = WSConnectionStandardOptions<TMessage> | WSConnectionUnifiedOptions<TMessage>;
41
+ export declare class WSConnection<TMessage = unknown> {
42
+ private readonly url;
43
+ private readonly autoReconnect;
44
+ private readonly reconnectInterval;
45
+ private readonly reconnectAttempts;
46
+ private readonly reconnectFalloff;
47
+ private readonly keepAlive;
48
+ private readonly keepAliveInterval;
49
+ private readonly unifiedMessages;
50
+ private readonly token;
51
+ private readonly onMessageCallback?;
52
+ private readonly onOpenCallback?;
53
+ private readonly onCloseCallback?;
54
+ private readonly onErrorCallback?;
55
+ private socket;
56
+ private keepAliveTimer;
57
+ private reconnectIntervalTimer;
58
+ private reconnectAttemptsCount;
59
+ private manuallyClosed;
60
+ get connected(): boolean;
61
+ get connection(): WebSocket | null;
62
+ constructor(options: WSConnectionOptions<TMessage>);
63
+ /**
64
+ * Gracefully handle sending messages
65
+ */
66
+ send(message: unknown): void;
67
+ /**
68
+ * Gracefully expose socket close
69
+ */
70
+ close(closeEvent: {
71
+ code: number;
72
+ reason: string;
73
+ }): void;
74
+ /**
75
+ * Emit the parsed message content for standard mode
76
+ */
77
+ private emitParsedMessage;
78
+ /**
79
+ * Emit a unified message if the unified flag is on
80
+ */
81
+ private emitUnifiedMessage;
82
+ /**
83
+ * Emit error
84
+ */
85
+ private emitErrorEvent;
86
+ /**
87
+ * Safe JSON parse message content
88
+ */
89
+ private parseEventData;
90
+ /**
91
+ * Open handler
92
+ */
93
+ private readonly onOpen;
94
+ /**
95
+ * Message handler
96
+ */
97
+ private readonly onMessage;
98
+ /**
99
+ * Close handler with the reconnection logic as needed
100
+ */
101
+ private readonly onClose;
102
+ /**
103
+ * Error handler
104
+ */
105
+ private readonly onError;
106
+ /**
107
+ * Attach socket listeners
108
+ */
109
+ private attachEventListeners;
110
+ /**
111
+ * set keep alive pings
112
+ */
113
+ private setKeepAlive;
114
+ /**
115
+ * Clear keep alive pings
116
+ */
117
+ private clearKeepAliveTimer;
118
+ /**
119
+ * Clear reconnect timer
120
+ */
121
+ private clearReconnectTimer;
122
+ /**
123
+ * detach all event listeners
124
+ */
125
+ private detachEventListeners;
126
+ /**
127
+ * Connect the socket
128
+ */
129
+ private connect;
130
+ /**
131
+ * Reconnect logic
132
+ */
133
+ private reconnect;
134
+ /**
135
+ * Calculate reconnect delay with fall off
136
+ */
137
+ private getReconnectDelay;
138
+ /**
139
+ * Determine if should reconnect based on the close event and props
140
+ */
141
+ private shouldReconnect;
142
+ /**
143
+ * Internal close socket - does not reset retry logic
144
+ */
145
+ private closeSocket;
146
+ }
147
+ export {};
@@ -0,0 +1,4 @@
1
+ export { SSEConnection } from './SSEConnection/SSEConnection';
2
+ export { WSConnection } from './WSConnection/WSConnection';
3
+ export type { SSECustomEvent, SSEConnectionCloseOption, SSEConnectionOptions, SSEEventMap, SSEUnifiedBuiltInMessage, SSEUnifiedCustomMessage, SSEUnifiedMessage, } from './SSEConnection/SSEConnection';
4
+ export type { UnifiedMessageEvent, WSConnectionOptions, } from './WSConnection/WSConnection';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apple-pie/slice",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.mjs",
6
6
  "exports": {
@@ -83,6 +83,11 @@
83
83
  "import": "./dist/esm/utils/index.mjs",
84
84
  "require": "./dist/cjs/utils/index.js"
85
85
  },
86
+ "./utils/objects": {
87
+ "types": "./dist/types/utils/objects/index.d.ts",
88
+ "import": "./dist/esm/utils/objects/index.mjs",
89
+ "require": "./dist/cjs/utils/objects/index.js"
90
+ },
86
91
  "./components/*": {
87
92
  "types": "./dist/types/components/*/index.d.ts",
88
93
  "import": "./dist/esm/components/*/index.mjs",
@@ -1,70 +0,0 @@
1
- "use strict";
2
-
3
- var FormFactor, zustand = require("zustand");
4
-
5
- /**
6
- * Form factor names
7
- */ exports.FormFactor = void 0, (FormFactor = exports.FormFactor || (exports.FormFactor = {})).Mobile = "mobile",
8
- FormFactor.Tablet = "tablet", FormFactor.Desktop = "desktop", FormFactor.DesktopL = "desktopL",
9
- FormFactor.DesktopXL = "desktopXL";
10
-
11
- /**
12
- * Use bootstrap breakpoints
13
- */
14
- const bootstrapBreakPoints = {
15
- [exports.FormFactor.Mobile]: 576,
16
- [exports.FormFactor.Tablet]: 768,
17
- [exports.FormFactor.Desktop]: 992,
18
- [exports.FormFactor.DesktopL]: 1200,
19
- [exports.FormFactor.DesktopXL]: 1400
20
- }, orderedBps = Object.entries(bootstrapBreakPoints).sort((a, b) => -b[1] - a[1]), getFormFactor = width => {
21
- if (!width || width <= 0) return exports.FormFactor.Mobile;
22
- for (const [formFactor, breakpoint] of orderedBps) if (width >= breakpoint) return formFactor;
23
- return exports.FormFactor.Mobile;
24
- }, initialSize = processWindowSize(), useWindowStore = zustand.create((set, get) => {
25
- var _a;
26
- return {
27
- formFactor: initialSize.formFactor,
28
- viewportWidth: initialSize.viewportWidth,
29
- viewportHeight: initialSize.viewportHeight,
30
- height: initialSize.height,
31
- isElectron: checkElectron(),
32
- isAppleDevice: navigator.platform.startsWith("Mac") || "iPhone" === navigator.platform,
33
- isTouchDevice: "ontouchstart" in globalThis || navigator.maxTouchPoints > 0,
34
- dpr: null !== (_a = Math.min(Math.ceil(window.devicePixelRatio), 3)) && void 0 !== _a ? _a : 1,
35
- actions: {
36
- initialize: () => {
37
- var _a;
38
- const win = null !== (_a = globalThis.top) && void 0 !== _a ? _a : globalThis;
39
- return null == win || win.addEventListener("resize", handleResize), () => null == win ? void 0 : win.removeEventListener("resize", handleResize);
40
- }
41
- }
42
- };
43
- });
44
-
45
- function checkElectron() {
46
- return navigator.userAgent.toLowerCase().includes("electron/");
47
- }
48
-
49
- function handleResize() {
50
- const {formFactor: formFactor, viewportWidth: viewportWidth, viewportHeight: viewportHeight, height: height} = processWindowSize();
51
- useWindowStore.setState({
52
- formFactor: formFactor,
53
- viewportWidth: viewportWidth,
54
- viewportHeight: viewportHeight,
55
- height: height
56
- });
57
- }
58
-
59
- function processWindowSize(top = !0) {
60
- var _a, _b, _c;
61
- const viewport = null !== (_a = globalThis.top) && void 0 !== _a ? _a : globalThis, touch = "ontouchstart" in globalThis || navigator.maxTouchPoints > 0, windowWidth = null !== (_b = null == viewport ? void 0 : viewport.innerWidth) && void 0 !== _b ? _b : -1, windowHeight = null !== (_c = null == viewport ? void 0 : viewport.innerHeight) && void 0 !== _c ? _c : -1;
62
- return {
63
- formFactor: getFormFactor(windowWidth),
64
- viewportWidth: windowWidth,
65
- viewportHeight: windowHeight,
66
- height: viewport && touch ? `${windowHeight}px` : "100vh"
67
- };
68
- }
69
-
70
- exports.useWindowStore = useWindowStore;
@@ -1,71 +0,0 @@
1
- import { create } from "zustand";
2
-
3
- /**
4
- * Form factor names
5
- */ var FormFactor;
6
-
7
- !function(FormFactor) {
8
- FormFactor.Mobile = "mobile", FormFactor.Tablet = "tablet", FormFactor.Desktop = "desktop",
9
- FormFactor.DesktopL = "desktopL", FormFactor.DesktopXL = "desktopXL";
10
- }(FormFactor || (FormFactor = {}));
11
-
12
- /**
13
- * Use bootstrap breakpoints
14
- */
15
- const bootstrapBreakPoints = {
16
- [FormFactor.Mobile]: 576,
17
- [FormFactor.Tablet]: 768,
18
- [FormFactor.Desktop]: 992,
19
- [FormFactor.DesktopL]: 1200,
20
- [FormFactor.DesktopXL]: 1400
21
- }, orderedBps = Object.entries(bootstrapBreakPoints).sort((a, b) => -b[1] - a[1]), getFormFactor = width => {
22
- if (!width || width <= 0) return FormFactor.Mobile;
23
- for (const [formFactor, breakpoint] of orderedBps) if (width >= breakpoint) return formFactor;
24
- return FormFactor.Mobile;
25
- }, initialSize = processWindowSize(), useWindowStore = create((set, get) => {
26
- var _a;
27
- return {
28
- formFactor: initialSize.formFactor,
29
- viewportWidth: initialSize.viewportWidth,
30
- viewportHeight: initialSize.viewportHeight,
31
- height: initialSize.height,
32
- isElectron: checkElectron(),
33
- isAppleDevice: navigator.platform.startsWith("Mac") || "iPhone" === navigator.platform,
34
- isTouchDevice: "ontouchstart" in globalThis || navigator.maxTouchPoints > 0,
35
- dpr: null !== (_a = Math.min(Math.ceil(window.devicePixelRatio), 3)) && void 0 !== _a ? _a : 1,
36
- actions: {
37
- initialize: () => {
38
- var _a;
39
- const win = null !== (_a = globalThis.top) && void 0 !== _a ? _a : globalThis;
40
- return null == win || win.addEventListener("resize", handleResize), () => null == win ? void 0 : win.removeEventListener("resize", handleResize);
41
- }
42
- }
43
- };
44
- });
45
-
46
- function checkElectron() {
47
- return navigator.userAgent.toLowerCase().includes("electron/");
48
- }
49
-
50
- function handleResize() {
51
- const {formFactor: formFactor, viewportWidth: viewportWidth, viewportHeight: viewportHeight, height: height} = processWindowSize();
52
- useWindowStore.setState({
53
- formFactor: formFactor,
54
- viewportWidth: viewportWidth,
55
- viewportHeight: viewportHeight,
56
- height: height
57
- });
58
- }
59
-
60
- function processWindowSize(top = !0) {
61
- var _a, _b, _c;
62
- const viewport = null !== (_a = globalThis.top) && void 0 !== _a ? _a : globalThis, touch = "ontouchstart" in globalThis || navigator.maxTouchPoints > 0, windowWidth = null !== (_b = null == viewport ? void 0 : viewport.innerWidth) && void 0 !== _b ? _b : -1, windowHeight = null !== (_c = null == viewport ? void 0 : viewport.innerHeight) && void 0 !== _c ? _c : -1;
63
- return {
64
- formFactor: getFormFactor(windowWidth),
65
- viewportWidth: windowWidth,
66
- viewportHeight: windowHeight,
67
- height: viewport && touch ? `${windowHeight}px` : "100vh"
68
- };
69
- }
70
-
71
- export { FormFactor as F, useWindowStore as u };