solid-ctrl-flow 5.1.1 → 6.1.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
@@ -6,12 +6,6 @@ import { Owner } from 'solid-js';
6
6
  import { ParentProps } from 'solid-js';
7
7
  import { splitProps } from 'solid-js';
8
8
 
9
- /** Minimal representation of an event target */
10
- declare type AbstractEventTarget<K, F> = {
11
- addEventListener(k: K, f: F): void;
12
- removeEventListener(k: K, f: F): void;
13
- };
14
-
15
9
  /** Like a {@link Match} but gets the value from the closest {@link On} ancestor */
16
10
  export declare function Case(props: ParentProps<{
17
11
  value: unknown;
@@ -61,17 +55,23 @@ export declare class Extractor {
61
55
  constructor(name?: string);
62
56
  readonly ctx: Context<State | undefined>;
63
57
  Joint: (props: {
64
- children?: JSX.Element;
65
- }) => JSX.Element;
58
+ children?: JSX;
59
+ }) => JSX;
66
60
  Dest: (props: {
67
- children?: JSX.Element;
68
- }) => JSX.Element;
69
- Source: (props: Info) => JSX.Element;
61
+ children?: JSX;
62
+ }) => JSX;
63
+ Source: (props: Info) => JSX;
70
64
  SameContextSource: (props: {
71
65
  order?: number | undefined;
72
66
  } & {
73
- children?: JSX.Element;
74
- }) => JSX.Element;
67
+ children?: JSX;
68
+ }) => JSX;
69
+ /**
70
+ * Returns the number of sources available for the current {@link Extractor}.
71
+ * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
72
+ * If the current {@link Owner} is NOT under a {@link Joint}, it will return `undefined`
73
+ */
74
+ get getSourceCount(): (() => number) | undefined;
75
75
  /**
76
76
  * Returns the number of destinations available for the current {@link Extractor}.
77
77
  * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
@@ -157,16 +157,6 @@ export declare class OrderedLinkedListNode<T> extends OrderedLinkedList<T> {
157
157
  remove(): void;
158
158
  }
159
159
 
160
- /**
161
- * Adds the {@link f} event listener to the {@link k} event of the {@link obj} emitter and removes it when the current reactive context is disposed
162
- * @param obj The emitter to which to add the event listener
163
- * @param k The key of the event for which to add the listener
164
- * @param f The function to call when the event is emitted
165
- */
166
- export declare function registerEventListener<K extends keyof HTMLElementEventMap>(obj: HTMLElement, type: K, listener: (this: HTMLElement, e: HTMLElementEventMap[K]) => any): void;
167
-
168
- export declare function registerEventListener<K, F>(obj: AbstractEventTarget<K, F>, k: K, f: F): void;
169
-
170
160
  /**
171
161
  * Executes {@link f} with the provided value for the specified {@link Context}.
172
162
  * You can pass `undefined` to {@link value} in order to get back the default value for {@link ctx}.
@@ -209,9 +199,12 @@ declare type Standard<T> = ParentProps<{
209
199
  declare class State {
210
200
  #private;
211
201
  get sources(): OrderedLinkedList<Info>;
202
+ get sourceCount(): number;
212
203
  get destCount(): number;
213
- shift(k: number): void;
214
204
  force(): void;
205
+ shiftSource(k: number): void;
206
+ shiftDest(k: number): void;
207
+ shiftSourceAndForce(k: number): void;
215
208
  }
216
209
 
217
210
  /** Type of an element wrapping call-back which accepts an additional parameter */
package/dist/index.mjs CHANGED
@@ -1,12 +1,8 @@
1
- import { untrack, onCleanup, createRoot, getOwner, createMemo, Show, createContext, useContext, For, createRenderEffect, on, splitProps, createSignal, createSelector, equalFn, Match, runWithOwner } from "solid-js";
1
+ import { untrack, createRoot, getOwner, createMemo, Show, createContext, useContext, onCleanup, For, createRenderEffect, on, splitProps, createSignal, batch, createSelector, equalFn, Match, runWithOwner } from "solid-js";
2
2
  import { createComponent, memo, mergeProps, spread } from "solid-js/web";
3
3
  function untrackCall(f, ...args) {
4
4
  return untrack(() => f.apply(this, args));
5
5
  }
