solid-ctrl-flow 1.0.27 → 1.0.29

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
@@ -2,6 +2,7 @@ import { Accessor } from 'solid-js';
2
2
  import { Context } from 'solid-js';
3
3
  import { EffectFunction } from 'solid-js';
4
4
  import { JSX } from 'solid-js';
5
+ import { Ref } from 'solid-js';
5
6
  import { Resource } from 'solid-js';
6
7
  import { Setter } from 'solid-js';
7
8
  import { Signal } from 'solid-js';
@@ -156,6 +157,11 @@ export declare const Debug: {
156
157
  read: Accessor<boolean>;
157
158
  };
158
159
 
160
+ /** Component that gives you the owner {@link Document} */
161
+ export declare function Doc(props: {
162
+ ref: Ref<Document>;
163
+ }): Text;
164
+
159
165
  /** Informations about a {@link Source} component */
160
166
  declare type Info = {
161
167
  /** Order of the current source if there are many */
@@ -172,13 +178,23 @@ declare type Info = {
172
178
  export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[]>(obj: T, keys?: K): Pick<T, K[number]>;
173
179
 
174
180
  /**
175
- * Executes {@link f} with the provided {@link Context}
181
+ * Executes a {@link Ref}.
182
+ * Throws a {@link ReferenceError} if {@link ref} its not a function at runtime (Which should never be the case thanks to the solid compiler)
183
+ * @param ref The function to execute
184
+ * @param value The value to pass to the function
185
+ * @returns The same value of {@link value}
186
+ */
187
+ export declare function refCall<T, U extends T = T>(ref: Ref<T> | undefined, value: U): U;
188
+
189
+ /**
190
+ * Executes {@link f} with the provided value for the specified {@link Context}.
191
+ * You can pass `undefined` to {@link value} in order to get back the default value for {@link ctx}
176
192
  * @param ctx The context to which to set the value
177
193
  * @param value The value for the context
178
194
  * @param f The function to run
179
195
  * @returns The same thing {@link f} returned
180
196
  */
181
- export declare function runWithContext<T, R>(ctx: Context<T>, value: T, f: (x: T) => R): R;
197
+ export declare function runWithContext<T, R>(ctx: Context<T>, value: T | undefined, f: (x: T | undefined) => R): R;
182
198
 
183
199
  /**
184
200
  * Executes {@link splitProps} and memoizes the first of the two returned objects
package/dist/index.mjs CHANGED
@@ -1,8 +1,16 @@
1
- import { untrack, createMemo, splitProps, createEffect, on, createResource, createContext, createRoot, getOwner, createComponent, useContext, createRenderEffect, onCleanup, Match, mergeProps, For, Show } from "solid-js";
1
+ import { untrack, createMemo, splitProps, createContext, getOwner, runWithOwner, createEffect, on, createResource, createComponent, useContext, createRoot, createRenderEffect, onCleanup, Match, mergeProps, For, Show, onMount } 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));
5
5
  }
6
+ function refCall(ref, value) {
7
+ if (!ref)
8
+ return value;
9
+ if (typeof ref !== "function")
10
+ throw new ReferenceError('Il parametro "ref" deve essere stato compilato in una funzione a runtime');
11
+ ref(value);
12
+ return value;
13
+ }
6
14
  function memoProps(obj, keys = Object.keys(obj)) {
7
15
  const out = {};
8
16
  for (const elm of keys)
@@ -17,15 +25,6 @@ function splitAndMemoProps(obj, keys) {
17
25
  out[0] = memoProps(out[0], keys);
18
26
  return out;
19
27
  }
20
- function createReactiveResource(f) {
21
- var refetch;
22
- const memo = createMemo(f);
23
- createEffect(on(memo, () => refetch?.()));
24
- const [get] = [, {
25
- refetch
26
- }] = createResource(memo);
27
- return get;
28
- }
29
28
  function createOption(init, def = init, name) {
30
29
  const prop = "read";
31
30
  const ctx2 = createContext(() => init, {
@@ -44,19 +43,27 @@ function createOption(init, def = init, name) {
44
43
  return provider;
45
44
  }
46
45
  function runWithContext(ctx2, value, f) {
47
- return createRoot((d) => {
48
- try {
49
- getOwner().context = {
50
- [ctx2.id]: value
51
- };
52
- const out = f(value);
53
- if (out instanceof Promise)
54
- return out.finally(d);
55
- return d(), out;
56
- } catch (ex) {
57
- throw d(), ex;
58
- }
59
- });
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));
58
+ }
59
+ function createReactiveResource(f) {
60
+ var refetch;
61
+ const memo = createMemo(f);
62
+ createEffect(on(memo, () => refetch?.()));
63
+ const [get] = [, {
64
+ refetch
65
+ }] = createResource(memo);
66
+ return get;
60
67
  }
61
68
  const IDENTITY = (x) => x;
62
69
  function bind([get], [, set], to = IDENTITY) {
@@ -180,9 +187,15 @@ function Joint(props) {
180
187
  });
181
188
  }
182
189
  const Debug = createOption(false, true, "debug-scope");
190
+ function Doc(props) {
191
+ const node = document.createTextNode("");
192
+ onMount(() => (refCall(props.ref, node.ownerDocument), node.remove()));
193
+ return node;
194
+ }
183
195
  export {
184
196
  Case,
185
197
  Debug,
198
+ Doc,
186
199
  When,
187
200
  bind,
188
201
  bindTwoWay,
@@ -191,6 +204,7 @@ export {
191
204
  createOption,
192
205
  createReactiveResource,
193
206
  memoProps,
207
+ refCall,
194
208
  runWithContext,
195
209
  splitAndMemoProps,
196
210
  toSetter,
package/dist/index.umd.js CHANGED
@@ -5,6 +5,14 @@
5
5
  function untrackCall(f, ...args) {
6
6
  return solidJs.untrack(() => f.apply(this, args));
7
7
  }
8
+ function refCall(ref, value) {
9
+ if (!ref)
10
+ return value;
11
+ if (typeof ref !== "function")
12
+ throw new ReferenceError('Il parametro "ref" deve essere stato compilato in una funzione a runtime');
13
+ ref(value);
14
+ return value;
15
+ }
8
16
  function memoProps(obj, keys = Object.keys(obj)) {
9
17
  const out = {};
10
18
  for (const elm of keys)
@@ -19,15 +27,6 @@
19
27
  out[0] = memoProps(out[0], keys);
20
28
  return out;
21
29
  }
22
- function createReactiveResource(f) {
23
- var refetch;
24
- const memo = solidJs.createMemo(f);
25
- solidJs.createEffect(solidJs.on(memo, () => refetch?.()));
26
- const [get] = [, {
27
- refetch
28
- }] = solidJs.createResource(memo);
29
- return get;
30
- }
31
30
  function createOption(init, def = init, name) {
32
31
  const prop = "read";
33
32
  const ctx2 = solidJs.createContext(() => init, {
@@ -46,19 +45,27 @@
46
45
  return provider;
47
46
  }
48
47
  function runWithContext(ctx2, value, f) {
49
- return solidJs.createRoot((d) => {
50
- try {
51
- solidJs.getOwner().context = {
52
- [ctx2.id]: value
53
- };
54
- const out = f(value);
55
- if (out instanceof Promise)
56
- return out.finally(d);
57
- return d(), out;
58
- } catch (ex) {
59
- throw d(), ex;
60
- }
61
- });
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));
60
+ }
61
+ function createReactiveResource(f) {
62
+ var refetch;
63
+ const memo = solidJs.createMemo(f);
64
+ solidJs.createEffect(solidJs.on(memo, () => refetch?.()));
65
+ const [get] = [, {
66
+ refetch
67
+ }] = solidJs.createResource(memo);
68
+ return get;
62
69
  }
63
70
  const IDENTITY = (x) => x;
64
71
  function bind([get], [, set], to = IDENTITY) {
@@ -182,8 +189,14 @@
182
189
  });
183
190
  }
184
191
  const Debug = createOption(false, true, "debug-scope");
192
+ function Doc(props) {
193
+ const node = document.createTextNode("");
194
+ solidJs.onMount(() => (refCall(props.ref, node.ownerDocument), node.remove()));
195
+ return node;
196
+ }
185
197
  exports2.Case = Case;
186
198
  exports2.Debug = Debug;
199
+ exports2.Doc = Doc;
187
200
  exports2.When = When;
188
201
  exports2.bind = bind;
189
202
  exports2.bindTwoWay = bindTwoWay;
@@ -192,6 +205,7 @@
192
205
  exports2.createOption = createOption;
193
206
  exports2.createReactiveResource = createReactiveResource;
194
207
  exports2.memoProps = memoProps;
208
+ exports2.refCall = refCall;
195
209
  exports2.runWithContext = runWithContext;
196
210
  exports2.splitAndMemoProps = splitAndMemoProps;
197
211
  exports2.toSetter = toSetter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -47,6 +47,15 @@ return <>
47
47
  </>
48
48
  ```
49
49
 
50
+ ### `Doc`
51
+ Gives back the current document
52
+ ```tsx
53
+ var doc: Document;
54
+ return <>
55
+ <Doc ref={doc} />
56
+ </>
57
+ ```
58
+
50
59
  ### `createExtractor()`
51
60
  Creates a context for components that may need to be out of their parent in certain conditions.
52
61
  If you have this component
@@ -148,17 +157,20 @@ Utility functions needed for the components above
148
157
  ### `untrackCall()`
149
158
  Calls a function untracking what happens inside of it but not what gets passed as its argument
150
159
 
160
+ ### `refCall()`
161
+ Executes a `Ref`
162
+
151
163
  ### `memoProps()`
152
164
  Creates a partial version of an object with memoized remaining properties
153
165
 
154
166
  ### `splitAndMemoProps()`
155
167
  Like `splitProps()` but memoizes the whole local part
156
168
 
157
- ### `createReactiveResource()`
158
- Like `createResource()` but the provided function will be reactive
159
-
160
169
  ### `createOption()`
161
170
  Allows you to create simple reactive contexts; For example `Debug` is made with it
162
171
 
163
172
  ### `runWithContext()`
164
- Creates a context scope that persists for the duration of a function
173
+ Creates a context scope that persists for the duration of a function
174
+
175
+ ### `createReactiveResource()`
176
+ Like `createResource()` but the provided function will be reactive