solid-ctrl-flow 1.0.19 → 1.0.21

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,33 @@
1
1
  import { Accessor } from 'solid-js';
2
2
  import { Context } from 'solid-js';
3
+ import { EffectFunction } from 'solid-js';
3
4
  import { JSX } from 'solid-js';
5
+ import { Resource } from 'solid-js';
6
+ import { Setter } from 'solid-js';
7
+ import { Signal } from 'solid-js';
8
+
9
+ /**
10
+ * Creates a one-way binding between two {@link Signal}s
11
+ * @param source The source of the binding
12
+ * @param dest The destination of the binding
13
+ * @param to Conversion function from {@link S} to {@link D}
14
+ * @returns A function that disposes the binding
15
+ */
16
+ export declare function bind<S>(source: Signal<S>, dest: Signal<S>): () => void;
17
+
18
+ export declare function bind<S, D>(source: Signal<S>, dest: Signal<D>, to: Convert<S, D>): () => void;
19
+
20
+ /**
21
+ * Creates a two-way binding between two {@link Signal}s
22
+ * @param source The source of the binding
23
+ * @param dest The destination of the binding
24
+ * @param to Conversion function from {@link S} to {@link D}
25
+ * @param from Conversion function from {@link D} to {@link S}
26
+ * @returns A function that disposes the binding
27
+ */
28
+ export declare function bindTwoWay<S>(source: Signal<S>, dest: Signal<S>): () => void;
29
+
30
+ export declare function bindTwoWay<S, D>(source: Signal<S>, dest: Signal<D>, to: Convert<S, D>, from: Convert<D, S>): () => void;
4
31
 
5
32
  /**
6
33
  * Allows the use of {@link When}s inside of {@link Switch}es.
@@ -37,6 +64,9 @@ export declare function Case(props: {
37
64
  children: JSX.Element;
38
65
  }): JSX.Element;
39
66
 
67
+ /** Conversion function for bindings */
68
+ export declare type Convert<S, D> = (x: S, prev: D) => D;
69
+
40
70
  /**
41
71
  * Creates a slot that allows you to show a component outside of its parent.
42
72
  * It works with arbitrary nesting.
@@ -96,6 +126,13 @@ export declare function createOption<T>(init: T, def?: T, name?: string): {
96
126
  read: Accessor<T>;
97
127
  };
98
128
 
129
+ /**
130
+ * Creates a {@link Resource} from the function {@link f}.
131
+ * The resource will be refetched each time one of the dependencies of {@link f} changes
132
+ * @param f A reactive resource fetcher
133
+ */
134
+ export declare function createReactiveResource<R>(f: EffectFunction<R | undefined, R>): Resource<Awaited<R>>;
135
+
99
136
  /** Creates a scope for the value of {@link Debug.read}, which allows you to display different things in debug mode */
