solid-ctrl-flow 1.0.19 → 1.0.20

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 CHANGED
@@ -1,6 +1,31 @@
1
1
  import { Accessor } from 'solid-js';
2
2
  import { Context } from 'solid-js';
3
3
  import { JSX } from 'solid-js';
4
+ import { Setter } from 'solid-js';
5
+ import { Signal } from 'solid-js';
6
+
7
+ /**
8
+ * Creates a one-way binding between two {@link Signal}s
9
+ * @param source The source of the binding
10
+ * @param dest The destination of the binding
11
+ * @param to Conversion function from {@link S} to {@link D}
12
+ * @returns A function that disposes the binding
13
+ */
14
+ export declare function bind<S>(source: Signal<S>, dest: Signal<S>): () => void;
15
+
16
+ export declare function bind<S, D>(source: Signal<S>, dest: Signal<D>, to: Convert<S, D>): () => void;
17
+
18
+ /**
19
+ * Creates a two-way binding between two {@link Signal}s
20
+ * @param source The source of the binding
21
+ * @param dest The destination of the binding
22
+ * @param to Conversion function from {@link S} to {@link D}
23
+ * @param from Conversion function from {@link D} to {@link S}
24
+ * @returns A function that disposes the binding
25
+ */
26
+ export declare function bindTwoWay<S>(source: Signal<S>, dest: Signal<S>): () => void;
27
+
28
+ export declare function bindTwoWay<S, D>(source: Signal<S>, dest: Signal<D>, to: Convert<S, D>, from: Convert<D, S>): () => void;
4
29
 
5
30
  /**
6
31
  * Allows the use of {@link When}s inside of {@link Switch}es.
@@ -37,6 +62,9 @@ export declare function Case(props: {
37
62
  children: JSX.Element;
38
63
  }): JSX.Element;
39
64
 
65
+ /** Conversion function for bindings */
66
+ export declare type Convert<S, D> = (x: S, prev: D) => D;
67
+
40
68
  /**
41
69
  * Creates a slot that allows you to show a component outside of its parent.
42
70
  * It works with arbitrary nesting.
@@ -138,6 +166,20 @@ export declare function runWithContext<T, R>(ctx: Context<T>, value: T, f: (x: T
138
166
  */
139
167
  export declare function splitAndMemoProps<T extends object, K extends readonly (keyof T)[]>(obj: T, keys: K): [Pick<T, Extract<K, readonly (keyof T)[]>[number]>, Omit<T, K[number]>];
140
168
 
