@simpreact/simpreact 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import * as SimpReactContext from '../context/index.js';
2
+ export declare const createContext: typeof SimpReactContext.createContext;
3
+ export declare const useContext: typeof SimpReactContext.useContext;
4
+ declare const _default: {
5
+ createContext: typeof SimpReactContext.createContext;
6
+ useContext: typeof SimpReactContext.useContext;
7
+ };
8
+ export default _default;
@@ -0,0 +1,7 @@
1
+ import * as SimpReactContext from '../context/index.js';
2
+ export const createContext = SimpReactContext.createContext;
3
+ export const useContext = SimpReactContext.useContext;
4
+ export default {
5
+ createContext,
6
+ useContext,
7
+ };
package/compat/core.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import * as SimpReactInternal from '../core/internal.js';
2
- import { identity } from './utils.js';
3
2
  export declare const Children: {
4
3
  map(children: SimpReactInternal.SimpNode, fn: (child: SimpReactInternal.SimpNode, index: number) => SimpReactInternal.SimpNode): SimpReactInternal.SimpNode[];
5
4
  forEach(children: SimpReactInternal.SimpNode, fn: (child: SimpReactInternal.SimpNode, index: number) => void): void;
@@ -16,10 +15,9 @@ export declare function Suspense(props: {
16
15
  export declare function forwardRef<P, T>(Component: (props: P, ref: SimpReactInternal.Ref<T>) => any): (props: P) => any;
17
16
  export declare const Fragment: SimpReactInternal.Fragment;
18
17
  export declare const createElement: typeof SimpReactInternal.createElement;
19
- export declare const createContext: typeof SimpReactInternal.createContext;
20
18
  export declare const createPortal: typeof SimpReactInternal.createPortal;
21
- export declare const memo: typeof identity;
22
- export declare const flushSync: typeof identity;
19
+ export declare const memo: typeof SimpReactInternal.memo;
20
+ export declare const flushSync: (value: any) => any;
23
21
  export declare class Component {
24
22
  constructor();
25
23
  }
@@ -37,10 +35,9 @@ declare const _default: {
37
35
  forwardRef: typeof forwardRef;
38
36
  Fragment: SimpReactInternal.Fragment;
39
37
  createElement: typeof SimpReactInternal.createElement;
40
- createContext: typeof SimpReactInternal.createContext;
41
38
  createPortal: typeof SimpReactInternal.createPortal;
42
- memo: typeof identity;
43
- flushSync: typeof identity;
39
+ memo: typeof SimpReactInternal.memo;
40
+ flushSync: (value: any) => any;
44
41
  Component: typeof Component;
45
42
  };
46
43
  export default _default;
package/compat/core.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as SimpReactInternal from '../core/internal.js';
2
2
  import * as SimpReactHooks from '../hooks/index.js';
3
- import { identity } from './utils.js';
4
3
  export const Children = {
5
4
  map(children, fn) {
6
5
  return Children.toArray(children).map(fn);
@@ -51,13 +50,14 @@ export function isValidElement(element) {
51
50
  export function Suspense(props) {
52
51
  const [isSuspended, setIsSuspended] = SimpReactHooks.useState(false);
53
52
  SimpReactHooks.useCatch(error => {
53
+ if (!(error instanceof Promise)) {
54
+ throw error;
55
+ }
54
56
  if (isSuspended) {
55
57
  return;
56
58
  }
57
- if (error instanceof Promise) {
58
- setIsSuspended(true);
59
- error.then(() => setIsSuspended(false));
60
- }
59
+ setIsSuspended(true);
60
+ error.then(() => setIsSuspended(false));
61
61
  });
62
62
  return isSuspended ? props.fallback : props.children;
63
63
  }
@@ -68,10 +68,9 @@ export function forwardRef(Component) {
68
68
  }
69
69
  export const Fragment = SimpReactInternal.Fragment;
70
70
  export const createElement = SimpReactInternal.createElement;
71
- export const createContext = SimpReactInternal.createContext;
72
71
  export const createPortal = SimpReactInternal.createPortal;
73
- export const memo = identity;
74
- export const flushSync = identity;
72
+ export const memo = SimpReactInternal.memo;
73
+ export const flushSync = (value) => value;
75
74
  export class Component {
76
75
  constructor() {
77
76
  throw new Error('Not implemented.');
@@ -85,7 +84,6 @@ export default {
85
84
  forwardRef,
86
85
  Fragment,
87
86
  createElement,
88
- createContext,
89
87
  createPortal,
90
88
  memo,
91
89
  flushSync,
package/compat/hooks.d.ts CHANGED
@@ -9,7 +9,6 @@ export declare const useEffect: typeof SimpReactHooks.useEffect;
9
9
  export declare const useLayoutEffect: typeof SimpReactHooks.useEffect;
10
10
  export declare const useInsertionEffect: typeof SimpReactHooks.useEffect;
11
11
  export declare const useRef: typeof SimpReactHooks.useRef;
12
- export declare const useContext: typeof SimpReactHooks.useContext;
13
12
  declare const _default: {
14
13
  useSyncExternalStore: typeof useSyncExternalStore;
15
14
  useReducer: typeof useReducer;
@@ -21,6 +20,5 @@ declare const _default: {
21
20
  useLayoutEffect: typeof SimpReactHooks.useEffect;
22
21
  useInsertionEffect: typeof SimpReactHooks.useEffect;
23
22
  useRef: typeof SimpReactHooks.useRef;
24
- useContext: typeof SimpReactHooks.useContext;
25
23
  };
26
24
  export default _default;
package/compat/hooks.js CHANGED
@@ -61,7 +61,6 @@ export const useEffect = SimpReactHooks.useEffect;
61
61
  export const useLayoutEffect = SimpReactHooks.useEffect;
62
62
  export const useInsertionEffect = SimpReactHooks.useEffect;
63
63
  export const useRef = SimpReactHooks.useRef;
64
- export const useContext = SimpReactHooks.useContext;
65
64
  export default {
66
65
  useSyncExternalStore,
67
66
  useReducer,
@@ -73,5 +72,4 @@ export default {
73
72
  useLayoutEffect,
74
73
  useInsertionEffect,
75
74
  useRef,
76
- useContext,
77
75
  };
package/compat/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './context.js';
1
2
  export * from './core.js';
2
3
  export * from './dom.js';
3
4
  export * from './hooks.js';
@@ -16,7 +17,6 @@ declare const _default: {
16
17
  useLayoutEffect: typeof import("../hooks/index.js").useEffect;
17
18
  useInsertionEffect: typeof import("../hooks/index.js").useEffect;
18
19
  useRef: typeof import("../hooks/index.js").useRef;
19
- useContext: typeof import("../hooks/index.js").useContext;
20
20
  hydrate: typeof import("../shared/utils.js").noop;
21
21
  render: typeof import("../dom/render.js").render;
22
22
  createRoot: typeof import("../dom/render.js").createRoot;
@@ -33,10 +33,11 @@ declare const _default: {
33
33
  forwardRef: typeof import("./core.js").forwardRef;
34
34
  Fragment: import("../core/fragment.js").Fragment;
35
35
  createElement: typeof import("../core/createElement.js").createElement;
36
- createContext: typeof import("../core/context.js").createContext;
37
36
  createPortal: typeof import("../core/portal.js").createPortal;
38
- memo: typeof import("./utils.js").identity;
39
- flushSync: typeof import("./utils.js").identity;
37
+ memo: typeof import("../core/memo.js").memo;
38
+ flushSync: (value: any) => any;
40
39
  Component: typeof import("./core.js").Component;
40
+ createContext: typeof import("../context/index.js").createContext;
41
+ useContext: typeof import("../context/index.js").useContext;
41
42
  };
42
43
  export default _default;
package/compat/index.js CHANGED
@@ -1,12 +1,15 @@
1
+ import contextDefault from './context.js';
1
2
  import coreDefault from './core.js';
2
3
  import domDefault from './dom.js';
3
4
  import hooksDefault from './hooks.js';
4
5
  import jsxRuntimeDefault from './jsx-runtime.js';
6
+ export * from './context.js';
5
7
  export * from './core.js';
6
8
  export * from './dom.js';
7
9
  export * from './hooks.js';
8
10
  export * from './jsx-runtime.js';
9
11
  export default {
12
+ ...contextDefault,
10
13
  ...coreDefault,
11
14
  ...domDefault,
12
15
  ...hooksDefault,
@@ -0,0 +1,24 @@
1
+ import type { FunctionComponent, SimpNode } from '../core/index.js';
2
+
3
+ export interface ProviderProps<T> {
4
+ value: T;
5
+ children?: SimpNode;
6
+ }
7
+
8
+ export interface ConsumerProps<T> {
9
+ children: (value: T) => SimpNode;
10
+ }
11
+
12
+ export type SimpContext<T> = {
13
+ Provider: Provider<T>;
14
+ Consumer: Consumer<T>;
15
+ };
16
+
17
+ export type ContextType<C extends SimpContext<any>> = C extends SimpContext<infer T> ? T : never;
18
+
19
+ export type Provider<T> = FunctionComponent<ProviderProps<T>>;
20
+ export type Consumer<T> = FunctionComponent<ConsumerProps<T>>;
21
+
22
+ declare function createContext<T>(defaultValue: T): SimpContext<T>;
23
+
24
+ declare function useContext<T>(context: SimpContext<T>): T;
@@ -0,0 +1,64 @@
1
+ import { lifecycleEventBus, rerender } from '../core/internal.js';
2
+ // In runtime these variables are nullable.
3
+ let currentElement;
4
+ let phase;
5
+ lifecycleEventBus.subscribe(event => {
6
+ if (event.type === 'beforeRender') {
7
+ currentElement = event.element;
8
+ phase = event.phase;
9
+ }
10
+ if (event.type === 'afterRender') {
11
+ currentElement = null;
12
+ phase = null;
13
+ }
14
+ if (event.type === 'unmounted') {
15
+ event.element.context?.forEach(value => value.subs.delete(event.element.store));
16
+ }
17
+ });
18
+ export function createContext(defaultValue) {
19
+ const context = {
20
+ defaultValue,
21
+ Provider(props) {
22
+ if (!currentElement.context) {
23
+ currentElement.context = new Map();
24
+ }
25
+ else if (phase === 'mounting') {
26
+ currentElement.context = new Map(currentElement.context);
27
+ }
28
+ if (phase === 'mounting') {
29
+ currentElement.context.set(context, { value: props.value, subs: new Set() });
30
+ return props.children;
31
+ }
32
+ let contextEntry = currentElement.context.get(context);
33
+ if (!contextEntry) {
34
+ contextEntry = { value: props.value, subs: new Set() };
35
+ currentElement.context.set(context, contextEntry);
36
+ return props.children;
37
+ }
38
+ if (Object.is(contextEntry.value, props.value)) {
39
+ return props.children;
40
+ }
41
+ contextEntry.value = props.value;
42
+ for (let sub of contextEntry.subs) {
43
+ sub.forceRender = true;
44
+ rerender(sub.latestElement);
45
+ }
46
+ return props.children;
47
+ },
48
+ Consumer(props) {
49
+ return props.children(currentElement.context?.get(context)?.value ?? defaultValue);
50
+ },
51
+ };
52
+ return context;
53
+ }
54
+ export function useContext(context) {
55
+ const contextEntry = currentElement.context?.get(context);
56
+ if (!contextEntry) {
57
+ // No provider above: just return the default value, don't subscribe
58
+ return context.defaultValue;
59
+ }
60
+ if (!contextEntry.subs.has(currentElement.store)) {
61
+ contextEntry.subs.add(currentElement.store);
62
+ }
63
+ return contextEntry.value;
64
+ }
@@ -1,33 +1,30 @@
1
1
  import type { Many, Maybe, Nullable, SimpText } from '../shared/index.js';
2
2
  import type { HostReference } from './hostAdapter.js';
3
- import type { SimpContextMap } from './context.js';
4
3
  export type SimpNode = SimpElement | SimpText | Array<SimpNode> | boolean | null | undefined;
5
4
  export type Key = string | number | bigint;
6
- export interface FunctionComponent {
7
- (props: any): SimpNode;
8
- }
9
- export type FC = FunctionComponent;
10
- export type SimpElementFlag = 'FC' | 'HOST' | 'TEXT' | 'FRAGMENT' | 'PROVIDER' | 'CONSUMER' | 'PORTAL';
5
+ export type FC = (props: any) => SimpNode;
6
+ export type SimpElementFlag = 'FC' | 'HOST' | 'TEXT' | 'FRAGMENT' | 'PORTAL';
11
7
  export interface SimpElementStore {
12
8
  latestElement?: Maybe<SimpElement>;
13
9
  hostNamespace?: Maybe<string>;
10
+ forceRender?: boolean;
14
11
  [key: string]: unknown;
15
12
  }
16
13
  export interface SimpElement {
17
14
  flag: SimpElementFlag;
18
15
  parent: Nullable<SimpElement>;
19
16
  key?: Maybe<Key>;
20
- type?: string | FunctionComponent;
17
+ type?: string | FC;
21
18
  props?: any;
22
19
  children?: Maybe<SimpNode>;
23
20
  className?: Maybe<string>;
24
21
  reference?: Maybe<HostReference>;
25
22
  store?: SimpElementStore;
26
- contextMap?: Maybe<SimpContextMap>;
23
+ context?: any;
27
24
  ref?: any;
28
25
  unmounted?: boolean;
29
26
  }
30
- export declare function createElement(type: string | FunctionComponent, props?: any, ...children: SimpNode[]): SimpElement;
27
+ export declare function createElement(type: string | FC, props?: any, ...children: SimpNode[]): SimpElement;
31
28
  export declare function createTextElement(text: SimpText): SimpElement;
32
29
  export declare function normalizeChildren(children: SimpNode, skipIgnoredCheck: boolean): Maybe<Many<SimpElement>>;
33
30
  export declare function normalizeRoot(node: SimpNode, skipIgnoredCheck: boolean): Maybe<SimpElement>;
@@ -1,6 +1,5 @@
1
1
  import { isSimpText } from '../shared/index.js';
2
2
  import { Fragment } from './fragment.js';
3
- import { isConsumer, isProvider } from './context.js';
4
3
  export function createElement(type, props, ...children) {
5
4
  let newProps;
6
5
  let className;
@@ -83,28 +82,6 @@ export function createElement(type, props, ...children) {
83
82
  }
84
83
  return element;
85
84
  }
86
- else if (isProvider(type)) {
87
- const element = { flag: 'PROVIDER', type, props: { value: props.value }, parent: null };
88
- if ((definedChildren = normalizeChildren(definedChildren || props.children, false))) {
89
- element.children = definedChildren;
90
- }
91
- if (props != null && props.key) {
92
- element.key = props?.key;
93
- }
94
- return element;
95
- }
96
- else if (isConsumer(type)) {
97
- const element = {
98
- flag: 'CONSUMER',
99
- type,
100
- props: { children: definedChildren || (props != null ? props.children : null) },
101
- parent: null,
102
- };
103
- if (props != null && props.key) {
104
- element.key = props?.key;
105
- }
106
- return element;
107
- }
108
85
  else {
109
86
  if (props != null) {
110
87
  for (const propName in props) {
@@ -210,7 +187,7 @@ function isIgnoredNode(node) {
210
187
  if (isSimpText(node)) {
211
188
  return false;
212
189
  }
213
- if (node.flag === 'FRAGMENT' || node.flag === 'PROVIDER' || node.flag === 'PORTAL') {
190
+ if (node.flag === 'FRAGMENT' || node.flag === 'PORTAL') {
214
191
  return node.children == null;
215
192
  }
216
193
  return false;
package/core/index.d.ts CHANGED
@@ -26,25 +26,6 @@ export interface SimpElement<P = unknown, T extends string | FunctionComponent<P
26
26
 
27
27
  export type SimpNode = SimpElement | SimpText | Array<SimpNode> | boolean | null | undefined;
28
28
 
29
- export interface ProviderProps<T> {
30
- value: T;
31
- children?: SimpNode | undefined;
32
- }
33
-
34
- export interface ConsumerProps<T> {
35
- children: (value: T) => SimpNode;
36
- }
37
-
38
- export type SimpContext<T> = {
39
- Provider: Provider<T>;
40
- Consumer: Consumer<T>;
41
- };
42
-
43
- export type ContextType<C extends SimpContext<any>> = C extends SimpContext<infer T> ? T : never;
44
-
45
- export type Provider<T> = FunctionComponent<ProviderProps<T>>;
46
- export type Consumer<T> = FunctionComponent<ConsumerProps<T>>;
47
-
48
29
  declare function createElement<P extends {}, T>(
49
30
  type: string,
50
31
  props?: (RefAttributes<T> & P) | null,
@@ -58,11 +39,11 @@ declare function createElement<P extends {}>(
58
39
 
59
40
  declare function createPortal<HostRef = {}>(children: SimpNode, container: HostRef): SimpElement;
60
41
 
61
- declare function createContext<T>(defaultValue: T): SimpContext<T>;
62
-
63
42
  declare function Fragment(props: PropsWithChildren): SimpElement;
64
43
 
65
44
  export type FunctionComponent<P = {}> = (props: P) => SimpNode;
66
45
  export type FC<P = {}> = FunctionComponent<P>;
67
46
 
68
47
  export type PropsWithChildren<P = {}> = P & { children?: SimpNode | undefined };
48
+
49
+ declare function memo<P = {}>(Component: FC<P>, compare?: (objA: Readonly<P>, objB: Readonly<P>) => boolean): FC<P>;
package/core/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createElement } from './createElement.js';
2
2
  import { Fragment } from './fragment.js';
3
- import { createContext } from './context.js';
3
+ import { memo } from './memo.js';
4
4
  import { createPortal } from './portal.js';
5
- export { createElement, Fragment, createContext, createPortal };
6
- export default { createElement, Fragment, createContext, createPortal };
5
+ export { createElement, Fragment, memo, createPortal };
6
+ export default { createElement, Fragment, memo, createPortal };
@@ -1,8 +1,8 @@
1
- export * from './context.js';
2
1
  export * from './createElement.js';
3
2
  export * from './fragment.js';
4
3
  export * from './hostAdapter.js';
5
4
  export * from './lifecycleEventBus.js';
5
+ export * from './memo.js';
6
6
  export * from './mounting.js';
7
7
  export * from './patching.js';
8
8
  export * from './portal.js';
package/core/internal.js CHANGED
@@ -1,8 +1,8 @@
1
- export * from './context.js';
2
1
  export * from './createElement.js';
3
2
  export * from './fragment.js';
4
3
  export * from './hostAdapter.js';
5
4
  export * from './lifecycleEventBus.js';
5
+ export * from './memo.js';
6
6
  export * from './mounting.js';
7
7
  export * from './patching.js';
8
8
  export * from './portal.js';
package/core/memo.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { shallowEqual } from '../shared/index.js';
2
+ import type { FC, SimpNode } from './createElement.js';
3
+ export interface MemoizedComponent {
4
+ (props: any): SimpNode;
5
+ _isMemo: true;
6
+ _render: FC;
7
+ _compare: (prevProps: any, nextProps: any) => boolean;
8
+ }
9
+ export declare function memo(Component: FC, compare?: typeof shallowEqual): MemoizedComponent;
10
+ export declare function isMemo(type: any): type is MemoizedComponent;
package/core/memo.js ADDED
@@ -0,0 +1,11 @@
1
+ import { shallowEqual } from '../shared/index.js';
2
+ export function memo(Component, compare = shallowEqual) {
3
+ const Memoized = (props => Component(props));
4
+ Memoized._isMemo = true;
5
+ Memoized._render = Component;
6
+ Memoized._compare = compare;
7
+ return Memoized;
8
+ }
9
+ export function isMemo(type) {
10
+ return type._isMemo;
11
+ }
@@ -1,13 +1,10 @@
1
1
  import type { Maybe, Nullable } from '../shared/index.js';
2
2
  import type { HostReference } from './hostAdapter.js';
3
3
  import type { SimpElement } from './createElement.js';
4
- import type { SimpContextMap } from './context.js';
5
- export declare function mount(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
4
+ export declare function mount(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, context: unknown, hostNamespace: Maybe<string>): void;
6
5
  export declare function mountTextElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>): void;
7
- export declare function mountHostElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
8
- export declare function mountFunctionalElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
9
- export declare function mountFragment(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
10
- export declare function mountArrayChildren(children: SimpElement[], reference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, parentElement: SimpElement, hostNamespace: Maybe<string>): void;
11
- export declare function mountProvider(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
12
- export declare function mountConsumer(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
13
- export declare function mountPortal(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
6
+ export declare function mountHostElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, context: unknown, hostNamespace: Maybe<string>): void;
7
+ export declare function mountFunctionalElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, context: unknown, hostNamespace: Maybe<string>): void;
8
+ export declare function mountFragment(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, context: unknown, hostNamespace: Maybe<string>): void;
9
+ export declare function mountArrayChildren(children: SimpElement[], reference: Nullable<HostReference>, nextReference: Nullable<HostReference>, context: unknown, parentElement: SimpElement, hostNamespace: Maybe<string>): void;
10
+ export declare function mountPortal(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, context: unknown): void;
package/core/mounting.js CHANGED
@@ -1,30 +1,23 @@
1
- import { emptyMap, emptyObject } from '../shared/index.js';
1
+ import { emptyObject } from '../shared/index.js';
2
2
  import { hostAdapter } from './hostAdapter.js';
3
3
  import { createTextElement, normalizeRoot } from './createElement.js';
4
4
  import { applyRef } from './ref.js';
5
5
  import { lifecycleEventBus } from './lifecycleEventBus.js';
6
- import { batchingRerenderLocker } from './rerender.js';
7
- export function mount(element, parentReference, nextReference, contextMap, hostNamespace) {
6
+ export function mount(element, parentReference, nextReference, context, hostNamespace) {
8
7
  if (element.flag === 'TEXT') {
9
8
  mountTextElement(element, parentReference, nextReference);
10
9
  }
11
10
  else if (element.flag === 'HOST') {
12
- mountHostElement(element, parentReference, nextReference, contextMap, hostNamespace);
11
+ mountHostElement(element, parentReference, nextReference, context, hostNamespace);
13
12
  }
14
13
  else if (element.flag === 'FC') {
15
- mountFunctionalElement(element, parentReference, nextReference, contextMap, hostNamespace);
14
+ mountFunctionalElement(element, parentReference, nextReference, context, hostNamespace);
16
15
  }
17
16
  else if (element.flag === 'FRAGMENT') {
18
- mountFragment(element, parentReference, nextReference, contextMap, hostNamespace);
19
- }
20
- else if (element.flag === 'PROVIDER') {
21
- mountProvider(element, parentReference, nextReference, contextMap, hostNamespace);
22
- }
23
- else if (element.flag === 'PORTAL') {
24
- mountPortal(element, parentReference, nextReference, contextMap);
17
+ mountFragment(element, parentReference, nextReference, context, hostNamespace);
25
18
  }
26
19
  else {
27
- mountConsumer(element, parentReference, nextReference, contextMap, hostNamespace);
20
+ mountPortal(element, parentReference, nextReference, context);
28
21
  }
29
22
  }
30
23
  export function mountTextElement(element, parentReference, nextReference) {
@@ -33,7 +26,7 @@ export function mountTextElement(element, parentReference, nextReference) {
33
26
  hostAdapter.insertOrAppend(parentReference, reference, nextReference);
34
27
  }
35
28
  }
36
- export function mountHostElement(element, parentReference, nextReference, contextMap, hostNamespace) {
29
+ export function mountHostElement(element, parentReference, nextReference, context, hostNamespace) {
37
30
  const hostNamespaces = hostAdapter.getHostNamespaces(element, hostNamespace);
38
31
  hostNamespace = hostNamespaces?.self;
39
32
  const hostReference = (element.reference = hostAdapter.createReference(element.type, hostNamespace));
@@ -41,11 +34,11 @@ export function mountHostElement(element, parentReference, nextReference, contex
41
34
  // HOST element always has Maybe<Many<SimpElement>> children due to normalization process.
42
35
  const children = element.children;
43
36
  if (Array.isArray(children)) {
44
- mountArrayChildren(children, hostReference, null, contextMap, element, hostNamespaces?.children);
37
+ mountArrayChildren(children, hostReference, null, context, element, hostNamespaces?.children);
45
38
  }
46
39
  else if (children) {
47
40
  children.parent = element;
48
- mount(children, hostReference, null, contextMap, hostNamespaces?.children);
41
+ mount(children, hostReference, null, context, hostNamespaces?.children);
49
42
  }
50
43
  if (element.props) {
51
44
  hostAdapter.mountProps(hostReference, element, hostNamespace);
@@ -62,9 +55,9 @@ export function mountHostElement(element, parentReference, nextReference, contex
62
55
  }
63
56
  applyRef(element);
64
57
  }
65
- export function mountFunctionalElement(element, parentReference, nextReference, contextMap, hostNamespace) {
66
- if (contextMap) {
67
- element.contextMap = contextMap;
58
+ export function mountFunctionalElement(element, parentReference, nextReference, context, hostNamespace) {
59
+ if (context) {
60
+ element.context = context;
68
61
  }
69
62
  if (element.unmounted) {
70
63
  element.unmounted = false;
@@ -87,18 +80,10 @@ export function mountFunctionalElement(element, parentReference, nextReference,
87
80
  do {
88
81
  triedToRerender = false;
89
82
  if (++rerenderCounter >= 25) {
90
- lifecycleEventBus.publish({
91
- type: 'errored',
92
- element,
93
- error: new Error('Too many re-renders. SimpReact limits the number of renders to prevent an infinite loop.'),
94
- phase: 'mounting',
95
- });
96
- return;
83
+ throw new Error('Too many re-renders.');
97
84
  }
98
85
  lifecycleEventBus.publish({ type: 'beforeRender', element, phase: 'mounting' });
99
- batchingRerenderLocker.lock();
100
86
  children = element.type(element.props || emptyObject);
101
- batchingRerenderLocker.flush();
102
87
  lifecycleEventBus.publish({ type: 'afterRender', element, phase: 'mounting' });
103
88
  } while (triedToRerender);
104
89
  children = normalizeRoot(children, false);
@@ -112,50 +97,30 @@ export function mountFunctionalElement(element, parentReference, nextReference,
112
97
  }
113
98
  if (children) {
114
99
  children.parent = element;
115
- mount((element.children = children), parentReference, nextReference, contextMap, hostNamespace);
100
+ mount((element.children = children), parentReference, nextReference, element.context, hostNamespace);
116
101
  }
117
102
  lifecycleEventBus.publish({ type: 'mounted', element });
118
103
  }
119
- export function mountFragment(element, parentReference, nextReference, contextMap, hostNamespace) {
104
+ export function mountFragment(element, parentReference, nextReference, context, hostNamespace) {
120
105
  // FRAGMENT element always has Maybe<Many<SimpElement>> children due to normalization process.
121
106
  if (Array.isArray(element.children)) {
122
- mountArrayChildren(element.children, parentReference, nextReference, contextMap, element, hostNamespace);
107
+ mountArrayChildren(element.children, parentReference, nextReference, context, element, hostNamespace);
123
108
  }
124
109
  else if (element.children) {
125
110
  element.children.parent = element;
126
- mount(element.children, parentReference, nextReference, contextMap, hostNamespace);
111
+ mount(element.children, parentReference, nextReference, context, hostNamespace);
127
112
  }
128
113
  }
129
- export function mountArrayChildren(children, reference, nextReference, contextMap, parentElement, hostNamespace) {
114
+ export function mountArrayChildren(children, reference, nextReference, context, parentElement, hostNamespace) {
130
115
  for (const child of children) {
131
116
  child.parent = parentElement;
132
- mount(child, reference, nextReference, contextMap, hostNamespace);
133
- }
134
- }
135
- export function mountProvider(element, parentReference, nextReference, contextMap, hostNamespace) {
136
- contextMap = new Map(contextMap);
137
- contextMap.set(element.type.context, element.props.value);
138
- // PROVIDER element always has Maybe<Many<SimpElement>> children due to normalization process.
139
- if (Array.isArray(element.children)) {
140
- mountArrayChildren(element.children, parentReference, nextReference, contextMap, element, hostNamespace);
141
- }
142
- else if (element.children) {
143
- element.children.parent = element;
144
- mount(element.children, parentReference, nextReference, contextMap, hostNamespace);
145
- }
146
- }
147
- export function mountConsumer(element, parentReference, nextReference, contextMap, hostNamespace) {
148
- const children = normalizeRoot(element.type(element.props || emptyObject, contextMap || emptyMap), false);
149
- if (!children) {
150
- return;
117
+ mount(child, reference, nextReference, context, hostNamespace);
151
118
  }
152
- children.parent = element;
153
- mount((element.children = children), parentReference, nextReference, contextMap, hostNamespace);
154
119
  }
155
- export function mountPortal(element, parentReference, nextReference, contextMap) {
120
+ export function mountPortal(element, parentReference, nextReference, context) {
156
121
  if (element.children) {
157
122
  element.children.parent = element;
158
- mount(element.children, element.ref, null, contextMap, hostAdapter.getHostNamespaces(element.children, undefined)?.self);
123
+ mount(element.children, element.ref, null, context, hostAdapter.getHostNamespaces(element.children, undefined)?.self);
159
124
  }
160
125
  const placeHolderElement = createTextElement('');
161
126
  mountTextElement(placeHolderElement, parentReference, nextReference);
@@ -1,10 +1,9 @@
1
1
  import type { Maybe, Nullable } from '../shared/index.js';
2
2
  import type { SimpElement } from './createElement.js';
3
3
  import type { HostReference } from './hostAdapter.js';
4
- import type { SimpContextMap } from './context.js';
5
- export declare function patch(prevElement: SimpElement, nextElement: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
6
- export declare function patchPortal(prevElement: SimpElement, nextElement: SimpElement, contextMap: Nullable<SimpContextMap>): void;
7
- export declare function updateFunctionalComponent(element: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
8
- export declare function patchKeyedChildren(prevChildren: SimpElement[], nextChildren: SimpElement[], parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
4
+ export declare function patch(prevElement: SimpElement, nextElement: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, context: unknown, hostNamespace: Maybe<string>): void;
5
+ export declare function patchPortal(prevElement: SimpElement, nextElement: SimpElement, context: unknown): void;
6
+ export declare function updateFunctionalComponent(element: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, context: unknown, hostNamespace: Maybe<string>): void;
7
+ export declare function patchKeyedChildren(prevChildren: SimpElement[], nextChildren: SimpElement[], parentReference: HostReference, nextReference: Nullable<HostReference>, context: unknown, hostNamespace: Maybe<string>): void;
9
8
  export declare function findParentReferenceFromElement(element: SimpElement): Nullable<HostReference>;
10
9
  export declare function findHostReferenceFromElement(element: SimpElement): Nullable<HostReference>;