Add some type utils, findFeed, and fix feed listener to use limit: 0 instead of since
This commit is contained in:
@@ -30,6 +30,12 @@ export class FeedController {
|
||||
|
||||
// Load events with specified limit
|
||||
load(limit: number): Promise<void>
|
||||
|
||||
// Get listener function (memoized)
|
||||
getListener(): Promise<() => Promise<void>>
|
||||
|
||||
// Listen for new events in the feed
|
||||
listen(): Promise<void>
|
||||
}
|
||||
```
|
||||
|
||||
@@ -90,4 +96,10 @@ await controller.load(50)
|
||||
|
||||
// Load more events
|
||||
await controller.load(50)
|
||||
|
||||
// Listen for new events
|
||||
const unlisten = controller.listen()
|
||||
|
||||
// Unsubscribe from listener
|
||||
unlisten()
|
||||
```
|
||||
|
||||
@@ -123,6 +123,23 @@ walkFeed(feed, (node) => {
|
||||
})
|
||||
```
|
||||
|
||||
Find a specific feed in a feed tree:
|
||||
|
||||
```typescript
|
||||
const feed = makeIntersectionFeed(
|
||||
makeAuthorFeed("pubkey1"),
|
||||
makeUnionFeed(
|
||||
makeKindFeed(1),
|
||||
makeTagFeed("#t", "bitcoin")
|
||||
)
|
||||
)
|
||||
|
||||
// Find a feed matching a specific condition
|
||||
const bitcoinTagFeed = findFeed(feed, (f) =>
|
||||
isTagFeed(f) && getFeedArgs(f)[1] === "bitcoin"
|
||||
)
|
||||
```
|
||||
|
||||
## Feed Simplification
|
||||
|
||||
Flatten nested feeds of the same type:
|
||||
|
||||
@@ -2,6 +2,36 @@
|
||||
|
||||
The `Tools` module provides a comprehensive collection of utility functions for common programming tasks. It includes functions for array manipulation, object handling, type checking, math operations, and more.
|
||||
|
||||
## Working with types
|
||||
|
||||
Utility types for TypeScript development:
|
||||
|
||||
```typescript
|
||||
// Override properties of type T with properties from type R
|
||||
export type Override<T, R> = Omit<T, keyof R> & R;
|
||||
|
||||
// Make specific properties of type T optional
|
||||
export type MakeOptional<T, K extends keyof T> = Override<T, Partial<Pick<T, K>>>;
|
||||
```
|
||||
|
||||
Examples:
|
||||
```typescript
|
||||
interface User {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
age: number;
|
||||
}
|
||||
|
||||
// Override the id to be a number instead
|
||||
type UserWithNumericId = Override<User, { id: number }>;
|
||||
// Result: { id: number; name: string; email: string; age: number; }
|
||||
|
||||
// Make email and age optional
|
||||
type PartialUser = MakeOptional<User, 'email' | 'age'>;
|
||||
// Result: { id: string; name: string; email?: string; age?: number; }
|
||||
```
|
||||
|
||||
## Basic functional programming utilities
|
||||
|
||||
```typescript
|
||||
|
||||
Reference in New Issue
Block a user