@zum/flash-client
Advanced tools
Comparing version 0.0.11 to 0.0.12
import { Socket } from 'socket.io-client'; | ||
import { FlashEvent } from './types/flashEvent'; | ||
import { FlashOperationArgument } from './types/flashOperation'; | ||
import { FlashConfig } from './types/initializerTypes'; | ||
import { FlashConfig, FlashEvent, FlashOperationArgument } from './types'; | ||
export declare class FlashSocket { | ||
private config; | ||
private static socket; | ||
private readonly socket; | ||
constructor(config: FlashConfig); | ||
/** | ||
* The socket always do auto connection. But in the | ||
* case when we manual disconnect eg: logout, | ||
* we must connect it again manually. | ||
* In the case when we manually disconnect (e.g. logout), we must connect again manually. | ||
*/ | ||
connect: () => Promise<void>; | ||
getSocket: () => Socket; | ||
private connect; | ||
/** | ||
* Invoked both on connect and reconnection case | ||
* @param connectionHandler | ||
* Invoked on both connect and reconnect cases | ||
*/ | ||
notifyOnConnect: (connectionHandler: (socket: Socket) => void) => void; | ||
onConnect: (connectionHandler: (socket: Socket) => void) => void; | ||
onFlashEvent: (flashEventHandler: (event: FlashEvent) => void) => void; | ||
execute: (operationArgument: FlashOperationArgument) => Promise<void>; | ||
disconnect: () => void; | ||
private initializeSocket; | ||
private newConnection; | ||
private setAuthorizationHeader; | ||
@@ -26,0 +20,0 @@ private reConnectOnError; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const socket_io_client_1 = require("socket.io-client"); | ||
const flashOperation_1 = require("./types/flashOperation"); | ||
const types_1 = require("./types"); | ||
const RECONNECT_ON_ERROR_TIMEOUT_MILLIS = 1000; | ||
class FlashSocket { | ||
@@ -9,41 +10,43 @@ constructor(config) { | ||
/** | ||
* The socket always do auto connection. But in the | ||
* case when we manual disconnect eg: logout, | ||
* we must connect it again manually. | ||
* In the case when we manually disconnect (e.g. logout), we must connect again manually. | ||
*/ | ||
this.connect = async () => { | ||
if (!FlashSocket.socket.connected) { | ||
FlashSocket.socket.connect(); | ||
// TODO(Sreejith): this is probably not needed if we have only "connect" and "disconnect" public package methods | ||
// Check if it is already connected since the connection is | ||
// automatically made when it is initialized for the first time. | ||
// This can be added to login lifecycle, which happens if the socket is | ||
// already initialized or not. | ||
if (!this.socket.connected) { | ||
this.socket.connect(); | ||
await new Promise((resolve, reject) => { | ||
FlashSocket.socket.on('connect', resolve); | ||
FlashSocket.socket.on('disconnect', reject); | ||
this.socket.on('connect', resolve); | ||
this.socket.on('disconnect', reject); | ||
}); | ||
} | ||
}; | ||
this.getSocket = () => { | ||
return FlashSocket.socket; | ||
}; | ||
/** | ||
* Invoked both on connect and reconnection case | ||
* @param connectionHandler | ||
* Invoked on both connect and reconnect cases | ||
*/ | ||
this.notifyOnConnect = (connectionHandler) => { | ||
const socket = this.getSocket(); | ||
socket.on('connect', () => { | ||
connectionHandler(socket); | ||
this.onConnect = (connectionHandler) => { | ||
this.socket.on('connect', () => { | ||
connectionHandler(this.socket); | ||
}); | ||
}; | ||
this.onFlashEvent = (flashEventHandler) => { | ||
this.getSocket().on(flashOperation_1.FlashOperation.MESSAGE, flashEventHandler); | ||
this.socket.on(types_1.FlashOperation.MESSAGE, flashEventHandler); | ||
}; | ||
this.execute = async (operationArgument) => { | ||
return new Promise((resolve, reject) => { | ||
this.getSocket().emit(operationArgument.operation, operationArgument.args, (response) => { | ||
if (response.status === flashOperation_1.FlashOperationResponseEnum.SUCCESS) { | ||
resolve(); | ||
this.socket.emit(operationArgument.operation, operationArgument.args, (response) => { | ||
switch (response.status) { | ||
case types_1.FlashOperationResponseEnum.SUCCESS: | ||
resolve(); | ||
break; | ||
case types_1.FlashOperationResponseEnum.ERROR: | ||
case types_1.FlashOperationResponseEnum.UNAUTHORIZED: | ||
reject(response); | ||
break; | ||
default: | ||
throw Error(`Unsupported status received`); | ||
} | ||
if (response.status === flashOperation_1.FlashOperationResponseEnum.ERROR || | ||
response.status === flashOperation_1.FlashOperationResponseEnum.UNAUTHORIZED) { | ||
reject(response); | ||
} | ||
}); | ||
@@ -53,5 +56,5 @@ }); | ||
this.disconnect = () => { | ||
FlashSocket.socket.disconnect(); | ||
this.socket.disconnect(); | ||
}; | ||
this.initializeSocket = () => { | ||
this.newConnection = () => { | ||
const opts = { | ||
@@ -62,32 +65,31 @@ path: '/flash', | ||
}; | ||
const socket = socket_io_client_1.io(this.config.baseUrl, opts); | ||
this.reConnectOnError(socket); | ||
return socket; | ||
return socket_io_client_1.io(this.config.baseUrl, opts); | ||
}; | ||
this.setAuthorizationHeader = () => { | ||
return async (callback) => { | ||
let accessToken; | ||
try { | ||
const accessToken = await this.config.accessTokenIssuer(); | ||
callback({ | ||
role: this.config.appRole, | ||
access_token: accessToken, | ||
}); | ||
accessToken = await this.config.accessTokenIssuer(); | ||
} | ||
catch (e) { | ||
//Unable to get the auth token, possibly logged out | ||
console.error(`[Flash] Unable to get the access token, possibly forgot to disconnect on logout`); | ||
this.disconnect(); | ||
throw e; // TODO(Sreejith): is it safe to throw or better return w/o a callback? | ||
} | ||
callback({ | ||
role: this.config.appRole, | ||
access_token: accessToken, | ||
}); | ||
}; | ||
}; | ||
this.reConnectOnError = (socket) => { | ||
socket.on('connect_error', () => () => { | ||
console.log(`Flash Connect Error : ${Date.now()} `); | ||
this.reConnectOnError = () => { | ||
this.socket.on('connect_error', (error) => () => { | ||
console.error(`[Flash] Failed to connect the socket`, error); | ||
setTimeout(() => { | ||
socket.connect(); | ||
}, 1000); | ||
this.socket.connect(); | ||
}, RECONNECT_ON_ERROR_TIMEOUT_MILLIS); | ||
}); | ||
}; | ||
if (!FlashSocket.socket) { | ||
FlashSocket.socket = this.initializeSocket(); | ||
} | ||
this.socket = this.newConnection(); | ||
this.reConnectOnError(); | ||
} | ||
@@ -94,0 +96,0 @@ } |
import { Observable } from 'rxjs'; | ||
import { FlashSocket } from './flashSocket'; | ||
import { FlashEvent } from './types/flashEvent'; | ||
import { FlashEvent } from './types'; | ||
export declare class SubscriptionManager { | ||
private flashSocket; | ||
private subscriptions; | ||
private readonly subscriptions; | ||
constructor(flashSocket: FlashSocket); | ||
subscribe: (topic: string, entityIds: string[]) => Observable<FlashEvent>; | ||
unsubscribeTopic: (topic: string) => Promise<void>; | ||
unsubscribeAll: () => void; | ||
disconnect: () => void; | ||
private resubscribe; | ||
@@ -12,0 +12,0 @@ private socketSubscribe; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const rxjs_1 = require("rxjs"); | ||
const flashOperation_1 = require("./types/flashOperation"); | ||
const types_1 = require("./types"); | ||
class SubscriptionManager { | ||
@@ -13,9 +13,2 @@ constructor(flashSocket) { | ||
void this.socketSubscribe(topic, entityIds, subscriber); | ||
//TODO verify the tear down handler | ||
return () => { | ||
const subscription = this.subscriptions.get(topic); | ||
if ((subscription === null || subscription === void 0 ? void 0 : subscription.entityIds.sort().toString()) === entityIds.sort().toString()) { | ||
void this.unsubscribeTopic(topic); | ||
} | ||
}; | ||
}); | ||
@@ -26,3 +19,3 @@ }; | ||
await this.flashSocket.execute({ | ||
operation: flashOperation_1.FlashOperation.UNSUBSCRIBE_ALL, | ||
operation: types_1.FlashOperation.UNSUBSCRIBE_ALL, | ||
args: { topic }, | ||
@@ -32,3 +25,3 @@ }); | ||
}; | ||
this.unsubscribeAll = () => { | ||
this.disconnect = () => { | ||
this.flashSocket.disconnect(); | ||
@@ -45,6 +38,6 @@ this.subscriptions.clear(); | ||
await this.flashSocket.execute({ | ||
operation: flashOperation_1.FlashOperation.SUBSCRIBE, | ||
operation: types_1.FlashOperation.SUBSCRIBE, | ||
args: { topic, entityIds }, | ||
}); | ||
// Fake event to mark prevent event loss during subscription | ||
// Fake event to prevent event loss during subscription | ||
const subscription = this.subscriptions.get(topic); | ||
@@ -57,2 +50,3 @@ if (subscription) { | ||
entityId, | ||
timestamp: Date.now(), | ||
}); | ||
@@ -67,3 +61,3 @@ }); | ||
}; | ||
flashSocket.notifyOnConnect(() => { | ||
flashSocket.onConnect(() => { | ||
void this.resubscribe(); | ||
@@ -77,3 +71,3 @@ }); | ||
else { | ||
console.error(`Got non subscribed event:${event.topic} ${event.entityId}`); | ||
console.error(`[Flash] Received an event w/o a subscription`, event); | ||
} | ||
@@ -80,0 +74,0 @@ }); |
@@ -6,4 +6,4 @@ export declare type Topic = string; | ||
entityId: EntityId; | ||
vStamp?: number; | ||
timestamp: number; | ||
}; | ||
//# sourceMappingURL=flashEvent.d.ts.map |
import { EntityId, Topic } from './flashEvent'; | ||
export declare enum FlashOperation { | ||
SUBSCRIBE = "SUBSCRIBE", | ||
UNSUBSCRIBE = "UNSUBSCRIBE", | ||
UNSUBSCRIBE_ALL = "UNSUBSCRIBE_ALL", | ||
@@ -15,8 +14,2 @@ MESSAGE = "MESSAGE" | ||
} | { | ||
operation: FlashOperation.UNSUBSCRIBE; | ||
args: { | ||
topic: Topic; | ||
entityIds: EntityId[]; | ||
}; | ||
} | { | ||
operation: FlashOperation.UNSUBSCRIBE_ALL; | ||
@@ -23,0 +16,0 @@ args: { |
@@ -6,3 +6,2 @@ "use strict"; | ||
FlashOperation["SUBSCRIBE"] = "SUBSCRIBE"; | ||
FlashOperation["UNSUBSCRIBE"] = "UNSUBSCRIBE"; | ||
FlashOperation["UNSUBSCRIBE_ALL"] = "UNSUBSCRIBE_ALL"; | ||
@@ -9,0 +8,0 @@ FlashOperation["MESSAGE"] = "MESSAGE"; |
@@ -0,4 +1,4 @@ | ||
export * from './flashConfig'; | ||
export * from './flashEvent'; | ||
export * from './flashOperation'; | ||
export * from './initializerTypes'; | ||
//# sourceMappingURL=index.d.ts.map |
export * from './connect/types'; | ||
export * from './sdk/api/ride'; | ||
export { disconnectFlash, initializeFlash } from './sdk/init/initializer'; | ||
export { connect, disconnect } from './sdk/init/initializer'; | ||
export { FlashTopic } from './sdk/topic/topicTypes'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -9,6 +9,6 @@ "use strict"; | ||
var initializer_1 = require("./sdk/init/initializer"); | ||
exports.disconnectFlash = initializer_1.disconnectFlash; | ||
exports.initializeFlash = initializer_1.initializeFlash; | ||
exports.connect = initializer_1.connect; | ||
exports.disconnect = initializer_1.disconnect; | ||
var topicTypes_1 = require("./sdk/topic/topicTypes"); | ||
exports.FlashTopic = topicTypes_1.FlashTopic; | ||
//# sourceMappingURL=index.js.map |
@@ -15,2 +15,3 @@ "use strict"; | ||
}; | ||
// TODO(Sreejith): how to prevent a race condition b/w subscribe and unsubscribe? | ||
exports.unsubscribeToRidesPrimary = () => unsubscribe(topicTypes_1.FlashTopic.RIDE_LIST_PRIMARY); | ||
@@ -17,0 +18,0 @@ exports.subscribeToRidesSecondary = (rideIds, getRideImpl) => { |
import { SubscriptionManager } from '../../connect/subscriptionManager'; | ||
import { FlashConfig } from '../../connect/types/initializerTypes'; | ||
import { FlashConfig } from '../../connect/types'; | ||
export declare const getSubscriptionManager: () => SubscriptionManager; | ||
export declare const initializeFlash: (config: FlashConfig) => void; | ||
export declare const disconnectFlash: () => void; | ||
export declare const connect: (config: FlashConfig) => void; | ||
export declare const disconnect: () => void; | ||
//# sourceMappingURL=initializer.d.ts.map |
@@ -6,22 +6,16 @@ "use strict"; | ||
let subscriptionManager; | ||
let flashSocket; | ||
exports.getSubscriptionManager = () => { | ||
if (!subscriptionManager) { | ||
if (flashSocket) { | ||
subscriptionManager = new subscriptionManager_1.SubscriptionManager(flashSocket); | ||
} | ||
else { | ||
throw new Error('Flash socket must be initialized before accessing'); | ||
} | ||
throw new Error('Flash client must be initialized first'); | ||
} | ||
return subscriptionManager; | ||
}; | ||
exports.initializeFlash = (config) => { | ||
if (!flashSocket) { | ||
flashSocket = new flashSocket_1.FlashSocket(config); | ||
} | ||
exports.connect = (config) => { | ||
exports.disconnect(); // TODO(Sreejith): please check if this allows for clients to re-initialize cleanly if needed | ||
const flashSocket = new flashSocket_1.FlashSocket(config); | ||
subscriptionManager = new subscriptionManager_1.SubscriptionManager(flashSocket); | ||
}; | ||
exports.disconnectFlash = () => { | ||
subscriptionManager === null || subscriptionManager === void 0 ? void 0 : subscriptionManager.unsubscribeAll(); | ||
exports.disconnect = () => { | ||
subscriptionManager === null || subscriptionManager === void 0 ? void 0 : subscriptionManager.disconnect(); | ||
}; | ||
//# sourceMappingURL=initializer.js.map |
import { io as socketIO, Socket } from 'socket.io-client'; | ||
import { FlashEvent } from './types/flashEvent'; | ||
import { | ||
FlashConfig, | ||
FlashEvent, | ||
FlashOperation, | ||
@@ -8,25 +9,28 @@ FlashOperationArgument, | ||
FlashOperationResponseEnum, | ||
} from './types/flashOperation'; | ||
import { FlashConfig } from './types/initializerTypes'; | ||
} from './types'; | ||
const RECONNECT_ON_ERROR_TIMEOUT_MILLIS = 1000; | ||
export class FlashSocket { | ||
private static socket: Socket; | ||
private readonly socket: Socket; | ||
constructor(private config: FlashConfig) { | ||
if (!FlashSocket.socket) { | ||
FlashSocket.socket = this.initializeSocket(); | ||
} | ||
this.socket = this.newConnection(); | ||
this.reConnectOnError(); | ||
} | ||
/** | ||
* The socket always do auto connection. But in the | ||
* case when we manual disconnect eg: logout, | ||
* we must connect it again manually. | ||
* In the case when we manually disconnect (e.g. logout), we must connect again manually. | ||
*/ | ||
public connect = async (): Promise<void> => { | ||
if (!FlashSocket.socket.connected) { | ||
FlashSocket.socket.connect(); | ||
private connect = async (): Promise<void> => { | ||
// TODO(Sreejith): this is probably not needed if we have only "connect" and "disconnect" public package methods | ||
// Check if it is already connected since the connection is | ||
// automatically made when it is initialized for the first time. | ||
// This can be added to login lifecycle, which happens if the socket is | ||
// already initialized or not. | ||
if (!this.socket.connected) { | ||
this.socket.connect(); | ||
await new Promise<void>((resolve, reject) => { | ||
FlashSocket.socket.on('connect', resolve); | ||
FlashSocket.socket.on('disconnect', reject); | ||
this.socket.on('connect', resolve); | ||
this.socket.on('disconnect', reject); | ||
}); | ||
@@ -36,14 +40,8 @@ } | ||
public getSocket = (): Socket => { | ||
return FlashSocket.socket; | ||
}; | ||
/** | ||
* Invoked both on connect and reconnection case | ||
* @param connectionHandler | ||
* Invoked on both connect and reconnect cases | ||
*/ | ||
public notifyOnConnect = (connectionHandler: (socket: Socket) => void): void => { | ||
const socket = this.getSocket(); | ||
socket.on('connect', () => { | ||
connectionHandler(socket); | ||
public onConnect = (connectionHandler: (socket: Socket) => void): void => { | ||
this.socket.on('connect', () => { | ||
connectionHandler(this.socket); | ||
}); | ||
@@ -53,3 +51,3 @@ }; | ||
public onFlashEvent = (flashEventHandler: (event: FlashEvent) => void): void => { | ||
this.getSocket().on(FlashOperation.MESSAGE, flashEventHandler); | ||
this.socket.on(FlashOperation.MESSAGE, flashEventHandler); | ||
}; | ||
@@ -59,12 +57,14 @@ | ||
return new Promise((resolve, reject) => { | ||
this.getSocket().emit(operationArgument.operation, operationArgument.args, (response: FlashOperationResponse) => { | ||
if (response.status === FlashOperationResponseEnum.SUCCESS) { | ||
resolve(); | ||
this.socket.emit(operationArgument.operation, operationArgument.args, (response: FlashOperationResponse) => { | ||
switch (response.status) { | ||
case FlashOperationResponseEnum.SUCCESS: | ||
resolve(); | ||
break; | ||
case FlashOperationResponseEnum.ERROR: | ||
case FlashOperationResponseEnum.UNAUTHORIZED: | ||
reject(response); | ||
break; | ||
default: | ||
throw Error(`Unsupported status received`); | ||
} | ||
if ( | ||
response.status === FlashOperationResponseEnum.ERROR || | ||
response.status === FlashOperationResponseEnum.UNAUTHORIZED | ||
) { | ||
reject(response); | ||
} | ||
}); | ||
@@ -75,6 +75,6 @@ }); | ||
public disconnect = (): void => { | ||
FlashSocket.socket.disconnect(); | ||
this.socket.disconnect(); | ||
}; | ||
private initializeSocket = (): Socket => { | ||
private newConnection = (): Socket => { | ||
const opts = { | ||
@@ -85,5 +85,3 @@ path: '/flash', | ||
}; | ||
const socket = socketIO(this.config.baseUrl, opts); | ||
this.reConnectOnError(socket); | ||
return socket; | ||
return socketIO(this.config.baseUrl, opts); | ||
}; | ||
@@ -93,23 +91,25 @@ | ||
return async (callback: (data: Record<string, string>) => void) => { | ||
let accessToken; | ||
try { | ||
const accessToken = await this.config.accessTokenIssuer(); | ||
callback({ | ||
role: this.config.appRole, | ||
access_token: accessToken, | ||
}); | ||
accessToken = await this.config.accessTokenIssuer(); | ||
} catch (e) { | ||
//Unable to get the auth token, possibly logged out | ||
console.error(`[Flash] Unable to get the access token, possibly forgot to disconnect on logout`); | ||
this.disconnect(); | ||
throw e; // TODO(Sreejith): is it safe to throw or better return w/o a callback? | ||
} | ||
callback({ | ||
role: this.config.appRole, | ||
access_token: accessToken, | ||
}); | ||
}; | ||
}; | ||
private reConnectOnError = (socket: Socket): void => { | ||
socket.on('connect_error', () => () => { | ||
console.log(`Flash Connect Error : ${Date.now()} `); | ||
private reConnectOnError = (): void => { | ||
this.socket.on('connect_error', (error: any) => () => { | ||
console.error(`[Flash] Failed to connect the socket`, error); | ||
setTimeout(() => { | ||
socket.connect(); | ||
}, 1000); | ||
this.socket.connect(); | ||
}, RECONNECT_ON_ERROR_TIMEOUT_MILLIS); | ||
}); | ||
}; | ||
} |
import { Observable, Subscriber } from 'rxjs'; | ||
import { FlashSocket } from './flashSocket'; | ||
import { EntityId, FlashEvent, Topic } from './types/flashEvent'; | ||
import { FlashOperation } from './types/flashOperation'; | ||
import { EntityId, FlashEvent, FlashOperation, Topic } from './types'; | ||
export class SubscriptionManager { | ||
private subscriptions = new Map<string, { entityIds: string[]; subscriber: Subscriber<FlashEvent> }>(); | ||
private readonly subscriptions = new Map<Topic, { entityIds: EntityId[]; subscriber: Subscriber<FlashEvent> }>(); | ||
constructor(private flashSocket: FlashSocket) { | ||
flashSocket.notifyOnConnect(() => { | ||
flashSocket.onConnect(() => { | ||
void this.resubscribe(); | ||
@@ -18,3 +17,3 @@ }); | ||
} else { | ||
console.error(`Got non subscribed event:${event.topic} ${event.entityId}`); | ||
console.error(`[Flash] Received an event w/o a subscription`, event); | ||
} | ||
@@ -28,10 +27,2 @@ }); | ||
void this.socketSubscribe(topic, entityIds, subscriber); | ||
//TODO verify the tear down handler | ||
return () => { | ||
const subscription = this.subscriptions.get(topic); | ||
if (subscription?.entityIds.sort().toString() === entityIds.sort().toString()) { | ||
void this.unsubscribeTopic(topic); | ||
} | ||
}; | ||
}); | ||
@@ -48,3 +39,3 @@ }; | ||
public unsubscribeAll = (): void => { | ||
public disconnect = (): void => { | ||
this.flashSocket.disconnect(); | ||
@@ -71,3 +62,3 @@ this.subscriptions.clear(); | ||
// Fake event to mark prevent event loss during subscription | ||
// Fake event to prevent event loss during subscription | ||
const subscription = this.subscriptions.get(topic); | ||
@@ -80,2 +71,3 @@ if (subscription) { | ||
entityId, | ||
timestamp: Date.now(), | ||
}); | ||
@@ -82,0 +74,0 @@ }); |
@@ -7,3 +7,3 @@ export type Topic = string; | ||
entityId: EntityId; | ||
vStamp?: number; | ||
timestamp: number; | ||
}; |
@@ -5,3 +5,2 @@ import { EntityId, Topic } from './flashEvent'; | ||
SUBSCRIBE = 'SUBSCRIBE', | ||
UNSUBSCRIBE = 'UNSUBSCRIBE', | ||
UNSUBSCRIBE_ALL = 'UNSUBSCRIBE_ALL', | ||
@@ -16,6 +15,2 @@ MESSAGE = 'MESSAGE', | ||
| { | ||
operation: FlashOperation.UNSUBSCRIBE; | ||
args: { topic: Topic; entityIds: EntityId[] }; | ||
} | ||
| { | ||
operation: FlashOperation.UNSUBSCRIBE_ALL; | ||
@@ -22,0 +17,0 @@ args: { topic: Topic }; |
@@ -0,3 +1,3 @@ | ||
export * from './flashConfig'; | ||
export * from './flashEvent'; | ||
export * from './flashOperation'; | ||
export * from './initializerTypes'; |
export * from './connect/types'; | ||
export * from './sdk/api/ride'; | ||
export { disconnectFlash, initializeFlash } from './sdk/init/initializer'; | ||
export { connect, disconnect } from './sdk/init/initializer'; | ||
export { FlashTopic } from './sdk/topic/topicTypes'; |
@@ -26,3 +26,3 @@ import { asyncScheduler, merge, Observable } from 'rxjs'; | ||
}; | ||
// TODO(Sreejith): how to prevent a race condition b/w subscribe and unsubscribe? | ||
export const unsubscribeToRidesPrimary = (): Promise<void> => unsubscribe(FlashTopic.RIDE_LIST_PRIMARY); | ||
@@ -29,0 +29,0 @@ |
import { FlashSocket } from '../../connect/flashSocket'; | ||
import { SubscriptionManager } from '../../connect/subscriptionManager'; | ||
import { FlashConfig } from '../../connect/types/initializerTypes'; | ||
import { FlashConfig } from '../../connect/types'; | ||
let subscriptionManager: SubscriptionManager | undefined; | ||
let flashSocket: FlashSocket | undefined; | ||
export const getSubscriptionManager = (): SubscriptionManager => { | ||
if (!subscriptionManager) { | ||
if (flashSocket) { | ||
subscriptionManager = new SubscriptionManager(flashSocket); | ||
} else { | ||
throw new Error('Flash socket must be initialized before accessing'); | ||
} | ||
throw new Error('Flash client must be initialized first'); | ||
} | ||
@@ -19,10 +14,10 @@ return subscriptionManager; | ||
export const initializeFlash = (config: FlashConfig): void => { | ||
if (!flashSocket) { | ||
flashSocket = new FlashSocket(config); | ||
} | ||
export const connect = (config: FlashConfig): void => { | ||
disconnect(); // TODO(Sreejith): please check if this allows for clients to re-initialize cleanly if needed | ||
const flashSocket = new FlashSocket(config); | ||
subscriptionManager = new SubscriptionManager(flashSocket); | ||
}; | ||
export const disconnectFlash = (): void => { | ||
subscriptionManager?.unsubscribeAll(); | ||
export const disconnect = (): void => { | ||
subscriptionManager?.disconnect(); | ||
}; |
{ | ||
"name": "@zum/flash-client", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Client for connecting to Zum Flash server", | ||
@@ -5,0 +5,0 @@ "license": "UNLICENSED", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
584337
70
927