@stardyn/angular-updater
Version:
Angular Version Tracker Package - Lightweight, configurable version tracking and update notification service for Angular applications with Service Worker support
175 lines (169 loc) • 7.66 kB
JavaScript
import * as i0 from '@angular/core';
import { Optional, Injectable, NgZone, provideAppInitializer, inject } from '@angular/core';
import { interval } from 'rxjs';
import * as i1 from '@stardyn/angular-ui-actions';
import { ActionTheme, ActionDialogType, XConUIActionsService } from '@stardyn/angular-ui-actions';
import * as i2 from '@angular/service-worker';
import { SwUpdate } from '@angular/service-worker';
import * as i3 from '@stardyn/angular-console';
import { ModuleConsoleService } from '@stardyn/angular-console';
class XconVersionTrackerService {
constructor(actionsService, swUpdate, zone, consoleService) {
this.actionsService = actionsService;
this.swUpdate = swUpdate;
this.zone = zone;
this.config = {
checkInterval: 60,
debugMode: false,
theme: ActionTheme.LIGHT
};
this.versionCheckIntervalSeconds = 60;
if (!consoleService) {
this.dconsole = {
debug: (...args) => console.debug('[XconVersionTracker]', ...args),
info: (...args) => console.info('[XconVersionTracker]', ...args),
warn: (...args) => console.warn('[XconVersionTracker]', ...args),
error: (...args) => console.error('[XconVersionTracker]', ...args),
logError: (message, error) => console.error('[XconVersionTracker]', message, error),
logServiceInit: (name, config) => console.info(`[XconVersionTracker] Service initialized: ${name}`, config),
isDebugEnabled: () => true
};
}
else {
this.dconsole = consoleService;
}
this.dconsole?.debug('XconVersionTracker service initialized');
}
configure(config) {
this.config = { ...this.config, ...config };
this.dconsole.debug('XconVersionTracker configured', this.config);
}
initialize() {
if (!this.swUpdate.isEnabled)
return;
const self = this;
this.dconsole.info(`Version Tracker Initializing`);
this.zone.runOutsideAngular(() => {
interval(this.versionCheckIntervalSeconds * 1000).subscribe(() => {
// console.log('Checking for SW Update');
this.swUpdate
.checkForUpdate()
.then((versionFound) => {
// console.log('CHECK RESULT:', versionFound);
})
.catch(() => this.dconsole.error('SW Update check failed?'));
});
});
let upTheme = this.config.theme || this.actionsService.DefaultTheme;
this.swUpdate.versionUpdates.subscribe((versionEvent) => {
switch (versionEvent.type) {
case 'VERSION_DETECTED':
this.dconsole.info(`New application version found, beginning download: ${versionEvent.version.hash}`);
break;
case 'VERSION_READY':
this.dconsole.info(`New app version ready: ${versionEvent.latestVersion.hash}`);
self.actionsService.showMessage("New version available. Would you like to update?", ActionDialogType.YesNo, (accept, item) => {
if (!accept) {
return;
}
window.location.reload();
}, { theme: upTheme });
break;
}
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.0", ngImport: i0, type: XconVersionTrackerService, deps: [{ token: i1.XConUIActionsService }, { token: i2.SwUpdate }, { token: i0.NgZone }, { token: i3.ModuleConsoleService, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.0.0", ngImport: i0, type: XconVersionTrackerService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.0", ngImport: i0, type: XconVersionTrackerService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: i1.XConUIActionsService }, { type: i2.SwUpdate }, { type: i0.NgZone }, { type: i3.ModuleConsoleService, decorators: [{
type: Optional
}] }] });
// Singleton instance to prevent multiple instances
let versionTrackerInstance = null;
function createXconVersionTrackerService(config, actionsService, swUpdate, ngZone) {
// Return existing instance if available
if (versionTrackerInstance) {
console.debug('[VersionTrackerFactory] Returning existing XconVersionTracker instance');
return versionTrackerInstance;
}
// Create ModuleConsoleService instance
const moduleConsole = new ModuleConsoleService();
moduleConsole.configure({
moduleName: 'XconVersionTracker',
version: require('../package.json').version,
debugMode: config.debugMode ?? true,
showTimestamp: true
});
// Create XconVersionTrackerService instance
versionTrackerInstance = new XconVersionTrackerService(actionsService, swUpdate, ngZone, moduleConsole);
// Configure VersionTracker
versionTrackerInstance.configure({
checkInterval: config.checkInterval || 60,
debugMode: config.debugMode ?? false,
theme: config.theme || ActionTheme.LIGHT,
});
moduleConsole.logServiceInit('[VersionTrackerFactory] Creating new XconVersionTracker instance', {
checkInterval: config.checkInterval || 60,
debugMode: config.debugMode ?? false,
instanceId: 'SINGLETON'
});
return versionTrackerInstance;
}
/**
* Provider factory class for creating XconVersionTracker services
*/
class ProvideXconVersionTracker {
/**
* Provide XconVersionTrackerService with configuration
* @param config Version tracker configuration
* @returns Array of Angular Providers and EnvironmentProviders
*/
static forRoot(config) {
return [
// Provide XconVersionTrackerService (Singleton)
{
provide: XconVersionTrackerService,
useFactory: (actionsService, swUpdate, ngZone) => {
return createXconVersionTrackerService(config, actionsService, swUpdate, ngZone);
},
deps: [XConUIActionsService, SwUpdate, NgZone]
},
provideAppInitializer(() => {
return inject(XconVersionTrackerService).initialize();
})
];
}
}
/**
* Simple provider function for XconVersionTrackerService
* @param config Version tracker configuration
* @returns Angular Provider and EnvironmentProviders array
*/
function provideXconVersionTracker(config) {
return ProvideXconVersionTracker.forRoot(config);
}
/**
* Reset singleton instance (for testing or re-configuration)
*/
function resetVersionTrackerInstance() {
console.debug('[VersionTrackerFactory] Resetting singleton instance');
if (versionTrackerInstance) {
// Cleanup existing instance if needed
try {
// Stop any running timers/subscriptions
versionTrackerInstance.cleanup?.();
}
catch (error) {
console.warn('[VersionTrackerFactory] Error during cleanup:', error);
}
}
versionTrackerInstance = null;
}
// Main entry point for @stardyn/angular-updater package
/**
* Generated bundle index. Do not edit.
*/
export { XconVersionTrackerService, provideXconVersionTracker, resetVersionTrackerInstance };
//# sourceMappingURL=index.mjs.map