6
- function registerEventListener(obj, k, f) {
7
- obj.addEventListener(k, f);
8
- onCleanup(() => obj.removeEventListener(k, f));
9
- }
10
6
  function runWithContext(ctx2, value, f) {
11
7
  return createRoot((d) => {
12
8
  try {
@@ -89,24 +85,37 @@ const COMPARATOR = (a, b) => (a.order ?? 0) - (b.order ?? 0);
89
85
  const SOURCES_OPTS = {
90
86
  internal: true,
91
87
  equals: false
92
- }, DEST_COUNT_OPTS = {
88
+ }, COUNT_OPTS = {
93
89
  internal: true
94
90
  };
95
91
  class State {
96
92
  #sources = createSignal(new OrderedLinkedList(), SOURCES_OPTS);
97
- #destCount = createSignal(0, DEST_COUNT_OPTS);
93
+ #sourceCount = createSignal(0, COUNT_OPTS);
94
+ #destCount = createSignal(0, COUNT_OPTS);
98
95
  get sources() {
99
96
  return this.#sources[0]();
100
97
  }
98
+ get sourceCount() {
99
+ return this.#sourceCount[0]();
100
+ }
101
101
  get destCount() {
102
102
  return this.#destCount[0]();
103
103
  }
104
- shift(k) {
105
- this.#destCount[1]((v) => v + k);
106
- }
107
104
  force() {
108
105
  this.#sources[1]((v) => v);
109
106
  }
107
+ shiftSource(k) {
108
+ this.#sourceCount[1]((v) => v + k);
109
+ }
110
+ shiftDest(k) {
111
+ this.#destCount[1]((v) => v + k);
112
+ }
113
+ shiftSourceAndForce(k) {
114
+ batch(() => {
115
+ this.shiftSource(k);
116
+ this.force();
117
+ });
118
+ }
110
119
  }
111
120
  class Extractor {
112
121
  constructor(name) {
@@ -119,6 +128,15 @@ class Extractor {
119
128
  Dest = Dest.bind(this);
120
129
  Source = Source.bind(this);
121
130
  SameContextSource = SameContextSource.bind(this);
131
+ /**
132
+ * Returns the number of sources available for the current {@link Extractor}.
133
+ * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
134
+ * If the current {@link Owner} is NOT under a {@link Joint}, it will return `undefined`
135
+ */
136
+ get getSourceCount() {
137
+ const state = useContext(this.ctx);
138
+ return state && (() => state.sourceCount);
139
+ }
122
140
  /**
123
141
  * Returns the number of destinations available for the current {@link Extractor}.
124
142
  * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
@@ -142,8 +160,8 @@ function Dest(props) {
142
160
  const state = useContext(this.ctx);
143
161
  if (!state)
144
162
  return memo(() => props.children);
145
- onCleanup(() => state.shift(-1));
146
- state.shift(1);
163
+ onCleanup(() => state.shiftDest(-1));
164
+ state.shiftDest(1);
147
165
  return createComponent(For, {
148
166
  get each() {
149
167
  return [...state.sources];
@@ -174,10 +192,10 @@ function Source(props) {
174
192
  createRenderEffect(on(order, () => {
175
193
  onCleanup(() => {
176
194
  node.remove();
177
- state.force();
195
+ state.shiftSourceAndForce(-1);
178
196
  });
179
197
  sources.add(node, COMPARATOR);
180
- state.force();
198
+ state.shiftSourceAndForce(1);
181
199
  }));
182
200
  }
183
201
  function SameContextSource(props) {
@@ -274,7 +292,6 @@ export {
274
292
  SameContext,
275
293
  Spread,
276
294
  memoProps,
277
- registerEventListener,
278
295
  runWithContext,
279
296
  splitAndMemoProps,
280
297
  untrackCall,
package/dist/index.umd.js CHANGED
@@ -5,10 +5,6 @@
5
5
  function untrackCall(f, ...args) {
6
6
  return solidJs.untrack(() => f.apply(this, args));
7
7
  }
8
- function registerEventListener(obj, k, f) {
9
- obj.addEventListener(k, f);
10
- solidJs.onCleanup(() => obj.removeEventListener(k, f));
11
- }
12
8
  function runWithContext(ctx2, value, f) {
13
9
  return solidJs.createRoot((d) => {
14
10
  try {
@@ -91,24 +87,37 @@
91
87
  const SOURCES_OPTS = {
92
88
  internal: true,
93
89
  equals: false
94
- }, DEST_COUNT_OPTS = {
90
+ }, COUNT_OPTS = {
95
91
  internal: true
96
92
  };
97
93
  class State {
98
94
  #sources = solidJs.createSignal(new OrderedLinkedList(), SOURCES_OPTS);
99
- #destCount = solidJs.createSignal(0, DEST_COUNT_OPTS);
95
+ #sourceCount = solidJs.createSignal(0, COUNT_OPTS);
96
+ #destCount = solidJs.createSignal(0, COUNT_OPTS);
100
97
  get sources() {
101
98
  return this.#sources[0]();
102
99
  }
100
+ get sourceCount() {
101
+ return this.#sourceCount[0]();
102
+ }
103
103
  get destCount() {
104
104
  return this.#destCount[0]();
105
105
  }
106
- shift(k) {
107
- this.#destCount[1]((v) => v + k);
108
- }
109
106
  force() {
110
107
  this.#sources[1]((v) => v);
111
108
  }
109
+ shiftSource(k) {
110
+ this.#sourceCount[1]((v) => v + k);
111
+ }
112
+ shiftDest(k) {
113
+ this.#destCount[1]((v) => v + k);
114
+ }
115
+ shiftSourceAndForce(k) {
116
+ solidJs.batch(() => {
117
+ this.shiftSource(k);
118
+ this.force();
119
+ });
120
+ }
112
121
  }
113
122
  class Extractor {
114
123
  constructor(name) {
@@ -121,6 +130,15 @@
121
130
  Dest = Dest.bind(this);
122
131
  Source = Source.bind(this);
123
132
  SameContextSource = SameContextSource.bind(this);
133
+ /**
134
+ * Returns the number of sources available for the current {@link Extractor}.
135
+ * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
136
+ * If the current {@link Owner} is NOT under a {@link Joint}, it will return `undefined`
137
+ */
138
+ get getSourceCount() {
139
+ const state = solidJs.useContext(this.ctx);
140
+ return state && (() => state.sourceCount);
141
+ }
124
142
  /**
125
143
  * Returns the number of destinations available for the current {@link Extractor}.
126
144
  * It's a getter that returns a function that will be bound to the {@link Owner} in which the getter was called.
@@ -144,8 +162,8 @@
144
162
  const state = solidJs.useContext(this.ctx);
145
163
  if (!state)
146
164
  return web.memo(() => props.children);
147
- solidJs.onCleanup(() => state.shift(-1));
148
- state.shift(1);
165
+ solidJs.onCleanup(() => state.shiftDest(-1));
166
+ state.shiftDest(1);
149
167
  return web.createComponent(solidJs.For, {
150
168
  get each() {
151
169
  return [...state.sources];
@@ -176,10 +194,10 @@
176
194
  solidJs.createRenderEffect(solidJs.on(order, () => {
177
195
  solidJs.onCleanup(() => {
178
196
  node.remove();
179
- state.force();
197
+ state.shiftSourceAndForce(-1);
180
198
  });
181
199
  sources.add(node, COMPARATOR);
182
- state.force();
200
+ state.shiftSourceAndForce(1);
183
201
  }));
184
202
  }
185
203
  function SameContextSource(props) {
@@ -275,7 +293,6 @@
275
293
  exports2.SameContext = SameContext;
276
294
  exports2.Spread = Spread;
277
295
  exports2.memoProps = memoProps;
278
- exports2.registerEventListener = registerEventListener;
279
296
  exports2.runWithContext = runWithContext;
280
297
  exports2.splitAndMemoProps = splitAndMemoProps;
281
298
  exports2.untrackCall = untrackCall;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "5.1.1",
3
+ "version": "6.1.0",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -196,6 +196,8 @@ return <>
196
196
  </e.Joint>
197
197
  </>
198
198
  ```
199
+ > Same applies to `Extractor.getSourceCount()`
200
+
199
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`
200
202
  ```tsx
201
203
  const e = new Extractor(), ctx = createContext(1);
@@ -233,8 +235,5 @@ Like `splitProps()` but passes the every part except the last one to [`memoProps
233
235
  ### `untrackCall()`
234
236
  Calls a function untracking what happens inside of it but not what gets passed as its argument
235
237
 
236
- ### `registerEventListener()`
237
- Adds an event listener and registers its removal
238
-
239
238
  ### `runWithContext()`
240
239
  Creates a context scope that persists for the duration of a function