-
Notifications
You must be signed in to change notification settings - Fork 2
experiment(react-web-sdk): initialize SDK on server (V2 drop useLifecycle) #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
David Nalchevanidze (nalchevanidze)
wants to merge
22
commits into
fix-ssr
from
experiment/ssr-sdk-lifecycle
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
be961e0
experiment: initialize SDK on server via SSR-safe LocalStore and sync…
nalchevanidze b43eae8
experiment: fix useLayoutEffect browser path regressions
nalchevanidze 72d087e
chore: merge origin/fix-ssr — remove SSR stub, keep server-side useSt…
nalchevanidze 4d1bd07
experiment(ssr-sdk-init): initialize web SDK on server, skip singleto…
nalchevanidze 35e8fc5
fix(ssr-sdk-init): use 'window' in globalThis instead of typeof window
nalchevanidze c56b438
refactor(core-sdk, web-sdk): make singleton enforcement explicit via …
nalchevanidze eb1866c
refactor(react-web-sdk): replace serverEnv IS_SERVER with isBrowser()…
nalchevanidze 4cd251c
fix(web-sdk): guard entry interaction start against missing browser A…
nalchevanidze ac21746
fix(react-web-sdk): dispose server SDK before replacing with browser …
nalchevanidze 6f0dacb
fix(react-web-sdk): always initialize SDK synchronously in useState t…
nalchevanidze a82e4b9
refactor(react-web-sdk): extract useLifecycle hook and simplify Optim…
nalchevanidze 8ebde6b
fix(react-web-sdk): destroy stale window singleton before constructin…
nalchevanidze b1326fb
fix(react-web-sdk): inline isBrowser check to fix CI build
nalchevanidze 2d1acf3
fix(react-web-sdk): export isBrowser from web-sdk index and use it in…
nalchevanidze 4c0660c
fix(react-web-sdk): export isBrowser from web-sdk index and use it in…
nalchevanidze 4e813b1
fix(react-web-sdk): restore idempotent init() — revert dispose-on-rei…
nalchevanidze 65fad95
feat(react-web-sdk): replace useLifecycle with page-level SDK singlet…
nalchevanidze 93010be
refactor(react-web-sdk): collapse canUseInjectedSdkDuringInitialRende…
nalchevanidze c11a5c5
refactor(react-web-sdk): simplify mutable-props effect and rename pre…
nalchevanidze fc218e8
refactor(react-web-sdk): drop createOptimizationRootSdkBinding from i…
nalchevanidze 4daea71
fix(react-web-sdk): restore narrowing alias for setConfig effect, ren…
nalchevanidze 2120a77
refactor(react-web-sdk): inline intermediate prop type aliases
nalchevanidze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/universal/core-sdk/src/lib/singleton/StatefulRuntimeSingleton.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { | ||
| acquireStatefulRuntimeSingleton, | ||
| releaseStatefulRuntimeSingleton, | ||
| } from './StatefulRuntimeSingleton' | ||
|
|
||
| type SingletonGlobal = typeof globalThis & { | ||
| __ctfl_optimization_stateful_runtime_lock__?: unknown | ||
| } | ||
|
|
||
| function clearLock(): void { | ||
| const g = globalThis as SingletonGlobal | ||
| g.__ctfl_optimization_stateful_runtime_lock__ = undefined | ||
| } | ||
|
|
||
| describe('StatefulRuntimeSingleton — enforce: true (browser)', () => { | ||
| beforeEach(() => { | ||
| clearLock() | ||
| }) | ||
| afterEach(() => { | ||
| clearLock() | ||
| }) | ||
|
|
||
| it('acquires the lock for the first owner', () => { | ||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton('owner-1', true) | ||
| }).not.toThrow() | ||
| }) | ||
|
|
||
| it('throws when a second owner tries to acquire a held lock', () => { | ||
| acquireStatefulRuntimeSingleton('owner-1', true) | ||
|
|
||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton('owner-2', true) | ||
| }).toThrowError(/already initialized/i) | ||
| }) | ||
|
|
||
| it('releases the lock so a new owner can acquire it', () => { | ||
| acquireStatefulRuntimeSingleton('owner-1', true) | ||
| releaseStatefulRuntimeSingleton('owner-1', true) | ||
|
|
||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton('owner-2', true) | ||
| }).not.toThrow() | ||
| }) | ||
|
|
||
| it('does not release the lock when a different owner calls release', () => { | ||
| acquireStatefulRuntimeSingleton('owner-1', true) | ||
| releaseStatefulRuntimeSingleton('owner-2', true) | ||
|
|
||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton('owner-3', true) | ||
| }).toThrowError(/already initialized/i) | ||
| }) | ||
|
|
||
| it('allows repeated acquire/release cycles', () => { | ||
| for (let i = 0; i < 3; i++) { | ||
| const owner = `owner-${i}` | ||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton(owner, true) | ||
| }).not.toThrow() | ||
| expect(() => { | ||
| releaseStatefulRuntimeSingleton(owner, true) | ||
| }).not.toThrow() | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe('StatefulRuntimeSingleton — enforce: false (server / SSR)', () => { | ||
| beforeEach(() => { | ||
| clearLock() | ||
| }) | ||
| afterEach(() => { | ||
| clearLock() | ||
| }) | ||
|
|
||
| it('does not throw when acquiring', () => { | ||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton('owner-1', false) | ||
| }).not.toThrow() | ||
| }) | ||
|
|
||
| it('does not throw on a second acquire — no lock contention', () => { | ||
| acquireStatefulRuntimeSingleton('owner-1', false) | ||
|
|
||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton('owner-2', false) | ||
| }).not.toThrow() | ||
| }) | ||
|
|
||
| it('does not throw when releasing', () => { | ||
| acquireStatefulRuntimeSingleton('owner-1', false) | ||
|
|
||
| expect(() => { | ||
| releaseStatefulRuntimeSingleton('owner-1', false) | ||
| }).not.toThrow() | ||
| }) | ||
|
|
||
| it('allows repeated acquire/release cycles — simulates multiple SSR requests', () => { | ||
| for (let i = 0; i < 3; i++) { | ||
| const owner = `owner-${i}` | ||
| expect(() => { | ||
| acquireStatefulRuntimeSingleton(owner, false) | ||
| }).not.toThrow() | ||
| expect(() => { | ||
| releaseStatefulRuntimeSingleton(owner, false) | ||
| }).not.toThrow() | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd understand this better if I knew what "enforce" means and what its intent is. Is there possibly a more descriptive name? If this is getting more nuanced usage with the additional flag, we may also need to add some TSDoc, even though this is otherwise internal functionality.
I'm also wondering whether "enforcement" should be opt-out instead of opt-in, since the normal use case for a Stateful SDK would be for use in stateful environments.