Skip to content

Commit

Permalink
Merge pull request #2014 from genesiscommunitysuccess/PTL-1697
Browse files Browse the repository at this point in the history
docs: observer utilities PTL-1697
  • Loading branch information
patrickoneill-genesis authored Dec 2, 2024
2 parents 374a3aa + b34ebef commit 828019d
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: 'Observer utilities'
sidebar_label: 'Observer utilities'
id: observer-util
keywords: [utils, utility, observer, publish, subscribe, custom, events]
tags:
- utils
- utility
- observer
- publish
- subscribe
- custom
- events
---

This includes utilities for creating event observers and handling element visibility changes, facilitating the subscription to and publication of custom events, and responding to changes in element visibility.

## Key Features

- **`Listener` Type:** Defines a callback function that receives an event of a specific type.
- **`Subscribe` Type:** Represents a function that subscribes a listener to receive event notifications, returning an unsubscribe function for cleanup.
- **`Publish` Type:** A function that broadcasts an event to all subscribed listeners.
- **`Observer` Interface:** Outlines the structure of an event observer, including methods for subscribing to events and publishing events.

## Examples

### Event Observer

The event observer utility provides a pattern for creating a publish-subscribe mechanism, allowing parts of your application to subscribe to and receive notifications for specific events.

#### Creating an Observer

```typescript
import { createObserver, Listener } from '@genesislcap/foundation-utils';

interface ExampleEvent {
message: string;
}

const exampleObserver = createObserver<ExampleEvent>();

const unsubscribe = exampleObserver.subscribe((ev) => {
console.log(ev.message);
});

// Publishing an event
exampleObserver.publish({ message: 'Hello Observer Pattern!' });
```

### Visibility Observer

This utility leverages the `IntersectionObserver` API to monitor the visibility of a specified HTML element, invoking a callback whenever the visibility state changes.

#### respondToVisibility Usage

```typescript
import { respondToVisibility } from '@genesislcap/foundation-utils';

const targetElement = document.getElementById('target');

respondToVisibility(targetElement, (isVisible) => {
console.log(`Element is now ${isVisible ? 'visible' : 'hidden'}`);
});
```

## Key Points

- **Promises:** All operations return Promises, allowing for async handling.
- **Event Listeners:** Use event listeners to react to database changes and perform additional actions.
- **In-Memory Database:** This module is designed for small-scale applications and prototyping. For larger projects, consider using a dedicated database solution.

0 comments on commit 828019d

Please sign in to comment.