@gloom-sh/terminal 0.20.16 → 0.20.18
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.
- package/package.json +3 -1
- package/src/account/auth-dialog.tsx +19 -1
- package/src/account/billing.ts +2 -1
- package/src/account/heard-from-question.tsx +235 -0
- package/src/account/heard-from.ts +97 -0
- package/src/api-client/data.ts +16 -5
- package/src/api-client/heard-from.ts +72 -0
- package/src/api-client/index.ts +63 -6
- package/src/api-client/mna.ts +18 -0
- package/src/api-client/supply-chain.ts +9 -0
- package/src/api-client/types/billing.ts +35 -1
- package/src/api-client/types/econ.ts +3 -0
- package/src/app/global-shortcuts.ts +5 -3
- package/src/app/pro-welcome/keys.ts +27 -0
- package/src/app/pro-welcome/model.ts +158 -0
- package/src/app/pro-welcome/row.tsx +176 -0
- package/src/app/pro-welcome/use-pro-welcome.ts +156 -0
- package/src/app/runtime/external-profile-changes.ts +135 -0
- package/src/app.tsx +33 -2
- package/src/architecture/core-builtin-allowlist.json +1 -1
- package/src/architecture/floating-promise-allowlist.json +0 -1
- package/src/architecture/shortcut-labels-allowlist.json +1 -0
- package/src/cli/alert-listing.ts +98 -0
- package/src/cli/commands/fred-series.ts +3 -1
- package/src/cli/commands/market.ts +138 -22
- package/src/cli/commands/overview.ts +72 -14
- package/src/cli/commands/plugins.ts +3 -2
- package/src/cli/commands/search.ts +30 -6
- package/src/cli/context.ts +16 -5
- package/src/cli/csv-export.ts +74 -0
- package/src/cli/desktop-pane-shot.ts +75 -11
- package/src/cli/glossary.ts +64 -10
- package/src/cli/help.ts +4 -2
- package/src/cli/history-demergers.ts +117 -0
- package/src/cli/history-rows.ts +6 -2
- package/src/cli/index.ts +1 -0
- package/src/cli/known-gaps.ts +16 -0
- package/src/cli/known-us-stocks.ts +18 -0
- package/src/cli/listing-arg.ts +105 -15
- package/src/cli/pane-functions/catalog-snapshot-build.ts +83 -0
- package/src/cli/pane-functions/catalog-snapshot.json +866 -0
- package/src/cli/pane-functions/catalog-snapshot.ts +62 -0
- package/src/cli/pane-functions/catalog.ts +50 -16
- package/src/cli/pane-functions/data.ts +16 -4
- package/src/cli/pane-functions/dom.ts +24 -6
- package/src/cli/pane-functions/freshness.ts +17 -1
- package/src/cli/pane-functions/headless.ts +7 -2
- package/src/cli/pane-functions/index.ts +102 -44
- package/src/cli/pane-functions/resolver.ts +8 -5
- package/src/cli/pane-functions/screenshot-assessment.ts +30 -7
- package/src/cli/pane-functions/screenshot-bridge.ts +4 -2
- package/src/cli/pane-functions/screenshot-payload.ts +21 -0
- package/src/cli/pane-functions/screenshot-render.ts +3 -1
- package/src/cli/quote-answer.ts +32 -16
- package/src/cli/registry.ts +22 -2
- package/src/cli/report-tables.ts +17 -3
- package/src/cli/result.ts +8 -10
- package/src/cli/series-search.ts +45 -6
- package/src/cli/shot-temp-dir.ts +4 -9
- package/src/components/chart/composite/legend.tsx +80 -25
- package/src/components/chart/composite/panel-surface/panel-cursor.tsx +3 -3
- package/src/components/chart/composite/price-axis-labels.tsx +7 -5
- package/src/components/chart-table/header.tsx +9 -0
- package/src/components/chart-table/layout.ts +11 -11
- package/src/components/command-bar/pane-templates/items.ts +17 -1
- package/src/components/command-bar/routes/root/provider-search.ts +10 -1
- package/src/components/command-bar/routes/root/results.ts +34 -2
- package/src/components/command-bar/routes/root/runtime.ts +8 -1
- package/src/components/command-bar/routes/ticker-search/route.ts +5 -0
- package/src/components/command-bar/workflow/alert.ts +19 -8
- package/src/components/data-table/view.tsx +15 -2
- package/src/components/form-modal/content.tsx +9 -4
- package/src/components/index.ts +1 -1
- package/src/components/layout/header.tsx +9 -5
- package/src/components/layout/pane/capture-status.ts +6 -0
- package/src/components/layout/pane/footer/index.tsx +2 -0
- package/src/components/layout/pane/footer/model.ts +113 -15
- package/src/components/layout/pane/header.tsx +16 -2
- package/src/components/market-board/index.tsx +4 -2
- package/src/components/onboarding/onboarding-frame.tsx +6 -2
- package/src/components/onboarding/onboarding-keyboard.ts +7 -1
- package/src/components/onboarding/onboarding-wizard.tsx +3 -1
- package/src/components/onboarding/portfolio-step/positions-panel.tsx +24 -15
- package/src/components/onboarding/setup-cards.ts +36 -0
- package/src/components/onboarding/use-onboarding-progress.ts +22 -1
- package/src/components/onboarding/wizard-account.ts +6 -1
- package/src/components/onboarding/wizard-model.ts +32 -6
- package/src/components/onboarding/wizard-positions.ts +1 -1
- package/src/components/sign-in-gate.tsx +3 -1
- package/src/components/toggle-list.tsx +6 -8
- package/src/components/ui/confirm-dialog.tsx +47 -7
- package/src/components/ui/data-table/display-columns.ts +4 -1
- package/src/components/ui/data-table/hooks.ts +2 -3
- package/src/components/ui/data-table/index.tsx +4 -1
- package/src/components/ui/data-table/types.ts +19 -4
- package/src/components/ui/data-table/visible-range.ts +11 -1
- package/src/components/ui/display.tsx +17 -2
- package/src/components/ui/index.ts +1 -1
- package/src/components/ui/stat-grid.tsx +4 -0
- package/src/components/ui/status.tsx +13 -1
- package/src/components/ui/table-layout.ts +2 -1
- package/src/components/use-pane-notice-footer.tsx +16 -3
- package/src/data/config/store/index.ts +31 -0
- package/src/data/config/store/node.ts +61 -11
- package/src/data/config/store/normalize.ts +4 -6
- package/src/data/config/store/shared-file.ts +309 -0
- package/src/data/profile-merge.ts +299 -0
- package/src/data/sqlite/checkpointer.ts +11 -1
- package/src/data/sqlite/database.ts +1 -1
- package/src/data/ticker-repository.ts +134 -10
- package/src/data/ticker-store.ts +67 -0
- package/src/data/ticker-writes.ts +5 -0
- package/src/i18n/es.ts +27 -0
- package/src/i18n/ja.ts +27 -0
- package/src/i18n/ko.ts +27 -0
- package/src/i18n/zh-cn.ts +27 -0
- package/src/i18n/zh-tw.ts +27 -0
- package/src/layout-marketplace/gallery-desktop.tsx +2 -2
- package/src/market-data/ah-pair.ts +103 -0
- package/src/market-data/depositary-pair.ts +6 -2
- package/src/market-data/hooks.tsx +33 -2
- package/src/market-data/market/change-basis.ts +2 -1
- package/src/market-data/market/header-market.ts +11 -0
- package/src/market-data/split-terms.ts +28 -0
- package/src/plugins/builtin/account-management/footer.ts +1 -1
- package/src/plugins/builtin/account-management/keyboard.ts +4 -0
- package/src/plugins/builtin/account-management/model.ts +1 -0
- package/src/plugins/builtin/account-management/pane.tsx +75 -7
- package/src/plugins/builtin/account-management/payment-recovery-row.tsx +12 -5
- package/src/plugins/builtin/account-management/pro-cancel.tsx +197 -0
- package/src/plugins/builtin/account-management/trial-cancel.ts +1 -1
- package/src/plugins/builtin/alerts/cli-move.ts +2 -2
- package/src/plugins/builtin/alerts/cli.ts +36 -30
- package/src/plugins/builtin/alerts/constants.ts +10 -0
- package/src/plugins/builtin/alerts/earnings-listing.ts +20 -0
- package/src/plugins/builtin/alerts/format.ts +24 -8
- package/src/plugins/builtin/alerts/index.tsx +38 -9
- package/src/plugins/builtin/alerts/move-checks.ts +7 -0
- package/src/plugins/builtin/alerts/pane.tsx +1 -1
- package/src/plugins/builtin/alerts/quotes.ts +26 -2
- package/src/plugins/builtin/alerts/scheduled.ts +6 -1
- package/src/plugins/builtin/alerts/types.ts +7 -0
- package/src/plugins/builtin/analytics/manual-value.ts +61 -0
- package/src/plugins/builtin/analytics/risk-book.ts +7 -7
- package/src/plugins/builtin/analytics/risk-client.ts +26 -13
- package/src/plugins/builtin/analytics/risk-headless.ts +13 -8
- package/src/plugins/builtin/analytics/risk-model.ts +92 -4
- package/src/plugins/builtin/analytics/risk-pane.tsx +85 -38
- package/src/plugins/builtin/awards/pane.tsx +7 -3
- package/src/plugins/builtin/broker-trading/orders.tsx +2 -2
- package/src/plugins/builtin/central-bank-rates/model.ts +8 -2
- package/src/plugins/builtin/central-bank-rates/pane.tsx +5 -3
- package/src/plugins/builtin/chart-composer/cli-options.ts +4 -2
- package/src/plugins/builtin/chart-composer/data-catalog-pane.tsx +2 -6
- package/src/plugins/builtin/chart-composer/pane.tsx +9 -3
- package/src/plugins/builtin/cloud/auth-actions.tsx +11 -6
- package/src/plugins/builtin/cloud/company-picker.tsx +5 -3
- package/src/plugins/builtin/cloud/upgrade-command.ts +1 -1
- package/src/plugins/builtin/company-kpis/headless.ts +9 -4
- package/src/plugins/builtin/company-kpis/latest.ts +45 -0
- package/src/plugins/builtin/company-kpis/model.ts +38 -8
- package/src/plugins/builtin/company-kpis/pane.tsx +22 -8
- package/src/plugins/builtin/composite/alt-data.ts +20 -0
- package/src/plugins/builtin/composite/application.ts +14 -0
- package/src/plugins/builtin/composite/broker.ts +12 -0
- package/src/plugins/builtin/composite/credit.ts +33 -0
- package/src/plugins/builtin/composite/crypto.ts +10 -0
- package/src/plugins/builtin/composite/earnings.ts +12 -0
- package/src/plugins/builtin/composite/filings.ts +13 -0
- package/src/plugins/builtin/composite/futures-commodities.ts +23 -0
- package/src/plugins/builtin/composite/global-markets.ts +12 -0
- package/src/plugins/builtin/composite/options-volatility.ts +23 -0
- package/src/plugins/builtin/composite/ownership.ts +23 -0
- package/src/plugins/builtin/composite/portfolio.ts +10 -0
- package/src/plugins/builtin/composite/quant.ts +15 -0
- package/src/plugins/builtin/composite/rates-macro.ts +30 -0
- package/src/plugins/builtin/composite/screeners.ts +11 -0
- package/src/plugins/builtin/composite/shared.ts +11 -0
- package/src/plugins/builtin/composite/ticker-core.ts +36 -0
- package/src/plugins/builtin/congress-trades/headless.ts +31 -3
- package/src/plugins/builtin/correlation/index.tsx +13 -17
- package/src/plugins/builtin/correlation/matrix/model.ts +18 -0
- package/src/plugins/builtin/credit-conditions/series.ts +2 -2
- package/src/plugins/builtin/dividend-yield/client.ts +57 -16
- package/src/plugins/builtin/dividend-yield/headless.ts +17 -11
- package/src/plugins/builtin/dividend-yield/model.ts +4 -3
- package/src/plugins/builtin/dividend-yield/pane.tsx +22 -18
- package/src/plugins/builtin/dividend-yield/ranking.ts +1 -1
- package/src/plugins/builtin/dividend-yield/types.ts +4 -0
- package/src/plugins/builtin/dividend-yield/view.ts +23 -12
- package/src/plugins/builtin/earnings/board-model.ts +12 -0
- package/src/plugins/builtin/earnings/board-table.ts +7 -4
- package/src/plugins/builtin/earnings/board.tsx +2 -2
- package/src/plugins/builtin/earnings/fiscal-quarter.ts +39 -0
- package/src/plugins/builtin/earnings/headless.ts +7 -4
- package/src/plugins/builtin/earnings/highlight.ts +1 -1
- package/src/plugins/builtin/earnings/history-model.ts +6 -4
- package/src/plugins/builtin/earnings/history-table.ts +4 -3
- package/src/plugins/builtin/earnings-calls/pane.tsx +8 -2
- package/src/plugins/builtin/econ/calendar-model.ts +18 -0
- package/src/plugins/builtin/econ/fred-catalog.ts +1 -256
- package/src/plugins/builtin/econ/index.tsx +3 -0
- package/src/plugins/builtin/econ-statistics/cache.ts +1 -1
- package/src/plugins/builtin/econ-statistics/client.ts +11 -10
- package/src/plugins/builtin/econ-statistics/detail.tsx +1 -0
- package/src/plugins/builtin/econ-statistics/headless.ts +42 -6
- package/src/plugins/builtin/econ-statistics/index.tsx +1 -0
- package/src/plugins/builtin/econ-statistics/pane.tsx +7 -2
- package/src/plugins/builtin/econ-statistics/stats.ts +2 -1
- package/src/plugins/builtin/econ-statistics/view.ts +9 -3
- package/src/plugins/builtin/equity-screener/headless.ts +4 -1
- package/src/plugins/builtin/equity-screener/model.ts +7 -6
- package/src/plugins/builtin/estimate-revisions/headless.ts +1 -1
- package/src/plugins/builtin/exposure/model.ts +10 -0
- package/src/plugins/builtin/exposure/pane.tsx +10 -3
- package/src/plugins/builtin/fx-matrix/amount.ts +15 -1
- package/src/plugins/builtin/fx-matrix/headless.ts +12 -6
- package/src/plugins/builtin/fx-matrix/index.tsx +23 -5
- package/src/plugins/builtin/fx-matrix/settings.ts +9 -4
- package/src/plugins/builtin/kelly-sizer/pane.tsx +9 -4
- package/src/plugins/builtin/market-valuation/pane.tsx +22 -10
- package/src/plugins/builtin/mna/detail.tsx +67 -6
- package/src/plugins/builtin/mna/headless.ts +128 -13
- package/src/plugins/builtin/mna/model.ts +219 -16
- package/src/plugins/builtin/mna/pane.tsx +34 -6
- package/src/plugins/builtin/money-markets/model.ts +14 -5
- package/src/plugins/builtin/money-markets/pane.tsx +4 -3
- package/src/plugins/builtin/news/wire/news/footer.ts +3 -8
- package/src/plugins/builtin/options-scenario/csp-screen-pane.tsx +22 -6
- package/src/plugins/builtin/options-scenario/csp-screen.ts +39 -12
- package/src/plugins/builtin/options-scenario/strategy-screen.ts +7 -5
- package/src/plugins/builtin/pe-band/client.ts +13 -1
- package/src/plugins/builtin/pe-band/headless.ts +2 -2
- package/src/plugins/builtin/pe-band/model.ts +63 -10
- package/src/plugins/builtin/pe-band/pane.tsx +5 -5
- package/src/plugins/builtin/portfolio-list/allocation-rows.ts +26 -7
- package/src/plugins/builtin/portfolio-list/cli/portfolio-command.ts +66 -13
- package/src/plugins/builtin/portfolio-list/cli/render.ts +76 -33
- package/src/plugins/builtin/portfolio-list/headless.ts +69 -22
- package/src/plugins/builtin/portfolio-list/pane/data.ts +3 -3
- package/src/plugins/builtin/portfolio-list/pane/index.tsx +29 -14
- package/src/plugins/builtin/portfolio-list/summary/index.tsx +2 -2
- package/src/plugins/builtin/relative-rotation/pane.tsx +2 -3
- package/src/plugins/builtin/research/corporate-actions-pane.tsx +5 -1
- package/src/plugins/builtin/research/event-model.ts +31 -13
- package/src/plugins/builtin/research/headless.ts +8 -1
- package/src/plugins/builtin/research/relative-valuation-pane.tsx +19 -17
- package/src/plugins/builtin/sectors/index.tsx +6 -9
- package/src/plugins/builtin/shared/cloud-upgrade.ts +1 -1
- package/src/plugins/builtin/shared/earnings-report.ts +19 -4
- package/src/plugins/builtin/shared/fred-catalog.ts +256 -0
- package/src/plugins/builtin/shared/report-freshness.ts +2 -3
- package/src/plugins/builtin/shared/series-cache.ts +3 -0
- package/src/plugins/builtin/shared/series-detail-chart.tsx +3 -0
- package/src/plugins/builtin/short-interest/watch-pane.tsx +7 -7
- package/src/plugins/builtin/supply-chain/client.ts +34 -9
- package/src/plugins/builtin/supply-chain/evidence.ts +3 -2
- package/src/plugins/builtin/supply-chain/flow.tsx +4 -3
- package/src/plugins/builtin/supply-chain/graph-model.ts +14 -7
- package/src/plugins/builtin/supply-chain/graph-pane.tsx +3 -1
- package/src/plugins/builtin/supply-chain/graph.tsx +6 -3
- package/src/plugins/builtin/supply-chain/headless.ts +19 -4
- package/src/plugins/builtin/supply-chain/model.ts +50 -6
- package/src/plugins/builtin/supply-chain/pane.tsx +76 -16
- package/src/plugins/builtin/ticker-detail/adr-pair/ah.ts +62 -0
- package/src/plugins/builtin/ticker-detail/adr-pair/headless.ts +119 -20
- package/src/plugins/builtin/ticker-detail/adr-pair/model.ts +67 -1
- package/src/plugins/builtin/ticker-detail/adr-pair/tab.tsx +31 -16
- package/src/plugins/builtin/ticker-detail/overview/model.ts +17 -1
- package/src/plugins/builtin/ticker-detail/overview/pane.tsx +4 -0
- package/src/plugins/builtin/ticker-detail/overview-tab.tsx +9 -2
- package/src/plugins/builtin/ticker-detail/quote-monitor/card.tsx +9 -2
- package/src/plugins/builtin/ticker-detail/quote-monitor/index.tsx +5 -2
- package/src/plugins/builtin/time-sales/headless.ts +4 -1
- package/src/plugins/builtin/time-sales/pane.tsx +2 -0
- package/src/plugins/builtin/time-sales/window.ts +12 -0
- package/src/plugins/builtin/treasury-auctions/client.ts +4 -0
- package/src/plugins/builtin/treasury-auctions/treasury-direct.ts +2 -0
- package/src/plugins/builtin/treasury-daily/client.ts +23 -11
- package/src/plugins/builtin/volatility/model.ts +17 -0
- package/src/plugins/builtin/volatility/pane.tsx +13 -4
- package/src/plugins/builtin/world-indices/index.tsx +2 -0
- package/src/plugins/builtin/world-indices/table.tsx +15 -0
- package/src/plugins/builtin/world-venue-map/evidence.ts +3 -2
- package/src/plugins/builtin/world-venue-map/headless.ts +28 -16
- package/src/plugins/builtin/world-venue-map/index.ts +19 -6
- package/src/plugins/builtin/world-venue-map/layered-pane.tsx +9 -5
- package/src/plugins/builtin/world-venue-map/layers.ts +33 -11
- package/src/plugins/builtin/world-venue-map/map.tsx +12 -0
- package/src/plugins/builtin/world-venue-map/pane.tsx +35 -6
- package/src/plugins/builtin/world-venue-map/place.ts +119 -0
- package/src/plugins/catalog-data.ts +20 -0
- package/src/plugins/catalog-lazy.ts +51 -0
- package/src/plugins/catalog-ui.ts +16 -18
- package/src/plugins/host-smoke.ts +5 -2
- package/src/portfolio/allocation.ts +79 -19
- package/src/portfolio/cash-balances.ts +76 -0
- package/src/portfolio/mutations.ts +43 -8
- package/src/public/__snapshots__/public-api.test.ts.snap +1 -0
- package/src/renderers/desktop/window-layouts.ts +14 -0
- package/src/renderers/dom/data-table/row.tsx +2 -0
- package/src/renderers/dom/host/tabs.tsx +57 -8
- package/src/renderers/dom/styles.css +13 -5
- package/src/renderers/electrobun/bun/desktop/backend-requests.ts +4 -0
- package/src/renderers/electrobun/bun/desktop/backend.ts +22 -1
- package/src/renderers/electrobun/bun/window/native-min-size.ts +6 -1
- package/src/renderers/electrobun/shared/protocol.ts +4 -0
- package/src/renderers/electrobun/view/cli-pane-shot-entry.tsx +2 -0
- package/src/renderers/electrobun/view/cli-pane-shot-frame.tsx +50 -12
- package/src/renderers/electrobun/view/remote/ticker-repository.ts +10 -0
- package/src/series/standalone-catalog.ts +5 -4
- package/src/series/standalone.ts +13 -7
- package/src/sources/gloomberb-cloud/fred-publisher.ts +25 -0
- package/src/sources/gloomberb-cloud/fred-series.ts +1 -1
- package/src/sources/gloomberb-cloud/index.ts +14 -3
- package/src/sources/gloomberb-cloud/perpetuals.ts +60 -3
- package/src/sources/provider-router/financials.ts +20 -13
- package/src/sources/provider-router/index.ts +4 -0
- package/src/sources/provider-router/search.ts +1 -0
- package/src/sync/collections-contributor.ts +3 -1
- package/src/sync/config-merge.ts +10 -1
- package/src/sync/sanitize.ts +26 -2
- package/src/tickers/company-name-listing.ts +16 -2
- package/src/tickers/listing-fate.ts +12 -8
- package/src/tickers/renamed-listings.ts +43 -10
- package/src/tickers/search/home-listing.ts +28 -0
- package/src/tickers/search/index.ts +19 -7
- package/src/tickers/terminal-codes.ts +11 -1
- package/src/time-series/base-series.ts +3 -0
- package/src/time-series/ice-credit.ts +1 -0
- package/src/time-series/resolve.ts +3 -1
- package/src/time-series/spec.ts +2 -2
- package/src/time-series/types.ts +2 -1
- package/src/types/data-provider.ts +11 -0
- package/src/types/financials.ts +39 -3
- package/src/types/headless.ts +4 -2
- package/src/types/ticker.ts +10 -2
- package/src/utils/cli-output.ts +1 -1
- package/src/utils/declared-dividends.ts +13 -1
- package/src/utils/dividend-cash.ts +5 -1
- package/src/utils/forward-dividend.ts +4 -3
- package/src/utils/remove-folder.ts +37 -0
- package/src/utils/throttled-fetch.ts +15 -1
- package/src/utils/trailing-dividend.ts +1 -1
- package/src/plugins/builtin/composite-plugins.ts +0 -285
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Folds one writer's change to a saved profile document (config.json, a
|
|
3
|
+
* ticker row) into another copy of it, so two programs writing the same
|
|
4
|
+
* profile keep each other's edits: the app open in a terminal and a `gloom`
|
|
5
|
+
* command run beside it.
|
|
6
|
+
*
|
|
7
|
+
* `diffProfile` lists what changed between the copy a writer started from
|
|
8
|
+
* and the one it wrote; `applyProfileEdits` replays that list on any other
|
|
9
|
+
* copy. The edits are fine-grained, so unrelated changes never collide:
|
|
10
|
+
*
|
|
11
|
+
* - objects merge key by key;
|
|
12
|
+
* - lists of records with an `id` (or a pane's `instanceId`) merge record by
|
|
13
|
+
* record, keeping each side's additions, removals and order;
|
|
14
|
+
* - lists of plain values (symbols, ids) merge as sets;
|
|
15
|
+
* - a string that holds JSON (the alert lists) merges as the JSON it holds.
|
|
16
|
+
*
|
|
17
|
+
* When both sides changed the same value, the copy the edits land on keeps
|
|
18
|
+
* its own. A removal always lands: an alert or a watchlist one side deleted
|
|
19
|
+
* stays deleted, whatever the other changed in it meanwhile.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export type ProfileJson = null | boolean | number | string | ProfileJson[] | { [key: string]: ProfileJson };
|
|
23
|
+
type ProfileObject = { [key: string]: ProfileJson };
|
|
24
|
+
|
|
25
|
+
/** One step into a document: an object key, a record in a list by its id, or the JSON a string holds. */
|
|
26
|
+
type ProfileStep = { key: string } | { id: string } | { json: true };
|
|
27
|
+
|
|
28
|
+
export type ProfileEdit =
|
|
29
|
+
| { kind: "set"; path: ProfileStep[]; from: ProfileJson | undefined; to: ProfileJson }
|
|
30
|
+
| { kind: "remove"; path: ProfileStep[] }
|
|
31
|
+
| { kind: "members"; path: ProfileStep[]; from: ProfileJson[]; to: ProfileJson[]; add: ProfileJson[]; remove: ProfileJson[] }
|
|
32
|
+
| { kind: "insert"; path: ProfileStep[]; after: string | null; record: ProfileObject }
|
|
33
|
+
| { kind: "order"; path: ProfileStep[]; from: string[]; to: string[] };
|
|
34
|
+
|
|
35
|
+
const RECORD_KEYS = ["id", "instanceId"] as const;
|
|
36
|
+
type RecordKey = typeof RECORD_KEYS[number];
|
|
37
|
+
|
|
38
|
+
function isObject(value: unknown): value is ProfileObject {
|
|
39
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function sameProfileJson(a: unknown, b: unknown): boolean {
|
|
43
|
+
if (a === b) return true;
|
|
44
|
+
if (Array.isArray(a)) {
|
|
45
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
46
|
+
return a.every((entry, index) => sameProfileJson(entry, b[index]));
|
|
47
|
+
}
|
|
48
|
+
if (isObject(a)) {
|
|
49
|
+
if (!isObject(b)) return false;
|
|
50
|
+
const keys = Object.keys(a);
|
|
51
|
+
if (keys.length !== Object.keys(b).length) return false;
|
|
52
|
+
return keys.every((key) => Object.hasOwn(b, key) && sameProfileJson(a[key], b[key]));
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The field that names each record of a list, when every entry is a record with a distinct one. */
|
|
58
|
+
function recordKeyOf(list: readonly ProfileJson[]): RecordKey | null {
|
|
59
|
+
if (list.length === 0) return null;
|
|
60
|
+
for (const key of RECORD_KEYS) {
|
|
61
|
+
const ids = new Set<string>();
|
|
62
|
+
if (list.every((entry) => isObject(entry) && typeof entry[key] === "string" && !ids.has(entry[key] as string) && ids.add(entry[key] as string))) {
|
|
63
|
+
return key;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Both lists' record field: empty lists take the other's. */
|
|
70
|
+
function sharedRecordKey(a: readonly ProfileJson[], b: readonly ProfileJson[]): RecordKey | null {
|
|
71
|
+
const aKey = recordKeyOf(a);
|
|
72
|
+
const bKey = recordKeyOf(b);
|
|
73
|
+
if (a.length === 0) return bKey;
|
|
74
|
+
if (b.length === 0) return aKey;
|
|
75
|
+
return aKey === bKey ? aKey : null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function recordId(entry: ProfileJson, key: RecordKey | null): string | null {
|
|
79
|
+
if (!isObject(entry)) return null;
|
|
80
|
+
if (key) return typeof entry[key] === "string" ? entry[key] as string : null;
|
|
81
|
+
for (const field of RECORD_KEYS) if (typeof entry[field] === "string") return entry[field] as string;
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isPrimitive(value: ProfileJson): boolean {
|
|
86
|
+
return value === null || typeof value !== "object";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A list of plain values with no repeats, which merges as a set. */
|
|
90
|
+
function isValueSet(list: readonly ProfileJson[]): boolean {
|
|
91
|
+
return list.every(isPrimitive) && new Set(list).size === list.length;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function parseJsonText(value: string): ProfileJson | undefined {
|
|
95
|
+
const text = value.trimStart();
|
|
96
|
+
if (!text.startsWith("[") && !text.startsWith("{")) return undefined;
|
|
97
|
+
try {
|
|
98
|
+
return JSON.parse(value) as ProfileJson;
|
|
99
|
+
} catch {
|
|
100
|
+
// Intentional ignore: a string that only looks like JSON is compared as text.
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The edits that turn `base` into `next`. */
|
|
106
|
+
export function diffProfile(base: ProfileJson | undefined, next: ProfileJson | undefined): ProfileEdit[] {
|
|
107
|
+
const edits: ProfileEdit[] = [];
|
|
108
|
+
diffInto(base, next, [], edits);
|
|
109
|
+
return edits;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function diffInto(base: ProfileJson | undefined, next: ProfileJson | undefined, path: ProfileStep[], edits: ProfileEdit[]): void {
|
|
113
|
+
if (sameProfileJson(base, next)) return;
|
|
114
|
+
if (next === undefined) {
|
|
115
|
+
edits.push({ kind: "remove", path });
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (isObject(base) && isObject(next)) {
|
|
119
|
+
for (const key of Object.keys(base)) {
|
|
120
|
+
if (!Object.hasOwn(next, key)) edits.push({ kind: "remove", path: [...path, { key }] });
|
|
121
|
+
}
|
|
122
|
+
for (const key of Object.keys(next)) diffInto(base[key], next[key], [...path, { key }], edits);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (Array.isArray(base) && Array.isArray(next)) {
|
|
126
|
+
const key = sharedRecordKey(base, next);
|
|
127
|
+
if (key) {
|
|
128
|
+
diffRecords(base, next, key, path, edits);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (isValueSet(base) && isValueSet(next)) {
|
|
132
|
+
const add = next.filter((entry) => !base.includes(entry));
|
|
133
|
+
const remove = base.filter((entry) => !next.includes(entry));
|
|
134
|
+
edits.push(add.length === 0 && remove.length === 0
|
|
135
|
+
? { kind: "set", path, from: base, to: next }
|
|
136
|
+
: { kind: "members", path, from: base, to: next, add, remove });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (typeof base === "string" && typeof next === "string") {
|
|
141
|
+
const baseJson = parseJsonText(base);
|
|
142
|
+
const nextJson = parseJsonText(next);
|
|
143
|
+
if (baseJson !== undefined && nextJson !== undefined) {
|
|
144
|
+
diffInto(baseJson, nextJson, [...path, { json: true }], edits);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
edits.push({ kind: "set", path, from: base, to: next });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function diffRecords(base: ProfileJson[], next: ProfileJson[], key: RecordKey, path: ProfileStep[], edits: ProfileEdit[]): void {
|
|
152
|
+
const baseById = new Map(base.map((entry) => [recordId(entry, key)!, entry]));
|
|
153
|
+
const nextIds = next.map((entry) => recordId(entry, key)!);
|
|
154
|
+
const nextIdSet = new Set(nextIds);
|
|
155
|
+
for (const id of baseById.keys()) {
|
|
156
|
+
if (!nextIdSet.has(id)) edits.push({ kind: "remove", path: [...path, { id }] });
|
|
157
|
+
}
|
|
158
|
+
next.forEach((entry, index) => {
|
|
159
|
+
const id = nextIds[index]!;
|
|
160
|
+
const before = baseById.get(id);
|
|
161
|
+
if (before === undefined) {
|
|
162
|
+
edits.push({ kind: "insert", path, after: index > 0 ? nextIds[index - 1]! : null, record: entry as ProfileObject });
|
|
163
|
+
} else {
|
|
164
|
+
diffInto(before, entry, [...path, { id }], edits);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
const from = [...baseById.keys()].filter((id) => nextIdSet.has(id));
|
|
168
|
+
const to = nextIds.filter((id) => baseById.has(id));
|
|
169
|
+
if (from.some((id, index) => id !== to[index])) edits.push({ kind: "order", path, from, to });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const REMOVED = Symbol("removed");
|
|
173
|
+
type Updated = ProfileJson | undefined | typeof REMOVED;
|
|
174
|
+
|
|
175
|
+
export interface ApplyProfileEditsOptions {
|
|
176
|
+
/**
|
|
177
|
+
* Who keeps a value both sides changed: the target (`keep`, the default),
|
|
178
|
+
* or the side the edits came from (`take`).
|
|
179
|
+
*/
|
|
180
|
+
conflicts?: "keep" | "take";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** `target` with every edit replayed on it; the same object when none lands. */
|
|
184
|
+
export function applyProfileEdits<T extends ProfileJson>(target: T, edits: readonly ProfileEdit[], options: ApplyProfileEditsOptions = {}): T {
|
|
185
|
+
const take = options.conflicts === "take";
|
|
186
|
+
let result: ProfileJson = target;
|
|
187
|
+
for (const edit of edits) {
|
|
188
|
+
const next = updateAt(result, edit.path, 0, (current) => applyLeaf(current, edit, take));
|
|
189
|
+
if (next !== REMOVED && next !== undefined) result = next;
|
|
190
|
+
}
|
|
191
|
+
return result as T;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Rewrites the value at `path` with `update`. A step that no longer resolves
|
|
196
|
+
* (a record or key the target removed, a string that no longer holds JSON)
|
|
197
|
+
* leaves the target as it is: that side's removal wins.
|
|
198
|
+
*/
|
|
199
|
+
function updateAt(value: ProfileJson | undefined, path: readonly ProfileStep[], index: number, update: (current: ProfileJson | undefined) => Updated): Updated {
|
|
200
|
+
if (index === path.length) return update(value);
|
|
201
|
+
const step = path[index]!;
|
|
202
|
+
if ("json" in step) {
|
|
203
|
+
if (typeof value !== "string") return value;
|
|
204
|
+
const parsed = parseJsonText(value);
|
|
205
|
+
if (parsed === undefined) return value;
|
|
206
|
+
const next = updateAt(parsed, path, index + 1, update);
|
|
207
|
+
if (next === parsed || next === REMOVED || next === undefined) return value;
|
|
208
|
+
return value.includes("\n") ? JSON.stringify(next, null, 2) : JSON.stringify(next);
|
|
209
|
+
}
|
|
210
|
+
if ("key" in step) {
|
|
211
|
+
if (!isObject(value)) return value;
|
|
212
|
+
const child = value[step.key];
|
|
213
|
+
// Below a key the target no longer has, there is nothing left to change.
|
|
214
|
+
if (child === undefined && index + 1 < path.length) return value;
|
|
215
|
+
const next = updateAt(child, path, index + 1, update);
|
|
216
|
+
if (next === child) return value;
|
|
217
|
+
if (next === REMOVED || next === undefined) {
|
|
218
|
+
if (!Object.hasOwn(value, step.key)) return value;
|
|
219
|
+
const { [step.key]: _removed, ...rest } = value;
|
|
220
|
+
return rest;
|
|
221
|
+
}
|
|
222
|
+
return { ...value, [step.key]: next };
|
|
223
|
+
}
|
|
224
|
+
if (!Array.isArray(value)) return value;
|
|
225
|
+
const position = value.findIndex((entry) => recordId(entry, null) === step.id);
|
|
226
|
+
if (position < 0) return value;
|
|
227
|
+
const child = value[position]!;
|
|
228
|
+
const next = updateAt(child, path, index + 1, update);
|
|
229
|
+
if (next === child) return value;
|
|
230
|
+
if (next === REMOVED || next === undefined) return value.filter((_, at) => at !== position);
|
|
231
|
+
return value.map((entry, at) => (at === position ? next : entry));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function applyLeaf(current: ProfileJson | undefined, edit: ProfileEdit, take: boolean): Updated {
|
|
235
|
+
switch (edit.kind) {
|
|
236
|
+
case "remove":
|
|
237
|
+
return current === undefined ? current : REMOVED;
|
|
238
|
+
case "set":
|
|
239
|
+
if (sameProfileJson(current, edit.to)) return current;
|
|
240
|
+
if (take || sameProfileJson(current, edit.from)) return edit.to;
|
|
241
|
+
// Both sides added the same key: keep this side's values and take what only the other has.
|
|
242
|
+
if (edit.from === undefined && isObject(current) && isObject(edit.to)) return fillMissing(current, edit.to);
|
|
243
|
+
return current;
|
|
244
|
+
case "members": {
|
|
245
|
+
if (sameProfileJson(current, edit.from)) return edit.to;
|
|
246
|
+
if (!Array.isArray(current)) return current;
|
|
247
|
+
const kept = current.filter((entry) => !edit.remove.some((removed) => sameProfileJson(removed, entry)));
|
|
248
|
+
const added = edit.add.filter((entry) => !kept.some((present) => sameProfileJson(present, entry)));
|
|
249
|
+
const next = [...kept, ...added];
|
|
250
|
+
return sameProfileJson(next, current) ? current : next;
|
|
251
|
+
}
|
|
252
|
+
case "insert": {
|
|
253
|
+
if (!Array.isArray(current)) return current;
|
|
254
|
+
const id = recordId(edit.record, null);
|
|
255
|
+
const existing = current.findIndex((entry) => recordId(entry, null) === id);
|
|
256
|
+
if (existing >= 0) {
|
|
257
|
+
const present = current[existing]!;
|
|
258
|
+
if (!isObject(present)) return current;
|
|
259
|
+
const filled = fillMissing(present, edit.record);
|
|
260
|
+
return filled === present ? current : current.map((entry, at) => (at === existing ? filled : entry));
|
|
261
|
+
}
|
|
262
|
+
const afterIndex = edit.after === null ? -1 : current.findIndex((entry) => recordId(entry, null) === edit.after);
|
|
263
|
+
const at = edit.after === null ? 0 : afterIndex >= 0 ? afterIndex + 1 : current.length;
|
|
264
|
+
return [...current.slice(0, at), edit.record, ...current.slice(at)];
|
|
265
|
+
}
|
|
266
|
+
case "order": {
|
|
267
|
+
if (!Array.isArray(current)) return current;
|
|
268
|
+
const wanted = new Set(edit.to);
|
|
269
|
+
const positions: number[] = [];
|
|
270
|
+
const present: string[] = [];
|
|
271
|
+
current.forEach((entry, at) => {
|
|
272
|
+
const id = recordId(entry, null);
|
|
273
|
+
if (id !== null && wanted.has(id)) {
|
|
274
|
+
positions.push(at);
|
|
275
|
+
present.push(id);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
// A list this side reordered keeps its own order.
|
|
279
|
+
const fromOrder = edit.from.filter((id) => present.includes(id));
|
|
280
|
+
if (fromOrder.some((id, at) => id !== present[at])) return current;
|
|
281
|
+
const toOrder = edit.to.filter((id) => present.includes(id));
|
|
282
|
+
const byId = new Map(current.map((entry) => [recordId(entry, null), entry]));
|
|
283
|
+
const next = [...current];
|
|
284
|
+
positions.forEach((position, at) => { next[position] = byId.get(toOrder[at]!)!; });
|
|
285
|
+
return sameProfileJson(next, current) ? current : next;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** `present` with the keys only `added` has, recursively. */
|
|
291
|
+
function fillMissing(present: ProfileObject, added: ProfileObject): ProfileObject {
|
|
292
|
+
let result = present;
|
|
293
|
+
for (const [key, value] of Object.entries(added)) {
|
|
294
|
+
const mine = present[key];
|
|
295
|
+
const next = mine === undefined ? value : isObject(mine) && isObject(value) ? fillMissing(mine, value) : mine;
|
|
296
|
+
if (next !== mine) result = { ...result, [key]: next };
|
|
297
|
+
}
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
@@ -87,11 +87,20 @@ self.onmessage = (event) => {
|
|
|
87
87
|
};
|
|
88
88
|
`;
|
|
89
89
|
|
|
90
|
+
/** What `PRAGMA wal_checkpoint` answers: frames in the WAL, and how many of them now sit in the database. */
|
|
91
|
+
export interface WalCheckpointResult {
|
|
92
|
+
busy?: number;
|
|
93
|
+
log?: number;
|
|
94
|
+
checkpointed?: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
90
97
|
export interface WalCheckpointerOptions {
|
|
91
98
|
/** Overrides how long the thread may owe an answer before it counts as stuck. */
|
|
92
99
|
stuckAfterMs?: number;
|
|
93
100
|
/** Overrides the thread's code, so a test can run one that never answers. */
|
|
94
101
|
workerSource?: string;
|
|
102
|
+
/** Runs on the app's thread after each checkpoint, so a test can wait for one instead of the clock. */
|
|
103
|
+
onCheckpoint?: (result: WalCheckpointResult) => void;
|
|
95
104
|
}
|
|
96
105
|
|
|
97
106
|
export interface WalCheckpointer {
|
|
@@ -107,7 +116,7 @@ interface WorkerMessage {
|
|
|
107
116
|
ms?: number;
|
|
108
117
|
startedAt?: number;
|
|
109
118
|
endedAt?: number;
|
|
110
|
-
result?:
|
|
119
|
+
result?: WalCheckpointResult;
|
|
111
120
|
}
|
|
112
121
|
|
|
113
122
|
/**
|
|
@@ -251,6 +260,7 @@ export function startWalCheckpointer(
|
|
|
251
260
|
endedAt: Math.round(message.endedAt ?? 0),
|
|
252
261
|
}, { lastStep: false });
|
|
253
262
|
if (pending) schedule();
|
|
263
|
+
options.onCheckpoint?.(message.result ?? {});
|
|
254
264
|
}
|
|
255
265
|
};
|
|
256
266
|
worker.onerror = (event) => {
|
|
@@ -78,7 +78,7 @@ export interface SqliteDatabaseOptions {
|
|
|
78
78
|
* another program's lock (see access.ts). Commands and tests wait it out.
|
|
79
79
|
*/
|
|
80
80
|
interactive?: boolean;
|
|
81
|
-
/** Tests only: the WAL checkpointer's thread
|
|
81
|
+
/** Tests only: the WAL checkpointer's thread, how long it may go silent, and what it reports. */
|
|
82
82
|
checkpointer?: WalCheckpointerOptions;
|
|
83
83
|
}
|
|
84
84
|
|
|
@@ -1,20 +1,60 @@
|
|
|
1
1
|
import { logFailure } from "../utils/log-failure";
|
|
2
2
|
import type { TickerRecord, TickerMetadata } from "../types/ticker";
|
|
3
|
-
import type { TickerStore } from "./ticker-store";
|
|
3
|
+
import type { ResolvedTickerRow, TickerStore } from "./ticker-store";
|
|
4
4
|
import { hydrateTickerMetadata } from "../tickers/metadata";
|
|
5
|
-
import { runTickerWrite } from "./ticker-writes";
|
|
5
|
+
import { hasPendingTickerWrite, runTickerWrite } from "./ticker-writes";
|
|
6
|
+
import { serializeJson } from "./sqlite/json";
|
|
7
|
+
import { applyProfileEdits, diffProfile, type ProfileJson } from "./profile-merge";
|
|
6
8
|
|
|
7
9
|
function parseTickerMetadata(json: string): TickerMetadata {
|
|
8
10
|
return hydrateTickerMetadata(JSON.parse(json) as Record<string, unknown>);
|
|
9
11
|
}
|
|
10
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Ticker rows are shared like config.json: a `gloom watchlist add` or
|
|
15
|
+
* `portfolio position set` run beside the app writes the row the app holds a
|
|
16
|
+
* copy of, and another copy of the app (the desktop app beside the terminal
|
|
17
|
+
* one) can delete a row in its cleanup. Each row's copy is kept as it was read
|
|
18
|
+
* or last saved here. A save over a row another program changed since keeps
|
|
19
|
+
* that program's change and adds this one's (see `profile-merge.ts`); a save
|
|
20
|
+
* over a row another program deleted since leaves it deleted, as a removal
|
|
21
|
+
* always lands. Which of those it is gets decided in the write's own
|
|
22
|
+
* transaction, when it lands (`TickerStore.saveResolved`).
|
|
23
|
+
*/
|
|
11
24
|
export class TickerRepository {
|
|
25
|
+
/**
|
|
26
|
+
* Each row as this program's copy of it was read or last saved, by symbol:
|
|
27
|
+
* what the next save of it is compared with. Only a write that landed moves
|
|
28
|
+
* it, never one still queued or dropped for a later write of the same row.
|
|
29
|
+
*/
|
|
30
|
+
private readonly copies = new Map<string, string>();
|
|
31
|
+
/**
|
|
32
|
+
* Rows this program holds a copy of that a read of every row found gone:
|
|
33
|
+
* another program deleted them. Their copies stay, so a save of the app's
|
|
34
|
+
* copy still leaves them deleted, until the app has dropped them from what
|
|
35
|
+
* it shows (`takeRowsDeletedElsewhere`) and lets them go.
|
|
36
|
+
*/
|
|
37
|
+
private readonly goneElsewhere = new Set<string>();
|
|
38
|
+
|
|
12
39
|
constructor(private readonly tickers: TickerStore) {}
|
|
13
40
|
|
|
41
|
+
/** Changes when another program commits to the database (see `TickerStore.changesElsewhere`). */
|
|
42
|
+
changesElsewhere(): number | null {
|
|
43
|
+
return this.tickers.changesElsewhere();
|
|
44
|
+
}
|
|
45
|
+
|
|
14
46
|
async loadAllTickers(): Promise<TickerRecord[]> {
|
|
15
|
-
|
|
47
|
+
const rows = this.tickers.getAll();
|
|
48
|
+
// A row this program holds a copy of that is gone was deleted elsewhere, whoever wrote it last.
|
|
49
|
+
const present = new Set(rows.map((row) => row.symbol));
|
|
50
|
+
for (const symbol of this.copies.keys()) {
|
|
51
|
+
if (!present.has(symbol) && !this.writing(symbol)) this.goneElsewhere.add(symbol);
|
|
52
|
+
}
|
|
53
|
+
return rows.flatMap((row) => {
|
|
16
54
|
try {
|
|
17
|
-
|
|
55
|
+
const record = { metadata: parseTickerMetadata(row.metadata) };
|
|
56
|
+
this.noteRead(row.symbol, row.metadata);
|
|
57
|
+
return [record];
|
|
18
58
|
} catch (error) {
|
|
19
59
|
logFailure("data.ticker-repository", "Reading stored ticker metadata failed")(error);
|
|
20
60
|
return [];
|
|
@@ -24,9 +64,14 @@ export class TickerRepository {
|
|
|
24
64
|
|
|
25
65
|
async loadTicker(symbol: string): Promise<TickerRecord | null> {
|
|
26
66
|
const json = this.tickers.get(symbol);
|
|
27
|
-
if (!json)
|
|
67
|
+
if (!json) {
|
|
68
|
+
this.noteRead(symbol, null);
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
28
71
|
try {
|
|
29
|
-
|
|
72
|
+
const record = { metadata: parseTickerMetadata(json) };
|
|
73
|
+
this.noteRead(symbol, json);
|
|
74
|
+
return record;
|
|
30
75
|
} catch (error) {
|
|
31
76
|
logFailure("data.ticker-repository", "Reading stored ticker metadata failed")(error);
|
|
32
77
|
return null;
|
|
@@ -35,26 +80,105 @@ export class TickerRepository {
|
|
|
35
80
|
|
|
36
81
|
saveTicker(ticker: TickerRecord): Promise<void> {
|
|
37
82
|
return runTickerWrite(this, ticker.metadata.ticker, () => {
|
|
38
|
-
this.tickers.
|
|
83
|
+
this.tickers.saveResolved([this.rowToSave(ticker.metadata)]);
|
|
39
84
|
});
|
|
40
85
|
}
|
|
41
86
|
|
|
42
87
|
/** Saves several tickers in one write. */
|
|
43
88
|
saveTickers(tickers: readonly TickerRecord[]): Promise<void> {
|
|
44
89
|
return runTickerWrite(this, tickers.map((ticker) => ticker.metadata.ticker), () => {
|
|
45
|
-
this.tickers.
|
|
90
|
+
this.tickers.saveResolved(tickers.map((ticker) => this.rowToSave(ticker.metadata)));
|
|
46
91
|
});
|
|
47
92
|
}
|
|
48
93
|
|
|
49
94
|
createTicker(metadata: TickerMetadata): Promise<TickerRecord> {
|
|
50
95
|
const ticker: TickerRecord = { metadata };
|
|
51
96
|
return runTickerWrite(this, metadata.ticker, () => {
|
|
52
|
-
this.tickers.
|
|
97
|
+
this.tickers.saveResolved([this.rowToSave(metadata)]);
|
|
53
98
|
return ticker;
|
|
54
99
|
});
|
|
55
100
|
}
|
|
56
101
|
|
|
57
102
|
deleteTicker(symbol: string): Promise<void> {
|
|
58
|
-
return runTickerWrite(this, symbol, () =>
|
|
103
|
+
return runTickerWrite(this, symbol, () => {
|
|
104
|
+
this.tickers.delete(symbol);
|
|
105
|
+
this.copies.delete(symbol);
|
|
106
|
+
this.goneElsewhere.delete(symbol);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The rows another program deleted that this program held, as reads of
|
|
112
|
+
* every row found them gone since the last call, for the app to drop from
|
|
113
|
+
* what it shows; their copies go with them, so a ticker added again after
|
|
114
|
+
* is a new row. A row with a save of it on its way is kept for a later
|
|
115
|
+
* call: that save is still compared with its copy.
|
|
116
|
+
*/
|
|
117
|
+
takeRowsDeletedElsewhere(): string[] {
|
|
118
|
+
const taken = [...this.goneElsewhere].filter((symbol) => !this.writing(symbol));
|
|
119
|
+
for (const symbol of taken) {
|
|
120
|
+
this.goneElsewhere.delete(symbol);
|
|
121
|
+
this.copies.delete(symbol);
|
|
122
|
+
}
|
|
123
|
+
return taken;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Whether a save of `symbol` is on its way, or waits behind another program's lock. */
|
|
127
|
+
private writing(symbol: string): boolean {
|
|
128
|
+
return hasPendingTickerWrite(this, symbol) || this.tickers.isQueued(symbol);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A row read is what this program's copy of it is built on from now (null:
|
|
133
|
+
* there is none, so the next save writes a new row), but not while a save
|
|
134
|
+
* of the row is on its way or waits behind another program's lock: that
|
|
135
|
+
* save was built on the copy before, and is compared with that one, and
|
|
136
|
+
* what the read sees of the row is that save's own.
|
|
137
|
+
*/
|
|
138
|
+
private noteRead(symbol: string, metadata: string | null): void {
|
|
139
|
+
if (this.writing(symbol)) return;
|
|
140
|
+
this.goneElsewhere.delete(symbol);
|
|
141
|
+
if (metadata === null) this.copies.delete(symbol);
|
|
142
|
+
else this.copies.set(symbol, metadata);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* `metadata`, this program's copy, as a row the store resolves when the
|
|
147
|
+
* write lands, against the copy it was built on (`copies`, as it stands
|
|
148
|
+
* then):
|
|
149
|
+
* - no copy: a row this program has not read, written as it is;
|
|
150
|
+
* - the stored row is gone: another program deleted it since, and it stays
|
|
151
|
+
* deleted until the app has dropped it (`takeRowsDeletedElsewhere`) or
|
|
152
|
+
* reads that one row anew (`loadTicker`);
|
|
153
|
+
* - the stored row is the copy, or this very row: written as it is;
|
|
154
|
+
* - otherwise another program changed it, and that change is folded in.
|
|
155
|
+
* Once written, the copy is what this program built, so a later save of a
|
|
156
|
+
* copy that never read the change folds it in again.
|
|
157
|
+
*/
|
|
158
|
+
private rowToSave(metadata: TickerMetadata): ResolvedTickerRow {
|
|
159
|
+
const symbol = metadata.ticker;
|
|
160
|
+
const mine = serializeJson(metadata);
|
|
161
|
+
return {
|
|
162
|
+
symbol,
|
|
163
|
+
preview: metadata,
|
|
164
|
+
resolve: (stored) => {
|
|
165
|
+
const base = this.copies.get(symbol);
|
|
166
|
+
if (base === undefined) return metadata;
|
|
167
|
+
if (stored === null) return null;
|
|
168
|
+
if (stored === base || stored === mine) return metadata;
|
|
169
|
+
try {
|
|
170
|
+
const edits = diffProfile(JSON.parse(base) as ProfileJson, JSON.parse(stored) as ProfileJson);
|
|
171
|
+
return applyProfileEdits(JSON.parse(mine) as ProfileJson, edits);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
logFailure("data.ticker-repository", "Reading a ticker row another program changed failed")(error);
|
|
174
|
+
return metadata;
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
landed: (written) => {
|
|
178
|
+
if (written === null) return;
|
|
179
|
+
this.copies.set(symbol, mine);
|
|
180
|
+
this.goneElsewhere.delete(symbol);
|
|
181
|
+
},
|
|
182
|
+
};
|
|
59
183
|
}
|
|
60
184
|
}
|
package/src/data/ticker-store.ts
CHANGED
|
@@ -12,6 +12,21 @@ interface PendingTicker {
|
|
|
12
12
|
metadata: string | null;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* A row to save that was built on an earlier copy of it, so what it writes is
|
|
17
|
+
* only known once the write lands: `resolve` gets the row as stored at that
|
|
18
|
+
* moment (null when there is none) and returns what to write, or null to
|
|
19
|
+
* leave the row as it is. It can run more than once (a write the lock held up
|
|
20
|
+
* runs again), so it has no effects of its own; `landed` hears what was
|
|
21
|
+
* written, null for nothing. `preview` is what reads see while it waits.
|
|
22
|
+
*/
|
|
23
|
+
export interface ResolvedTickerRow {
|
|
24
|
+
symbol: string;
|
|
25
|
+
preview: unknown;
|
|
26
|
+
resolve: (stored: string | null) => unknown;
|
|
27
|
+
landed?: (written: string | null) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
15
30
|
export class TickerStore {
|
|
16
31
|
/** Writes queued behind another program's lock, by symbol, read back until they land. */
|
|
17
32
|
private readonly pending = new Map<string, PendingTicker>();
|
|
@@ -38,6 +53,27 @@ export class TickerStore {
|
|
|
38
53
|
return [...bySymbol.values()].sort((a, b) => (a.symbol < b.symbol ? -1 : a.symbol > b.symbol ? 1 : 0));
|
|
39
54
|
}
|
|
40
55
|
|
|
56
|
+
/**
|
|
57
|
+
* A number that changes when another program (a `gloom` command) commits
|
|
58
|
+
* to the database, and only then: SQLite's `data_version`. Null when the
|
|
59
|
+
* database cannot be read just now.
|
|
60
|
+
*/
|
|
61
|
+
changesElsewhere(): number | null {
|
|
62
|
+
try {
|
|
63
|
+
return this.access.run("read data version", () => (
|
|
64
|
+
this.db.query<{ data_version: number }, []>("PRAGMA data_version").get()?.data_version ?? null
|
|
65
|
+
));
|
|
66
|
+
} catch {
|
|
67
|
+
// Intentional ignore: an unreadable database is asked again on the next look.
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Whether a write of `symbol` waits behind another program's lock: reads see it, the database does not yet. */
|
|
73
|
+
isQueued(symbol: string): boolean {
|
|
74
|
+
return this.pending.has(symbol);
|
|
75
|
+
}
|
|
76
|
+
|
|
41
77
|
get(symbol: string): string | null {
|
|
42
78
|
const pending = this.pending.get(symbol);
|
|
43
79
|
if (pending) return pending.metadata;
|
|
@@ -75,6 +111,37 @@ export class TickerStore {
|
|
|
75
111
|
);
|
|
76
112
|
}
|
|
77
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Saves rows whose content depends on the row stored when the write lands
|
|
116
|
+
* (`ResolvedTickerRow`): each is read, resolved and written in one
|
|
117
|
+
* immediate transaction, so another program's commit can never fall between
|
|
118
|
+
* the read and the write, whether the write lands now or later, behind
|
|
119
|
+
* another program's lock.
|
|
120
|
+
*/
|
|
121
|
+
saveResolved(rows: readonly ResolvedTickerRow[]): void {
|
|
122
|
+
if (rows.length === 0) return;
|
|
123
|
+
this.write(
|
|
124
|
+
rows.length === 1 ? "save ticker" : "save tickers",
|
|
125
|
+
rows.map((row) => [row.symbol, { metadata: serializeJson(row.preview) }]),
|
|
126
|
+
() => {
|
|
127
|
+
const read = this.db.query<{ metadata: string }, [string]>("SELECT metadata FROM tickers WHERE symbol = ?");
|
|
128
|
+
const insert = this.db.query(
|
|
129
|
+
`INSERT OR REPLACE INTO tickers (symbol, metadata, updated_at)
|
|
130
|
+
VALUES (?, ?, ?)`,
|
|
131
|
+
);
|
|
132
|
+
const now = Date.now();
|
|
133
|
+
const written = this.db.transaction(() => rows.map((row) => {
|
|
134
|
+
const next = row.resolve(read.get(row.symbol)?.metadata ?? null);
|
|
135
|
+
if (next == null) return null;
|
|
136
|
+
const metadata = serializeJson(next);
|
|
137
|
+
insert.run(row.symbol, metadata, now);
|
|
138
|
+
return metadata;
|
|
139
|
+
})).immediate();
|
|
140
|
+
rows.forEach((row, index) => row.landed?.(written[index] ?? null));
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
78
145
|
delete(symbol: string): void {
|
|
79
146
|
this.write("delete ticker", [[symbol, { metadata: null }]], () => {
|
|
80
147
|
this.db.query("DELETE FROM tickers WHERE symbol = ?").run(symbol);
|
|
@@ -71,6 +71,11 @@ export function hasPendingTickerWrites(repository: object): boolean {
|
|
|
71
71
|
return (repositories.get(repository)?.pending.size ?? 0) > 0;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/** Whether a write of `symbol` is requested and not yet done. */
|
|
75
|
+
export function hasPendingTickerWrite(repository: object, symbol: string): boolean {
|
|
76
|
+
return repositories.get(repository)?.tails.has(tickerKey(symbol)) ?? false;
|
|
77
|
+
}
|
|
78
|
+
|
|
74
79
|
/** Let save-await callers publish their edits before cleanup reads the current rows. */
|
|
75
80
|
export async function settleTickerWrites(repository: object): Promise<void> {
|
|
76
81
|
const writes = writesFor(repository);
|
package/src/i18n/es.ts
CHANGED
|
@@ -1615,6 +1615,12 @@ export const es: Record<string, string> = {
|
|
|
1615
1615
|
"TICKERS": "TICKERS",
|
|
1616
1616
|
"Which tickers do you follow?": "¿Qué tickers sigues?",
|
|
1617
1617
|
"Pick up to 3 for your watchlist.": "Elige hasta 3 para tu lista de seguimiento.",
|
|
1618
|
+
// "Where did you hear about Gloom?", once, after an account signs up
|
|
1619
|
+
"Where did you hear about Gloom?": "¿Dónde conociste Gloom?",
|
|
1620
|
+
"AI assistant": "Asistente de IA",
|
|
1621
|
+
"Friend or colleague": "Un amigo o colega",
|
|
1622
|
+
"Article or newsletter": "Un artículo o newsletter",
|
|
1623
|
+
"Where?": "¿Dónde?",
|
|
1618
1624
|
"Skip": "Omitir",
|
|
1619
1625
|
"Equities": "Renta variable",
|
|
1620
1626
|
"Futures & commodities": "Futuros y materias primas",
|
|
@@ -1825,4 +1831,25 @@ export const es: Record<string, string> = {
|
|
|
1825
1831
|
"pre": "pre",
|
|
1826
1832
|
"post": "post",
|
|
1827
1833
|
"closed": "cerrado",
|
|
1834
|
+
// ── Pro: the first weeks' row and the paid cancel ──────────
|
|
1835
|
+
"Try Pro on your tickers": "Prueba Pro con tus tickers",
|
|
1836
|
+
"Try Pro": "Prueba Pro",
|
|
1837
|
+
"Stop suggesting Pro functions": "Dejar de sugerir funciones Pro",
|
|
1838
|
+
"Open {command}": "Abrir {command}",
|
|
1839
|
+
"Options flow": "Flujo de opciones",
|
|
1840
|
+
"X mentions": "Menciones en X",
|
|
1841
|
+
"Hiring": "Contratación",
|
|
1842
|
+
"8-K events": "Eventos 8-K",
|
|
1843
|
+
"Risk factors": "Factores de riesgo",
|
|
1844
|
+
"Cancel Pro": "Cancelar Pro",
|
|
1845
|
+
"Cancel Pro?": "¿Cancelar Pro?",
|
|
1846
|
+
"Keep Pro": "Mantener Pro",
|
|
1847
|
+
"Pro until {date}": "Pro hasta el {date}",
|
|
1848
|
+
"Cancelling...": "Cancelando...",
|
|
1849
|
+
"Cancel Subscription": "Cancelar suscripción",
|
|
1850
|
+
"Keep My Subscription": "Mantener mi suscripción",
|
|
1851
|
+
"Continue Cancellation": "Continuar con la cancelación",
|
|
1852
|
+
"Pro stays on until {date}.": "Pro sigue activo hasta el {date}.",
|
|
1853
|
+
"Pro stays on until {date}. Cancelling now stops the renewal after that date.": "Pro sigue activo hasta el {date}. Si cancelas ahora, no se renovará después de esa fecha.",
|
|
1854
|
+
"Cancelled. Pro stays on until {date}.": "Cancelado. Pro sigue activo hasta el {date}.",
|
|
1828
1855
|
};
|