Skip to content

Commit

Permalink
Create getServerContext
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Sep 9, 2024
1 parent b21001f commit 60198c8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/interactivity/src/scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,24 @@ export const getElement = () => {
attributes: deepImmutable( attributes ),
} );
};

/**
* TODO: write tsdoc comment.
*
* @param namespace Store namespace. By default, the namespace where the calling
* function exists is used.
* @return The context content.
*/
export const getServerContext = < T extends object >(
namespace?: string
): T => {
const scope = getScope();
if ( globalThis.SCRIPT_DEBUG ) {
if ( ! scope ) {
throw Error(
'Cannot call `getServerContext()` when there is no scope. If you are using an async function, please consider using a generator instead. If you are using some sort of async callbacks, like `setTimeout`, please wrap the callback with `withScope(callback)`.'
);
}
}
return scope.serverContext[ namespace || getNamespace() ];
};

0 comments on commit 60198c8

Please sign in to comment.