mirror of
https://github.com/bastienwirtz/homer.git
synced 2026-07-17 16:38:59 +00:00
fix: simplify scheduler implementation
This commit is contained in:
parent
c46b9a311b
commit
1d665305c3
2 changed files with 23 additions and 32 deletions
13
AGENTS.md
13
AGENTS.md
|
|
@ -32,6 +32,19 @@ All service components follow this architecture:
|
||||||
- Use the `service.js` mixin (`src/mixins/service.js`) for common API functionality
|
- Use the `service.js` mixin (`src/mixins/service.js`) for common API functionality
|
||||||
- Use a custom `fetch` method provided by the service mixin to seamlessly support proxy configuration, custom headers, and credentials.
|
- Use a custom `fetch` method provided by the service mixin to seamlessly support proxy configuration, custom headers, and credentials.
|
||||||
|
|
||||||
|
### Auto-Update Configuration
|
||||||
|
|
||||||
|
Services support automatic data refreshing using a centralized scheduler system with global and per-service configuration:
|
||||||
|
|
||||||
|
#### Global Configuration
|
||||||
|
|
||||||
|
`autoUpdate: 30000` - Set default interval for all services (30 seconds)
|
||||||
|
|
||||||
|
#### Service Configuration
|
||||||
|
- **Service-specific interval**: `updateInterval: 10000` - Override global default
|
||||||
|
- **Disable per service**: `autoUpdateInterval: false` - Disable for specific service
|
||||||
|
- **Use global default**: Omit `autoUpdateInterval` to use global setting
|
||||||
|
|
||||||
### Configuration & Routing
|
### Configuration & Routing
|
||||||
|
|
||||||
- **Multi-page Support**: Hash-based routing without Vue Router
|
- **Multi-page Support**: Hash-based routing without Vue Router
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ class UpdateScheduler {
|
||||||
this.registeredComponents = new Map();
|
this.registeredComponents = new Map();
|
||||||
this.globalTimer = null;
|
this.globalTimer = null;
|
||||||
this.tickCount = 0;
|
this.tickCount = 0;
|
||||||
this.isRunning = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
register(component, intervalMs, updateMethod) {
|
register(component, intervalMs, updateMethod) {
|
||||||
|
|
@ -32,6 +31,11 @@ class UpdateScheduler {
|
||||||
const intervalSeconds = Math.floor(intervalMs / 1000);
|
const intervalSeconds = Math.floor(intervalMs / 1000);
|
||||||
const componentId = this.generateComponentId(component);
|
const componentId = this.generateComponentId(component);
|
||||||
|
|
||||||
|
if (!componentId) {
|
||||||
|
console.error(`UpdateScheduler: invalid component id`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.registeredComponents.set(componentId, {
|
this.registeredComponents.set(componentId, {
|
||||||
component,
|
component,
|
||||||
interval: intervalSeconds,
|
interval: intervalSeconds,
|
||||||
|
|
@ -60,12 +64,11 @@ class UpdateScheduler {
|
||||||
|
|
||||||
generateComponentId(component) {
|
generateComponentId(component) {
|
||||||
// Use component's unique identifier or Vue instance uid
|
// Use component's unique identifier or Vue instance uid
|
||||||
return component._uid || component.$.uid || Symbol("component");
|
return component._uid || component.$.uid
|
||||||
}
|
}
|
||||||
|
|
||||||
startGlobalTimer() {
|
startGlobalTimer() {
|
||||||
if (!this.globalTimer && !this.isRunning) {
|
if (!this.globalTimer) {
|
||||||
this.isRunning = true;
|
|
||||||
this.tickCount = 0;
|
this.tickCount = 0;
|
||||||
|
|
||||||
this.globalTimer = setInterval(() => {
|
this.globalTimer = setInterval(() => {
|
||||||
|
|
@ -81,7 +84,6 @@ class UpdateScheduler {
|
||||||
if (this.globalTimer) {
|
if (this.globalTimer) {
|
||||||
clearInterval(this.globalTimer);
|
clearInterval(this.globalTimer);
|
||||||
this.globalTimer = null;
|
this.globalTimer = null;
|
||||||
this.isRunning = false;
|
|
||||||
this.tickCount = 0;
|
this.tickCount = 0;
|
||||||
console.log("UpdateScheduler: Global timer stopped");
|
console.log("UpdateScheduler: Global timer stopped");
|
||||||
}
|
}
|
||||||
|
|
@ -99,30 +101,6 @@ class UpdateScheduler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pause() {
|
|
||||||
if (this.globalTimer) {
|
|
||||||
clearInterval(this.globalTimer);
|
|
||||||
this.globalTimer = null;
|
|
||||||
this.isRunning = false;
|
|
||||||
console.log("UpdateScheduler: Paused");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resume() {
|
|
||||||
if (!this.globalTimer && this.registeredComponents.size > 0) {
|
|
||||||
this.startGlobalTimer();
|
|
||||||
console.log("UpdateScheduler: Resumed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getStatus() {
|
|
||||||
return {
|
|
||||||
isRunning: this.isRunning,
|
|
||||||
registeredCount: this.registeredComponents.size,
|
|
||||||
tickCount: this.tickCount,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and export global singleton instance
|
// Create and export global singleton instance
|
||||||
|
|
@ -132,9 +110,9 @@ const updateScheduler = new UpdateScheduler();
|
||||||
if (typeof document !== "undefined") {
|
if (typeof document !== "undefined") {
|
||||||
document.addEventListener("visibilitychange", () => {
|
document.addEventListener("visibilitychange", () => {
|
||||||
if (document.hidden) {
|
if (document.hidden) {
|
||||||
updateScheduler.pause();
|
updateScheduler.stopGlobalTimer();
|
||||||
} else {
|
} else if (updateScheduler.registeredComponents.size > 0) {
|
||||||
updateScheduler.resume();
|
updateScheduler.startGlobalTimer();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue