fix(dexie-cloud-addon): add configurable:true to awareness defineProperty#2280
fix(dexie-cloud-addon): add configurable:true to awareness defineProperty#2280dfahlander merged 8 commits intomasterfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughHandler now checks for an existing Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
addons/dexie-cloud/src/yjs/createYHandler.ts (1)
27-37: Potential unnecessary initialization when cleaning up stale awareness.When
existingDescriptorexists but the old getter'sawarenessvariable was never populated (user never accessedprovider.awarenessbefore HMR), accessingprovider.awarenesson line 32 invokes the OLD getter, which will lazily create an awareness with the OLDdbinstance, only to immediately destroy it.A more efficient approach would check the descriptor type and avoid triggering the getter if possible:
♻️ Suggested optimization to avoid unnecessary initialization
const existingDescriptor = Object.getOwnPropertyDescriptor(provider, 'awareness'); if (existingDescriptor) { // Provider already initialized — likely a leaked handler from a previous db instance // (e.g. HMR where db.close() didn't fire). Destroy the stale awareness so the new // handler can take over cleanly. - const staleAwareness = provider.awareness as import('y-protocols/awareness').Awareness | undefined; + // Only attempt cleanup if there's an actual value (avoid triggering lazy init on old getter) + const staleAwareness = 'value' in existingDescriptor + ? existingDescriptor.value + : existingDescriptor.get?.call(provider); if (staleAwareness) { staleAwareness.destroy(); awarenessWeakMap.delete(doc); } }That said, this is an edge case during HMR, so the current implementation is acceptable if you prefer simplicity.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@addons/dexie-cloud/src/yjs/createYHandler.ts` around lines 27 - 37, The code currently reads provider.awareness which can invoke the old getter and lazily re-create an Awareness; instead, check the existingDescriptor to avoid triggering a getter: inspect existingDescriptor.value and existingDescriptor.get and only access provider.awareness when the descriptor is a data property (existingDescriptor.get is undefined) or when existingDescriptor.value is already set (use existingDescriptor.value as the staleAwareness); if the descriptor has a getter and value is undefined, skip destroying to avoid initializing the old awareness. Use the symbols existingDescriptor, provider.awareness, staleAwareness, awarenessWeakMap and doc to locate and update the logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@addons/dexie-cloud/src/yjs/createYHandler.ts`:
- Around line 27-37: The code currently reads provider.awareness which can
invoke the old getter and lazily re-create an Awareness; instead, check the
existingDescriptor to avoid triggering a getter: inspect
existingDescriptor.value and existingDescriptor.get and only access
provider.awareness when the descriptor is a data property
(existingDescriptor.get is undefined) or when existingDescriptor.value is
already set (use existingDescriptor.value as the staleAwareness); if the
descriptor has a getter and value is undefined, skip destroying to avoid
initializing the old awareness. Use the symbols existingDescriptor,
provider.awareness, staleAwareness, awarenessWeakMap and doc to locate and
update the logic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1be0bcba-d32b-4625-ad45-447bc0f8bd75
📒 Files selected for processing (1)
addons/dexie-cloud/src/yjs/createYHandler.ts
87c1cd9 to
b92ab46
Compare
- prettier ^3.8.1 added to devDependencies (was only run via npx) - Added pnpm format / format:check scripts - .vscode/settings.json: formatOnSave + prettier as default formatter - .vscode/extensions.json: recommends prettier-vscode extension - .gitignore: allow .vscode/settings.json and extensions.json to be tracked (only launch.json and snippets remain ignored)
…erty in createYHandler Prevents TypeError: Cannot redefine property: awareness when the DexieYProvider 'new' event fires twice on the same provider instance. This happens during Vite HMR in development. Safe non-breaking change — normal production flow is unaffected.
…ialized When a leaked handler (e.g. from HMR where db.close() did not fire) runs a second time on an already-initialized provider: 1. Detect existing descriptor via getOwnPropertyDescriptor 2. Destroy the stale awareness (broadcasts offline state, cleans up timers) 3. Delete from awarenessWeakMap so new handler owns the provider cleanly This ensures the freshest db instance always wins, and stale awareness objects don't linger and emit phantom presence updates.
b92ab46 to
15da54f
Compare
Problem
Object.defineProperty(provider, 'awareness', {...})increateYHandler()was missingconfigurable: true, causing:when the
DexieYProvider 'new'event fires twice on the same provider instance. This reproducibly happens during Vite HMR in development.Fix
Added
configurable: trueto thedefinePropertycall. This is a safe, non-breaking one-liner:if (awareness)guard already handles idempotencyReported by @bennie.forss in Discord.
Summary by CodeRabbit