Skip to content
Open
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
10 changes: 5 additions & 5 deletions .cursor/rules/actor-core-rules.mdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: ActorCore rules
globs:
globs:
alwaysApply: false
---
# ActorCore Development Guide
Expand Down Expand Up @@ -59,11 +59,11 @@ When importing from workspace packages, always check the package's `package.json
- **Formatting:** Uses Biome for consistent formatting
- **Imports:** Organized imports enforced, unused imports warned
- **TypeScript:** Strict mode enabled, target ESNext
- **Naming:**
- **Naming:**
- camelCase for variables, functions
- PascalCase for classes, interfaces, types
- UPPER_CASE for constants
- **Error Handling:**
- **Error Handling:**
- Use `UserError` for client-safe errors
- Use `InternalError` for internal errors

Expand Down Expand Up @@ -105,7 +105,7 @@ When importing from workspace packages, always check the package's `package.json

- `createState()`: Function that returns initial actor state
- `onStart(c)`: Called any time actor is started (after restart/upgrade)
- `onStateChange(c, newState)`: Called when actor state changes
- `onStateChange(c,prevState, newState)`: Called when actor state changes
- `onBeforeConnect(c)`: Called when new client connects
- `onConnect(c)`: Executed after client connection succeeds
- `onDisconnect(c)`: Called when client disconnects
Expand Down Expand Up @@ -144,4 +144,4 @@ When importing from workspace packages, always check the package's `package.json
- Use `assertUnreachable(x: never)` for exhaustive type checking
- Add proper JSDoc comments for public APIs
- Run `yarn check-types` regularly during development
- Use `tsx` CLI to execute TypeScript scripts directly
- Use `tsx` CLI to execute TypeScript scripts directly
1 change: 1 addition & 0 deletions packages/core/src/actor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ interface BaseActorConfig<
TAuthData,
TDatabase
>,
prevState: TState,
newState: TState,
) => void;

Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/actor/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,18 @@ export class ActorInstance<
});
}
this.#persistChanged = true;

const previousState = structuredClone(this.#persist.s);
// Inform the inspector about state changes
this.inspector.emitter.emit("stateUpdated", this.#persist.s);

// Call onStateChange if it exists
if (this.#config.onStateChange && this.#ready) {
try {
this.#config.onStateChange(this.actorContext, this.#persistRaw.s);
this.#config.onStateChange(
this.actorContext,
previousState,
this.#persistRaw.s,
);
} catch (error) {
logger().error("error in `_onStateChange`", {
error: stringifyError(error),
Expand Down