TinyBase logoTinyBase β

getMetrics

The getMetrics function returns the default Metrics object from the current Provider context (or a named one if an Id is provided).

getMetrics(id?: string): Metrics | undefined
TypeDescription
id?string

An optional Id of a named Metrics object in the Provider context.

returnsMetrics | undefined

The Metrics object, or undefined if not found.

Example

This example reads a TinyBase object from Svelte context inside a child component.

Child.svelte
<svelte:options runes={true} />

<script>
  import {getMetrics} from 'tinybase/ui-svelte';
</script>

{getMetrics()?.getMetric('petCount')}
App.svelte
<svelte:options runes={true} />

<script>
  import {Provider} from 'tinybase/ui-svelte';
  import Child from './Child.svelte';

  let {metrics} = $props();
</script>

<Provider {metrics}>
  <Child />
</Provider>
import {flushSync, mount} from 'svelte';
import {createMetrics, createStore} from 'tinybase';
import App from './App.svelte';

const store = createStore().setCell('pets', 'fido', 'species', 'dog');
const metrics = createMetrics(store).setMetricDefinition(
  'petCount',
  'pets',
  'count',
);
const app = document.body.appendChild(document.createElement('div'));
flushSync(() => mount(App, {target: app, props: {metrics}}));
console.log(app.textContent);
// -> '1'

Since

v8.1.0