Skip to content

Commit

Permalink
support forceHydration on PromptString (sourcegraph#5884)
Browse files Browse the repository at this point in the history
PromptString instances can't be cloned because their reference value is
used as the key in the WeakMap that implements immutability and
encapsulation for PromptString.

(This broke something on a separate branch, so I extracted the fix and
test case to merge separately here.)

## Test plan

Added test case
  • Loading branch information
sqs authored Oct 12, 2024
1 parent 71a17f0 commit 44a2293
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/shared/src/editor/hydrateAfterPostMessage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'vitest'

import { hydrateAfterPostMessage, isDehydratedUri } from './hydrateAfterPostMessage'
import { ps } from '../prompt/prompt-string'
import { forceHydration, hydrateAfterPostMessage, isDehydratedUri } from './hydrateAfterPostMessage'

// Mock URI hydration function
const mockHydrateUri = (value: unknown) => {
Expand Down Expand Up @@ -115,3 +116,11 @@ describe('hydrateAfterPostMessage', () => {
expect(originalValue.foo.bar).toEqual('baz')
})
})

describe('forceHydration', () => {
test('handles PromptString', async () => {
const ps1 = ps`foo`
expect(ps1.toJSON()).toBe('foo')
expect(forceHydration(ps1).toJSON()).toBe('foo')
})
})
6 changes: 6 additions & 0 deletions lib/shared/src/editor/hydrateAfterPostMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type * as vscode from 'vscode'
import type { URI } from 'vscode-uri'
import { PromptString } from '../prompt/prompt-string'

/**
* Forces hydration by cloning any part that may be lazily hydrated. This is necessary before using
Expand All @@ -10,6 +11,11 @@ export function forceHydration(object: any): any {
if (typeof object !== 'object' || object === null) {
return object
}
if (object instanceof PromptString) {
// Return as-is, because PromptString object references are used as keys in a WeakMap that
// implements immutability and encapsulation for PromptString.
return object
}
if (Array.isArray(object)) {
return object.map(forceHydration)
}
Expand Down

0 comments on commit 44a2293

Please sign in to comment.