Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-rockets-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ventyd": minor
---

refactor: remove `subscribe()` API
48 changes: 0 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1012,54 +1012,6 @@ const badPlugin: Plugin = {
};
```

## Subscribing to State Changes

Entities support subscribing to state changes, allowing you to react whenever an entity's state is updated. This is useful for UI updates, logging, or triggering side effects.

### Basic Usage

```typescript
const user = User.create({
body: {
nickname: "John",
email: "[email protected]"
}
});

// Subscribe to state changes
user.subscribe(() => {
console.log("User state changed:", user.state);
});

// This will trigger the listener
user.updateProfile({ bio: "Software Engineer" });
```

### Multiple Listeners

You can register multiple listeners on the same entity. They will be called in the order they were registered:

```typescript
const user = User.create({
body: {
nickname: "Alice",
email: "[email protected]"
}
});

// Register multiple listeners
user.subscribe(() => {
console.log("Listener 1: State changed");
});

user.subscribe(() => {
console.log("Listener 2: State changed");
});

// Both listeners will be called when state changes
user.updateProfile({ bio: "Engineer" });
```

## Best Practices

### 1. Event Naming
Expand Down
11 changes: 0 additions & 11 deletions src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,6 @@ export function Entity<$$Schema extends DefaultSchema>(
});
}

subscribe(listener: () => void): () => void {
this[" $$listeners"].push(listener);

return () => {
const index = this[" $$listeners"].indexOf(listener);
if (index > -1) {
this[" $$listeners"].splice(index, 1);
}
};
}

// ----------------------
// private methods
// ----------------------
Expand Down
37 changes: 0 additions & 37 deletions src/types/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,8 @@ export interface Entity<$$Schema> {
/** @internal */
" $$readonly": boolean;
/** @internal */
" $$listeners": (() => void)[];
/** @internal */
" $$now": () => Date;

/**
* Subscribes to state changes in this entity.
*
* @param listener - A callback function that will be invoked whenever the entity's state changes
* @returns A disposer function that can be called to unsubscribe the listener
*
* @remarks
* The listener is called immediately after each event is dispatched and the state is updated.
* Listeners are called synchronously in the order they were registered.
*
* Multiple listeners can be registered on the same entity.
*
* @example
* ```typescript
* const user = User.create({
* body: {
* nickname: "John",
* email: "[email protected]"
* }
* });
*
* // Subscribe to state changes
* const unsubscribe = user.subscribe(() => {
* console.log("User state changed:", user.state);
* });
*
* // This will trigger the listener
* user.updateProfile({ bio: "Software Engineer" });
*
* // Unsubscribe when done
* unsubscribe();
* ```
*/
subscribe(listener: () => void): () => void;

// ----------------------
// private methods
// ----------------------
Expand Down
Loading