solid-ctrl-flow 1.0.30 → 1.0.32

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
@@ -172,6 +172,13 @@ declare type Info = {
172
172
  */
173
173
  export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[]>(obj: T, keys?: K): Pick<T, K[number]>;
174
174
 
175
+ /** Like a {@link Suspence}, but enables you to turn it off and show the loading state */
176
+ export declare function OptionalSuspense(props: {
177
+ active: boolean;
178
+ children: JSX.Element;
179
+ fallback?: JSX.Element;
180
+ }): JSX.Element;
181
+
175
182
  /**
176
183
  * Executes a {@link Ref}.
177
184
  * Throws a {@link ReferenceError} if {@link ref} its not a function at runtime (Which should never be the case thanks to the solid compiler)
@@ -181,9 +188,17 @@ export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[
181
188
  */
182
189
  export declare function refCall<T, U extends T = T>(ref: Ref<T> | undefined, value: U): U;
183
190
 
191
+ /**
192
+ * Executes {@link f} and then calls {@link d} regardless of errors.
193
+ * If {@link f} returns a {@link Promise}, {@link d} will be called asynchronously
194
+ * @returns The same thing {@link f} returned
195
+ */
196
+ export declare function runAndDispose<R>(d: () => void, f: () => R): R;
197
+
184
198
  /**
185
199
  * Executes {@link f} with the provided value for the specified {@link Context}.
186
- * You can pass `undefined` to {@link value} in order to get back the default value for {@link ctx}
200
+ * You can pass `undefined` to {@link value} in order to get back the default value for {@link ctx}.
201
+ * Everything that happens {@link f} will be disposed as soon as the execution ends
187
202
  * @param ctx The context to which to set the value
188
203
  * @param value The value for the context
189
204
  * @param f The function to run
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { untrack, createMemo, splitProps, createContext, getOwner, runWithOwner, createEffect, on, createResource, createComponent, useContext, createRoot, createRenderEffect, onCleanup, Match, mergeProps, For, Show } from "solid-js";
1
+ import { untrack, createMemo, splitProps, createContext, createRoot, createEffect, on, createResource, createComponent, useContext, getOwner, createRenderEffect, onCleanup, Match, mergeProps, For, Show, Suspense } 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));
@@ -43,18 +43,12 @@ function createOption(init, def = init, name) {
43
43
  return provider;
44
44
  }
45
45
  function runWithContext(ctx2, value, f) {
46
- const owner = getOwner();
47
- const context = {
48
- ...owner?.context,
49
- [ctx2.id]: value
50
- };
51
- const temp = {
52
- context,
53
- owner,
54
- owned: null,
55
- cleanups: null
56
- };
57
- return runWithOwner(temp, () => f(value));
46
+ return createRoot((d) => {
47
+ return runAndDispose(d, () => {
48
+ (getOwner().context ??= {})[ctx2.id] = value;
49
+ return f(value);
50
+ });
51
+ });
58
52
  }
59
53
  function createReactiveResource(f) {
60
54
  var refetch;
@@ -65,6 +59,14 @@ function createReactiveResource(f) {
65
59
  }] = createResource(memo);
66
60
  return get;
67
61
  }
62
+ function runAndDispose(d, f) {
63
+ try {
64
+ const out = f();
65
+ return out instanceof Promise ? out.finally(d) : (d(), out);
66
+ } catch (ex) {
67
+ throw d(), ex;
68
+ }
69
+ }
68
70
  const IDENTITY = (x) => x;
69
71
  function bind([get], [, set], to = IDENTITY) {
70
72
  const d = createRoot((d2) => (createRenderEffect(on(get, (x) => set((prev) => to(x, prev)))), d2));
@@ -187,9 +189,30 @@ function Joint(props) {
187
189
  });
188
190
  }
189
191
  const Debug = createOption(false, true, "debug-scope");
192
+ function OptionalSuspense(props) {
193
+ return createComponent(Show, {
194
+ get when() {
195
+ return props.active;
196
+ },
197
+ get fallback() {
198
+ return props.children;
199
+ },
200
+ get children() {
201
+ return createComponent(Suspense, {
202
+ get fallback() {
203
+ return props.fallback;
204
+ },
205
+ get children() {
206
+ return props.children;
207
+ }
208
+ });
209
+ }
210
+ });
211
+ }
190
212
  export {
191
213
  Case,
192
214
  Debug,
215
+ OptionalSuspense,
193
216
  When,
194
217
  bind,
195
218
  bindTwoWay,
@@ -199,6 +222,7 @@ export {
199
222
  createReactiveResource,
200
223
  memoProps,
201
224
  refCall,
225
+ runAndDispose,
202
226
  runWithContext,
203
227
  splitAndMemoProps,
204
228
  toSetter,
package/dist/index.umd.js CHANGED
@@ -45,18 +45,12 @@
45
45
  return provider;
46
46
  }
47
47
  function runWithContext(ctx2, value, f) {
48
- const owner = solidJs.getOwner();
49
- const context = {
50
- ...owner?.context,
51
- [ctx2.id]: value
52
- };
53
- const temp = {
54
- context,
55
- owner,
56
- owned: null,
57
- cleanups: null
58
- };
59
- return solidJs.runWithOwner(temp, () => f(value));
48
+ return solidJs.createRoot((d) => {
49
+ return runAndDispose(d, () => {
50
+ (solidJs.getOwner().context ??= {})[ctx2.id] = value;
51
+ return f(value);
52
+ });
53
+ });
60
54
  }
61
55
  function createReactiveResource(f) {
62
56
  var refetch;
@@ -67,6 +61,14 @@
67
61
  }] = solidJs.createResource(memo);
68
62
  return get;
69
63
  }
64
+ function runAndDispose(d, f) {
65
+ try {
66
+ const out = f();
67
+ return out instanceof Promise ? out.finally(d) : (d(), out);
68
+ } catch (ex) {
69
+ throw d(), ex;
70
+ }
71
+ }
70
72
  const IDENTITY = (x) => x;
71
73
  function bind([get], [, set], to = IDENTITY) {
72
74
  const d = solidJs.createRoot((d2) => (solidJs.createRenderEffect(solidJs.on(get, (x) => set((prev) => to(x, prev)))), d2));
@@ -189,8 +191,29 @@
189
191
  });
190
192
  }
191
193
  const Debug = createOption(false, true, "debug-scope");
194
+ function OptionalSuspense(props) {
195
+ return solidJs.createComponent(solidJs.Show, {
196
+ get when() {
197
+ return props.active;
198
+ },
199
+ get fallback() {
200
+ return props.children;
201
+ },
202
+ get children() {
203
+ return solidJs.createComponent(solidJs.Suspense, {
204
+ get fallback() {
205
+ return props.fallback;
206
+ },
207
+ get children() {
208
+ return props.children;
209
+ }
210
+ });
211
+ }
212
+ });
213
+ }
192
214
  exports2.Case = Case;
193
215
  exports2.Debug = Debug;
216
+ exports2.OptionalSuspense = OptionalSuspense;
194
217
  exports2.When = When;
195
218
  exports2.bind = bind;
196
219
  exports2.bindTwoWay = bindTwoWay;
@@ -200,6 +223,7 @@
200
223
  exports2.createReactiveResource = createReactiveResource;
201
224
  exports2.memoProps = memoProps;
202
225
  exports2.refCall = refCall;
226
+ exports2.runAndDispose = runAndDispose;
203
227
  exports2.runWithContext = runWithContext;
204
228
  exports2.splitAndMemoProps = splitAndMemoProps;
205
229
  exports2.toSetter = toSetter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -47,6 +47,9 @@ return <>
47
47
  </>
48
48
  ```
49
49
 
50
+ ### `OptionalSuspense`
51
+ Like a `Suspence`, but enables you to turn it off and show the loading state
52
+
50
53
  ### `createExtractor()`
51
54
  Creates a context for components that may need to be out of their parent in certain conditions.
52
55
  If you have this component
@@ -164,4 +167,7 @@ Allows you to create simple reactive contexts; For example `Debug` is made with
164
167
  Creates a context scope that persists for the duration of a function
165
168
 
166
169
  ### `createReactiveResource()`
167
- Like `createResource()` but the provided function will be reactive
170
+ Like `createResource()` but the provided function will be reactive
171
+
172
+ ### `runAndDispose()`
173
+ Executes a function THEN calls a dispose callback; It works with async functions too