dreamstate
Version:
Store management library based on react context and observers
38 lines (35 loc) • 1.84 kB
JavaScript
import { createElement } from 'react';
import { ScopedProvider } from './ScopedProvider.js';
/**
* Recursively creates React elements within the current node scope for the provided context managers.
*
* This function generates React elements by recursively creating observers for each context manager
* in the `sources` array, wrapping them in their respective context providers. It ensures that the
* React tree is rendered within the current observation and data provision scope, allowing for
* scoped data management across the component tree.
*
* @template T - The type of the context data being observed and provided.
* @param {Array<TAnyContextManagerConstructor>} sources - An array of context manager class references
* that will be used to create the observer tree.
* @param {IProviderProps<T>} props - The properties of the current parent observer, which include
* context data, dependencies, and other necessary configurations.
* @param {IScopeContext} scope - The current scope context used for observation and data provisioning.
* @param {number} current - The current iteration index for the recursive rendering process.
* @returns {ReactNode} A React node representing the recursively created observer tree for the given
* context managers within the specified scope.
*/
function createScopedProviderTree(sources, props, scope, current) {
if (current === void 0) {
current = 0;
}
/*
* Wrap children node with observers until all source observer elements are created.
*/
return current >= sources.length ? props.children : createElement(ScopedProvider, {
ManagerClass: sources[current],
initialState: props.initialState,
dependencies: sources,
scope: scope
}, createScopedProviderTree(sources, props, scope, current + 1));
}
export { createScopedProviderTree };