@potok-web-framework/core 0.1.0 → 0.2.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.
Files changed (94) hide show
  1. package/dist/block.d.ts +17 -0
  2. package/dist/bootstrap-app.d.ts +8 -0
  3. package/dist/client-only.d.ts +3 -0
  4. package/dist/client.mjs +133 -0
  5. package/dist/constants-BOAOReQ3.mjs +26 -0
  6. package/dist/constants.d.ts +2 -0
  7. package/dist/context.d.ts +12 -0
  8. package/dist/detect-child.d.ts +6 -0
  9. package/dist/error-boundary.d.ts +5 -0
  10. package/dist/exports/client.d.ts +1 -0
  11. package/dist/exports/hmr.d.ts +1 -0
  12. package/dist/exports/index.d.ts +14 -0
  13. package/dist/exports/jsx-runtime.d.ts +4 -0
  14. package/dist/exports/server.d.ts +1 -0
  15. package/dist/fragment-BahmURhz.mjs +17 -0
  16. package/dist/fragment.d.ts +4 -0
  17. package/dist/hmr/hmr-dev.d.ts +9 -0
  18. package/dist/hmr/register-component.d.ts +2 -0
  19. package/dist/hmr/registered-component.d.ts +23 -0
  20. package/dist/hmr/registry.d.ts +12 -0
  21. package/dist/hmr/types.d.ts +4 -0
  22. package/dist/hmr/utils.d.ts +3 -0
  23. package/dist/hmr.mjs +42 -0
  24. package/dist/html-element-Cm0RtMkT.mjs +42 -0
  25. package/dist/html-element.d.ts +6 -0
  26. package/dist/index.mjs +199 -0
  27. package/dist/jsx-runtime.mjs +10 -0
  28. package/dist/jsx-types.d.ts +11 -0
  29. package/dist/lazy.d.ts +7 -0
  30. package/dist/lib-context-reader.d.ts +5 -0
  31. package/dist/lib-scripts.d.ts +2 -0
  32. package/dist/lifecycle-4vjEuXGy.mjs +57 -0
  33. package/dist/lifecycle.d.ts +8 -0
  34. package/dist/list.d.ts +9 -0
  35. package/dist/portal-CbcYOHLv.mjs +44 -0
  36. package/dist/portal.d.ts +10 -0
  37. package/dist/prop-types.d.ts +939 -0
  38. package/dist/ref.d.ts +6 -0
  39. package/dist/render-to-dom.d.ts +8 -0
  40. package/dist/render-to-string.d.ts +2 -0
  41. package/dist/server-node.d.ts +10 -0
  42. package/dist/server.mjs +1192 -0
  43. package/dist/show.d.ts +6 -0
  44. package/dist/signals.d.ts +32 -0
  45. package/dist/store.d.ts +26 -0
  46. package/dist/text.d.ts +5 -0
  47. package/dist/types.d.ts +39 -0
  48. package/dist/utils-CAe_kbSH.mjs +345 -0
  49. package/dist/utils.d.ts +11 -0
  50. package/package.json +7 -2
  51. package/CHANGELOG.md +0 -7
  52. package/bun.lock +0 -25
  53. package/src/block.ts +0 -102
  54. package/src/bootstrap-app.ts +0 -115
  55. package/src/client-only.ts +0 -17
  56. package/src/constants.ts +0 -27
  57. package/src/context.ts +0 -85
  58. package/src/detect-child.ts +0 -21
  59. package/src/error-boundary.ts +0 -51
  60. package/src/exports/client.ts +0 -1
  61. package/src/exports/hmr.ts +0 -1
  62. package/src/exports/index.ts +0 -21
  63. package/src/exports/jsx-runtime.ts +0 -4
  64. package/src/exports/server.ts +0 -1
  65. package/src/fragment.ts +0 -28
  66. package/src/global.dev.d.ts +0 -12
  67. package/src/hmr/hmr-dev.ts +0 -10
  68. package/src/hmr/register-component.ts +0 -109
  69. package/src/hmr/registered-component.ts +0 -59
  70. package/src/hmr/registry.ts +0 -78
  71. package/src/hmr/types.ts +0 -6
  72. package/src/hmr/utils.ts +0 -20
  73. package/src/html-element.ts +0 -95
  74. package/src/jsx-types.ts +0 -13
  75. package/src/lazy.ts +0 -44
  76. package/src/lib-context-reader.ts +0 -33
  77. package/src/lib-scripts.ts +0 -8
  78. package/src/lifecycle.ts +0 -44
  79. package/src/list.ts +0 -175
  80. package/src/portal.ts +0 -101
  81. package/src/prop-types.ts +0 -1165
  82. package/src/ref.ts +0 -11
  83. package/src/render-to-dom.ts +0 -325
  84. package/src/render-to-string.ts +0 -65
  85. package/src/server-node.ts +0 -98
  86. package/src/show.ts +0 -46
  87. package/src/signals.ts +0 -323
  88. package/src/store.ts +0 -68
  89. package/src/text.ts +0 -35
  90. package/src/types.ts +0 -69
  91. package/src/utils.ts +0 -118
  92. package/tests/signals.test.ts +0 -403
  93. package/tsconfig.json +0 -17
  94. package/vite.config.ts +0 -21
