Skip to content

Commit f75c96c

Browse files
refactor: remove subscribe() API (#50)
1 parent 8866d16 commit f75c96c

File tree

5 files changed

+5
-377
lines changed

5 files changed

+5
-377
lines changed

.changeset/lemon-rockets-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ventyd": minor
3+
---
4+
5+
refactor: remove `subscribe()` API

README.md

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,54 +1012,6 @@ const badPlugin: Plugin = {
10121012
};
10131013
```
10141014

1015-
## Subscribing to State Changes
1016-
1017-
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.
1018-
1019-
### Basic Usage
1020-
1021-
```typescript
1022-
const user = User.create({
1023-
body: {
1024-
nickname: "John",
1025-
1026-
}
1027-
});
1028-
1029-
// Subscribe to state changes
1030-
user.subscribe(() => {
1031-
console.log("User state changed:", user.state);
1032-
});
1033-
1034-
// This will trigger the listener
1035-
user.updateProfile({ bio: "Software Engineer" });
1036-
```
1037-
1038-
### Multiple Listeners
1039-
1040-
You can register multiple listeners on the same entity. They will be called in the order they were registered:
1041-
1042-
```typescript
1043-
const user = User.create({
1044-
body: {
1045-
nickname: "Alice",
1046-
1047-
}
1048-
});
1049-
1050-
// Register multiple listeners
1051-
user.subscribe(() => {
1052-
console.log("Listener 1: State changed");
1053-
});
1054-
1055-
user.subscribe(() => {
1056-
console.log("Listener 2: State changed");
1057-
});
1058-
1059-
// Both listeners will be called when state changes
1060-
user.updateProfile({ bio: "Engineer" });
1061-
```
1062-
10631015
## Best Practices
10641016

10651017
### 1. Event Naming

src/Entity.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,6 @@ export function Entity<$$Schema extends DefaultSchema>(
232232
});
233233
}
234234

235-
subscribe(listener: () => void): () => void {
236-
this[" $$listeners"].push(listener);
237-
238-
return () => {
239-
const index = this[" $$listeners"].indexOf(listener);
240-
if (index > -1) {
241-
this[" $$listeners"].splice(index, 1);
242-
}
243-
};
244-
}
245-
246235
// ----------------------
247236
// private methods
248237
// ----------------------

src/types/Entity.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,8 @@ export interface Entity<$$Schema> {
5757
/** @internal */
5858
" $$readonly": boolean;
5959
/** @internal */
60-
" $$listeners": (() => void)[];
61-
/** @internal */
6260
" $$now": () => Date;
6361

64-
/**
65-
* Subscribes to state changes in this entity.
66-
*
67-
* @param listener - A callback function that will be invoked whenever the entity's state changes
68-
* @returns A disposer function that can be called to unsubscribe the listener
69-
*
70-
* @remarks
71-
* The listener is called immediately after each event is dispatched and the state is updated.
72-
* Listeners are called synchronously in the order they were registered.
73-
*
74-
* Multiple listeners can be registered on the same entity.
75-
*
76-
* @example
77-
* ```typescript
78-
* const user = User.create({
79-
* body: {
80-
* nickname: "John",
81-
* email: "john@example.com"
82-
* }
83-
* });
84-
*
85-
* // Subscribe to state changes
86-
* const unsubscribe = user.subscribe(() => {
87-
* console.log("User state changed:", user.state);
88-
* });
89-
*
90-
* // This will trigger the listener
91-
* user.updateProfile({ bio: "Software Engineer" });
92-
*
93-
* // Unsubscribe when done
94-
* unsubscribe();
95-
* ```
96-
*/
97-
subscribe(listener: () => void): () => void;
98-
9962
// ----------------------
10063
// private methods
10164
// ----------------------

0 commit comments

Comments
 (0)