@simpreact/simpreact 0.0.0-alpha.1f6ee65

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.
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Dmitrii Paskhin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # simpreact
@@ -0,0 +1,18 @@
1
+ import type { SimpNode } from './createElement';
2
+ type Provider<T = any> = (props: {
3
+ value: T;
4
+ children: SimpNode;
5
+ }) => SimpNode;
6
+ type Consumer<T = any> = (props: {
7
+ children: (value: T) => SimpNode;
8
+ }, contextMap: SimpContextMap) => SimpNode;
9
+ export interface SimpContext<T> {
10
+ defaultValue: T;
11
+ Provider: Provider<T>;
12
+ Consumer: Consumer<T>;
13
+ }
14
+ export type SimpContextMap = Map<SimpContext<any>, any>;
15
+ export declare function createContext<T>(defaultValue: T): SimpContext<T>;
16
+ export declare function isProvider(type: any): boolean;
17
+ export declare function isConsumer(type: any): boolean;
18
+ export {};
@@ -0,0 +1,18 @@
1
+ export function createContext(defaultValue) {
2
+ const context = {
3
+ defaultValue,
4
+ Provider: Object.create(null),
5
+ Consumer(props, contextMap) {
6
+ return props.children(contextMap.get(context) ?? defaultValue);
7
+ },
8
+ };
9
+ Object.defineProperty(context.Consumer, 'isConsumer', { value: true });
10
+ Object.defineProperty(context.Provider, 'context', { value: context, enumerable: true });
11
+ return context;
12
+ }
13
+ export function isProvider(type) {
14
+ return type != null && type.context != null;
15
+ }
16
+ export function isConsumer(type) {
17
+ return type != null && type.isConsumer === true;
18
+ }
@@ -0,0 +1,26 @@
1
+ import type { Many, Maybe, Primitive } from '../shared';
2
+ import type { HostReference } from './hostAdapter';
3
+ import type { SimpContextMap } from './context';
4
+ export type SimpNode = SimpElement | string | number | bigint | Array<SimpNode> | boolean | null | undefined;
5
+ export type Props = any;
6
+ export type Key = string | number | bigint;
7
+ export interface FunctionComponent<P = Props> {
8
+ (props: P): SimpNode;
9
+ }
10
+ export type FC<P = Props> = FunctionComponent<P>;
11
+ export type SimpElementFlag = 'FC' | 'HOST' | 'TEXT' | 'FRAGMENT' | 'PROVIDER' | 'CONSUMER';
12
+ export interface SimpElement<T = Props> {
13
+ flag: SimpElementFlag;
14
+ key?: Maybe<Key>;
15
+ type?: Maybe<string | FunctionComponent<T>>;
16
+ props?: Maybe<T>;
17
+ children?: Maybe<SimpNode>;
18
+ className?: Maybe<string>;
19
+ reference?: Maybe<HostReference>;
20
+ store?: unknown;
21
+ contextMap?: Maybe<SimpContextMap>;
22
+ }
23
+ export declare function createElement<P = Props>(type: string | FunctionComponent<Readonly<P>>, props?: Maybe<P>, ...children: SimpNode[]): SimpElement<P>;
24
+ export declare function createTextElement(text: Primitive): SimpElement;
25
+ export declare function normalizeChildren(children: SimpNode): Maybe<Many<SimpElement>>;
26
+ export declare function normalizeRoot(node: SimpNode): SimpElement | undefined;
@@ -0,0 +1,166 @@
1
+ import { isPrimitive } from '../shared';
2
+ import { Fragment } from './fragment';
3
+ import { isConsumer, isProvider } from './context';
4
+ export function createElement(type, props, ...children) {
5
+ let newProps;
6
+ let className;
7
+ let key;
8
+ let definedChildren;
9
+ const childLength = children.length;
10
+ if (childLength === 1) {
11
+ definedChildren = children[0];
12
+ }
13
+ else if (childLength > 1) {
14
+ definedChildren = [];
15
+ for (let i = 0; i < childLength; i++) {
16
+ definedChildren.push(children[i]);
17
+ }
18
+ }
19
+ if (typeof type === 'string') {
20
+ if (props != null) {
21
+ for (const propName in props) {
22
+ if (propName === 'className') {
23
+ className = props[propName];
24
+ }
25
+ else if (propName === 'key') {
26
+ key = props[propName];
27
+ }
28
+ else if (propName === 'children') {
29
+ if (definedChildren === undefined) {
30
+ definedChildren = props[propName];
31
+ }
32
+ }
33
+ else {
34
+ (newProps ||= {})[propName] = props[propName];
35
+ }
36
+ }
37
+ }
38
+ const element = {
39
+ flag: 'HOST',
40
+ type,
41
+ };
42
+ if (className) {
43
+ element.className = className;
44
+ }
45
+ if (key) {
46
+ element.key = key;
47
+ }
48
+ if ((definedChildren = normalizeChildren(definedChildren))) {
49
+ element.children = definedChildren;
50
+ }
51
+ if (newProps) {
52
+ element.props = newProps;
53
+ }
54
+ return element;
55
+ }
56
+ else if (type === Fragment) {
57
+ const element = {
58
+ flag: 'FRAGMENT',
59
+ };
60
+ element.children = normalizeChildren(definedChildren || (props != null ? props.children : null));
61
+ if (props != null && props.key) {
62
+ element.key = props?.key;
63
+ }
64
+ return element;
65
+ }
66
+ else if (isProvider(type)) {
67
+ const element = { flag: 'PROVIDER', type, props: { value: props.value } };
68
+ element.children = normalizeChildren(definedChildren || props.children);
69
+ if (props != null && props.key) {
70
+ element.key = props?.key;
71
+ }
72
+ return element;
73
+ }
74
+ else if (isConsumer(type)) {
75
+ const element = {
76
+ flag: 'CONSUMER',
77
+ type,
78
+ props: { children: definedChildren || (props != null ? props.children : null) },
79
+ };
80
+ if (props != null && props.key) {
81
+ element.key = props?.key;
82
+ }
83
+ return element;
84
+ }
85
+ else {
86
+ if (props != null) {
87
+ for (const propName in props) {
88
+ if (propName === 'key') {
89
+ key = props[propName];
90
+ }
91
+ else if (propName === 'children') {
92
+ if (definedChildren === undefined) {
93
+ definedChildren = props[propName];
94
+ }
95
+ }
96
+ else {
97
+ (newProps ||= {})[propName] = props[propName];
98
+ }
99
+ }
100
+ }
101
+ if (definedChildren !== undefined) {
102
+ (newProps ||= {})['children'] = definedChildren;
103
+ }
104
+ const element = {
105
+ flag: 'FC',
106
+ type,
107
+ };
108
+ if (key) {
109
+ element.key = key;
110
+ }
111
+ if (newProps != null) {
112
+ element.props = newProps;
113
+ }
114
+ return element;
115
+ }
116
+ }
117
+ export function createTextElement(text) {
118
+ return {
119
+ flag: 'TEXT',
120
+ children: text == null || text === true || text === false ? '' : text,
121
+ };
122
+ }
123
+ export function normalizeChildren(children) {
124
+ if (children == null || typeof children === 'boolean') {
125
+ return;
126
+ }
127
+ const result = [];
128
+ normalizeNode(children, result);
129
+ if (result.length === 0) {
130
+ return;
131
+ }
132
+ return result.length === 1 ? result[0] : result;
133
+ }
134
+ function normalizeNode(child, result) {
135
+ if (child == null || typeof child === 'boolean') {
136
+ return;
137
+ }
138
+ if (isPrimitive(child)) {
139
+ result.push(createTextElement(child));
140
+ return;
141
+ }
142
+ if (Array.isArray(child)) {
143
+ for (const nestedChild of child) {
144
+ normalizeNode(nestedChild, result);
145
+ }
146
+ return;
147
+ }
148
+ if (typeof child === 'object') {
149
+ if (typeof child.flag !== 'string') {
150
+ throw new TypeError(`Objects are not valid as a child: ${JSON.stringify(child)}.`);
151
+ }
152
+ result.push(child);
153
+ }
154
+ }
155
+ export function normalizeRoot(node) {
156
+ if (node == null || typeof node === 'boolean') {
157
+ return;
158
+ }
159
+ if (isPrimitive(node)) {
160
+ return createTextElement(node);
161
+ }
162
+ if (Array.isArray(node)) {
163
+ return createElement(Fragment, { children: node });
164
+ }
165
+ return node;
166
+ }
@@ -0,0 +1,5 @@
1
+ import type { SimpNode } from './createElement';
2
+ export type Fragment = (props: {
3
+ children?: SimpNode;
4
+ }) => SimpNode;
5
+ export declare const Fragment: Fragment;
@@ -0,0 +1 @@
1
+ export const Fragment = Object.freeze(Object.create(null));
@@ -0,0 +1,21 @@
1
+ import type { HostAdapter } from './hostAdapter';
2
+ import type { SimpElement } from './createElement';
3
+ import { EventBus } from '../shared';
4
+ export type LifecycleEvent = {
5
+ type: 'beforeRender';
6
+ element: SimpElement;
7
+ } | {
8
+ type: 'afterRender';
9
+ } | {
10
+ type: 'mounted';
11
+ element: SimpElement;
12
+ } | {
13
+ type: 'unmounted';
14
+ element: SimpElement;
15
+ };
16
+ interface Global {
17
+ hostAdapter: HostAdapter;
18
+ eventBus: EventBus<LifecycleEvent>;
19
+ }
20
+ export declare const GLOBAL: Global;
21
+ export {};
package/core/global.js ADDED
@@ -0,0 +1,5 @@
1
+ import { EventBus } from '../shared';
2
+ export const GLOBAL = {
3
+ hostAdapter: null,
4
+ eventBus: new EventBus(),
5
+ };
@@ -0,0 +1,17 @@
1
+ import type { Dict, Maybe, Nullable } from '../shared';
2
+ export type HostReference = never;
3
+ export interface HostAdapter<HostRef = any, HostTextRef = any> {
4
+ createReference(type: string): HostRef;
5
+ createTextReference(text: string): HostTextRef;
6
+ mountProps(reference: HostRef, props: Dict): void;
7
+ patchProp(reference: HostRef, propName: string, prevValue: unknown, nextValue: unknown): void;
8
+ setClassname(reference: HostRef, className: Maybe<string>): void;
9
+ setTextContent(reference: HostRef, text: string): void;
10
+ appendChild(parent: HostRef, child: HostRef | HostTextRef): void;
11
+ removeChild(parent: HostRef, child: HostRef | HostTextRef): void;
12
+ replaceChild(parent: HostRef, replacer: HostRef | HostTextRef, toBeReplaced: HostRef | HostTextRef): void;
13
+ insertBefore(parent: HostRef, child: HostRef | HostTextRef, before: Nullable<HostRef | HostTextRef>): void;
14
+ insertOrAppend(parent: HostRef, child: HostRef | HostTextRef, before: Nullable<HostRef | HostTextRef>): void;
15
+ findParentReference(reference: HostRef | HostTextRef): Nullable<HostRef>;
16
+ clearNode(reference: HostRef | HostTextRef): void;
17
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export { type FC, type SimpElement, createElement, type SimpNode, type FunctionComponent } from './createElement';
2
+ export { Fragment } from './fragment';
3
+ export { type SimpContext, createContext } from './context';
package/core/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createElement } from './createElement';
2
+ export { Fragment } from './fragment';
3
+ export { createContext } from './context';
@@ -0,0 +1,8 @@
1
+ export * from './index';
2
+ export * from './context';
3
+ export * from './createElement';
4
+ export * from './rerender';
5
+ export * from './global';
6
+ export * from './hostAdapter';
7
+ export * from './mounting';
8
+ export * from './patching';
@@ -0,0 +1,8 @@
1
+ export * from './index';
2
+ export * from './context';
3
+ export * from './createElement';
4
+ export * from './rerender';
5
+ export * from './global';
6
+ export * from './hostAdapter';
7
+ export * from './mounting';
8
+ export * from './patching';
@@ -0,0 +1,12 @@
1
+ import type { Nullable } from '../shared';
2
+ import type { HostReference } from './hostAdapter';
3
+ import type { SimpElement } from './createElement';
4
+ import type { SimpContextMap } from './context';
5
+ export declare function mount(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
6
+ 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>): void;
8
+ export declare function mountFunctionalElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
9
+ export declare function mountFragment(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
10
+ export declare function mountArrayChildren(children: SimpElement[], reference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
11
+ export declare function mountProvider(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
12
+ export declare function mountConsumer(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
@@ -0,0 +1,95 @@
1
+ import { EMPTY_MAP, EMPTY_OBJECT } from '../shared';
2
+ import { GLOBAL } from './global';
3
+ import { normalizeRoot } from './createElement';
4
+ export function mount(element, parentReference, nextReference, contextMap) {
5
+ if (element.flag === 'TEXT') {
6
+ mountTextElement(element, parentReference, nextReference);
7
+ }
8
+ else if (element.flag === 'HOST') {
9
+ mountHostElement(element, parentReference, nextReference, contextMap);
10
+ }
11
+ else if (element.flag === 'FC') {
12
+ mountFunctionalElement(element, parentReference, nextReference, contextMap);
13
+ }
14
+ else if (element.flag === 'FRAGMENT') {
15
+ mountFragment(element, parentReference, nextReference, contextMap);
16
+ }
17
+ else if (element.flag === 'PROVIDER') {
18
+ mountProvider(element, parentReference, nextReference, contextMap);
19
+ }
20
+ else {
21
+ mountConsumer(element, parentReference, nextReference, contextMap);
22
+ }
23
+ }
24
+ export function mountTextElement(element, parentReference, nextReference) {
25
+ const reference = (element.reference ||= GLOBAL.hostAdapter.createTextReference(element.children));
26
+ if (parentReference != null) {
27
+ GLOBAL.hostAdapter.insertOrAppend(parentReference, reference, nextReference);
28
+ }
29
+ }
30
+ export function mountHostElement(element, parentReference, nextReference, contextMap) {
31
+ const props = element.props;
32
+ const className = element.className;
33
+ const hostReference = (element.reference = GLOBAL.hostAdapter.createReference(element.type));
34
+ // HOST element always has Maybe<Many<SimpElement>> children due to normalization process.
35
+ const children = element.children;
36
+ if (className != null && className !== '') {
37
+ GLOBAL.hostAdapter.setClassname(hostReference, className);
38
+ }
39
+ if (Array.isArray(children)) {
40
+ mountArrayChildren(children, hostReference, null, contextMap);
41
+ }
42
+ else if (children != null) {
43
+ mount(children, hostReference, null, contextMap);
44
+ }
45
+ if (parentReference != null) {
46
+ GLOBAL.hostAdapter.insertOrAppend(parentReference, hostReference, nextReference);
47
+ }
48
+ if (props != null) {
49
+ GLOBAL.hostAdapter.mountProps(hostReference, props);
50
+ }
51
+ }
52
+ export function mountFunctionalElement(element, parentReference, nextReference, contextMap) {
53
+ const type = element.type;
54
+ let children;
55
+ element.contextMap = contextMap;
56
+ GLOBAL.eventBus.publish({ type: 'beforeRender', element });
57
+ children = normalizeRoot(type(element.props || EMPTY_OBJECT));
58
+ GLOBAL.eventBus.publish({ type: 'afterRender' });
59
+ if (children != null) {
60
+ mount((element.children = children), parentReference, nextReference, contextMap);
61
+ }
62
+ GLOBAL.eventBus.publish({ type: 'mounted', element });
63
+ }
64
+ export function mountFragment(element, parentReference, nextReference, contextMap) {
65
+ // FRAGMENT element always has Maybe<Many<SimpElement>> children due to normalization process.
66
+ if (Array.isArray(element.children)) {
67
+ mountArrayChildren(element.children, parentReference, nextReference, contextMap);
68
+ }
69
+ else if (element.children != null) {
70
+ mount(element.children, parentReference, nextReference, contextMap);
71
+ }
72
+ }
73
+ export function mountArrayChildren(children, reference, nextReference, contextMap) {
74
+ for (const child of children) {
75
+ mount(child, reference, nextReference, contextMap);
76
+ }
77
+ }
78
+ export function mountProvider(element, parentReference, nextReference, contextMap) {
79
+ contextMap = new Map(contextMap);
80
+ contextMap.set(element.type.context, element.props.value);
81
+ // PROVIDER element always has Maybe<Many<SimpElement>> children due to normalization process.
82
+ if (Array.isArray(element.children)) {
83
+ mountArrayChildren(element.children, parentReference, nextReference, contextMap);
84
+ }
85
+ else if (element.children != null) {
86
+ mount(element.children, parentReference, nextReference, contextMap);
87
+ }
88
+ }
89
+ export function mountConsumer(element, parentReference, nextReference, contextMap) {
90
+ const children = normalizeRoot(element.type(element.props || EMPTY_OBJECT, contextMap || EMPTY_MAP));
91
+ if (children == null) {
92
+ return;
93
+ }
94
+ mount((element.children = children), parentReference, nextReference, contextMap);
95
+ }
@@ -0,0 +1,8 @@
1
+ import type { SimpElement } from './createElement';
2
+ import type { Nullable } from '../shared';
3
+ import type { HostReference } from './hostAdapter';
4
+ import type { SimpContextMap } from './context';
5
+ export declare function patch(prevElement: SimpElement, nextElement: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
6
+ export declare function findHostReferenceFromElement(element: SimpElement): Nullable<HostReference>;
7
+ export declare function updateFunctionalComponent(element: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
8
+ export declare function patchKeyedChildren(prevChildren: SimpElement[], nextChildren: SimpElement[], parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;