Skip to content

Commit a3e5f79

Browse files
author
Anatoly Ostrovsky
committed
Type improvements. Refactor SSE to stream
1 parent 8afd4b1 commit a3e5f79

File tree

40 files changed

+596
-725
lines changed

40 files changed

+596
-725
lines changed

@types/core/compile/attributes.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Attributes {
4242
*/
4343
$attr: {};
4444
/** @type {import("../../shared/noderef.js").NodeRef} */
45-
$nodeRef: import("../../shared/noderef.js").NodeRef;
45+
_nodeRef: import("../../shared/noderef.js").NodeRef;
4646
/** @type {Node|Element} */
4747
get $$element(): Node | Element;
4848
/**

@types/directive/form/form.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class FormController {
8787
$animate: ng.AnimateService,
8888
$interpolate: ng.InterpolateService,
8989
);
90-
$$controls: any[];
90+
_controls: any[];
9191
$name: string;
9292
/**
9393
* @property {boolean} $dirty True if user has already interacted with the form.

@types/directive/input/input.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ export function hiddenInputBrowserCacheDirective(): ng.Directive;
6464
* @returns {ng.Directive}
6565
*/
6666
export function ngValueDirective(): ng.Directive;
67+
/**
68+
* @param {Date} date
69+
* @param {any} timezone
70+
* @param {undefined} [reverse]
71+
*/
72+
export function convertTimezoneToLocal(
73+
date: Date,
74+
timezone: any,
75+
reverse?: undefined,
76+
): Date;
77+
/**
78+
* @param {any} timezone
79+
* @param {number} [fallback]
80+
* @returns {number}
81+
*/
82+
export function timezoneToOffset(timezone: any, fallback?: number): number;
6783
export const ISO_DATE_REGEXP: RegExp;
6884
export const URL_REGEXP: RegExp;
6985
export const EMAIL_REGEXP: RegExp;

@types/directive/model/model.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ export class NgModelController {
125125
};
126126
$$updateEvents: string;
127127
$$updateEventHandler(ev: any): void;
128-
$$parsedNgModel: import("../../core/parse/interface.ts").CompiledExpression;
129-
$$parsedNgModelAssign: (context: any, value: any) => any;
128+
_parsedNgModel: import("../../core/parse/interface.ts").CompiledExpression;
129+
_parsedNgModelAssign: (context: any, value: any) => any;
130130
/**
131131
* @type {import("../../core/parse/interface.ts").CompiledExpression |
132132
* (function(ng.Scope): any)}
133133
*/
134-
$$ngModelGet:
134+
_ngModelGet:
135135
| import("../../core/parse/interface.ts").CompiledExpression
136136
| ((arg0: ng.Scope) => any);
137-
$$ngModelSet: (context: any, value: any) => any;
137+
_ngModelSet: (context: any, value: any) => any;
138138
$$pendingDebounce: number;
139139
$$parserValid: boolean;
140140
/** @type {string} */

@types/services/location/location.d.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ export class Location {
182182
* Current url
183183
* @type {string}
184184
*/
185-
$$url: string;
185+
_url: string;
186186
/**
187187
* @ignore
188188
* Callback to update browser url
189-
* @type {Function}
189+
* @type {Function | undefined}
190190
*/
191-
$$updateBrowser: Function;
191+
_updateBrowser: Function | undefined;
192192
/**
193193
* Change path, search and hash, when called with parameter and return `$location`.
194194
*
@@ -249,7 +249,7 @@ export class Location {
249249
* @private
250250
* Compose url and update `url` and `absUrl` property
251251
*/
252-
private $$compose;
252+
private _compose;
253253
/**
254254
* Change the history state object when called with one parameter and return `$location`.
255255
* The state object is later passed to `pushState` or `replaceState`.
@@ -285,15 +285,16 @@ export class LocationProvider {
285285
hashPrefixConf: string;
286286
/** @type {import("./interface.ts").Html5Mode} */
287287
html5ModeConf: import("./interface.ts").Html5Mode;
288-
/** @type {Array<import("./interface.ts").UrlChangeListener>} */
289-
urlChangeListeners: Array<import("./interface.ts").UrlChangeListener>;
290-
urlChangeInit: boolean;
291-
/** @type {History['state']} */
292-
cachedState: History["state"];
293-
/** @type {History['state']} */
294-
lastHistoryState: History["state"];
295-
/** @type {string} */
296-
lastBrowserUrl: string;
288+
/** @private @type {Array<import("./interface.ts").UrlChangeListener>} */
289+
private _urlChangeListeners;
290+
/** @private */
291+
private _urlChangeInit;
292+
/** @private @type {History['state']} */
293+
private _cachedState;
294+
/** @private @type {History['state']} */
295+
private _lastHistoryState;
296+
/** @private @type {string} */
297+
private _lastBrowserUrl;
297298
/**
298299
* Updates the browser's current URL and history state.
299300
*

@types/services/sse/interface.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ export interface SseConfig {
3232
export interface SseConnection {
3333
/** Manually close the SSE connection and stop all reconnect attempts */
3434
close(): void;
35-
/** Manually restart the SSE connection */
35+
/**
36+
* Manually restart the SSE connection.
37+
* @remarks
38+
* Any previous event listeners are preserved; reconnects use the original configuration.
39+
*/
3640
connect(): void;
3741
}
3842
/**
3943
* $sse service type
4044
* Returns a managed SSE connection that automatically reconnects when needed.
45+
* @param url - The endpoint to connect to
46+
* @param config - Optional configuration object
47+
* @throws {URIError} If the URL is invalid
4148
*/
4249
export type SseService = (url: string, config?: SseConfig) => SseConnection;

@types/services/sse/sse.d.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
/**
2-
* SSE Provider
3-
*
4-
* Usage:
5-
* const source = $sse('/events', {
6-
* onMessage: (data) => console.log(data),
7-
* onError: (err) => console.error(err),
8-
* retryDelay: 2000,
9-
* heartbeatTimeout: 10000,
10-
* });
11-
*
12-
* source.close();
13-
*/
141
export class SseProvider {
15-
/**
16-
* Optional provider-level defaults
17-
* @type {ng.SseConfig}
18-
*/
2+
/** @type {ng.SseConfig} */
193
defaults: ng.SseConfig;
20-
$get: (string | ((log: ng.LogService) => ng.SseService))[];
4+
$get: (
5+
| string
6+
| ((
7+
log: ng.LogService,
8+
) => (
9+
url: string,
10+
config?: import("./interface.ts").SseConfig,
11+
) => import("./interface.ts").SseConnection)
12+
)[];
2113
_$log: import("../log/interface.ts").LogService;
2214
#private;
2315
}

@types/services/stream/interface.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface StreamConnectionConfig {
22
/** Called when the connection opens */
33
onOpen?: (event: Event) => void;
44
/** Called when a message is received */
5-
onMessage?: (data: any, event: Event) => void;
5+
onMessage?: (data: any, event: Event | MessageEvent) => void;
66
/** Called when an error occurs */
77
onError?: (err: any) => void;
88
/** Called when a reconnect attempt happens */

@types/services/stream/stream.d.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Shared Stream Connection Manager
3+
* Handles reconnect, heartbeat, and event callbacks for SSE or WebSocket
4+
*/
5+
export class StreamConnection {
6+
/**
7+
* @param {() => EventSource | WebSocket} createFn - Function that creates a new EventSource or WebSocket.
8+
* @param {ng.StreamConnectionConfig} config - Configuration object with callbacks, retries, heartbeat, transformMessage.
9+
* @param {ng.LogService} log - Optional logger (default: console).
10+
*/
11+
constructor(
12+
createFn: () => EventSource | WebSocket,
13+
config?: ng.StreamConnectionConfig,
14+
log?: ng.LogService,
15+
);
16+
createFn: () => EventSource | WebSocket;
17+
config: {
18+
onOpen?: (event: Event) => void;
19+
onMessage?: (data: any, event: Event | MessageEvent) => void;
20+
onError?: (err: any) => void;
21+
onReconnect?: (attempt: number) => void;
22+
retryDelay: number;
23+
maxRetries: number;
24+
heartbeatTimeout: number;
25+
transformMessage: (data: string) => any;
26+
};
27+
$log: import("../log/interface.ts").LogService;
28+
retryCount: number;
29+
closed: boolean;
30+
heartbeatTimer: number;
31+
/** @type {EventSource | WebSocket | null} */
32+
connection: EventSource | WebSocket | null;
33+
/**
34+
* Establishes a new connection using the provided createFn.
35+
* Closes any existing connection before creating a new one.
36+
*/
37+
connect(): void;
38+
/**
39+
* Binds event handlers to the underlying connection (EventSource or WebSocket)
40+
* for open, message, error, and close events.
41+
*/
42+
bindEvents(): void;
43+
/**
44+
* Handles the open event from the connection.
45+
* @param {Event} event - The open event.
46+
*/
47+
handleOpen(event: Event): void;
48+
/**
49+
* Handles incoming messages, applies the transformMessage function,
50+
* and calls the onMessage callback.
51+
* @param {any} data - Raw message data.
52+
* @param {Event} event - The message event.
53+
*/
54+
handleMessage(data: any, event: Event): void;
55+
/**
56+
* Handles errors emitted from the connection.
57+
* Calls onError callback and schedules a reconnect.
58+
* @param {any} err - Error object or message.
59+
*/
60+
handleError(err: any): void;
61+
/**
62+
* Handles close events for WebSocket connections.
63+
* Triggers reconnect logic.
64+
*/
65+
handleClose(): void;
66+
/**
67+
* Schedules a reconnect attempt based on retryCount and config.maxRetries.
68+
* Calls onReconnect callback if reconnecting.
69+
*/
70+
scheduleReconnect(): void;
71+
/**
72+
* Resets the heartbeat timer. If the timer expires, the connection is closed
73+
* and a reconnect is attempted.
74+
*/
75+
resetHeartbeat(): void;
76+
/**
77+
* Sends data over a WebSocket connection.
78+
* Logs a warning if called on a non-WebSocket connection.
79+
* @param {any} data - Data to send.
80+
*/
81+
send(data: any): void;
82+
/**
83+
* Closes the connection manually and clears the heartbeat timer.
84+
*/
85+
close(): void;
86+
_closeInternal(): void;
87+
}

@types/shared/common.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ export function applyPairs(memo: any, keyValTuple: any): any;
113113
/**
114114
* Returns the last element of an array, or undefined if the array is empty.
115115
* @template T
116-
* @param {T[]} arr - The input array.
116+
* @param {string} arr - The input array.
117117
* @returns {T | undefined} The last element or undefined.
118118
*/
119-
export function tail<T>(arr: T[]): T | undefined;
119+
export function tail<T>(arr: string): T | undefined;
120120
/**
121121
* shallow copy from src to dest
122122
*/

0 commit comments

Comments
 (0)