From feab5a19b3d88b538b8e355bc41e816964ec734c Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sun, 11 Oct 2020 22:44:58 +0200 Subject: [PATCH] Autoload OH if country code is not yet loaded --- Logic/UIEventSource.ts | 8 ++++++-- State.ts | 2 +- UI/OhVisualization.ts | 9 +++++++++ test.ts | 5 +++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Logic/UIEventSource.ts b/Logic/UIEventSource.ts index 47c48a7..016d661 100644 --- a/Logic/UIEventSource.ts +++ b/Logic/UIEventSource.ts @@ -113,12 +113,16 @@ export class UIEventSource{ return newSource; } - public static Chronic(millis: number):UIEventSource{ + public static Chronic(millis: number, asLong: () => boolean = undefined): UIEventSource { const source = new UIEventSource(undefined); + function run() { source.setData(new Date()); - window.setTimeout(run, millis); + if (asLong === undefined || asLong()) { + window.setTimeout(run, millis); + } } + run(); return source; diff --git a/State.ts b/State.ts index c7ef864..fcfc4e2 100644 --- a/State.ts +++ b/State.ts @@ -23,7 +23,7 @@ export default class State { // The singleton of the global state public static state: State; - public static vNumber = "0.1.0c"; + public static vNumber = "0.1.0d"; // The user journey states thresholds when a new feature gets unlocked public static userJourney = { diff --git a/UI/OhVisualization.ts b/UI/OhVisualization.ts index 0e06e3e..adeed24 100644 --- a/UI/OhVisualization.ts +++ b/UI/OhVisualization.ts @@ -13,7 +13,13 @@ export default class OpeningHoursVisualization extends UIElement { super(tags); this._key = key; this.ListenTo(UIEventSource.Chronic(60*1000)); // Automatically reload every minute + this.ListenTo(UIEventSource.Chronic(500, () => { + return tags.data._country === undefined; + })); + + } + private static GetRanges(oh: any, from: Date, to: Date): ({ @@ -144,6 +150,9 @@ export default class OpeningHoursVisualization extends UIElement { nextSunday.setDate(nextSunday.getDate() + 7); const tags = this._source.data; + if(tags._country === undefined){ + return "Loading..."; + } const oh = new opening_hours(tags[this._key], { lat: tags._lat, lon: tags._lon, diff --git a/test.ts b/test.ts index 276fcdd..47cf57d 100644 --- a/test.ts +++ b/test.ts @@ -5,10 +5,11 @@ import {UIEventSource} from "./Logic/UIEventSource"; import OpeningHoursVisualization from "./UI/OhVisualization"; const oh = "Tu-Fr 09:00-17:00 'as usual'; mo off 'yyy'; su off 'xxx'" - -new OpeningHoursVisualization(new UIEventSource({opening_hours:oh}), 'opening_hours').AttachTo('maindiv') +const tags = new UIEventSource({opening_hours:oh}); +new OpeningHoursVisualization(tags, 'opening_hours').AttachTo('maindiv') +window.setTimeout(() => {tags.data._country = "be"; }, 5000) /*/