Skip to content

Commit

Permalink
chore: added onXXX handlers. added retry event.
Browse files Browse the repository at this point in the history
  • Loading branch information
yusinto committed Nov 21, 2023
1 parent 9653202 commit cb4ed6e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/sdk/react-native/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type {
Crypto,
Encoding,
EventName,
EventSource,
EventSourceInitDict,
Hasher,
Expand All @@ -13,8 +14,7 @@ import type {
Requests,
Response,
SdkData,
} from '@launchdarkly/js-sdk-common';
import { EventName } from '@launchdarkly/js-sdk-common';
} from '@launchdarkly/js-client-sdk-common';

import { name, version } from '../package.json';
import { btoa, EventSource as RNEventSource, uuidv4 } from './polyfills';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class EventSource<E extends string = never> {
private pollAgain(time: number, allowZero: boolean) {
if (time > 0 || allowZero) {
this.logDebug(`[EventSource] Will open new connection in ${time} ms.`);
this.dispatch('retry', { type: 'retry' });
this.pollTimer = setTimeout(() => {
this.open();
}, time);
Expand Down Expand Up @@ -271,13 +272,24 @@ export default class EventSource<E extends string = never> {
}

dispatch<T extends EventType<E>>(type: T, data: EventSourceEvent<T>) {
const availableTypes = Object.keys(this.eventHandlers);

if (!availableTypes.includes(type)) {
return;
this.eventHandlers[type]?.forEach((handler: EventSourceListener<E, T>) => handler(data));

switch (type) {
case 'open':
this.onopen();
break;
case 'close':
this.onclose();
break;
case 'error':
this.onerror();
break;
case 'retry':
this.onretrying();
break;
default:
break;
}

this.eventHandlers[type].forEach((handler: EventSourceListener<E, T>) => handler(data));
}

close() {
Expand All @@ -289,4 +301,9 @@ export default class EventSource<E extends string = never> {

this.dispatch('close', { type: 'close' });
}

onopen() {}
onclose() {}
onerror() {}
onretrying() {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type BuiltInEventType = 'open' | 'message' | 'error' | 'close';
export type BuiltInEventType = 'open' | 'message' | 'error' | 'close' | 'retry';
export type EventType<E extends string = never> = E | BuiltInEventType;

export interface MessageEvent {
Expand All @@ -16,6 +16,10 @@ export interface CloseEvent {
type: 'close';
}

export interface RetryEvent {
type: 'retry';
}

export interface TimeoutEvent {
type: 'timeout';
}
Expand Down Expand Up @@ -56,11 +60,13 @@ type BuiltInEventMap = {
open: OpenEvent;
close: CloseEvent;
error: ErrorEvent | TimeoutEvent | ExceptionEvent;
retry: RetryEvent;
};

export type EventSourceEvent<E extends T, T extends string = any> = E extends BuiltInEventType
? BuiltInEventMap[E]
: CustomEvent<E>;

export type EventSourceListener<E extends string = never, T extends EventType<E> = EventType<E>> = (
event: EventSourceEvent<T>,
) => void;

0 comments on commit cb4ed6e

Please sign in to comment.