solid-ctrl-flow 1.0.49 → 1.0.51

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
@@ -127,13 +127,22 @@ export declare function createReactiveResource<R>(f: EffectFunction<R | undefine
127
127
  /** Built-in {@link ReactiveContext} which allows you to display different things in debug mode */
128
128
  export declare const debug: ReactiveContext<boolean>;
129
129
 
130
+ /**
131
+ * Defines a reactive property on an object
132
+ * @param obj The object on which to define the property
133
+ * @param k The key of the property that will be defined
134
+ * @param param2 The {@link Signal} to use as the data store of the property
135
+ * @returns The same thing passed as {@link obj}
136
+ */
137
+ export declare function defineSignalProperty<const T, K extends keyof T>(obj: T, k: K, [get, set]: Signal<T[K]>): T;
138
+
130
139
  /**
131
140
  * Eventually wraps its content into a template if a certain condition is met.
132
- * The content is memoized in the beginning and will be recycled even if the wrapper changes.
133
- * You can avoid the memoization trought {@link Standard.unrecycled}, this is needed if you want to use a context provider on your template.
141
+ * If you set {@link Standard.recycled} to `true`, the content will be memoized in the beginning and will be recycled even if the wrapper changes.
142
+ * You can't use memoization if you want to use a context provider on your template.
134
143
  * If {@link Keyed.keyed} is active, the template will be executed each time the content of {@link Standard.when} changes.
135
- * If {@link Standard.unrecycled} is active, the content will be executed each time it is mounted (Each time {@link Standard.when} goes from a falsish value to a truish one and vice versa).
136
- * If both {@link Keyed.keyed} and {@link Standard.unrecycled} are active, the content will be executed each time {@link Standard.when} changes
144
+ * If {@link Standard.recycled} is not active, the content will be executed each time it is mounted (Each time {@link Standard.when} goes from a falsish value to a truish one and vice versa).
145
+ * If {@link Keyed.keyed} is active and {@link Standard.recycled} isn't, the content will be executed each time {@link Standard.when} changes
137
146
  */
138
147
  export declare function Enfold<T>(props: Keyed<T>): JSX.Element;
139
148
 
@@ -160,25 +169,6 @@ export declare function memoProps<T>(obj: T, keys?: undefined, equals?: Equals):
160
169
 
161
170
  export declare function memoProps<T, K extends readonly (keyof T)[]>(obj: T, keys: K, equals?: Equals): Pick<T, K[number]>;
162
171
 
163
- /**
164
- * Like a {@link For}, but instead of putting an element after another it nests them.
165
- * The component will try to recycle the layers when the source array changes (Once again, like the {@link For}).
166
- * Due to the recyling, the layers will be created indipendently, thus their {@link Owner}s won't be nested in their parents' ones, this makes the component unsuitable to wrap the content with context providers.
167
- * Example:
168
- * ```tsx
169
- * return <>
170
- * <Nest each={[ 1, 2, 3 ]} template={(c, x) => <>({x}, {c()}, -{x})</>}>
171
- * CONTENT
172
- * </Nest>
173
- * </>
174
- * ```
175
- * Will output "(1, (2, (3, CONTENT, -3), -2), -1)"
176
- */
177
- export declare function Nest<T>(props: ParentProps<{
178
- each: readonly T[] | undefined;
179
- template: (c: Accessor<JSX.Element>, x: T, i: Accessor<number>) => JSX.Element;
180
- }>): JSX.Element;
181
-
182
172
  /**
183
173
  * Allows the use of {@link Case}s inside of {@link Switch}es.
184
174
  * Example:
@@ -215,7 +205,7 @@ export declare const On: (props: ParentProps<{
215
205
 
216
206
  /**
217
207
  * Reactive {@link Context} with some additional built-in functionalities.
218
- * The call signature returns the value in the current {@link Owner} (It's like calling {@link read})
208
+ * The call signature returns the value in the current {@link Owner} (It's like calling {@link get})
219
209
  */
220
210
  export declare abstract class ReactiveContext<T> extends BASE_CTOR<T> {
221
211
  /** The actual {@link Context} that contains the reactive {@link Accessor} to the value */
@@ -224,7 +214,7 @@ export declare abstract class ReactiveContext<T> extends BASE_CTOR<T> {
224
214
  * Returns the {@link Accessor} for the value in the current {@link Owner}.
225
215
  * It's like calling {@link useContext}
226
216
  */
227
- get read(): Accessor<T>;
217
+ get get(): Accessor<T>;
228
218
  /**
229
219
  * Component that sets the value of the current {@link Context} for its children.
230
220
  * If no value is provided then it will use the default value for {@link ctx}.
@@ -297,7 +287,8 @@ export declare namespace Slot {
297
287
  }
298
288
 
299
289
  /**
300
- * Executes {@link splitProps} and memoizes the first of the two returned objects using {@link memoProps}
290
+ * Executes {@link splitProps} and memoizes the first of the two returned objects using {@link memoProps}.
291
+ * Unlike {@link splitProps}, this doesn't support multiple key-sets out of the box
301
292
  * @param obj Object to split and partially memoize
302
293
  * @param keys The keys of the properties to memoize and to include in the first of the two objects
303
294
  * @param equals The comparator function to use on the newly created memos
@@ -307,7 +298,7 @@ export declare function splitAndMemoProps<T extends Record<any, any>, K extends
307
298
  /** Standard parameters to pass to an {@link Enfold} */
308
299
  declare type Standard<T> = ParentProps<{
309
300
  when: T;
310
- unrecycled?: boolean;
301
+ recycled?: boolean;
311
302
  }>;
312
303
 
313
304
  /** Type of an element wrapping call-back which accepts an additional parameter */
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { useContext, createMemo, createComponent, createContext, createRoot, getOwner, untrack, createEffect, on, createResource, splitProps, Show, onCleanup, For, mapArray, createSignal, onMount, Match, mergeProps, createRenderEffect, getListener } from "solid-js";
1
+ import { useContext, createMemo, createComponent, createContext, createRoot, getOwner, untrack, createEffect, on, createResource, splitProps, Show, onCleanup, For, Match, mergeProps, createRenderEffect, getListener } from "solid-js";
2
2
  import { createMutable } from "solid-js/store";
3
3
  const BASE_CTOR = Function;
4
4
  class ReactiveContext extends BASE_CTOR {
@@ -7,7 +7,7 @@ class ReactiveContext extends BASE_CTOR {
7
7
  * Returns the {@link Accessor} for the value in the current {@link Owner}.
8
8
  * It's like calling {@link useContext}
9
9
  */
10
- get read() {
10
+ get get() {
11
11
  return useContext(this.ctx);
12
12
  }
13
13
  /**
@@ -48,7 +48,7 @@ class ReactiveContext extends BASE_CTOR {
48
48
  const ctx2 = createContext(() => defaultValue, {
49
49
  name
50
50
  });
51
- const f = () => out.read();
51
+ const f = () => out.get();
52
52
  f.ctx = ctx2;
53
53
  const out = Object.setPrototypeOf(f, ReactiveContext.prototype);
54
54
  return out;
@@ -67,6 +67,15 @@ function runWithContext(ctx2, value, f) {
67
67
  function untrackCall(f, ...args) {
68
68
  return untrack(() => f.apply(this, args));
69
69
  }
70
+ function defineSignalProperty(obj, k, [get, set]) {
71
+ return Object.defineProperty(obj, k, {
72
+ get,
73
+ set,
74
+ enumerable: true,
75
+ writable: true,
76
+ configurable: true
77
+ });
78
+ }
70
79
  function createReactiveResource(f) {
71
80
  var refetch;
72
81
  const memo = createMemo(f);
@@ -95,7 +104,7 @@ const HANDLER = new Proxy({}, {
95
104
  });
96
105
  function Enfold(props) {
97
106
  const f = () => props.children;
98
- const children = unwrap(createMemo(() => props.unrecycled ? f : createMemo(f)));
107
+ const children = unwrap(createMemo(() => props.recycled ? createMemo(f) : f));
99
108
  return createComponent(Show, {
100
109
  get keyed() {
101
110
  return props.keyed;
@@ -217,30 +226,17 @@ function createInfo(props) {
217
226
  }
218
227
  };
219
228
  }
220
- function Nest(props) {
221
- const memo = memoProps(props, ["template", "children"]);
222
- const content = mapArray(() => props.each, (x, i) => {
223
- const [get, set] = createSignal();
224
- const out = createMemo(() => untrackCall(memo.template, get, x, i));
225
- return (x2) => {
226
- onMount(() => set(() => x2));
227
- onCleanup(() => set(void 0));
228
- return out;
229
- };
230
- });
231
- return createMemo(() => content().reduceRight((a, b) => b(a), memo.children));
232
- }
233
229
  const ctx = ReactiveContext.create(void 0, "on-case");
234
230
  const On = ctx.Provider;
235
231
  function Case(props) {
236
232
  const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
237
233
  const {
238
- read
234
+ get
239
235
  } = ctx;
240
236
  const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
241
237
  return createComponent(Match, mergeProps({
242
238
  get when() {
243
- return pred(read());
239
+ return pred(get());
244
240
  }
245
241
  }, other));
246
242
  }
@@ -291,7 +287,6 @@ function SameContext(props) {
291
287
  export {
292
288
  Case,
293
289
  Enfold,
294
- Nest,
295
290
  On,
296
291
  ReactiveContext,
297
292
  SameContext,
@@ -302,6 +297,7 @@ export {
302
297
  createExtractor,
303
298
  createReactiveResource,
304
299
  debug,
300
+ defineSignalProperty,
305
301
  memoProps,
306
302
  runWithContext,
307
303
  splitAndMemoProps,
package/dist/index.umd.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * Returns the {@link Accessor} for the value in the current {@link Owner}.
10
10
  * It's like calling {@link useContext}
11
11
  */
12
- get read() {
12
+ get get() {
13
13
  return solidJs.useContext(this.ctx);
14
14
  }
15
15
  /**
@@ -50,7 +50,7 @@
50
50
  const ctx2 = solidJs.createContext(() => defaultValue, {
51
51
  name
52
52
  });
53
- const f = () => out.read();
53
+ const f = () => out.get();
54
54
  f.ctx = ctx2;
55
55
  const out = Object.setPrototypeOf(f, ReactiveContext.prototype);
56
56
  return out;
@@ -69,6 +69,15 @@
69
69
  function untrackCall(f, ...args) {
70
70
  return solidJs.untrack(() => f.apply(this, args));
71
71
  }
72
+ function defineSignalProperty(obj, k, [get, set]) {
73
+ return Object.defineProperty(obj, k, {
74
+ get,
75
+ set,
76
+ enumerable: true,
77
+ writable: true,
78
+ configurable: true
79
+ });
80
+ }
72
81
  function createReactiveResource(f) {
73
82
  var refetch;
74
83
  const memo = solidJs.createMemo(f);
@@ -97,7 +106,7 @@
97
106
  });
98
107
  function Enfold(props) {
99
108
  const f = () => props.children;
100
- const children = unwrap(solidJs.createMemo(() => props.unrecycled ? f : solidJs.createMemo(f)));
109
+ const children = unwrap(solidJs.createMemo(() => props.recycled ? solidJs.createMemo(f) : f));
101
110
  return solidJs.createComponent(solidJs.Show, {
102
111
  get keyed() {
103
112
  return props.keyed;
@@ -219,30 +228,17 @@
219
228
  }
220
229
  };
221
230
  }
222
- function Nest(props) {
223
- const memo = memoProps(props, ["template", "children"]);
224
- const content = solidJs.mapArray(() => props.each, (x, i) => {
225
- const [get, set] = solidJs.createSignal();
226
- const out = solidJs.createMemo(() => untrackCall(memo.template, get, x, i));
227
- return (x2) => {
228
- solidJs.onMount(() => set(() => x2));
229
- solidJs.onCleanup(() => set(void 0));
230
- return out;
231
- };
232
- });
233
- return solidJs.createMemo(() => content().reduceRight((a, b) => b(a), memo.children));
234
- }
235
231
  const ctx = ReactiveContext.create(void 0, "on-case");
236
232
  const On = ctx.Provider;
237
233
  function Case(props) {
238
234
  const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
239
235
  const {
240
- read
236
+ get
241
237
  } = ctx;
242
238
  const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
243
239
  return solidJs.createComponent(solidJs.Match, solidJs.mergeProps({
244
240
  get when() {
245
- return pred(read());
241
+ return pred(get());
246
242
  }
247
243
  }, other));
248
244
  }
@@ -292,7 +288,6 @@
292
288
  }
293
289
  exports2.Case = Case;
294
290
  exports2.Enfold = Enfold;
295
- exports2.Nest = Nest;
296
291
  exports2.On = On;
297
292
  exports2.ReactiveContext = ReactiveContext;
298
293
  exports2.SameContext = SameContext;
@@ -302,6 +297,7 @@
302
297
  exports2.createExtractor = createExtractor;
303
298
  exports2.createReactiveResource = createReactiveResource;
304
299
  exports2.debug = debug;
300
+ exports2.defineSignalProperty = defineSignalProperty;
305
301
  exports2.memoProps = memoProps;
306
302
  exports2.runWithContext = runWithContext;
307
303
  exports2.splitAndMemoProps = splitAndMemoProps;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -39,38 +39,16 @@ And if `value` was falsish, it would have been equivalent to this
39
39
  ```tsx
40
40
  return <>CONTENT</>
41
41
  ```
42
- If you are wrapping the content with a context provider you need to set the `unrecycled` attribute to `true` to run again the whole content, otherwise the `Owner` of the content won't be child of the template one
42
+ If you are NOT wrapping the content with a context provider you can set the `recycled` attribute to `true` in order to NOT run again the whole content each time its template gets added/removed, in this case the `Owner` of the content won't be child of the template one
43
43
  ```tsx
44
44
  return <>
45
- <Enfold unrecycled when={value} template={c => <ctx.Provider value={2} children={c()} />}>
45
+ <Enfold recycled when={value} template={c => <ctx.Provider value={2} children={c()} />}>
46
+ {/* This won't be 2 */}
46
47
  {useContext(ctx)}
47
48
  </Enfold>
48
49
  </>
49
50
  ```
50
51
 
51
- ### `Nest`
52
- Like a `For`, but instead of putting an element after another it nests them.
53
- The following code
54
- ```tsx
55
- return <>
56
- <Nest each={[ "red", "green", "blue" ]} template={(c, x) => <div style={{ background: x, padding: "1ch" }}>{c()}</div>}>
57
- CONTENT
58
- </Nest>
59
- </>
60
- ```
61
- Is equivalent to this
62
- ```tsx
63
- return <>
64
- <div style={{ background: "red", padding: "1ch" }}>
65
- <div style={{ background: "green", padding: "1ch" }}>
66
- <div style={{ background: "blue", padding: "1ch" }}>
67
- CONTENT
68
- </div>
69
- </div>
70
- </div>
71
- </>
72
- ```
73
-
74
52
  ### `On` - `Case`
75
53
  Allows you to not repeat the same condition across many `Match` components.
76
54
  The following code
@@ -210,7 +188,7 @@ Method that creates a reactive version of a solid `Context` with some additional
210
188
  ```ts
211
189
  const ctx = ReactiveContext.create("SOMETHING");
212
190
  const underlyingNormalContext = ctx.ctx;
213
- const valueAccessorForTheCurrentOwner = ctx.read;
191
+ const valueAccessorForTheCurrentOwner = ctx.get;
214
192
  const value = ctx();
215
193
  ```
216
194
 
@@ -237,6 +215,9 @@ return <>
237
215
  ### `untrackCall()`
238
216
  Calls a function untracking what happens inside of it but not what gets passed as its argument
239
217
 
218
+ ### `defineSignalProperty()`
219
+ Defines a reactive property on an object
220
+
240
221
  ### `createReactiveResource()`
241
222
  Like `createResource()` but the provided function will be reactive
242
223