solid-ctrl-flow 1.0.30 → 1.0.31

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
@@ -181,9 +181,17 @@ export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[
181
181
  */
182
182
  export declare function refCall<T, U extends T = T>(ref: Ref<T> | undefined, value: U): U;
183
183
 
184
+ /**
185
+ * Executes {@link f} and then calls {@link d} regardless of errors.
186
+ * If {@link f} returns a {@link Promise}, {@link d} will be called asynchronously
187
+ * @returns The same thing {@link f} returned
188
+ */
189
+ export declare function runAndDispose<R>(d: () => void, f: () => R): R;
190
+
184
191
  /**
185
192
  * 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}
193
+ * You can pass `undefined` to {@link value} in order to get back the default value for {@link ctx}.
194
+ * Everything that happens {@link f} will be disposed as soon as the execution ends
187
195
  * @param ctx The context to which to set the value
188
196
  * @param value The value for the context
189
197
  * @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 } 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));
@@ -199,6 +201,7 @@ export {
199
201
  createReactiveResource,
200
202
  memoProps,
201
203
  refCall,
204
+ runAndDispose,
202
205
  runWithContext,
203
206
  splitAndMemoProps,
204
207
  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));
@@ -200,6 +202,7 @@
200
202
  exports2.createReactiveResource = createReactiveResource;
201
203
  exports2.memoProps = memoProps;
202
204
  exports2.refCall = refCall;
205
+ exports2.runAndDispose = runAndDispose;
203
206
  exports2.runWithContext = runWithContext;
204
207
  exports2.splitAndMemoProps = splitAndMemoProps;
205
208
  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.31",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -164,4 +164,7 @@ Allows you to create simple reactive contexts; For example `Debug` is made with
164
164
  Creates a context scope that persists for the duration of a function
165
165
 
166
166
  ### `createReactiveResource()`
167
- Like `createResource()` but the provided function will be reactive
167
+ Like `createResource()` but the provided function will be reactive
168
+
169
+ ### `runAndDispose()`
170
+ Executes a function THEN calls a dispose callback; It works with async functions too