TinyBase logoTinyBase

addLinkedRowIdsListener

The addLinkedRowIdsListener method registers a listener function with the Relationships object that will be called whenever the linked Row Ids in a linked list Relationship change.

addLinkedRowIdsListener(
  relationshipId: string,
  firstRowId: string,
  listener: LinkedRowIdsListener,
): string
TypeDescription
relationshipIdstring

The Id of the Relationship to listen to.

firstRowIdstring

The Id of the first Row of the linked list to listen to.

listenerLinkedRowIdsListener

The function that will be called whenever the linked Row Ids change.

returnsstring

A unique Id for the listener that can later be used to remove it.

A linked list Relationship is one that has the same Table specified as both local Table Id and remote Table Id, allowing you to create a sequence of Row objects within that one Table.

You listen to changes to a linked list starting from a single first Row by specifying the Relationship Id and local Row Id as the method's first two parameters.

Unlike other listener registration methods, you cannot provide null wildcards for the first two parameters of the addLinkedRowIdsListener method. This prevents the prohibitive expense of tracking all the possible linked lists (and partial linked lists within them) in a Store.

The provided listener is a LinkedRowIdsListener function, and will be called with a reference to the Relationships object, the Id of the Relationship, and the Id of the first Row that had its linked list change.

Example

This example creates a Store, a Relationships object, and then registers a listener that responds to any changes to a specific first Row's linked Row objects.

import {createRelationships, createStore} from 'tinybase';
const store = createStore().setTable('pets', {
  fido: {species: 'dog', next: 'felix'},
  felix: {species: 'cat', next: 'cujo'},
  cujo: {species: 'dog'},
});

const relationships = createRelationships(store);
relationships.setRelationshipDefinition(
  'petSequence',
  'pets',
  'pets',
  'next',
);

const listenerId = relationships.addLinkedRowIdsListener(
  'petSequence',
  'fido',
  (relationships) => {
    console.log('petSequence linked list (from fido) changed');
    console.log(relationships.getLinkedRowIds('petSequence', 'fido'));
  },
);

store.setRow('pets', 'toto', {species: 'dog'});
store.setCell('pets', 'cujo', 'next', 'toto');
// -> 'petSequence linked list (from fido) changed'
// -> ['fido', 'felix', 'cujo', 'toto']

relationships.delListener(listenerId);

Since

v1.0.0