@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.
- package/compat/context.d.ts +8 -0
- package/compat/context.js +7 -0
- package/compat/core.d.ts +4 -7
- package/compat/core.js +7 -9
- package/compat/hooks.d.ts +0 -2
- package/compat/hooks.js +0 -2
- package/compat/index.d.ts +5 -4
- package/compat/index.js +3 -0
- package/context/index.d.ts +24 -0
- package/context/index.js +64 -0
- package/core/createElement.d.ts +6 -9
- package/core/createElement.js +1 -24
- package/core/index.d.ts +2 -21
- package/core/index.js +3 -3
- package/core/internal.d.ts +1 -1
- package/core/internal.js +1 -1
- package/core/memo.d.ts +10 -0
- package/core/memo.js +11 -0
- package/core/mounting.d.ts +6 -9
- package/core/mounting.js +21 -56
- package/core/patching.d.ts +4 -5
- package/core/patching.js +55 -85
- package/core/rerender.d.ts +5 -4
- package/core/rerender.js +76 -24
- package/core/unmounting.js +3 -3
- package/dom/attach-element-to-dom.js +10 -1
- package/dom/events.d.ts +6 -1
- package/dom/events.js +12 -2
- package/hooks/index.d.ts +1 -3
- package/hooks/index.js +20 -21
- package/package.json +1 -1
- package/shared/index.d.ts +6 -0
- package/shared/index.js +12 -3
- package/shared/utils.d.ts +1 -0
- package/shared/utils.js +20 -0
- package/compat/utils.d.ts +0 -1
- package/compat/utils.js +0 -3
- package/core/context.d.ts +0 -18
- package/core/context.js +0 -18
package/hooks/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { noop } from '../shared/index.js';
|
|
3
|
-
import { callOrGet } from '../shared/utils.js';
|
|
1
|
+
import { lifecycleEventBus, rerender as _rerender } from '../core/internal.js';
|
|
2
|
+
import { callOrGet, noop } from '../shared/index.js';
|
|
4
3
|
let currentIndex = 0;
|
|
5
4
|
// In runtime this is a nullable variable.
|
|
6
5
|
let currentElement;
|
|
@@ -21,19 +20,16 @@ lifecycleEventBus.subscribe(event => {
|
|
|
21
20
|
if (event.type === 'mounted') {
|
|
22
21
|
const element = event.element;
|
|
23
22
|
if (element.store?.effectsHookStates) {
|
|
24
|
-
batchingRerenderLocker.lock();
|
|
25
23
|
const effects = element.store.effectsHookStates;
|
|
26
24
|
element.store.effectsHookStates = undefined;
|
|
27
25
|
for (const state of effects) {
|
|
28
26
|
state.cleanup = state.effect() || undefined;
|
|
29
27
|
}
|
|
30
|
-
batchingRerenderLocker.flush();
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
30
|
if (event.type === 'updated') {
|
|
34
31
|
const element = event.element;
|
|
35
32
|
if (element.store?.effectsHookStates) {
|
|
36
|
-
batchingRerenderLocker.lock();
|
|
37
33
|
const effects = element.store.effectsHookStates;
|
|
38
34
|
element.store.effectsHookStates = undefined;
|
|
39
35
|
for (const state of effects) {
|
|
@@ -42,13 +38,14 @@ lifecycleEventBus.subscribe(event => {
|
|
|
42
38
|
}
|
|
43
39
|
state.cleanup = state.effect() || undefined;
|
|
44
40
|
}
|
|
45
|
-
batchingRerenderLocker.flush();
|
|
46
41
|
}
|
|
47
42
|
}
|
|
48
43
|
if (event.type === 'unmounted') {
|
|
49
44
|
const element = event.element;
|
|
50
45
|
if (element.store?.hookStates) {
|
|
51
|
-
|
|
46
|
+
const hookStates = element.store.hookStates;
|
|
47
|
+
element.store.hookStates = undefined;
|
|
48
|
+
for (const state of hookStates) {
|
|
52
49
|
if (state && 'cleanup' in state && typeof state.cleanup === 'function') {
|
|
53
50
|
state.cleanup();
|
|
54
51
|
}
|
|
@@ -56,17 +53,21 @@ lifecycleEventBus.subscribe(event => {
|
|
|
56
53
|
}
|
|
57
54
|
}
|
|
58
55
|
if (event.type === 'errored') {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
function handleError(element, error) {
|
|
57
|
+
element = findElementWithCatchHandlers(element);
|
|
58
|
+
if (!element) {
|
|
59
|
+
throw new Error('Error occurred during rendering a component', { cause: error });
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
for (const state of element.store.catchHandlers) {
|
|
63
|
+
state(error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
handleError(element.parent, error);
|
|
67
68
|
}
|
|
68
|
-
batchingRerenderLocker.flush();
|
|
69
69
|
}
|
|
70
|
+
handleError(event.element, event.error);
|
|
70
71
|
}
|
|
71
72
|
});
|
|
72
73
|
function findElementWithCatchHandlers(element) {
|
|
@@ -91,6 +92,7 @@ export function useRerender() {
|
|
|
91
92
|
if (!hookStates[currentIndex]) {
|
|
92
93
|
const elementStore = currentElement.store;
|
|
93
94
|
hookStates[currentIndex] = function rerender() {
|
|
95
|
+
elementStore.forceRender = true;
|
|
94
96
|
_rerender(elementStore.latestElement);
|
|
95
97
|
};
|
|
96
98
|
}
|
|
@@ -108,6 +110,7 @@ export function useState(initialState) {
|
|
|
108
110
|
return;
|
|
109
111
|
}
|
|
110
112
|
state[0] = nextValue;
|
|
113
|
+
elementStore.forceRender = true;
|
|
111
114
|
_rerender(elementStore.latestElement);
|
|
112
115
|
};
|
|
113
116
|
}
|
|
@@ -141,9 +144,6 @@ export function useUnmounted(cleanup) {
|
|
|
141
144
|
}
|
|
142
145
|
currentIndex++;
|
|
143
146
|
}
|
|
144
|
-
export function useContext(context) {
|
|
145
|
-
return currentElement.contextMap?.get(context) ?? context.defaultValue;
|
|
146
|
-
}
|
|
147
147
|
export function useCatch(cb) {
|
|
148
148
|
if (!currentElement.store) {
|
|
149
149
|
currentElement.store = {};
|
|
@@ -189,7 +189,6 @@ export default {
|
|
|
189
189
|
useEffect,
|
|
190
190
|
useMounted,
|
|
191
191
|
useUnmounted,
|
|
192
|
-
useContext,
|
|
193
192
|
useCatch,
|
|
194
193
|
areDepsEqual,
|
|
195
194
|
};
|
package/package.json
CHANGED
package/shared/index.d.ts
CHANGED
|
@@ -17,3 +17,9 @@ declare class EventBus<Event = void> {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
declare function isSimpText(value: unknown): value is SimpText;
|
|
20
|
+
|
|
21
|
+
declare function noop(): void;
|
|
22
|
+
|
|
23
|
+
declare function callOrGet<T, A extends any[]>(value: T | ((...args: A) => T), ...args: A): T;
|
|
24
|
+
|
|
25
|
+
declare function shallowEqual(objA: any, objB: any): boolean;
|
package/shared/index.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { EventBus } from './EventBus.js';
|
|
2
2
|
import { emptyArray, emptyMap, emptyObject } from './lang.js';
|
|
3
|
-
import { isSimpText, noop } from './utils.js';
|
|
4
|
-
export { emptyObject, emptyMap, emptyArray, isSimpText, EventBus, noop };
|
|
5
|
-
export default {
|
|
3
|
+
import { callOrGet, isSimpText, noop, shallowEqual } from './utils.js';
|
|
4
|
+
export { emptyObject, emptyMap, emptyArray, isSimpText, EventBus, noop, callOrGet, shallowEqual };
|
|
5
|
+
export default {
|
|
6
|
+
isSimpText,
|
|
7
|
+
EMPTY_MAP: emptyMap,
|
|
8
|
+
EMPTY_ARRAY: emptyArray,
|
|
9
|
+
EMPTY_OBJECT: emptyObject,
|
|
10
|
+
EventBus,
|
|
11
|
+
noop,
|
|
12
|
+
callOrGet,
|
|
13
|
+
emptyObject,
|
|
14
|
+
};
|
package/shared/utils.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ import type { SimpText } from './public.js';
|
|
|
2
2
|
export declare function isSimpText(value: unknown): value is SimpText;
|
|
3
3
|
export declare function noop(): void;
|
|
4
4
|
export declare function callOrGet<T, A extends any[]>(value: T | ((...args: A) => T), ...args: A): T;
|
|
5
|
+
export declare function shallowEqual(objA: any, objB: any): boolean;
|
package/shared/utils.js
CHANGED
|
@@ -18,3 +18,23 @@ export function callOrGet(value) {
|
|
|
18
18
|
}
|
|
19
19
|
return value(...args);
|
|
20
20
|
}
|
|
21
|
+
export function shallowEqual(objA, objB) {
|
|
22
|
+
if (Object.is(objA, objB)) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const keysA = Object.keys(objA);
|
|
29
|
+
const keysB = Object.keys(objB);
|
|
30
|
+
if (keysA.length !== keysB.length) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
34
|
+
const currentKey = keysA[i];
|
|
35
|
+
if (!Object.prototype.hasOwnProperty.call(objB, currentKey) || !Object.is(objA[currentKey], objB[currentKey])) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}
|
package/compat/utils.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function identity(value: any): any;
|
package/compat/utils.js
DELETED
package/core/context.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { SimpNode } from './createElement.js';
|
|
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 {};
|
package/core/context.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
}
|