solid-ctrl-flow 1.0.34 → 1.0.36
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/dist/index.d.ts +24 -4
- package/dist/index.mjs +20 -6
- package/dist/index.umd.js +19 -5
- package/package.json +1 -1
- package/readMe.md +55 -33
package/dist/index.d.ts
CHANGED
|
@@ -16,14 +16,14 @@ declare const BASE_CTOR: new <T>() => Accessor<T>;
|
|
|
16
16
|
* - As soon as the function is done executing the two {@link Signal}s will have the same value
|
|
17
17
|
* - If {@link source} changes, {@link dest} wont be instantly updated
|
|
18
18
|
* - If the current owner gets disposed before {@link dest} is updated, it never will
|
|
19
|
-
* @param source The source of the binding
|
|
20
|
-
* @param dest The destination of the binding
|
|
19
|
+
* @param source The getter of the source of the binding
|
|
20
|
+
* @param dest The setter of the destination of the binding
|
|
21
21
|
* @param to Conversion function from {@link S} to {@link D}
|
|
22
22
|
* @returns A function that disposes the binding
|
|
23
23
|
*/
|
|
24
|
-
export declare function bind<S>(source:
|
|
24
|
+
export declare function bind<S>(source: Accessor<S>, dest: Setter<S>): () => void;
|
|
25
25
|
|
|
26
|
-
export declare function bind<S, D>(source:
|
|
26
|
+
export declare function bind<S, D>(source: Accessor<S>, dest: Setter<D>, to: Convert<S, D>): () => void;
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Creates a two-way binding between two {@link Signal}s calling {@link bind} two times
|
|
@@ -147,6 +147,26 @@ declare type Info = {
|
|
|
147
147
|
*/
|
|
148
148
|
export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[]>(obj: T, keys?: K): Pick<T, K[number]>;
|
|
149
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Like a {@link For}, but instead of putting an element after another it nests them.
|
|
152
|
+
* The component will try to recycle the layers when the source array changes (Once again, like the {@link For}).
|
|
153
|
+
* Due to the recyling, the layers will be created indipendently, thus their {@link Owner}s won't be nested in their parents' ones.
|
|
154
|
+
* Example:
|
|
155
|
+
* ```tsx
|
|
156
|
+
* return <>
|
|
157
|
+
* <Nest each={[ 1, 2, 3 ]} template={(x, y) => <>({x}, {y()}, -{x})</>}>
|
|
158
|
+
* content
|
|
159
|
+
* </Nest>
|
|
160
|
+
* </>
|
|
161
|
+
* ```
|
|
162
|
+
* Will output "(1, (2, (3, content, -3), -2), -1)"
|
|
163
|
+
*/
|
|
164
|
+
export declare function Nest<T>(props: {
|
|
165
|
+
each: T[];
|
|
166
|
+
template: (x: T, c: Accessor<JSX.Element>, i: Accessor<number>) => JSX.Element;
|
|
167
|
+
children: JSX.Element;
|
|
168
|
+
}): JSX.Element;
|
|
169
|
+
|
|
150
170
|
/**
|
|
151
171
|
* Reactive {@link Context} with some additional built-in functionalities.
|
|
152
172
|
* The call signature returns the value in the current {@link Owner} (It's like calling {@link read})
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useContext, createMemo, createComponent, createContext, Match, mergeProps, onCleanup, For, Show, createRoot, createRenderEffect, on, untrack, splitProps, getOwner, createEffect, createResource } from "solid-js";
|
|
1
|
+
import { useContext, createMemo, createComponent, createContext, Match, mergeProps, onCleanup, For, Show, mapArray, createSignal, onMount, createRoot, createRenderEffect, on, untrack, splitProps, getOwner, createEffect, createResource } from "solid-js";
|
|
2
2
|
import { createMutable } from "solid-js/store";
|
|
3
3
|
const BASE_CTOR = Function;
|
|
4
4
|
class ReactiveContext extends BASE_CTOR {
|
|
@@ -141,14 +141,27 @@ function Joint(props) {
|
|
|
141
141
|
}
|
|
142
142
|
});
|
|
143
143
|
}
|
|
144
|
+
function Nest(props) {
|
|
145
|
+
const memo = memoProps(props, ["template", "children"]);
|
|
146
|
+
const content = mapArray(() => props.each, (x, i) => {
|
|
147
|
+
const [get, set] = createSignal();
|
|
148
|
+
const out = memo.template(x, get, i);
|
|
149
|
+
return (x2) => {
|
|
150
|
+
onMount(() => set(() => x2));
|
|
151
|
+
onCleanup(() => set(void 0));
|
|
152
|
+
return out;
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
return createMemo(() => content().reduceRight((a, b) => b(a), memo.children));
|
|
156
|
+
}
|
|
144
157
|
const IDENTITY = (x) => x;
|
|
145
|
-
function bind(
|
|
146
|
-
const d = createRoot((d2) => (createRenderEffect(on(
|
|
158
|
+
function bind(source, dest, to = IDENTITY) {
|
|
159
|
+
const d = createRoot((d2) => (createRenderEffect(on(source, (x) => dest((prev) => to(x, prev)))), d2));
|
|
147
160
|
return onCleanup(d), d;
|
|
148
161
|
}
|
|
149
|
-
function bindTwoWay(source, dest, to
|
|
150
|
-
const a = bind(source, dest, to);
|
|
151
|
-
const b = bind(dest, source, from);
|
|
162
|
+
function bindTwoWay(source, dest, to, from) {
|
|
163
|
+
const a = bind(source[0], dest[1], to);
|
|
164
|
+
const b = bind(dest[0], source[1], from);
|
|
152
165
|
return () => (a(), b());
|
|
153
166
|
}
|
|
154
167
|
function toSetter(get, set) {
|
|
@@ -214,6 +227,7 @@ function createReactiveResource(f) {
|
|
|
214
227
|
const debug = ReactiveContext.create(false, "debug-scope");
|
|
215
228
|
export {
|
|
216
229
|
Case,
|
|
230
|
+
Nest,
|
|
217
231
|
ReactiveContext,
|
|
218
232
|
When,
|
|
219
233
|
bind,
|
package/dist/index.umd.js
CHANGED
|
@@ -143,14 +143,27 @@
|
|
|
143
143
|
}
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
|
+
function Nest(props) {
|
|
147
|
+
const memo = memoProps(props, ["template", "children"]);
|
|
148
|
+
const content = solidJs.mapArray(() => props.each, (x, i) => {
|
|
149
|
+
const [get, set] = solidJs.createSignal();
|
|
150
|
+
const out = memo.template(x, get, i);
|
|
151
|
+
return (x2) => {
|
|
152
|
+
solidJs.onMount(() => set(() => x2));
|
|
153
|
+
solidJs.onCleanup(() => set(void 0));
|
|
154
|
+
return out;
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
return solidJs.createMemo(() => content().reduceRight((a, b) => b(a), memo.children));
|
|
158
|
+
}
|
|
146
159
|
const IDENTITY = (x) => x;
|
|
147
|
-
function bind(
|
|
148
|
-
const d = solidJs.createRoot((d2) => (solidJs.createRenderEffect(solidJs.on(
|
|
160
|
+
function bind(source, dest, to = IDENTITY) {
|
|
161
|
+
const d = solidJs.createRoot((d2) => (solidJs.createRenderEffect(solidJs.on(source, (x) => dest((prev) => to(x, prev)))), d2));
|
|
149
162
|
return solidJs.onCleanup(d), d;
|
|
150
163
|
}
|
|
151
|
-
function bindTwoWay(source, dest, to
|
|
152
|
-
const a = bind(source, dest, to);
|
|
153
|
-
const b = bind(dest, source, from);
|
|
164
|
+
function bindTwoWay(source, dest, to, from) {
|
|
165
|
+
const a = bind(source[0], dest[1], to);
|
|
166
|
+
const b = bind(dest[0], source[1], from);
|
|
154
167
|
return () => (a(), b());
|
|
155
168
|
}
|
|
156
169
|
function toSetter(get, set) {
|
|
@@ -215,6 +228,7 @@
|
|
|
215
228
|
}
|
|
216
229
|
const debug = ReactiveContext.create(false, "debug-scope");
|
|
217
230
|
exports2.Case = Case;
|
|
231
|
+
exports2.Nest = Nest;
|
|
218
232
|
exports2.ReactiveContext = ReactiveContext;
|
|
219
233
|
exports2.When = When;
|
|
220
234
|
exports2.bind = bind;
|
package/package.json
CHANGED
package/readMe.md
CHANGED
|
@@ -6,7 +6,7 @@ It's always under hard development
|
|
|
6
6
|
## Components
|
|
7
7
|
|
|
8
8
|
### `Case` - `When`
|
|
9
|
-
Allows you to not
|
|
9
|
+
Allows you to not repeat the same condition across many `Match` components.
|
|
10
10
|
The following code
|
|
11
11
|
```tsx
|
|
12
12
|
const value = 1;
|
|
@@ -34,17 +34,26 @@ return <>
|
|
|
34
34
|
</>
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
### `
|
|
38
|
-
|
|
37
|
+
### `Nest`
|
|
38
|
+
Like a `For`, but instead of putting an element after another it nests them.
|
|
39
|
+
The following code
|
|
39
40
|
```tsx
|
|
40
|
-
const value = false;
|
|
41
41
|
return <>
|
|
42
|
-
<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
<Nest each={[ "red", "green", "blue" ]} template={(x, y) => <div style={{ background: x, padding: "1ch" }}>{y()}</div>}>
|
|
43
|
+
content
|
|
44
|
+
</Nest>
|
|
45
|
+
</>
|
|
46
|
+
```
|
|
47
|
+
Is equivalent to this
|
|
48
|
+
```tsx
|
|
49
|
+
return <>
|
|
50
|
+
<div style={{ background: "red", padding: "1ch" }}>
|
|
51
|
+
<div style={{ background: "green", padding: "1ch" }}>
|
|
52
|
+
<div style={{ background: "blue", padding: "1ch" }}>
|
|
53
|
+
content
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
</div>
|
|
48
57
|
</>
|
|
49
58
|
```
|
|
50
59
|
|
|
@@ -122,6 +131,29 @@ Notice that you can use a `Source` component without adding it to the DOM:
|
|
|
122
131
|
return <>Something else</>
|
|
123
132
|
```
|
|
124
133
|
|
|
134
|
+
## Utility
|
|
135
|
+
|
|
136
|
+
### Bindings
|
|
137
|
+
Functions that create bindings between `Signal`s
|
|
138
|
+
|
|
139
|
+
> #### `bind()`
|
|
140
|
+
> Creates a one-way binding between two `Signal`s
|
|
141
|
+
>
|
|
142
|
+
> #### `bindTwoWay()`
|
|
143
|
+
> Creates a two-way binding between two `Signal`s
|
|
144
|
+
>
|
|
145
|
+
> #### `toSetter()`
|
|
146
|
+
> Creates a full-fledged solid `Setter` from a normal one
|
|
147
|
+
>
|
|
148
|
+
> #### `toSignal()`
|
|
149
|
+
> Creates a `Signal` from a property of an object
|
|
150
|
+
>
|
|
151
|
+
> #### `coalesceSignal()`
|
|
152
|
+
> Creates a non nullable `Signal` from a nullable one
|
|
153
|
+
>
|
|
154
|
+
> #### `unwrapSignal()`
|
|
155
|
+
> Calls an `Accessor` to a `Signal` maintaining its reactivity
|
|
156
|
+
|
|
125
157
|
### `ReactiveContext.create()`
|
|
126
158
|
Method that creates a reactive version of a solid `Context` with some additional built-in functionalities
|
|
127
159
|
```ts
|
|
@@ -131,29 +163,19 @@ const valueAccessorForTheCurrentOwner = ctx.read;
|
|
|
131
163
|
const value = ctx();
|
|
132
164
|
```
|
|
133
165
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
Creates a `Signal` from a property of an object
|
|
148
|
-
|
|
149
|
-
### `coalesceSignal()`
|
|
150
|
-
Creates a non nullable `Signal` from a nullable one
|
|
151
|
-
|
|
152
|
-
### `unwrapSignal()`
|
|
153
|
-
Calls an `Accessor` to a `Signal` maintaining its reactivity
|
|
154
|
-
|
|
155
|
-
## Utility functions
|
|
156
|
-
Utility functions needed for the components above
|
|
166
|
+
### `debug()`
|
|
167
|
+
Allows you to define ui section that are only visible in debug or NOT in debug
|
|
168
|
+
```tsx
|
|
169
|
+
const value = false;
|
|
170
|
+
return <>
|
|
171
|
+
<debug.Provider value={value}>
|
|
172
|
+
{/* (A LOT OF NESTING...) */}
|
|
173
|
+
<Show when={debug()}>
|
|
174
|
+
THIS WOULD BE VISIBLE IF "value" HAD BEEN {true}
|
|
175
|
+
</Show>
|
|
176
|
+
</debug.Provider>
|
|
177
|
+
</>
|
|
178
|
+
```
|
|
157
179
|
|
|
158
180
|
### `untrackCall()`
|
|
159
181
|
Calls a function untracking what happens inside of it but not what gets passed as its argument
|