100
137
  export declare const Debug: {
101
138
  (props: {
@@ -132,12 +169,26 @@ export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[
132
169
  export declare function runWithContext<T, R>(ctx: Context<T>, value: T, f: (x: T) => R): R;
133
170
 
134
171
  /**
135
- * Esecutes {@link splitProps} and memoizes the first of the two returned objects
172
+ * Executes {@link splitProps} and memoizes the first of the two returned objects
136
173
  * @param obj Object to split and partially memoize
137
174
  * @param keys The keys of the properties to memoize and to include in the first of the two objects
138
175
  */
139
176
  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
177
 
178
+ /**
179
+ * Creates a full-fledged solid {@link Setter} from a normal one
180
+ * @param get The getter of the {@link Signal}; It's needed when a function gets passed to the new setter
181
+ * @param set The simple setter to which to add functionalities
182
+ */
183
+ export declare function toSetter<T>(get: Accessor<T>, set: (x: T) => void): Setter<T>;
184
+
185
+ /**
186
+ * Creates a {@link Signal} from a property access
187
+ * @param obj The object from which to get the property
188
+ * @param k The key of the property
189
+ */
190
+ export declare function toSignal<T, K extends keyof T>(obj: T, k: K): Signal<T[K]>;
191
+
141
192
  /**
142
193
  * Executes {@link f} untracking it.
143
194
  * 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, createResource, createEffect, on, createContext, createRoot, getOwner, createComponent, useContext, 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));
@@ -17,6 +17,14 @@ function splitAndMemoProps(obj, keys) {
17
17
  out[0] = memoProps(out[0], keys);
18
18
  return out;
19
19
  }
20
+ function createReactiveResource(f) {
21
+ const memo = createMemo(f);
22
+ const [get, {
23
+ refetch
24
+ }] = createResource(memo);
25
+ createEffect(on(memo, (_x, _prev, ok) => (ok && refetch(), true)));
26
+ return get;
27
+ }
20
28
  function createOption(init, def = init, name) {
21
29
  const prop = "read";
22
30
  const ctx2 = createContext(() => init, {
@@ -49,6 +57,26 @@ function runWithContext(ctx2, value, f) {
49
57
  }
50
58
  });
51
59
  }
60
+ const IDENTITY = (x) => x;
61
+ function toSetter(get, set) {
62
+ return (x) => {
63
+ const out = typeof x === "function" ? x(untrack(get)) : x;
64
+ return set(out), out;
65
+ };
66
+ }
67
+ function toSignal(obj, k) {
68
+ const get = () => obj[k];
69
+ return [get, toSetter(get, (v) => obj[k] = v)];
70
+ }
71
+ function bind(source, dest, to = IDENTITY) {
72
+ const d = createRoot((d2) => (createEffect(on(source[0], (x) => dest[1]((prev) => to(x, prev)))), d2));
73
+ return onCleanup(d), d;
74
+ }
75
+ function bindTwoWay(source, dest, to = IDENTITY, from = IDENTITY) {
76
+ const a = bind(source, dest, to);
77
+ const b = bind(dest, source, from);
78
+ return () => (a(), b());
79
+ }
52
80
  const ctx = createContext(() => void 0, {
53
81
  name: "case-when"
54
82
  });
@@ -149,10 +177,15 @@ export {
149
177
  Case,
150
178
  Debug,
151
179
  When,
180
+ bind,
181
+ bindTwoWay,
152
182
  createExtractor,
153
183
  createOption,
184
+ createReactiveResource,
154
185
  memoProps,
155
186
  runWithContext,
156
187
  splitAndMemoProps,
188
+ toSetter,
189
+ toSignal,
157
190
  untrackCall
158
191
  };
package/dist/index.umd.js CHANGED
@@ -19,6 +19,14 @@
19
19
  out[0] = memoProps(out[0], keys);
20
20
  return out;
21
21
  }
22
+ function createReactiveResource(f) {
23
+ const memo = solidJs.createMemo(f);
24
+ const [get, {
25
+ refetch
26
+ }] = solidJs.createResource(memo);
27
+ solidJs.createEffect(solidJs.on(memo, (_x, _prev, ok) => (ok && refetch(), true)));
28
+ return get;
29
+ }
22
30
  function createOption(init, def = init, name) {
23
31
  const prop = "read";
24
32
  const ctx2 = solidJs.createContext(() => init, {
@@ -51,6 +59,26 @@
51
59
  }
52
60
  });
53
61
  }
62
+ const IDENTITY = (x) => x;
63
+ function toSetter(get, set) {
64
+ return (x) => {
65
+ const out = typeof x === "function" ? x(solidJs.untrack(get)) : x;
66
+ return set(out), out;
67
+ };
68
+ }
69
+ function toSignal(obj, k) {
70
+ const get = () => obj[k];
71
+ return [get, toSetter(get, (v) => obj[k] = v)];
72
+ }
73
+ function bind(source, dest, to = IDENTITY) {
74
+ const d = solidJs.createRoot((d2) => (solidJs.createEffect(solidJs.on(source[0], (x) => dest[1]((prev) => to(x, prev)))), d2));
75
+ return solidJs.onCleanup(d), d;
76
+ }
77
+ function bindTwoWay(source, dest, to = IDENTITY, from = IDENTITY) {
78
+ const a = bind(source, dest, to);
79
+ const b = bind(dest, source, from);
80
+ return () => (a(), b());
81
+ }
54
82
  const ctx = solidJs.createContext(() => void 0, {
55
83
  name: "case-when"
56
84
  });
@@ -150,11 +178,16 @@
150
178
  exports2.Case = Case;
151
179
  exports2.Debug = Debug;
152
180
  exports2.When = When;
181
+ exports2.bind = bind;
182
+ exports2.bindTwoWay = bindTwoWay;
153
183
  exports2.createExtractor = createExtractor;
154
184
  exports2.createOption = createOption;
185
+ exports2.createReactiveResource = createReactiveResource;
155
186
  exports2.memoProps = memoProps;
156
187
  exports2.runWithContext = runWithContext;
157
188
  exports2.splitAndMemoProps = splitAndMemoProps;
189
+ exports2.toSetter = toSetter;
190
+ exports2.toSignal = toSignal;
158
191
  exports2.untrackCall = untrackCall;
159
192
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
160
193
  });
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.21",
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
 
@@ -133,6 +149,9 @@ Creates a partial version of an object with memoized remaining properties
133
149
  ### `splitAndMemoProps()`
134
150
  Like `splitProps()` but memoizes the whole local part
135
151
 
152
+ ### `createReactiveResource()`
153
+ Like `createResource()` but the provided function will be reactive
154
+
136
155
  ### `createOption()`
137
156
  Allows you to create simple reactive contexts; For example `Debug` is made with it
138
157