solid-ctrl-flow 2.2.5 → 3.0.0

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,11 +57,23 @@ export declare class Extractor {
57
57
  Joint: (props: {
58
58
  children?: JSX.Element;
59
59
  }) => JSX.Element;
60
- Fallback: () => JSX.Element;
61
60
  Dest: (props: {
62
- hidden?: boolean | undefined;
61
+ children?: JSX.Element;
62
+ }) => JSX.Element;
63
+ Source: (props: {
64
+ order?: number | undefined;
65
+ } & {
66
+ children?: JSX.Element;
67
+ } & {
68
+ fallback?: JSX.Element;
69
+ }) => JSX.Element;
70
+ SameContextSource: (props: {
71
+ order?: number | undefined;
72
+ } & {
73
+ children?: JSX.Element;
74
+ } & {
75
+ fallback?: JSX.Element;
63
76
  }) => JSX.Element;
64
- Source: (props: Info) => JSX.Element;
65
77
  /**
66
78
  * Returns the number of destinations available for the current {@link Extractor}.
67
79
  * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createMemo, splitProps, untrack, createRoot, getOwner, createComponent, Show, createContext, useContext, createRenderEffect, on, onCleanup, For, createSignal, createSelector, equalFn, Match } from "solid-js";
1
+ import { createMemo, splitProps, untrack, createRoot, getOwner, createComponent, Show, createContext, useContext, onCleanup, For, createRenderEffect, on, mergeProps, createSignal, createSelector, equalFn, Match } from "solid-js";
2
2
  const NO_OP = () => {
3
3
  };
4
4
  function memoProps(obj, keys = Reflect.ownKeys(obj), equals = false) {
@@ -124,9 +124,9 @@ class Extractor {
124
124
  });
125
125
  }
126
126
  Joint = Joint.bind(this);
127
- Fallback = Fallback.bind(this);
128
127
  Dest = Dest.bind(this);
129
128
  Source = Source.bind(this);
129
+ SameContextSource = SameContextSource.bind(this);
130
130
  /**
131
131
  * Returns the number of destinations available for the current {@link Extractor}.
132
132
  * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
@@ -146,41 +146,26 @@ function Joint(props) {
146
146
  }
147
147
  });
148
148
  }
149
- function Fallback() {
150
- const _self$ = this;
151
- const f = this.getDestCount;
152
- return createComponent(Show, {
153
- get when() {
154
- return !f?.();
155
- },
156
- get children() {
157
- return createComponent(_self$.Dest, {
158
- hidden: true
159
- });
160
- }
161
- });
162
- }
163
149
  function Dest(props) {
164
150
  const state = useContext(this.ctx);
165
151
  if (!state)
166
- throw new Error("An extractor's destination must be inside one of its joints");
167
- createRenderEffect(on(createMemo(() => props.hidden), (x) => {
168
- if (x)
169
- return;
170
- onCleanup(() => state.shift(-1));
171
- state.shift(1);
172
- }));
152
+ return createMemo(() => props.children);
153
+ onCleanup(() => state.shift(-1));
154
+ state.shift(1);
173
155
  return createComponent(For, {
174
156
  get each() {
175
157
  return [...state.sources];
176
158
  },
159
+ get fallback() {
160
+ return props.children;
161
+ },
177
162
  children: (x) => createMemo(() => x.children)
178
163
  });
179
164
  }
180
165
  function Source(props) {
181
166
  const state = useContext(this.ctx);
182
167
  if (!state)
183
- throw new Error("An extractor's source must be inside one of its joints");
168
+ return createMemo(() => props.fallback);
184
169
  const {
185
170
  sources
186
171
  } = state;
@@ -202,7 +187,29 @@ function Source(props) {
202
187
  sources.add(node, COMPARATOR);
203
188
  state.force();
204
189
  }));
205
- return [];
190
+ return createComponent(Show, {
191
+ get when() {
192
+ return !state.destCount;
193
+ },
194
+ get children() {
195
+ return props.fallback;
196
+ }
197
+ });
198
+ }
199
+ function SameContextSource(props) {
200
+ const _self$ = this;
201
+ const [mine, other] = splitProps(props, ["children"]);
202
+ const owner = getOwner();
203
+ return createComponent(_self$.Source, mergeProps(other, {
204
+ get children() {
205
+ return createComponent(SameContext, {
206
+ owner,
207
+ get children() {
208
+ return mine.children;
209
+ }
210
+ });
211
+ }
212
+ }));
206
213
  }
207
214
  const ctx = createContext(void 0, {
208
215
  name: "on-case"
package/dist/index.umd.js CHANGED
@@ -127,9 +127,9 @@
127
127
  });
128
128
  }
129
129
  Joint = Joint.bind(this);
130
- Fallback = Fallback.bind(this);
131
130
  Dest = Dest.bind(this);
132
131
  Source = Source.bind(this);
132
+ SameContextSource = SameContextSource.bind(this);
133
133
  /**
134
134
  * Returns the number of destinations available for the current {@link Extractor}.
135
135
  * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
@@ -149,41 +149,26 @@
149
149
  }
150
150
  });
151
151
  }
152
- function Fallback() {
153
- const _self$ = this;
154
- const f = this.getDestCount;
155
- return solidJs.createComponent(solidJs.Show, {
156
- get when() {
157
- return !f?.();
158
- },
159
- get children() {
160
- return solidJs.createComponent(_self$.Dest, {
161
- hidden: true
162
- });
163
- }
164
- });
165
- }
166
152
  function Dest(props) {
167
153
  const state = solidJs.useContext(this.ctx);
168
154
  if (!state)
169
- throw new Error("An extractor's destination must be inside one of its joints");
170
- solidJs.createRenderEffect(solidJs.on(solidJs.createMemo(() => props.hidden), (x) => {
171
- if (x)
172
- return;
173
- solidJs.onCleanup(() => state.shift(-1));
174
- state.shift(1);
175
- }));
155
+ return solidJs.createMemo(() => props.children);
156
+ solidJs.onCleanup(() => state.shift(-1));
157
+ state.shift(1);
176
158
  return solidJs.createComponent(solidJs.For, {
177
159
  get each() {
178
160
  return [...state.sources];
179
161
  },
162
+ get fallback() {
163
+ return props.children;
164
+ },
180
165
  children: (x) => solidJs.createMemo(() => x.children)
181
166
  });
182
167
  }
183
168
  function Source(props) {
184
169
  const state = solidJs.useContext(this.ctx);
185
170
  if (!state)
186
- throw new Error("An extractor's source must be inside one of its joints");
171
+ return solidJs.createMemo(() => props.fallback);
187
172
  const {
188
173
  sources
189
174
  } = state;
@@ -205,7 +190,29 @@
205
190
  sources.add(node, COMPARATOR);
206
191
  state.force();
207
192
  }));
208
- return [];
193
+ return solidJs.createComponent(solidJs.Show, {
194
+ get when() {
195
+ return !state.destCount;
196
+ },
197
+ get children() {
198
+ return props.fallback;
199
+ }
200
+ });
201
+ }
202
+ function SameContextSource(props) {
203
+ const _self$ = this;
204
+ const [mine, other] = solidJs.splitProps(props, ["children"]);
205
+ const owner = solidJs.getOwner();
206
+ return solidJs.createComponent(_self$.Source, solidJs.mergeProps(other, {
207
+ get children() {
208
+ return solidJs.createComponent(SameContext, {
209
+ owner,
210
+ get children() {
211
+ return mine.children;
212
+ }
213
+ });
214
+ }
215
+ }));
209
216
  }
210
217
  const ctx = solidJs.createContext(void 0, {
211
218
  name: "on-case"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "2.2.5",
3
+ "version": "3.0.0",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -94,7 +94,9 @@ return <>
94
94
  </e.Joint>
95
95
  </>
96
96
  ```
97
- Both `Extractor.Source` and `Extractor.Dest` will throw if they're not inside a `Extractor.Joint`.
97
+ If a `Extractor.Dest` has no `Extractor.Source` to render (Or it isn't inside a `Extractor.Joint`), it will render whatever you pass inside the `children` attribute
98
+ <br />
99
+ If a `Extractor.Source` has no available `Extractor.Dest` (Or it isn't inside a `Extractor.Joint`), it will render whatever you pass inside the `fallback` attribute
98
100
  <br />
99
101
  Componets from different `Extractor`s don't "talk" to each other and there can be more than one source and destinations:
100
102
  ```tsx
@@ -117,7 +119,7 @@ return <>
117
119
  <a.Dest />
118
120
  <b.Joint>
119
121
  {/* No source */}
