solid-ctrl-flow 1.0.11 → 1.0.13

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
@@ -57,6 +57,7 @@ export declare function Case(props: {
57
57
  * </e.Joint>
58
58
  * <>
59
59
  * ```
60
+ * @param name Name to give to the context of the extractor
60
61
  */
61
62
  export declare function createExtractor(name?: string): {
62
63
  /** Bound {@link Dest} */
@@ -74,17 +75,28 @@ export declare function createExtractor(name?: string): {
74
75
  readonly isExtracted: () => number | null;
75
76
  };
76
77
 
77
- /** Sets the value that will be returned by {@link Debug.isDebug} for the children of the current component */
78
- export declare function Debug(props: {
79
- value?: boolean;
78
+ /**
79
+ * Creates a context with a reactive value.
80
+ * It returns the provider directly
81
+ * @param init Initial value for the context
82
+ * @param def Default value to set when calling the provider without one
83
+ * @param name Name to give to the context
84
+ */
85
+ export declare function createOption<T>(init: NonNullable<T>, def?: NonNullable<T>, name?: string): ((props: {
86
+ value?: T;
80
87
  children: JSX.Element;
81
- }): JSX.Element;
88
+ }) => JSX.Element) & {
89
+ /** Returns the {@link Accessor} for the value in the current context */
90
+ read: Accessor<NonNullable<T>>;
91
+ };
82
92
 
83
- /** Utility functions for the debug scopes */
84
- export declare namespace Debug {
85
- /** Gets an {@link Accessor} that tells if the context in which THIS getter has been executed is in debug mode */
86
- const isDebug: Accessor<boolean>;
87
- }
93
+ /** Creates a scope for the value of {@link Debug.read}, which allows you to display different things in debug mode */
94
+ export declare const Debug: ((props: {
95
+ value?: boolean | undefined;
96
+ children: JSX.Element;
97
+ }) => JSX.Element) & {
98
+ read: Accessor<boolean>;
99
+ };
88
100
 
89
101
  /** Informations about a {@link Source} component */
90
102
  declare type Info = {
package/dist/index.mjs CHANGED
@@ -1,11 +1,58 @@
1
- import { createContext, createMemo, createComponent, useContext, Match, mergeProps, onCleanup, For, Show, splitProps, createRoot, getOwner, untrack } from "solid-js";
1
+ import { untrack, createMemo, splitProps, createContext, createRoot, getOwner, createComponent, useContext, Match, mergeProps, onCleanup, For, Show } from "solid-js";
2
2
  import { createMutable } from "solid-js/store";
3
- const ctx$1 = createContext(() => void 0, {
3
+ function untrackCall(f, ...args) {
4
+ return untrack(() => f.apply(this, args));
5
+ }
6
+ function memoProps(obj, keys = Object.keys(obj)) {
7
+ const out = {};
8
+ for (const elm of keys)
9
+ Object.defineProperty(out, elm, {
10
+ enumerable: true,
11
+ get: createMemo(() => obj[elm])
12
+ });
13
+ return out;
14
+ }
15
+ function splitAndMemoProps(obj, keys) {
16
+ const [mine, other] = splitProps(obj, keys);
17
+ return [memoProps(mine, keys), other];
18
+ }
19
+ function createOption(init, def = init, name) {
20
+ const prop = "read";
21
+ const ctx2 = createContext(() => init, {
22
+ name
23
+ });
24
+ const provider = (props) => createComponent(ctx2.Provider, {
25
+ value: () => props.value ?? def,
26
+ get children() {
27
+ return props.children;
28
+ }
29
+ });
30
+ Object.defineProperty(provider, prop, {
31
+ get: () => useContext(ctx2)
32
+ });
33
+ return provider;
34
+ }
35
+ function runWithContext(ctx2, value, f) {
36
+ return createRoot(async (d) => {
37
+ try {
38
+ getOwner().context = {
39
+ [ctx2.id]: value
40
+ };
41
+ const out = f(value);
42
+ if (out instanceof Promise)
43
+ return out.finally(d);
44
+ return d(), out;
45
+ } catch (ex) {
46
+ throw d(), ex;
47
+ }
48
+ });
49
+ }
50
+ const ctx = createContext(() => void 0, {
4
51
  name: "case-when"
5
52
  });
6
53
  function Case(props) {
7
54
  const memo = createMemo(() => props.value);
8
- return createComponent(ctx$1.Provider, {
55
+ return createComponent(ctx.Provider, {
9
56
  value: memo,
10
57
  get children() {
11
58
  return props.children;
@@ -14,7 +61,7 @@ function Case(props) {
14
61
  }
15
62
  function When(props) {
16
63
  const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
17
- const value = useContext(ctx$1);
64
+ const value = useContext(ctx);
18
65
  const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
19
66
  return createComponent(Match, mergeProps({
20
67
  get when() {
@@ -22,22 +69,6 @@ function When(props) {
22
69
  }
23
70
  }, other));
24
71
  }
25
- const ctx = createContext(() => false, {
26
- name: "debug-scope"
27
- });
28
- function Debug(props) {
29
- return createComponent(ctx.Provider, {
30
- value: () => props.value ?? true,
31
- get children() {
32
- return props.children;
33
- }
34
- });
35
- }
36
- (function(_Debug) {
37
- Object.defineProperty(Debug, "isDebug", {
38
- get: () => useContext(ctx)
39
- });
40
- })(Debug || (Debug = {}));
41
72
  function createExtractor(name = "extractor") {
42
73
  const ctx2 = createContext(void 0, {
43
74
  name
@@ -111,37 +142,13 @@ function Joint(props) {
111
142
  }
112
143
  });
113
144
  }
114
- function memoProps(obj, keys = Object.keys(obj)) {
115
- const out = {};
116
- for (const elm of keys)
117
- Object.defineProperty(out, elm, { enumerable: true, get: createMemo(() => obj[elm]) });
118
- return out;
119
- }
120
- function splitAndMemoProps(obj, keys) {
121
- const [mine, other] = splitProps(obj, keys);
122
- return [memoProps(mine, keys), other];
123
- }
124
- function runWithContext(ctx2, value, f) {
125
- return createRoot(async (d) => {
126
- try {
127
- getOwner().context = { [ctx2.id]: value };
128
- const out = f(value);
129
- if (out instanceof Promise)
130
- return out.finally(d);
131
- return d(), out;
132
- } catch (ex) {
133
- throw d(), ex;
134
- }
135
- });
136
- }
137
- function untrackCall(f, ...args) {
138
- return untrack(() => f.apply(this, args));
139
- }
145
+ const Debug = createOption(false, true, "debug-scope");
140
146
  export {
141
147
  Case,
142
148
  Debug,
143
149
  When,
144
150
  createExtractor,
151
+ createOption,
145
152
  memoProps,
146
153
  runWithContext,
147
154
  splitAndMemoProps,
package/dist/index.umd.js CHANGED
@@ -2,12 +2,59 @@
2
2
  typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("solid-js"), require("solid-js/store")) : typeof define === "function" && define.amd ? define(["exports", "solid-js", "solid-js/store"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.solidCtrlFlow = {}, global.solidJs, global.solidJsStore));
3
3
  })(this, function(exports2, solidJs, store) {
4
4
  "use strict";
5
- const ctx$1 = solidJs.createContext(() => void 0, {
5
+ function untrackCall(f, ...args) {
6
+ return solidJs.untrack(() => f.apply(this, args));
7
+ }
8
+ function memoProps(obj, keys = Object.keys(obj)) {
9
+ const out = {};
10
+ for (const elm of keys)
11
+ Object.defineProperty(out, elm, {
12
+ enumerable: true,
13
+ get: solidJs.createMemo(() => obj[elm])
14
+ });
15
+ return out;
16
+ }
17
+ function splitAndMemoProps(obj, keys) {
18
+ const [mine, other] = solidJs.splitProps(obj, keys);
19
+ return [memoProps(mine, keys), other];
20
+ }
21
+ function createOption(init, def = init, name) {
22
+ const prop = "read";
23
+ const ctx2 = solidJs.createContext(() => init, {
24
+ name
25
+ });
26
+ const provider = (props) => solidJs.createComponent(ctx2.Provider, {
27
+ value: () => props.value ?? def,
28
+ get children() {
29
+ return props.children;
30
+ }
31
+ });
32
+ Object.defineProperty(provider, prop, {
33
+ get: () => solidJs.useContext(ctx2)
34
+ });
35
+ return provider;
36
+ }
37
+ function runWithContext(ctx2, value, f) {
38
+ return solidJs.createRoot(async (d) => {
39
+ try {
40
+ solidJs.getOwner().context = {
41
+ [ctx2.id]: value
42
+ };
43
+ const out = f(value);
44
+ if (out instanceof Promise)
45
+ return out.finally(d);
46
+ return d(), out;
47
+ } catch (ex) {
48
+ throw d(), ex;
49
+ }
50
+ });
51
+ }
52
+ const ctx = solidJs.createContext(() => void 0, {
6
53
  name: "case-when"
7
54
  });
8
55
  function Case(props) {
9
56
  const memo = solidJs.createMemo(() => props.value);
10
- return solidJs.createComponent(ctx$1.Provider, {
57
+ return solidJs.createComponent(ctx.Provider, {
11
58
  value: memo,
12
59
  get children() {
13
60
  return props.children;
@@ -16,7 +63,7 @@
16
63
  }
17
64
  function When(props) {
18
65
  const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
19
- const value = solidJs.useContext(ctx$1);
66
+ const value = solidJs.useContext(ctx);
20
67
  const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
21
68
  return solidJs.createComponent(solidJs.Match, solidJs.mergeProps({
22
69
  get when() {
@@ -24,22 +71,6 @@
24
71
  }
25
72
  }, other));
26
73
  }
27
- const ctx = solidJs.createContext(() => false, {
28
- name: "debug-scope"
29
- });
30
- function Debug(props) {
31
- return solidJs.createComponent(ctx.Provider, {
32
- value: () => props.value ?? true,
33
- get children() {
34
- return props.children;
35
- }
36
- });
37
- }
38
- (function(_Debug) {
39
- Object.defineProperty(Debug, "isDebug", {
40
- get: () => solidJs.useContext(ctx)
41
- });
42
- })(Debug || (Debug = {}));
43
74
  function createExtractor(name = "extractor") {
44
75
  const ctx2 = solidJs.createContext(void 0, {
45
76
  name
@@ -113,36 +144,12 @@
113
144
  }
114
145
  });
115
146
  }
116
- function memoProps(obj, keys = Object.keys(obj)) {
117
- const out = {};
118
- for (const elm of keys)
119
- Object.defineProperty(out, elm, { enumerable: true, get: solidJs.createMemo(() => obj[elm]) });
120
- return out;
121
- }
122
- function splitAndMemoProps(obj, keys) {
123
- const [mine, other] = solidJs.splitProps(obj, keys);
124
- return [memoProps(mine, keys), other];
125
- }
126
- function runWithContext(ctx2, value, f) {
127
- return solidJs.createRoot(async (d) => {
128
- try {
129
- solidJs.getOwner().context = { [ctx2.id]: value };
130
- const out = f(value);
131
- if (out instanceof Promise)
132
- return out.finally(d);
133
- return d(), out;
134
- } catch (ex) {
135
- throw d(), ex;
136
- }
137
- });
138
- }
139
- function untrackCall(f, ...args) {
140
- return solidJs.untrack(() => f.apply(this, args));
141
- }
147
+ const Debug = createOption(false, true, "debug-scope");
142
148
  exports2.Case = Case;
143
149
  exports2.Debug = Debug;
144
150
  exports2.When = When;
145
151
  exports2.createExtractor = createExtractor;
152
+ exports2.createOption = createOption;
146
153
  exports2.memoProps = memoProps;
147
154
  exports2.runWithContext = runWithContext;
148
155
  exports2.splitAndMemoProps = splitAndMemoProps;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -40,7 +40,7 @@ const value = false;
40
40
  return <>
41
41
  <Debug value={value}>
42
42
  {/* (A LOT OF NESTING...) */}
43
- <Show when={Debug.isDebug()}>
43
+ <Show when={Debug.read()}>
44
44
  THIS WOULD BE VISIBLE IF "value" HAD BEEN {true}
45
45
  </Show>
46
46
  </Debug>
@@ -63,7 +63,7 @@ function MyCustomComponent() {
63
63
  </>
64
64
  }
65
65
  ```
66
- If you use it normally it will output `4`, `1`, `2`, `3`, `5`
66
+ If you use it normally it will output `4`, ***`1`***, ***`2`***, ***`3`***, `5`
67
67
  ```tsx
68
68
  return <>
69
69
  4
@@ -116,11 +116,17 @@ return <>
116
116
  ## Utility functions
117
117
  Utility functions needed for the components above
118
118
 
119
+ ### `untrackCall()`
120
+ Calls a function untracking what happens inside of it but not what gets passed as its argument
121
+
119
122
  ### `memoProps()`
120
123
  Creates a partial version of an object with memoized remaining properties
121
124
 
122
125
  ### `splitAndMemoProps()`
123
126
  Like `splitProps()` but memoizes the whole local part
124
127
 
128
+ ### `createOption()`
129
+ Allows you to create simple reactive contexts; For example `Debug` is made with it
130
+
125
131
  ### `runWithContext()`
126
132
  Creates a context scope that persists for the duration of a function