askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
flags.py · 2.1 KB · 55 lines
| 1 | """Shared feature-flag helper, served at /flags as window.dbbasicFlags. |
| 2 | |
| 3 | Wraps GET /api/flags (packages/app-settings): effective value = the |
| 4 | caller's own user_prefs override ("flag:<name>") -> the instance-wide |
| 5 | feature_flags value, resolved server-side per docs/upgrade-and-customization.md |
| 6 | Rule 5. This module never talks to a collection directly -- it is a thin, |
| 7 | dependency-free cache in front of that one endpoint. |
| 8 | |
| 9 | API: |
| 10 | await window.dbbasicFlags.load() // fetch + cache the flag map (once); |
| 11 | // safe to call again, re-fetches fresh |
| 12 | window.dbbasicFlags(name) // sync lookup: cached string value, or |
| 13 | // null if unknown / not loaded yet |
| 14 | window.dbbasicFlags.on(name) // sync boolean: true when the cached |
| 15 | // value is "on" or "true" |
| 16 | |
| 17 | Typical use: `await window.dbbasicFlags.load()` once on page init, then |
| 18 | `window.dbbasicFlags.on("kanban_view")` anywhere in the page's render path. |
| 19 | Calling `dbbasicFlags(name)` before `load()` has resolved simply returns |
| 20 | null (fail dark), so gating a feature behind a flag never throws. |
| 21 | |
| 22 | Defined once here, so no page hand-rolls its own /api/flags fetch. |
| 23 | """ |
| 24 | |
| 25 | _JS = r""" |
| 26 | (function () { |
| 27 | let cache = null; |
| 28 | let pending = null; |
| 29 | |
| 30 | async function load() { |
| 31 | if (pending) return pending; |
| 32 | pending = fetch("/api/flags", {credentials: "same-origin", headers: {accept: "application/json"}}) |
| 33 | .then((res) => res.ok ? res.json() : {status: "error", flags: {}}) |
| 34 | .then((data) => { cache = (data && data.flags) || {}; pending = null; return cache; }) |
| 35 | .catch(() => { cache = cache || {}; pending = null; return cache; }); |
| 36 | return pending; |
| 37 | } |
| 38 | |
| 39 | function dbbasicFlags(name) { |
| 40 | if (!cache || !(name in cache)) return null; |
| 41 | return cache[name]; |
| 42 | } |
| 43 | dbbasicFlags.load = load; |
| 44 | dbbasicFlags.on = function (name) { |
| 45 | const v = dbbasicFlags(name); |
| 46 | return v === "on" || v === "true"; |
| 47 | }; |
| 48 | |
| 49 | window.dbbasicFlags = dbbasicFlags; |
| 50 | })(); |
| 51 | """ |
| 52 | |
| 53 | |
| 54 | def GET(request): |
| 55 | return {"content_type": "application/javascript; charset=utf-8", "body": _JS} |