@@ -1,95 +0,0 @@
1
- import { LibBlock } from './block'
2
- import type { LibHTMLElementTagNameMap } from './prop-types'
3
- import { createEffect } from './signals'
4
- import type { LibContext, LibHtmlElementNode, PotokElement } from './types'
5
- import {
6
- extractListenersFromProps,
7
- mergeContext,
8
- normalizeChildren,
9
- objectKeys,
10
- } from './utils'
11
-
12
- export type LibHTMLElementProps<Tag extends keyof LibHTMLElementTagNameMap> = {
13
- tag: Tag
14
- } & LibHTMLElementTagNameMap[Tag]
15
-
16
- class LibHTMLElement<
17
- Tag extends keyof LibHTMLElementTagNameMap
18
- > extends LibBlock {
19
- constructor(
20
- readonly props: LibHTMLElementProps<Tag>,
21
- readonly context: LibContext
22
- ) {
23
- super()
24
-
25
- const element: LibHtmlElementNode<Tag> = {
26
- type: 'html-element',
27
- context,
28
- props,
29
- }
30
-
31
- this.node = element
32
-
33
- const descriptors = Object.getOwnPropertyDescriptors(props)
34
-
35
- for (const [key, descriptor] of Object.entries(descriptors)) {
36
- if (['tag', 'children', 'ref'].includes(key)) {
37
- continue
38
- }
39
-
40
- if (descriptor.value) {
41
- context.updateHtmlElementNodeProp(element, key, descriptor.value)
42
- }
43
-
44
- const getter = descriptor.get
45
- if (getter) {
46
- this.addEffect(
47
- createEffect(() => {
48
- context.updateHtmlElementNodeProp(element, key, getter())
49
- }, true)
50
- )
51
- }
52
- }
53
-
54
- if (context.isHydrating) {
55
- this.insertNode()
56
- }
57
-
58
- normalizeChildren(props.children).forEach((childCreator, index) => {
59
- this.children.push(
60
- childCreator(
61
- mergeContext(context, {
62
- parentBlock: this,
63
- index,
64
- })
65
- )
66
- )
67
- })
68
-
69
- if (!context.isHydrating) {
70
- this.insertNode()
71
- }
72
- }
73
-
74
- override unmount() {
75
- if (this.node) {
76
- objectKeys(extractListenersFromProps(this.props)).forEach((key) => {
77
- this.context.listeners[key]?.delete(this.node as unknown as HTMLElement)
78
- })
79
- }
80
-
81
- if (this.props.ref) {
82
- this.props.ref.element = null
83
- }
84
-
85
- super.unmount()
86
- }
87
- }
88
-
89
- export function htmlElement<Tag extends keyof LibHTMLElementTagNameMap>(
90
- props: LibHTMLElementProps<Tag>
91
- ): PotokElement {
92
- return function (context: LibContext) {
93
- return new LibHTMLElement(props, context)
94
- }
95
- }
package/src/jsx-types.ts DELETED
@@ -1,13 +0,0 @@
1
- import { LibHTMLElementTagNameMap } from './prop-types'
2
-
3
- declare global {
4
- namespace JSX {
5
- interface IntrinsicElements extends LibHTMLElementTagNameMap {}
6
-
7
- interface ElementChildrenAttribute {
8
- children: {}
9
- }
10
- }
11
- }
12
-
13
- export {}
package/src/lazy.ts DELETED
@@ -1,44 +0,0 @@
1
- import { fragment } from './fragment'
2
- import { LibContextReader } from './lib-context-reader'
3
- import { Show } from './show'
4
- import { createSignal } from './signals'
5
- import { PotokElement } from './types'
6
-
7
- export type LazyProps<Module> = {
8
- import: () => Promise<Module>
9
- resolve: (module: Module) => PotokElement
10
- fallback?: PotokElement
11
- }
12
-
13
- export function Lazy<Module>(props: LazyProps<Module>): PotokElement {
14
- return LibContextReader({
15
- children: (context) => {
16
- const signal = createSignal({
17
- block: null as PotokElement | null,
18
- })
19
-
20
- context.promises.push(
21
- props.import().then((module) => {
22
- signal.block = props.resolve(module)
23
- }),
24
- )
25
-
26
- return fragment({
27
- children: [
28
- Show({
29
- get when() {
30
- return signal.block !== null
31
- },
32
- children: [signal.block!],
33
- }),
34
- Show({
35
- get when() {
36
- return signal.block === null
37
- },
38
- children: [props.fallback],
39
- }),
40
- ],
41
- })
42
- },
43
- })
44
- }
@@ -1,33 +0,0 @@
1
- import { LibBlock } from './block'
2
- import type { LibContext, MaybeArray, PotokElement } from './types'
3
- import { mergeContext, normalizeArray } from './utils'
4
-
5
- export type LibContextReaderProps = {
6
- children: MaybeArray<(context: LibContext) => PotokElement>
7
- }
8
-
9
- class LibContextReaderClass extends LibBlock {
10
- constructor(
11
- readonly props: LibContextReaderProps,
12
- readonly context: LibContext
13
- ) {
14
- super()
15
-
16
- normalizeArray(props.children).forEach((childCreator, index) => {
17
- this.children.push(
18
- childCreator(context)(
19
- mergeContext(context, {
20
- parentBlock: this,
21
- index,
22
- })
23
- )
24
- )
25
- })
26
- }
27
- }
28
-
29
- export function LibContextReader(props: LibContextReaderProps): PotokElement {
30
- return function (context: LibContext) {
31
- return new LibContextReaderClass(props, context)
32
- }
33
- }
@@ -1,8 +0,0 @@
1
- import { PortalOut } from './portal'
2
- import { PotokElement } from './types'
3
-
4
- export function LibScripts(): PotokElement {
5
- return PortalOut({
6
- name: 'lib-scripts',
7
- })
8
- }
package/src/lifecycle.ts DELETED
@@ -1,44 +0,0 @@
1
- import { LibBlock } from './block'
2
- import type { LibContext, PotokElement, WithChildren } from './types'
3
- import { mergeContext, normalizeChildren } from './utils'
4
-
5
- type OnMoutedReturnCallback = () => void
6
-
7
- export type LifecycleProps = WithChildren<{
8
- onMounted?: () => OnMoutedReturnCallback | void
9
- onUnmounted?: () => void
10
- }>
11
-
12
- class LifecycleClass extends LibBlock {
13
- private onMountedReturn?: OnMoutedReturnCallback | void
14
-
15
- constructor(readonly props: LifecycleProps, readonly context: LibContext) {
16
- super()
17
-
18
- normalizeChildren(props.children).forEach((childCreator, index) => {
19
- this.children.push(
20
- childCreator(
21
- mergeContext(context, {
22
- parentBlock: this,
23
- index,
24
- })
25
- )
26
- )
27
- })
28
-
29
- this.onMountedReturn = props.onMounted?.()
30
- }
31
-
32
- override unmount() {
33
- super.unmount()
34
-
35
- this.onMountedReturn?.()
36
- this.props.onUnmounted?.()
37
- }
38
- }
39
-
40
- export function Lifecycle(props: LifecycleProps): PotokElement {
41
- return function (context: LibContext) {
42
- return new LifecycleClass(props, context)
43
- }
44
- }
package/src/list.ts DELETED
@@ -1,175 +0,0 @@
1
- import { LibBlock } from './block'
2
- import {
3
- createEffect,
4
- createSignal,
5
- deepTrack,
6
- getSignal,
7
- isSignal,
8
- untrack,
9
- type Signal,
10
- } from './signals'
11
- import type { LibContext, PotokElement } from './types'
12
- import { mergeContext } from './utils'
13
-
14
- export type ListProps<Item> = {
15
- items: Item[]
16
- render: (each: { item: Item; index: number }) => PotokElement
17
- }
18
-
19
- type ArrayDiff<Item> =
20
- | {
21
- type: 'delete'
22
- index: number
23
- }
24
- | {
25
- type: 'add'
26
- index: number
27
- item: Item
28
- }
29
- | {
30
- type: 'replace'
31
- index: number
32
- oldItem: Item
33
- newItem: Item
34
- }
35
-
36
- class ListClass<Item> extends LibBlock {
37
- signals: Signal<{
38
- item: Item
39
- index: number
40
- }>[] = []
41
-
42
- constructor(readonly props: ListProps<Item>, readonly context: LibContext) {
43
- super()
44
-
45
- let prevItems: Item[] = []
46
-
47
- this.addEffect(
48
- createEffect(() => {
49
- const items = props.items
50
-
51
- if (isSignal(items)) {
52
- deepTrack(items, 1)
53
- }
54
-
55
- untrack(() => {
56
- const changes = this.diff(prevItems, items)
57
-
58
- changes.forEach((change) => {
59
- if (change.type === 'add') {
60
- const signal = createSignal({
61
- item: change.item,
62
- index: change.index,
63
- })
64
-
65
- this.children.splice(change.index, 0, null)
66
- this.signals.splice(change.index, 0, getSignal(signal)!)
67
-
68
- const creator = props.render(signal)
69
-
70
- const block = creator(
71
- mergeContext(context, {
72
- parentBlock: this,
73
- get index() {
74
- return signal.index
75
- },
76
- })
77
- )
78
-
79
- this.children[change.index] = block
80
- } else if (change.type === 'replace') {
81
- this.signals[change.index]!.proxy.item = change.newItem
82
- } else if (change.type === 'delete') {
83
- const child = this.children[change.index]
84
- this.children.splice(change.index, 1)
85
- this.signals.splice(change.index, 1)
86
- child?.unmount()
87
- }
88
- })
89
-
90
- this.signals.forEach((signal, index) => {
91
- if (signal.value.index !== index) {
92
- signal.proxy.index = index
93
- }
94
- })
95
-
96
- prevItems = [...items]
97
- })
98
- }, true)
99
- )
100
- }
101
-
102
- diff(firstArray: Item[], secondArray: Item[]): ArrayDiff<Item>[] {
103
- const paths: number[][] = []
104
-
105
- for (let y = 0; y <= firstArray.length; y++) {
106
- paths.push([y])
107
-
108
- for (let x = 1; x <= secondArray.length; x++) {
109
- if (y === 0) {
110
- paths[y]!.push(x)
111
- } else {
112
- const top = paths[y - 1]![x]! + 1
113
- const left = paths[y]![x - 1]! + 1
114
- const topLeft =
115
- paths[y - 1]![x - 1]! +
116
- (firstArray[y - 1] !== secondArray[x - 1] ? 1 : 0)
117
-
118
- paths[y]!.push(Math.min(top, left, topLeft))
119
- }
120
- }
121
- }
122
-
123
- const diffs: ArrayDiff<Item>[] = []
124
-
125
- let x = secondArray.length
126
- let y = firstArray.length
127
-
128
- while (x > 0 || y > 0) {
129
- const top = y > 0 ? paths[y - 1]![x]! : Infinity
130
- const left = x > 0 ? paths[y]![x - 1]! : Infinity
131
- const topLeft = y > 0 && x > 0 ? paths[y - 1]![x - 1]! : Infinity
132
-
133
- const min = Math.min(top, left, topLeft)
134
-
135
- if (min === topLeft) {
136
- if (firstArray[y - 1] !== secondArray[x - 1]) {
137
- diffs.push({
138
- type: 'replace',
139
- index: y - 1,
140
- oldItem: firstArray[y - 1]!,
141
- newItem: secondArray[x - 1]!,
142
- })
143
- }
144
- x--
145
- y--
146
- } else if (min === top) {
147
- diffs.push({
148
- type: 'delete',
149
- index: y - 1,
150
- })
151
- y--
152
- } else {
153
- diffs.push({
154
- type: 'add',
155
- index: y,
156
- item: secondArray[x - 1]!,
157
- })
158
- x--
159
- }
160
- }
161
-
162
- return diffs
163
- }
164
-
165
- override unmount() {
166
- super.unmount()
167
- this.signals = []
168
- }
169
- }
170
-
171
- export function List<Item>(props: ListProps<Item>): PotokElement {
172
- return function (context: LibContext) {
173
- return new ListClass(props, context)
174
- }
175
- }
package/src/portal.ts DELETED
@@ -1,101 +0,0 @@
1
- import { LibBlock } from './block'
2
- import { createEffect, untrack } from './signals'
3
- import type { LibContext, PotokElement, WithChildren } from './types'
4
- import { mergeContext, normalizeChildren } from './utils'
5
-
6
- type PortalInProps = WithChildren<{
7
- name: string
8
- }>
9
-
10
- class PortalInClass extends LibBlock {
11
- constructor(
12
- readonly props: PortalInProps,
13
- readonly context: LibContext,
14
- ) {
15
- super()
16
-
17
- let previousName: string
18
-
19
- if (context.portals[props.name]) {
20
- console.warn(`Портал ${props.name} уже существует`)
21
- } else {
22
- this.addEffect(
23
- createEffect(() => {
24
- const name = props.name
25
-
26
- if (name !== previousName) {
27
- untrack(() => {
28
- delete context.portals[name]
29
- context.portals[name] = props.children
30
- })
31
- }
32
-
33
- previousName = name
34
- }),
35
- )
36
- }
37
- }
38
-
39
- override unmount() {
40
- super.unmount()
41
- delete this.context.portals[this.props.name]
42
- }
43
- }
44
-
45
- export function PortalIn(props: PortalInProps): PotokElement {
46
- return function (context: LibContext) {
47
- return new PortalInClass(props, context)
48
- }
49
- }
50
-
51
- type PortalOutProps = {
52
- name: string
53
- }
54
-
55
- class PortalOutClass extends LibBlock {
56
- constructor(
57
- readonly props: PortalOutProps,
58
- readonly context: LibContext,
59
- ) {
60
- super()
61
-
62
- let previousChildren: PotokElement[] | null
63
-
64
- this.addEffect(
65
- createEffect(() => {
66
- const name = props.name
67
-
68
- const children = normalizeChildren(context.portals[name])
69
-
70
- if (!children) {
71
- this.unmountChildren()
72
-
73
- previousChildren = null
74
- } else if (children !== previousChildren) {
75
- this.unmountChildren()
76
-
77
- untrack(() => {
78
- children.forEach((childCreator, index) => {
79
- this.children.push(
80
- childCreator(
81
- mergeContext(context, {
82
- parentBlock: this,
83
- index,
84
- }),
85
- ),
86
- )
87
- })
88
- })
89
-
90
- previousChildren = children
91
- }
92
- }),
93
- )
94
- }
95
- }
96
-
97
- export function PortalOut(props: PortalOutProps): PotokElement {
98
- return function (context: LibContext) {
99
- return new PortalOutClass(props, context)
100
- }
101
- }