169
+ /**
170
+ * Creates a full-fledged solid {@link Setter} from a normal one
171
+ * @param get The getter of the {@link Signal}; It's needed when a function gets passed to the new setter
172
+ * @param set The simple setter to which to add functionalities
173
+ */
174
+ export declare function toSetter<T>(get: Accessor<T>, set: (x: T) => void): Setter<T>;
175
+
176
+ /**
177
+ * Creates a {@link Signal} from a property access
178
+ * @param obj The object from which to get the property
179
+ * @param k The key of the property
180
+ */
181
+ export declare function toSignal<T, K extends keyof T>(obj: T, k: K): Signal<T[K]>;
182
+
141
183
  /**
142
184
  * Executes {@link f} untracking it.
143
185
  * Due to the fact that the function is passed as input, it does not untrack its changes (It's intentional)
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { untrack, createMemo, splitProps, createContext, createRoot, getOwner, createComponent, useContext, Match, mergeProps, onCleanup, For, Show } from "solid-js";
1
+ import { untrack, createMemo, splitProps, createContext, createRoot, getOwner, createComponent, useContext, createEffect, on, onCleanup, Match, mergeProps, For, Show } from "solid-js";
2
2
  import { createMutable } from "solid-js/store";
3
3
  function untrackCall(f, ...args) {
4
4
  return untrack(() => f.apply(this, args));
@@ -49,6 +49,26 @@ function runWithContext(ctx2, value, f) {
49
49
  }
50
50
  });
51
51
  }
52
+ const IDENTITY = (x) => x;
53
+ function toSetter(get, set) {
54
+ return (x) => {
55
+ const out = typeof x === "function" ? x(untrack(get)) : x;
56
+ return set(out), out;
57
+ };
58
+ }
59
+ function toSignal(obj, k) {
60
+ const get = () => obj[k];
61
+ return [get, toSetter(get, (v) => obj[k] = v)];
62
+ }
63
+ function bind(source, dest, to = IDENTITY) {
64
+ const d = createRoot((d2) => (createEffect(on(source[0], (x) => dest[1]((prev) => to(x, prev)))), d2));
65
+ return onCleanup(d), d;
66
+ }
67
+ function bindTwoWay(source, dest, to = IDENTITY, from = IDENTITY) {
68
+ const a = bind(source, dest, to);
69
+ const b = bind(dest, source, from);
70
+ return () => (a(), b());
71
+ }
52
72
  const ctx = createContext(() => void 0, {
53
73
  name: "case-when"
54
74
  });
@@ -149,10 +169,14 @@ export {
149
169
  Case,
150
170
  Debug,
151
171
  When,
172
+ bind,
173
+ bindTwoWay,
152
174
  createExtractor,
153
175
  createOption,
154
176
  memoProps,
155
177
  runWithContext,
156
178
  splitAndMemoProps,
179
+ toSetter,
180
+ toSignal,
157
181
  untrackCall
158
182
  };
package/dist/index.umd.js CHANGED
@@ -51,6 +51,26 @@
51
51
  }
52
52
  });
53
53
  }
54
+ const IDENTITY = (x) => x;
55
+ function toSetter(get, set) {
56
+ return (x) => {
57
+ const out = typeof x === "function" ? x(solidJs.untrack(get)) : x;
58
+ return set(out), out;
59
+ };
60
+ }
61
+ function toSignal(obj, k) {
62
+ const get = () => obj[k];
63
+ return [get, toSetter(get, (v) => obj[k] = v)];
64
+ }
65
+ function bind(source, dest, to = IDENTITY) {
66
+ const d = solidJs.createRoot((d2) => (solidJs.createEffect(solidJs.on(source[0], (x) => dest[1]((prev) => to(x, prev)))), d2));
67
+ return solidJs.onCleanup(d), d;
68
+ }
69
+ function bindTwoWay(source, dest, to = IDENTITY, from = IDENTITY) {
70
+ const a = bind(source, dest, to);
71
+ const b = bind(dest, source, from);
72
+ return () => (a(), b());
73
+ }
54
74
  const ctx = solidJs.createContext(() => void 0, {
55
75
  name: "case-when"
56
76
  });
@@ -150,11 +170,15 @@
150
170
  exports2.Case = Case;
151
171
  exports2.Debug = Debug;
152
172
  exports2.When = When;
173
+ exports2.bind = bind;
174
+ exports2.bindTwoWay = bindTwoWay;
153
175
  exports2.createExtractor = createExtractor;
154
176
  exports2.createOption = createOption;
155
177
  exports2.memoProps = memoProps;
156
178
  exports2.runWithContext = runWithContext;
157
179
  exports2.splitAndMemoProps = splitAndMemoProps;
180
+ exports2.toSetter = toSetter;
181
+ exports2.toSignal = toSignal;
158
182
  exports2.untrackCall = untrackCall;
159
183
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
160
184
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -121,6 +121,22 @@ Notice that you can use a `Source` component without adding it to the DOM:
121
121
  return <>Something else</>
122
122
  ```
123
123
 
124
+ ## Bindings
125
+ Functions that create bindings between `Signal`s
126
+
127
+ ### `toSetter()`
128
+ Creates a full-fledged solid `Setter` from a normal one
129
+
130
+ ### `toSignal()`
131
+ Creates a `Signal` from a property of an object
132
+
133
+ ### `bind()`
134
+ Creates a one-way binding between two `Signal`s
135
+
136
+ ### `bindTwoWay()`
137
+ Creates a two-way binding between two `Signal`s
138
+
139
+
124
140
  ## Utility functions
125
141
  Utility functions needed for the components above
126
142