lithent 0.1.0
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 +21 -0
- package/README.md +202 -0
- package/dist/diff.d.ts +14 -0
- package/dist/hook/mountCallback.d.ts +3 -0
- package/dist/hook/ref.d.ts +3 -0
- package/dist/hook/unmount.d.ts +3 -0
- package/dist/hook/updateCallback.d.ts +2 -0
- package/dist/hook/useUpdate.d.ts +3 -0
- package/dist/index.d.ts +29 -0
- package/dist/lithent.mjs +867 -0
- package/dist/lithent.mjs.map +1 -0
- package/dist/lithent.umd.js +2 -0
- package/dist/lithent.umd.js.map +1 -0
- package/dist/render.d.ts +15 -0
- package/dist/types/index.d.ts +65 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/predicator.d.ts +36 -0
- package/dist/utils/universalRef.d.ts +59 -0
- package/dist/wDom.d.ts +17 -0
- package/examples/dist/assets/index.e76a0ef8.js +2 -0
- package/examples/dist/assets/index.e76a0ef8.js.map +1 -0
- package/examples/dist/index.html +13 -0
- package/examples/package.json +33 -0
- package/examples/src/index.tsx +109 -0
- package/helper/dist/hook/computed.d.ts +4 -0
- package/helper/dist/hook/effect.d.ts +1 -0
- package/helper/dist/hook/state.d.ts +4 -0
- package/helper/dist/hook/store.d.ts +8 -0
- package/helper/dist/index.d.ts +4 -0
- package/helper/dist/lithentHelper.mjs +56 -0
- package/helper/dist/lithentHelper.mjs.map +1 -0
- package/helper/dist/lithentHelper.umd.js +2 -0
- package/helper/dist/lithentHelper.umd.js.map +1 -0
- package/helper/package.json +32 -0
- package/helper/src/hook/computed.ts +23 -0
- package/helper/src/hook/effect.ts +20 -0
- package/helper/src/hook/state.ts +26 -0
- package/helper/src/hook/store.ts +25 -0
- package/helper/src/index.ts +4 -0
- package/package.json +90 -0
- package/router/dist/Router.d.ts +4 -0
- package/router/dist/Router.mjs +56 -0
- package/router/dist/Router.mjs.map +1 -0
- package/router/dist/Router.umd.js +2 -0
- package/router/dist/Router.umd.js.map +1 -0
- package/router/dist/hook/params.d.ts +2 -0
- package/router/package.json +37 -0
- package/router/src/Router.tsx +63 -0
- package/router/src/components/Main.tsx +14 -0
- package/router/src/components/Sub.tsx +7 -0
- package/router/src/components/Sub2.tsx +17 -0
- package/router/src/example.tsx +19 -0
- package/router/src/hook/params.ts +9 -0
- package/src/diff.ts +235 -0
- package/src/examples/callback.tsx +81 -0
- package/src/examples/insert.tsx +36 -0
- package/src/examples/loop.tsx +33 -0
- package/src/examples/nested.tsx +44 -0
- package/src/examples/renew.tsx +29 -0
- package/src/examples/selectbox.tsx +42 -0
- package/src/examples/textarea.tsx +24 -0
- package/src/hook/mountCallback.ts +41 -0
- package/src/hook/ref.ts +1 -0
- package/src/hook/unmount.ts +26 -0
- package/src/hook/updateCallback.ts +29 -0
- package/src/hook/useUpdate.ts +51 -0
- package/src/index.ts +50 -0
- package/src/render.ts +409 -0
- package/src/types/index.ts +107 -0
- package/src/utils/index.ts +52 -0
- package/src/utils/predicator.ts +119 -0
- package/src/utils/universalRef.ts +137 -0
- package/src/wDom.ts +284 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { h, Fragment, mount, render } from '@/index';
|
|
2
|
+
|
|
3
|
+
const Nested = mount(function (renew) {
|
|
4
|
+
let choiceNode = 7;
|
|
5
|
+
|
|
6
|
+
const handle = () => {
|
|
7
|
+
const randomValue = Math.floor(Math.random() * 10) + 1;
|
|
8
|
+
choiceNode = randomValue;
|
|
9
|
+
renew();
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const componentMaker = () => {
|
|
13
|
+
return (
|
|
14
|
+
<Fragment>
|
|
15
|
+
<button onClick={handle}>handle</button>
|
|
16
|
+
{choiceNode === 1 && <div>1</div>}
|
|
17
|
+
{choiceNode === 2 && <div>2</div>}
|
|
18
|
+
<Fragment>
|
|
19
|
+
{choiceNode === 3 && <div>3</div>}
|
|
20
|
+
{choiceNode === 4 && <div>4</div>}
|
|
21
|
+
<Fragment>
|
|
22
|
+
{choiceNode === 5 && <div>5</div>}
|
|
23
|
+
{choiceNode === 6 && <div>6</div>}
|
|
24
|
+
<div>6.5</div>
|
|
25
|
+
{choiceNode === 7 && <div>7</div>}
|
|
26
|
+
</Fragment>
|
|
27
|
+
{choiceNode === 8 && <div>8</div>}
|
|
28
|
+
{choiceNode === 9 && <div>9</div>}
|
|
29
|
+
</Fragment>
|
|
30
|
+
{choiceNode === 10 && <div>10</div>}
|
|
31
|
+
<div>11</div>
|
|
32
|
+
</Fragment>
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return componentMaker;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const Root = <Nested />;
|
|
40
|
+
|
|
41
|
+
//@ts-ignore
|
|
42
|
+
window.root = Root;
|
|
43
|
+
|
|
44
|
+
render(Root, document.getElementById('root'));
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// example.jsx
|
|
2
|
+
import { h, Fragment, render, Renew, mount } from '@/index';
|
|
3
|
+
|
|
4
|
+
const Renew = mount((renew, _props) => {
|
|
5
|
+
let count1 = 0;
|
|
6
|
+
let count2 = 0;
|
|
7
|
+
let count3 = 0;
|
|
8
|
+
let count4 = 0;
|
|
9
|
+
|
|
10
|
+
const change = () => {
|
|
11
|
+
count1 += 1;
|
|
12
|
+
count2 += 2;
|
|
13
|
+
count3 += 3;
|
|
14
|
+
count4 -= 1;
|
|
15
|
+
renew();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return () => (
|
|
19
|
+
<Fragment>
|
|
20
|
+
<li>count1: {count1}</li>
|
|
21
|
+
<li>count2: {count2}</li>
|
|
22
|
+
<li>count3: {count3}</li>
|
|
23
|
+
<li>count4: {count4}</li>
|
|
24
|
+
<button onClick={change}>change</button>
|
|
25
|
+
</Fragment>
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
render(<Renew />, document.getElementById('root'));
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// example.jsx
|
|
2
|
+
import { h, render, Fragment, mount } from '@/index';
|
|
3
|
+
|
|
4
|
+
const Selectbox = mount(renew => {
|
|
5
|
+
let value = '3';
|
|
6
|
+
|
|
7
|
+
const changeChange = (event: InputEvent) => {
|
|
8
|
+
value = (event.target as HTMLInputElement).value;
|
|
9
|
+
renew();
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
return () => (
|
|
13
|
+
<Fragment>
|
|
14
|
+
<div>{value}</div>
|
|
15
|
+
<div>
|
|
16
|
+
<select onChange={changeChange}>
|
|
17
|
+
<option value="1" selected={value === '1'}>
|
|
18
|
+
1
|
|
19
|
+
</option>
|
|
20
|
+
<option value="2" selected={value === '2'}>
|
|
21
|
+
2
|
|
22
|
+
</option>
|
|
23
|
+
<option value="3" selected={value === '3'}>
|
|
24
|
+
3
|
|
25
|
+
</option>
|
|
26
|
+
<option value="4" selected={value === '4'}>
|
|
27
|
+
4
|
|
28
|
+
</option>
|
|
29
|
+
<option value="5" selected={value === '5'}>
|
|
30
|
+
5
|
|
31
|
+
</option>
|
|
32
|
+
</select>
|
|
33
|
+
</div>
|
|
34
|
+
</Fragment>
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
const Root = <Selectbox />;
|
|
38
|
+
|
|
39
|
+
//@ts-ignore
|
|
40
|
+
window.root = Root;
|
|
41
|
+
|
|
42
|
+
render(Root, document.getElementById('root'));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// example.jsx
|
|
2
|
+
import { h, render, mount } from '@/index';
|
|
3
|
+
|
|
4
|
+
const TextArea = mount(renew => {
|
|
5
|
+
let text = '123';
|
|
6
|
+
|
|
7
|
+
const changeInput = (event: InputEvent) => {
|
|
8
|
+
text = (event?.target as HTMLInputElement).value;
|
|
9
|
+
renew();
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
return () => (
|
|
13
|
+
<div>
|
|
14
|
+
<textarea onInput={changeInput}>{text}</textarea>
|
|
15
|
+
<div>{text}</div>
|
|
16
|
+
</div>
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
const Root = <TextArea />;
|
|
20
|
+
|
|
21
|
+
//@ts-ignore
|
|
22
|
+
window.root = Root;
|
|
23
|
+
|
|
24
|
+
render(Root, document.getElementById('root'));
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { WDom } from '@/types';
|
|
2
|
+
import {
|
|
3
|
+
componentKeyRef,
|
|
4
|
+
componentRef,
|
|
5
|
+
getComponentKey,
|
|
6
|
+
} from '@/utils/universalRef';
|
|
7
|
+
|
|
8
|
+
import { unmount } from '@/hook/unmount';
|
|
9
|
+
|
|
10
|
+
export const mountCallback = (effectAction: () => void) => {
|
|
11
|
+
const mts = componentRef.get(getComponentKey())!.mts;
|
|
12
|
+
|
|
13
|
+
mts.push(effectAction);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const runMountedQueueFromWDom = (newWDom: WDom) => {
|
|
17
|
+
const { componentKey } = newWDom;
|
|
18
|
+
|
|
19
|
+
if (componentKey) {
|
|
20
|
+
const component = componentRef.get(componentKey);
|
|
21
|
+
const queue = component?.mts;
|
|
22
|
+
const sequence = component?.upS;
|
|
23
|
+
|
|
24
|
+
componentKeyRef.value = componentKey;
|
|
25
|
+
|
|
26
|
+
if (sequence) {
|
|
27
|
+
sequence.value = 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (queue) {
|
|
31
|
+
component.mts = [];
|
|
32
|
+
|
|
33
|
+
queue.forEach((effect: Function) => {
|
|
34
|
+
const callback = effect();
|
|
35
|
+
if (callback) {
|
|
36
|
+
unmount(callback);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
package/src/hook/ref.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ref = <T>(initValue: T) => ({ value: initValue });
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { WDom, Props } from '@/types';
|
|
2
|
+
import { componentRef, getComponentKey } from '@/utils/universalRef';
|
|
3
|
+
|
|
4
|
+
export const unmount = (effectAction: () => void) => {
|
|
5
|
+
componentRef.get(getComponentKey())!.umts.push(effectAction);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const runUnmountQueueFromWDom = (newWDom: WDom) => {
|
|
9
|
+
const { componentKey, nodeChildKey } = newWDom;
|
|
10
|
+
const childKeys = nodeChildKey?.value;
|
|
11
|
+
|
|
12
|
+
if (componentKey) {
|
|
13
|
+
removeItem(componentKey);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (childKeys) {
|
|
17
|
+
childKeys.forEach(childKey => removeItem(childKey));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const removeItem = (componentKey: Props) => {
|
|
22
|
+
const queue = componentRef.get(componentKey)!.umts;
|
|
23
|
+
componentRef.get(componentKey)!.umts = [];
|
|
24
|
+
queue.forEach(effect => effect());
|
|
25
|
+
componentRef.delete(componentKey);
|
|
26
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
componentRef,
|
|
3
|
+
redrawQueue,
|
|
4
|
+
redrawQueueTimeout,
|
|
5
|
+
getComponentKey,
|
|
6
|
+
} from '@/utils/universalRef';
|
|
7
|
+
|
|
8
|
+
import { useUpdated } from '@/hook/useUpdate';
|
|
9
|
+
|
|
10
|
+
export const updateCallback = (
|
|
11
|
+
effectAction: () => (() => void) | void,
|
|
12
|
+
dependencies: () => any[] = () => []
|
|
13
|
+
) => {
|
|
14
|
+
const updateReqs = componentRef.get(getComponentKey())!.upR;
|
|
15
|
+
|
|
16
|
+
updateReqs.push(() => useUpdated(effectAction, dependencies));
|
|
17
|
+
useUpdated(effectAction, dependencies);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const runUpdateCallback = () => {
|
|
21
|
+
const updateReqs = componentRef.get(getComponentKey())?.upR;
|
|
22
|
+
|
|
23
|
+
if (updateReqs?.length) {
|
|
24
|
+
updateReqs.forEach(callback => callback());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
redrawQueue.value = [];
|
|
28
|
+
redrawQueueTimeout.value = null;
|
|
29
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { WDom } from '@/types';
|
|
2
|
+
import {
|
|
3
|
+
componentKeyRef,
|
|
4
|
+
componentRef,
|
|
5
|
+
getComponentKey,
|
|
6
|
+
} from '@/utils/universalRef';
|
|
7
|
+
|
|
8
|
+
export const useUpdated = (
|
|
9
|
+
effectAction: () => (() => void) | void,
|
|
10
|
+
dependencies: () => any[] = () => []
|
|
11
|
+
) => {
|
|
12
|
+
const component = componentRef.get(getComponentKey());
|
|
13
|
+
const { upD, upS } = component!;
|
|
14
|
+
const def = upD[upS.value];
|
|
15
|
+
|
|
16
|
+
if (def && checkNeedPushQueue(def, dependencies())) {
|
|
17
|
+
const callback = effectAction();
|
|
18
|
+
if (callback) {
|
|
19
|
+
component!.upCB.push(callback);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
upD[upS.value] = dependencies();
|
|
24
|
+
upS.value += 1;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const runUpdatedQueueFromWDom = (newWDom: WDom) => {
|
|
28
|
+
const { componentKey } = newWDom;
|
|
29
|
+
|
|
30
|
+
if (componentKey) {
|
|
31
|
+
const component = componentRef.get(componentKey);
|
|
32
|
+
const queue = component?.upCB;
|
|
33
|
+
const sequence = component?.upS;
|
|
34
|
+
|
|
35
|
+
componentKeyRef.value = componentKey;
|
|
36
|
+
|
|
37
|
+
if (sequence) {
|
|
38
|
+
sequence.value = 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (newWDom.constructor && queue) {
|
|
42
|
+
component.upCB = [];
|
|
43
|
+
queue.forEach((effect: Function) => effect());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const checkNeedPushQueue = (originalDefs: unknown[], newDefs: unknown[]) =>
|
|
49
|
+
!originalDefs.length && !originalDefs.length
|
|
50
|
+
? true
|
|
51
|
+
: originalDefs.some((def, index) => def !== newDefs[index]);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { h, Fragment, mount } from '@/wDom';
|
|
2
|
+
import { render } from '@/render';
|
|
3
|
+
import { mountCallback } from '@/hook/mountCallback';
|
|
4
|
+
import { updateCallback } from '@/hook/updateCallback';
|
|
5
|
+
import { ref } from '@/hook/ref';
|
|
6
|
+
import { checkFunction } from '@/utils/predicator';
|
|
7
|
+
import {
|
|
8
|
+
componentRef,
|
|
9
|
+
componentKeyRef,
|
|
10
|
+
dataStoreStore,
|
|
11
|
+
dataStoreRenderQueue,
|
|
12
|
+
getComponentKey,
|
|
13
|
+
componentRender,
|
|
14
|
+
} from '@/utils/universalRef';
|
|
15
|
+
|
|
16
|
+
const ext = {
|
|
17
|
+
componentRef,
|
|
18
|
+
componentKeyRef,
|
|
19
|
+
dataStoreStore,
|
|
20
|
+
dataStoreRenderQueue,
|
|
21
|
+
checkFunction,
|
|
22
|
+
getComponentKey,
|
|
23
|
+
componentRender,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type {
|
|
27
|
+
WDom,
|
|
28
|
+
TagFunction,
|
|
29
|
+
TagFunctionResolver,
|
|
30
|
+
FragmentFunction,
|
|
31
|
+
ComponentSubKey,
|
|
32
|
+
ComponentRef,
|
|
33
|
+
Props,
|
|
34
|
+
Renew,
|
|
35
|
+
MiddleStateWDomChildren,
|
|
36
|
+
MiddleStateWDom,
|
|
37
|
+
UseDataStoreValue,
|
|
38
|
+
NodePointer,
|
|
39
|
+
Param,
|
|
40
|
+
} from '@/types';
|
|
41
|
+
|
|
42
|
+
declare global {
|
|
43
|
+
namespace JSX {
|
|
44
|
+
interface IntrinsicElements {
|
|
45
|
+
[name: string]: any;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { h, Fragment, render, mount, mountCallback, updateCallback, ref, ext };
|