Skip to content

Favourite Manager v1.0.60

singleton + class

Global singleton + instantiable class. Stores complete ILocus snapshots—the RNA / DNA / CDS views of the same gene are favourited independently.

import { favouriteManager, FavouriteManager } from "@rbrowser/main";
// Usually use the singleton directly: favouriteManager
// When you need multiple independent favourite lists: new FavouriteManager()

v1.0.50 switched to ILocus storage

Migrated in sync with historyManager. Legacy data in localStorage (key tbrowser_favorites_v1) is converted automatically; the public API exclusively sends and receives ILocus, with no more region string parameters.


Method

add

method

favouriteManager.add(locus: ILocus): void

Add to favourites. No-op when an existing entry with the same identity key is already present. Forces isFavorite: true on storage.

const locus = renderer.locusManager.getLocus();
favouriteManager.add(locus);

has / hasKey

method

favouriteManager.has(locus: ILocus): boolean
favouriteManager.hasKey(key: string): boolean

Check whether something is favourited by its identity key. Use hasKey() when you already have the key string—the internal hot path goes through this (LocusManager.getIsFavorite() uses it).

toggle

method

favouriteManager.toggle(locus: ILocus): boolean

Toggle the favourite state. Returns whether it is favourited after the toggle: true = added, false = removed.

const nowFav = favouriteManager.toggle(renderer.locusManager.getLocus());
console.log(nowFav ? "★ Favourited" : "☆ Removed");

remove / removeKey

method

favouriteManager.remove(locus: ILocus): boolean
favouriteManager.removeKey(key: string): boolean

Remove by identity key, returning whether something was actually deleted. removeKey() saves a computation when you already have the key.

clear

method

favouriteManager.clear(): void

Clear all favourites.

getAll

method

favouriteManager.getAll(): ILocus[]

Returns an array of all favourited ILocus. For front-end list rendering you can use each entry's identity as the React key and display coordinates / names.

count

getter

favouriteManager.count: number

The current number of favourites.

on

method

favouriteManager.on(listener: FavouriteListener): () => void

Subscribe to favourite-list events. Returns an unsubscribe function.

const unsubscribe = favouriteManager.on((event, detail) => {
  switch (event) {
    case "add":
    case "remove":
    case "toggle":
      console.log(event, detail?.key, "→", detail?.isFavourite);
      // detail.locus is the complete ILocus snapshot
      break;
    case "clear":
      console.log("Favourites cleared");
      break;
  }
});
unsubscribe();

Event

event

Event name Trigger detail fields
"add" add() called and the locus did not previously exist locus, key, isFavourite: true
"remove" remove() called and something was actually deleted locus, key, isFavourite: false
"toggle" toggle() called locus, key, isFavourite
"clear" clear() called ——

isFavourite vs isFavorite spelling

The field in the event detail uses the British spelling isFavourite; whereas the ILocus snapshot persisted to localStorage carries the American spelling isFavorite. These are two different fields in two different places—be careful not to mix them up.

FavouriteEvent / FavouriteListener

type

type FavouriteEvent = "add" | "remove" | "toggle" | "clear";

type FavouriteListener = (
  event: FavouriteEvent,
  detail?: {
    locus?: ILocus;
    key?: string;
    isFavourite?: boolean;
  },
) => void;