@ombori/grid-signals
Advanced tools
Comparing version 2.140.1 to 2.141.0
@@ -6,2 +6,19 @@ # Change Log | ||
# [2.141.0](https://github.com/ombori/gridapp/compare/v2.140.4...v2.141.0) (2021-12-20) | ||
### Bug Fixes | ||
* typos and proper grid-signals-react-init ([6474a27](https://github.com/ombori/gridapp/commit/6474a27f75bc3f1d4cbfe339e8b090a9e14524b0)) | ||
### Features | ||
* make cache expiration configurable ([192afc0](https://github.com/ombori/gridapp/commit/192afc03ac2c1cc5ce04b4561e71d3f6b4252530)) | ||
* session management and caching ([1edde84](https://github.com/ombori/gridapp/commit/1edde847e48b926e451798767ef1501214cc8423)) | ||
## [2.140.1](https://github.com/ombori/gridapp/compare/v2.140.0...v2.140.1) (2021-12-16) | ||
@@ -8,0 +25,0 @@ |
export declare const MESSAGE_PREFIX = "@ombori/grid-signals:"; | ||
export declare const EVENTS_CACHE_KEY = "_OMBORI_GRID_SIGNALS_EVENTS_CACHE"; | ||
export declare const CLIENT_CACHE_KEY = "_OMBORI_GRID_SIGNALS_CLIENT_CACHE"; | ||
export declare const SESSION_CACHE_KEY = "_OMBORI_GRID_SIGNALS_SESSION_CACHE"; | ||
export declare const LAST_ACTIVITY_CACHE_KEY = "_OMBORI_GRID_SIGNALS_LAST_ACTIVITY_CACHE"; | ||
export declare const CREATE_SESSION_ERROR_INIT_MESSAGE: string; | ||
@@ -8,1 +10,2 @@ export declare const TRACK_EVENT_ERROR_INIT_MESSAGE: string; | ||
export declare const INIT_NOT_CALLED: string; | ||
export declare const DEFAULT_MAX_SESSION_LAST_ACTIVITY_MINS = 30; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.INIT_NOT_CALLED = exports.INIT_ONCE_ERROR_MESSAGE = exports.TRACK_EVENT_ERROR_INIT_MESSAGE = exports.CREATE_SESSION_ERROR_INIT_MESSAGE = exports.CLIENT_CACHE_KEY = exports.EVENTS_CACHE_KEY = exports.MESSAGE_PREFIX = void 0; | ||
exports.DEFAULT_MAX_SESSION_LAST_ACTIVITY_MINS = exports.INIT_NOT_CALLED = exports.INIT_ONCE_ERROR_MESSAGE = exports.TRACK_EVENT_ERROR_INIT_MESSAGE = exports.CREATE_SESSION_ERROR_INIT_MESSAGE = exports.LAST_ACTIVITY_CACHE_KEY = exports.SESSION_CACHE_KEY = exports.CLIENT_CACHE_KEY = exports.EVENTS_CACHE_KEY = exports.MESSAGE_PREFIX = void 0; | ||
exports.MESSAGE_PREFIX = '@ombori/grid-signals:'; | ||
exports.EVENTS_CACHE_KEY = '_OMBORI_GRID_SIGNALS_EVENTS_CACHE'; | ||
exports.CLIENT_CACHE_KEY = '_OMBORI_GRID_SIGNALS_CLIENT_CACHE'; | ||
exports.SESSION_CACHE_KEY = '_OMBORI_GRID_SIGNALS_SESSION_CACHE'; | ||
exports.LAST_ACTIVITY_CACHE_KEY = '_OMBORI_GRID_SIGNALS_LAST_ACTIVITY_CACHE'; | ||
exports.CREATE_SESSION_ERROR_INIT_MESSAGE = `${exports.MESSAGE_PREFIX} init must be called before createSession`; | ||
@@ -11,1 +13,2 @@ exports.TRACK_EVENT_ERROR_INIT_MESSAGE = `${exports.MESSAGE_PREFIX} init must be called before trackEvent`; | ||
exports.INIT_NOT_CALLED = `${exports.MESSAGE_PREFIX} init not called!`; | ||
exports.DEFAULT_MAX_SESSION_LAST_ACTIVITY_MINS = 30; |
import { TrackEvent, InitProps, Instance, IdentityTypeEnum } from './types'; | ||
export default class Session { | ||
private instance; | ||
private maxSessionLastActivityMins; | ||
constructor(initParams?: InitProps); | ||
@@ -10,2 +11,4 @@ init: (initParams: InitProps) => Promise<any[] | undefined>; | ||
private trackEvent; | ||
private startSessionTracker; | ||
private updateLastActivity; | ||
/** | ||
@@ -12,0 +15,0 @@ * Custom event |
@@ -15,3 +15,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const uuid_1 = require("uuid"); | ||
const is_online_1 = require("./utils/is-online"); | ||
@@ -27,3 +26,3 @@ const is_browser_1 = __importDefault(require("./utils/is-browser")); | ||
const api_1 = require("./api/api"); | ||
const delay_1 = __importDefault(require("./utils/delay")); | ||
const session_1 = require("./utils/session"); | ||
let isOnline = false; | ||
@@ -56,2 +55,4 @@ is_online_1.isOnlineSubscription().subscribe((nextOnline) => { | ||
clientCreated: null, | ||
// Internal | ||
lastActivity: '', | ||
}; | ||
@@ -61,2 +62,3 @@ class Session { | ||
this.instance = defaultInstance; | ||
this.maxSessionLastActivityMins = constants_1.DEFAULT_MAX_SESSION_LAST_ACTIVITY_MINS; | ||
this.init = (initParams) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -74,11 +76,32 @@ if (this.instance.sessionId) { | ||
this.instance = Object.assign(Object.assign({}, this.instance), initParams); | ||
this.maxSessionLastActivityMins = | ||
typeof initParams.createNewSessionAfterLastActivityMins === 'number' | ||
? initParams.createNewSessionAfterLastActivityMins | ||
: this.maxSessionLastActivityMins; | ||
const clientInfo = get_client_id_1.default(this.instance); | ||
this.instance.clientId = clientInfo.clientId; | ||
this.instance.clientCreated = clientInfo.clientCreated; | ||
this.instance.sessionId = initParams.sessionId ? initParams.sessionId : uuid_1.v4(); | ||
this.instance.sessionCreated = get_date_string_1.default(); | ||
yield delay_1.default(1000); | ||
let sessionId = initParams.sessionId ? initParams.sessionId : ''; | ||
let sessionCreated = ''; | ||
if (sessionId) { | ||
sessionCreated = get_date_string_1.default(); | ||
session_1.cacheSession(sessionId, sessionCreated); | ||
} | ||
else { | ||
const generatedSession = session_1.generateSession({ | ||
maxSessionLastActivityMins: this.maxSessionLastActivityMins, | ||
forceCreateNewSession: typeof initParams.useValidCachedSessionOnInit === 'boolean' | ||
? !initParams.useValidCachedSessionOnInit | ||
: false, | ||
}); | ||
sessionId = generatedSession.sessionId; | ||
sessionCreated = generatedSession.sessionCreated; | ||
} | ||
this.instance.sessionId = sessionId; | ||
this.instance.sessionCreated = sessionCreated; | ||
this.startSessionTracker(); | ||
this.updateLastActivity(); | ||
return Promise.all([ | ||
...(!!initParams.sendAppStartOnInit ? [this.sendAppStart()] : []), | ||
this.sendOrCacheData(types_1.DataRequestTypeEnum.SESSION, instance_helper_1.getSessionFromInstance(this.instance)), | ||
this.sendAppStart(), | ||
...(!this.instance.clientId | ||
@@ -111,4 +134,8 @@ ? [] | ||
} | ||
this.instance.sessionId = uuid_1.v4(); | ||
this.instance.sessionCreated = get_date_string_1.default(); | ||
const { sessionId, sessionCreated } = session_1.generateSession({ | ||
forceCreateNewSession: true, | ||
maxSessionLastActivityMins: this.maxSessionLastActivityMins, | ||
}); | ||
this.instance.sessionId = sessionId; | ||
this.instance.sessionCreated = sessionCreated; | ||
return this.sendOrCacheData(types_1.DataRequestTypeEnum.SESSION, instance_helper_1.getSessionFromInstance(this.instance)); | ||
@@ -152,4 +179,23 @@ }); | ||
} | ||
this.updateLastActivity(); | ||
return this.sendOrCacheData(types_1.DataRequestTypeEnum.EVENT, Object.assign({ tenantId: this.instance.tenantId, spaceId: this.instance.spaceId, eventTime: get_date_string_1.default(), dataResidency: this.instance.dataResidency, sessionId: this.instance.sessionId, clientId: this.instance.clientId }, event)); | ||
}); | ||
this.startSessionTracker = () => { | ||
setInterval(() => { | ||
const sessionAndLastActivity = session_1.checkAndCreateSessionIfExpires({ | ||
maxSessionLastActivityMins: this.maxSessionLastActivityMins, | ||
}); | ||
if (sessionAndLastActivity) { | ||
this.instance.sessionId = sessionAndLastActivity.sessionId; | ||
this.instance.sessionCreated = sessionAndLastActivity.sessionCreated; | ||
if (sessionAndLastActivity.lastActivity) { | ||
this.instance.sessionCreated = sessionAndLastActivity.lastActivity; | ||
} | ||
} | ||
}, 60000); | ||
}; | ||
this.updateLastActivity = () => { | ||
const lastActivityCache = session_1.setLastActivityCache(get_date_string_1.default()); | ||
this.instance.lastActivity = lastActivityCache || get_date_string_1.default(); | ||
}; | ||
/** | ||
@@ -156,0 +202,0 @@ * Custom event |
@@ -99,3 +99,3 @@ export declare enum DataResidencyEnum { | ||
} | ||
export interface InitProps extends Omit<Instance, 'sessionId' | 'sessionCreated' | 'clientCreated' | 'clientId' | 'deviceId' | 'clientUserAgent' | 'username' | 'password'> { | ||
export interface InitProps extends Omit<Instance, 'sessionId' | 'sessionCreated' | 'clientCreated' | 'clientId' | 'deviceId' | 'clientUserAgent' | 'username' | 'password' | 'lastActivity'> { | ||
sessionId?: string; | ||
@@ -105,2 +105,5 @@ deviceId?: string; | ||
password?: string; | ||
sendAppStartOnInit?: boolean; | ||
useValidCachedSessionOnInit?: boolean; | ||
createNewSessionAfterLastActivityMins?: number; | ||
} | ||
@@ -110,2 +113,3 @@ export interface Instance extends Session, Client { | ||
password: string; | ||
lastActivity: string; | ||
} | ||
@@ -181,2 +185,4 @@ export declare type RequestData = Session | Event | Client; | ||
callback: (data: any) => void; | ||
onStop?: () => void; | ||
onRefresh?: () => void; | ||
}; | ||
@@ -183,0 +189,0 @@ export declare type SubscribeSessionStateResponse = GetSessionStateResponse; |
@@ -16,5 +16,5 @@ "use strict"; | ||
exports.flushCacheOnce = exports.cacheData = exports.cache = exports.supportsLocalStorage = void 0; | ||
const once_1 = __importDefault(require("lodash/once")); | ||
const lodash_once_1 = __importDefault(require("lodash.once")); | ||
const constants_1 = require("../constants"); | ||
exports.supportsLocalStorage = once_1.default(() => { | ||
exports.supportsLocalStorage = lodash_once_1.default(() => { | ||
const value = '_OMBORI_GRID_SIGNALS_CACHE_TEST'; | ||
@@ -54,4 +54,4 @@ try { | ||
exports.supportsLocalStorage() && localStorage.removeItem(constants_1.EVENTS_CACHE_KEY); | ||
exports.flushCacheOnce = once_1.default(flushCache); | ||
exports.flushCacheOnce = lodash_once_1.default(flushCache); | ||
}); | ||
exports.flushCacheOnce = once_1.default(flushCache); | ||
exports.flushCacheOnce = lodash_once_1.default(flushCache); |
@@ -1,2 +0,2 @@ | ||
export default function startSignalRConnection<T>({ name, url, accessToken, defaultMethodNameToListen, callback, }: { | ||
export default function startSignalRConnection<T>({ name, url, accessToken, defaultMethodNameToListen, callback, onStop, }: { | ||
name: string; | ||
@@ -7,4 +7,5 @@ url: string; | ||
callback: (data: T) => void; | ||
onStop?: () => void; | ||
}): Promise<{ | ||
stop(): Promise<void>; | ||
}>; |
@@ -32,3 +32,3 @@ "use strict"; | ||
const signalR = __importStar(require("@microsoft/signalr")); | ||
function startSignalRConnection({ name, url, accessToken, defaultMethodNameToListen, callback, }) { | ||
function startSignalRConnection({ name, url, accessToken, defaultMethodNameToListen, callback, onStop, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -65,2 +65,5 @@ let isStopped = false; | ||
yield connection.stop(); | ||
if (onStop) { | ||
onStop(); | ||
} | ||
}); | ||
@@ -67,0 +70,0 @@ }, |
{ | ||
"name": "@ombori/grid-signals", | ||
"version": "2.140.1", | ||
"version": "2.141.0", | ||
"main": "dist/index.js", | ||
@@ -35,3 +35,3 @@ "scripts": { | ||
}, | ||
"gitHead": "f4e87e7bf910a42f2429edbbfd1cfa298294d956" | ||
"gitHead": "e66ff6825881950651614931c184cd2a89ffd3b7" | ||
} |
export const MESSAGE_PREFIX = '@ombori/grid-signals:'; | ||
export const EVENTS_CACHE_KEY = '_OMBORI_GRID_SIGNALS_EVENTS_CACHE'; | ||
export const CLIENT_CACHE_KEY = '_OMBORI_GRID_SIGNALS_CLIENT_CACHE'; | ||
export const SESSION_CACHE_KEY = '_OMBORI_GRID_SIGNALS_SESSION_CACHE'; | ||
export const LAST_ACTIVITY_CACHE_KEY = '_OMBORI_GRID_SIGNALS_LAST_ACTIVITY_CACHE'; | ||
export const CREATE_SESSION_ERROR_INIT_MESSAGE = `${MESSAGE_PREFIX} init must be called before createSession`; | ||
@@ -8,1 +10,2 @@ export const TRACK_EVENT_ERROR_INIT_MESSAGE = `${MESSAGE_PREFIX} init must be called before trackEvent`; | ||
export const INIT_NOT_CALLED = `${MESSAGE_PREFIX} init not called!`; | ||
export const DEFAULT_MAX_SESSION_LAST_ACTIVITY_MINS = 30; |
@@ -19,2 +19,3 @@ import { v4 as uuid } from 'uuid'; | ||
MESSAGE_PREFIX, | ||
DEFAULT_MAX_SESSION_LAST_ACTIVITY_MINS, | ||
} from './constants'; | ||
@@ -40,3 +41,8 @@ import getClientInfo from './utils/get-client-id'; | ||
} from './api/api'; | ||
import delayPromise from './utils/delay'; | ||
import { | ||
generateSession, | ||
cacheSession, | ||
checkAndCreateSessionIfExpires, | ||
setLastActivityCache, | ||
} from './utils/session'; | ||
@@ -73,2 +79,5 @@ let isOnline = false; | ||
clientCreated: null, | ||
// Internal | ||
lastActivity: '', | ||
}; | ||
@@ -78,2 +87,3 @@ | ||
private instance = defaultInstance; | ||
private maxSessionLastActivityMins = DEFAULT_MAX_SESSION_LAST_ACTIVITY_MINS; | ||
@@ -105,2 +115,7 @@ constructor(initParams?: InitProps) { | ||
this.maxSessionLastActivityMins = | ||
typeof initParams.createNewSessionAfterLastActivityMins === 'number' | ||
? initParams.createNewSessionAfterLastActivityMins | ||
: this.maxSessionLastActivityMins; | ||
const clientInfo = getClientInfo(this.instance); | ||
@@ -110,8 +125,27 @@ | ||
this.instance.clientCreated = clientInfo.clientCreated; | ||
this.instance.sessionId = initParams.sessionId ? initParams.sessionId : uuid(); | ||
this.instance.sessionCreated = getDateString(); | ||
let sessionId = initParams.sessionId ? initParams.sessionId : ''; | ||
let sessionCreated = ''; | ||
await delayPromise(1000); | ||
if (sessionId) { | ||
sessionCreated = getDateString(); | ||
cacheSession(sessionId, sessionCreated); | ||
} else { | ||
const generatedSession = generateSession({ | ||
maxSessionLastActivityMins: this.maxSessionLastActivityMins, | ||
forceCreateNewSession: | ||
typeof initParams.useValidCachedSessionOnInit === 'boolean' | ||
? !initParams.useValidCachedSessionOnInit | ||
: false, | ||
}); | ||
sessionId = generatedSession.sessionId; | ||
sessionCreated = generatedSession.sessionCreated; | ||
} | ||
this.instance.sessionId = sessionId; | ||
this.instance.sessionCreated = sessionCreated; | ||
this.startSessionTracker(); | ||
this.updateLastActivity(); | ||
return Promise.all([ | ||
...(!!initParams.sendAppStartOnInit ? [this.sendAppStart()] : []), | ||
this.sendOrCacheData( | ||
@@ -121,3 +155,2 @@ DataRequestTypeEnum.SESSION, | ||
), | ||
this.sendAppStart(), | ||
...(!this.instance.clientId | ||
@@ -152,4 +185,8 @@ ? [] | ||
this.instance.sessionId = uuid(); | ||
this.instance.sessionCreated = getDateString(); | ||
const { sessionId, sessionCreated } = generateSession({ | ||
forceCreateNewSession: true, | ||
maxSessionLastActivityMins: this.maxSessionLastActivityMins, | ||
}); | ||
this.instance.sessionId = sessionId; | ||
this.instance.sessionCreated = sessionCreated; | ||
@@ -209,2 +246,4 @@ return this.sendOrCacheData( | ||
this.updateLastActivity(); | ||
return this.sendOrCacheData(DataRequestTypeEnum.EVENT, { | ||
@@ -221,2 +260,22 @@ tenantId: this.instance.tenantId, | ||
private startSessionTracker = () => { | ||
setInterval(() => { | ||
const sessionAndLastActivity = checkAndCreateSessionIfExpires({ | ||
maxSessionLastActivityMins: this.maxSessionLastActivityMins, | ||
}); | ||
if (sessionAndLastActivity) { | ||
this.instance.sessionId = sessionAndLastActivity.sessionId; | ||
this.instance.sessionCreated = sessionAndLastActivity.sessionCreated; | ||
if (sessionAndLastActivity.lastActivity) { | ||
this.instance.sessionCreated = sessionAndLastActivity.lastActivity; | ||
} | ||
} | ||
}, 60000); | ||
}; | ||
private updateLastActivity = () => { | ||
const lastActivityCache = setLastActivityCache(getDateString()); | ||
this.instance.lastActivity = lastActivityCache || getDateString(); | ||
}; | ||
/** | ||
@@ -223,0 +282,0 @@ * Custom event |
@@ -120,2 +120,3 @@ export enum DataResidencyEnum { | ||
| 'password' | ||
| 'lastActivity' | ||
> { | ||
@@ -126,2 +127,5 @@ sessionId?: string; | ||
password?: string; | ||
sendAppStartOnInit?: boolean; | ||
useValidCachedSessionOnInit?: boolean; | ||
createNewSessionAfterLastActivityMins?: number; | ||
} | ||
@@ -132,2 +136,3 @@ | ||
password: string; | ||
lastActivity: string; | ||
} | ||
@@ -215,2 +220,4 @@ | ||
callback: (data: any) => void; | ||
onStop?: () => void; | ||
onRefresh?: () => void; | ||
}; | ||
@@ -217,0 +224,0 @@ |
@@ -1,2 +0,2 @@ | ||
import once from 'lodash/once'; | ||
import once from 'lodash.once'; | ||
import { DataRequestTypeEnum, SendOrCacheDataProps, RequestData } from '../types'; | ||
@@ -3,0 +3,0 @@ import { EVENTS_CACHE_KEY } from '../constants'; |
@@ -9,2 +9,3 @@ import * as signalR from '@microsoft/signalr'; | ||
callback, | ||
onStop, | ||
}: { | ||
@@ -16,2 +17,3 @@ name: string; | ||
callback: (data: T) => void; | ||
onStop?: () => void; | ||
}) { | ||
@@ -29,3 +31,3 @@ let isStopped = false; | ||
if (!isStopped) { | ||
setTimeout(function () { | ||
setTimeout(function() { | ||
connection.start(); | ||
@@ -56,4 +58,7 @@ }, 5000); | ||
await connection.stop(); | ||
if (onStop) { | ||
onStop(); | ||
} | ||
}, | ||
}; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
140597
3545