@phis/ui 0.4.13 → 0.4.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,8 @@
13
13
  * limit collects. Neither event needs to invalidate anything.
14
14
  *
15
15
  * What does change a stored value is a write to the translation store -- an edit, a delete, a changed
16
- * translation configuration. Entries do not expire for it. Core keeps a change marker per store, one
16
+ * translation configuration. Entries do not wait to expire for it (they leave after a day for another
17
+ * reason, `PHI_TRANSLATION_CACHE_MAX_AGE_MS`). Core keeps a change marker per store, one
17
18
  * global and one per Site, and hands both out with the Site config; `syncPhiTranslationChangeMarkers`
18
19
  * empties a store whose marker differs from the one this process saw last. The process that forwarded
19
20
  * an edit reads the Site config fresh on its next render (its proxy cleared the read cache), and every
@@ -32,6 +33,14 @@
32
33
  * passed through `tr` cannot grow the process without bound.
33
34
  */
34
35
  export declare const PHI_TRANSLATION_CACHE_MAX_ENTRIES = 5000;
36
+ /**
37
+ * How long an entry is kept: a day, and up to four hours more so that entries written together do not
38
+ * all leave in the same minute. Not for freshness -- the change markers see to that -- but so that a text
39
+ * this Site still shows is asked for again once a day. Core notes when a message was last read
40
+ * (phis-server TRANSLATIONS.md, "When a message was last read"), and a text served from here forever
41
+ * would look unread there.
42
+ */
43
+ export declare const PHI_TRANSLATION_CACHE_MAX_AGE_MS: number;
35
44
  export type PhiTranslationCacheKeyInput = {
36
45
  /** The Site the translation belongs to, or an empty string for a global one. */
37
46
  scope: string;
@@ -13,7 +13,8 @@
13
13
  * limit collects. Neither event needs to invalidate anything.
14
14
  *
15
15
  * What does change a stored value is a write to the translation store -- an edit, a delete, a changed
16
- * translation configuration. Entries do not expire for it. Core keeps a change marker per store, one
16
+ * translation configuration. Entries do not wait to expire for it (they leave after a day for another
17
+ * reason, `PHI_TRANSLATION_CACHE_MAX_AGE_MS`). Core keeps a change marker per store, one
17
18
  * global and one per Site, and hands both out with the Site config; `syncPhiTranslationChangeMarkers`
18
19
  * empties a store whose marker differs from the one this process saw last. The process that forwarded
19
20
  * an edit reads the Site config fresh on its next render (its proxy cleared the read cache), and every
@@ -32,11 +33,20 @@
32
33
  * passed through `tr` cannot grow the process without bound.
33
34
  */
34
35
  export const PHI_TRANSLATION_CACHE_MAX_ENTRIES = 5_000;
36
+ /**
37
+ * How long an entry is kept: a day, and up to four hours more so that entries written together do not
38
+ * all leave in the same minute. Not for freshness -- the change markers see to that -- but so that a text
39
+ * this Site still shows is asked for again once a day. Core notes when a message was last read
40
+ * (phis-server TRANSLATIONS.md, "When a message was last read"), and a text served from here forever
41
+ * would look unread there.
42
+ */
43
+ export const PHI_TRANSLATION_CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000;
44
+ const PHI_TRANSLATION_CACHE_AGE_SPREAD_MS = 4 * 60 * 60 * 1000;
35
45
  /** ASCII unit separator: the one character a locale, a context, or a format will never contain. */
36
46
  const SEPARATOR = String.fromCharCode(31);
37
47
  // Named for its shape: a process that outlives a code change (a dev server's HMR) still holds the old
38
48
  // value under the old name, and must not hand it to code that expects this one.
39
- const TRANSLATION_CACHE_STORE_KEY = Symbol.for("phis-ui.translation-cache.entries-generation-markers");
49
+ const TRANSLATION_CACHE_STORE_KEY = Symbol.for("phis-ui.translation-cache.expiring-entries-generation-markers");
40
50
  const STATE = (() => {
41
51
  const holder = globalThis;
42
52
  holder[TRANSLATION_CACHE_STORE_KEY] ??= {
@@ -52,15 +62,19 @@ export function buildPhiTranslationCacheKey({ scope, sourceLocale, targetLocale,
52
62
  return [scope, sourceLocale, targetLocale, ctx, format, msg].join(SEPARATOR);
53
63
  }
54
64
  export function readPhiTranslationCache(key) {
55
- const value = TRANSLATION_CACHE.get(key);
56
- if (value == null) {
65
+ const entry = TRANSLATION_CACHE.get(key);
66
+ if (entry == null) {
67
+ return null;
68
+ }
69
+ if (entry.expiresAt <= Date.now()) {
70
+ TRANSLATION_CACHE.delete(key);
57
71
  return null;
58
72
  }
59
73
  // Re-inserting moves the entry to the end, so the size limit evicts what is least recently read
60
74
  // rather than what was written first -- a Navigation label read on every request outlives a one-off.
61
75
  TRANSLATION_CACHE.delete(key);
62
- TRANSLATION_CACHE.set(key, value);
63
- return value;
76
+ TRANSLATION_CACHE.set(key, entry);
77
+ return entry.value;
64
78
  }
65
79
  /** Taken before a request to Core, and handed back to `writePhiTranslationCache` with its answer. */
66
80
  export function readPhiTranslationCacheGeneration() {
@@ -75,7 +89,10 @@ export function writePhiTranslationCache(key, value, generation) {
75
89
  return;
76
90
  }
77
91
  TRANSLATION_CACHE.delete(key);
78
- TRANSLATION_CACHE.set(key, value);
92
+ TRANSLATION_CACHE.set(key, {
93
+ value,
94
+ expiresAt: Date.now() + PHI_TRANSLATION_CACHE_MAX_AGE_MS + Math.random() * PHI_TRANSLATION_CACHE_AGE_SPREAD_MS,
95
+ });
79
96
  while (TRANSLATION_CACHE.size > PHI_TRANSLATION_CACHE_MAX_ENTRIES) {
80
97
  const oldest = TRANSLATION_CACHE.keys().next();
81
98
  if (oldest.done) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phis/ui",
3
- "version": "0.4.13",
3
+ "version": "0.4.14",
4
4
  "license": "Apache-2.0",
5
5
  "publishConfig": {
6
6
  "access": "public"