120
- <b.Dest />
122
+ <b.Dest>fb1</b.Dest>
121
123
  <c.Joint>
122
124
  3
123
125
  {/* Double destination */}
@@ -126,7 +128,7 @@ return <>
126
128
  <a.Source>
127
129
  5
128
130
  </a.Source>
129
- <c.Source>
131
+ <c.Source fallback="fb2">
130
132
  {/* No destination */}
131
133
  6
132
134
  </c.Source>
@@ -145,7 +147,7 @@ return <>
145
147
  </a.Joint>
146
148
  </>
147
149
  ```
148
- The output of this code is ***1***, ***5***, 3, ***1***, ***5***, 4, 0, 2, ***8***, 7, ***8***.
150
+ The output of this code is ***1***, ***5***, ~~fb1~~, 3, ***1***, ***5***, 4, ~~fb2~~, 0, 2, ***8***, 7, ***8***.
149
151
  <br />
150
152
  You can detect the number of destinations available for a given extractor through `Extractor.getDestCount()`
151
153
  ```tsx
@@ -196,26 +198,26 @@ return <>
196
198
  </e.Joint>
197
199
  </>
198
200
  ```
199
- You can NOT do this to create a fallback destination for when there aren't any other
200
- ```tsx
201
- return <>
202
- <Show when={!someExtractor.getDestCount!()}>
203
- <someExtractor.Dest />
204
- </Show>
205
- </>
206
- ```
207
- Because it would start a strange loop due to which the new destination would disable itself. To do that you must use the `hidden` attribute, which will made sure that the destination is not considered by `Extractor.getDestCount()` at all
208
- ```tsx
209
- return <>
210
- <Show when={!someExtractor.getDestCount!()}>
211
- <someExtractor.Dest hidden />
212
- </Show>
213
- </>
214
- ```
215
- Due to this pattern being fairly common, there's the `Extractor.Fallback` component that does that exact thing
201
+ You can use `Extractor.SameContextSource` to automate the process of using a `SameContext` inside a `Extractor.Source`. When in doubt, use `Extractor.SameContextSource` instead of `Extractor.Source`
216
202
  ```tsx
203
+ const e = new Extractor(), ctx = createContext(1);
217
204
  return <>
218
- <someExtractor.Fallback />
205
+ <e.Joint>
206
+ <div>
207
+ {/* This will contain "1" and "3" */}
208
+ <e.Dest />
209
+ </div>
210
+ <ctx.Provider value={2}>
211
+ <div>
212
+ {/* This will contain "2" and "3" */}
213
+ <e.Dest />
214
+ </div>
215
+ <ctx.Provider value={3}>
216
+ <e.Source>{useContext(ctx)}</e.Source>
217
+ <e.SameContextSource>{useContext(ctx)}</e.SameContextSource>
218
+ </ctx.Provider>
219
+ </ctx.Provider>
220
+ </e.Joint>
219
221
  </>
220
222
  ```
221
223