solid-ctrl-flow 1.0.37 → 1.0.39

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 { MemoOptions } from 'solid-js';
5
6
  import { Ref } from 'solid-js';
6
7
  import { Resource } from 'solid-js';
7
8
  import { Setter } from 'solid-js';
@@ -132,6 +133,9 @@ export declare function createReactiveResource<R>(f: EffectFunction<R | undefine
132
133
  /** Built-in {@link ReactiveContext} which allows you to display different things in debug mode */
133
134
  export declare const debug: ReactiveContext<boolean>;
134
135
 
136
+ /** Handy type alias */
137
+ declare type Equals = MemoOptions<unknown>["equals"];
138
+
135
139
  /** Informations about a {@link Source} component */
136
140
  declare type Info = {
137
141
  /** Order of the current source if there are many */
@@ -140,12 +144,16 @@ declare type Info = {
140
144
  };
141
145
 
142
146
  /**
143
- * Memoizes some of the properties of {@link obj}
144
- * If {@link keys} is not provided, memoizes everything that get returned by {@link Object.keys} when provided with {@link obj}
147
+ * Memoizes some of the properties of {@link obj}.
148
+ * If {@link keys} is not provided, memoizes everything that get returned by {@link Object.keys} when provided with {@link obj}.
149
+ * The {@link equals} parameter defaults to `false` to allow the specific reactive value to have control over when its dependant effects are triggered
145
150
  * @param obj Object to partially memoize
146
151
  * @param keys The keys of the properties to memoize
152
+ * @param equals The comparator function to use on the newly created memos
147
153
  */
148
- export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[]>(obj: T, keys?: K): Pick<T, K[number]>;
154
+ export declare function memoProps<T>(obj: T, keys?: undefined, equals?: Equals): T;
155
+
156
+ export declare function memoProps<T, K extends readonly (keyof T)[]>(obj: T, keys: K, equals?: Equals): Pick<T, K[number]>;
149
157
 
150
158
  /**
151
159
  * Like a {@link For}, but instead of putting an element after another it nests them.
@@ -229,11 +237,12 @@ export declare function runWithContext<T, V extends T, R>(ctx: Context<T>, value
229
237
  export declare function runWithContext<T, R>(ctx: Context<T>, value: T | undefined, f: (x: T) => R): R;
230
238
 
231
239
  /**
232
- * Executes {@link splitProps} and memoizes the first of the two returned objects
240
+ * Executes {@link splitProps} and memoizes the first of the two returned objects using {@link memoProps}
233
241
  * @param obj Object to split and partially memoize
234
242
  * @param keys The keys of the properties to memoize and to include in the first of the two objects
243
+ * @param equals The comparator function to use on the newly created memos
235
244
  */
236
- export declare function splitAndMemoProps<T extends object, K extends readonly (keyof T)[]>(obj: T, keys: K): [Pick<T, Extract<K, readonly (keyof T)[]>[number]>, Omit<T, K[number]>];
245
+ export declare function splitAndMemoProps<T extends Record<any, any>, K extends readonly (keyof T)[]>(obj: T, keys: K, equals?: Equals): [Pick<T, Extract<K, readonly (keyof T)[]>[number]>, Omit<T, K[number]>];
237
246
 
238
247
  /**
239
248
  * Creates a full-fledged solid {@link Setter} from a normal one
@@ -260,10 +269,11 @@ export declare function toSignal<T, K extends keyof T>(obj: Accessor<T>, k: Acce
260
269
  export declare function untrackCall<F extends (...args: any[]) => unknown>(this: ThisParameterType<F>, f: F, ...args: Parameters<F>): ReturnType<F>;
261
270
 
262
271
  /**
263
- * Calls {@link f} maintaining its reactivity
264
- * @param f An {@link Accessor} to a {@link Signal}
272
+ * Calls an {@link Accessor} maintaining its reactivity
273
+ * @param f The {@link Accessor} to the value to emulate
274
+ * @returns A {@link Proxy} that copies the functionalities of the result of {@link f} calling it for each interaction, thus maintaining reactivity
265
275
  */
266
- export declare function unwrapSignal<T>(f: Accessor<Signal<T>>): Signal<T>;
276
+ export declare const unwrap: <T>(f: Accessor<T>) => T;
267
277
 
268
278
  /** Like a {@link Match} but gets the value from the closest {@link Case} ancestor */
269
279
  export declare function When(props: {
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { useContext, createMemo, createComponent, createContext, Match, mergeProps, onCleanup, For, Show, mapArray, createSignal, onMount, createRoot, createRenderEffect, on, untrack, splitProps, getOwner, createEffect, createResource } from "solid-js";
1
+ import { useContext, createMemo, createComponent, createContext, createRoot, getOwner, Match, mergeProps, onCleanup, For, Show, untrack, splitProps, createEffect, on, createResource, mapArray, createSignal, onMount, createRenderEffect } from "solid-js";
2
2
  import { createMutable } from "solid-js/store";
3
3
  const BASE_CTOR = Function;
4
4
  class ReactiveContext extends BASE_CTOR {
@@ -54,6 +54,16 @@ class ReactiveContext extends BASE_CTOR {
54
54
  return out;
55
55
  }
56
56
  }
57
+ function runWithContext(ctx2, value, f) {
58
+ return createRoot((d) => {
59
+ try {
60
+ (getOwner().context ??= {})[ctx2.id] = value;
61
+ return f(value === void 0 ? ctx2.defaultValue : value);
62
+ } finally {
63
+ d();
64
+ }
65
+ });
66
+ }
57
67
  const ctx = ReactiveContext.create(void 0, "case-when");
58
68
  const Case = ctx.Provider;
59
69
  function When(props) {
@@ -141,6 +151,42 @@ function Joint(props) {
141
151
  }
142
152
  });
143
153
  }
154
+ function untrackCall(f, ...args) {
155
+ return untrack(() => f.apply(this, args));
156
+ }
157
+ function refCall(ref, value) {
158
+ if (!ref)
159
+ return value;
160
+ if (typeof ref !== "function")
161
+ throw new ReferenceError('Il parametro "ref" deve essere stato compilato in una funzione a runtime');
162
+ ref(value);
163
+ return value;
164
+ }
165
+ function memoProps(obj, keys = Object.keys(obj), equals = false) {
166
+ const out = {};
167
+ for (const elm of keys)
168
+ Object.defineProperty(out, elm, {
169
+ enumerable: true,
170
+ get: createMemo(() => obj[elm], void 0, {
171
+ equals
172
+ })
173
+ });
174
+ return out;
175
+ }
176
+ function splitAndMemoProps(obj, keys, equals) {
177
+ const out = splitProps(obj, keys);
178
+ out[0] = memoProps(out[0], keys, equals);
179
+ return out;
180
+ }
181
+ function createReactiveResource(f) {
182
+ var refetch;
183
+ const memo = createMemo(f);
184
+ createEffect(on(memo, () => refetch?.()));
185
+ const [get] = [, {
186
+ refetch
187
+ }] = createResource(memo);
188
+ return get;
189
+ }
144
190
  function Nest(props) {
145
191
  const memo = memoProps(props, ["template", "children"]);
146
192
  const content = mapArray(() => props.each, (x, i) => {
@@ -177,53 +223,14 @@ function toSignal(obj, k) {
177
223
  function coalesceSignal([get, set], f) {
178
224
  return [() => get() ?? set(f), set];
179
225
  }
180
- function unwrapSignal(f) {
181
- return [() => f()[0](), (x) => f()[1](x)];
182
- }
183
- function untrackCall(f, ...args) {
184
- return untrack(() => f.apply(this, args));
185
- }
186
- function refCall(ref, value) {
187
- if (!ref)
188
- return value;
189
- if (typeof ref !== "function")
190
- throw new ReferenceError('Il parametro "ref" deve essere stato compilato in una funzione a runtime');
191
- ref(value);
192
- return value;
193
- }
194
- function memoProps(obj, keys = Object.keys(obj)) {
195
- const out = {};
196
- for (const elm of keys)
197
- Object.defineProperty(out, elm, {
198
- enumerable: true,
199
- get: createMemo(() => obj[elm])
200
- });
201
- return out;
202
- }
203
- function splitAndMemoProps(obj, keys) {
204
- const out = splitProps(obj, keys);
205
- out[0] = memoProps(out[0], keys);
206
- return out;
207
- }
208
- function runWithContext(ctx2, value, f) {
209
- return createRoot((d) => {
210
- try {
211
- (getOwner().context ??= {})[ctx2.id] = value;
212
- return f(value === void 0 ? ctx2.defaultValue : value);
213
- } finally {
214
- d();
215
- }
216
- });
217
- }
218
- function createReactiveResource(f) {
219
- var refetch;
220
- const memo = createMemo(f);
221
- createEffect(on(memo, () => refetch?.()));
222
- const [get] = [, {
223
- refetch
224
- }] = createResource(memo);
225
- return get;
226
- }
226
+ const unwrap = (f) => new Proxy(f, HANDLER);
227
+ const HANDLER = new Proxy({}, {
228
+ get(cache, k) {
229
+ return cache[k] ??= (t, ...args) => {
230
+ return Reflect[k](t(), ...args);
231
+ };
232
+ }
233
+ });
227
234
  const debug = ReactiveContext.create(false, "debug-scope");
228
235
  export {
229
236
  Case,
@@ -243,5 +250,5 @@ export {
243
250
  toSetter,
244
251
  toSignal,
245
252
  untrackCall,
246
- unwrapSignal
253
+ unwrap
247
254
  };
package/dist/index.umd.js CHANGED
@@ -56,6 +56,16 @@
56
56
  return out;
57
57
  }
58
58
  }
59
+ function runWithContext(ctx2, value, f) {
60
+ return solidJs.createRoot((d) => {
61
+ try {
62
+ (solidJs.getOwner().context ??= {})[ctx2.id] = value;
63
+ return f(value === void 0 ? ctx2.defaultValue : value);
64
+ } finally {
65
+ d();
66
+ }
67
+ });
68
+ }
59
69
  const ctx = ReactiveContext.create(void 0, "case-when");
60
70
  const Case = ctx.Provider;
61
71
  function When(props) {
@@ -143,6 +153,42 @@
143
153
  }
144
154
  });
145
155
  }
156
+ function untrackCall(f, ...args) {
157
+ return solidJs.untrack(() => f.apply(this, args));
158
+ }
159
+ function refCall(ref, value) {
160
+ if (!ref)
161
+ return value;
162
+ if (typeof ref !== "function")
163
+ throw new ReferenceError('Il parametro "ref" deve essere stato compilato in una funzione a runtime');
164
+ ref(value);
165
+ return value;
166
+ }
167
+ function memoProps(obj, keys = Object.keys(obj), equals = false) {
168
+ const out = {};
169
+ for (const elm of keys)
170
+ Object.defineProperty(out, elm, {
171
+ enumerable: true,
172
+ get: solidJs.createMemo(() => obj[elm], void 0, {
173
+ equals
174
+ })
175
+ });
176
+ return out;
177
+ }
178
+ function splitAndMemoProps(obj, keys, equals) {
179
+ const out = solidJs.splitProps(obj, keys);
180
+ out[0] = memoProps(out[0], keys, equals);
181
+ return out;
182
+ }
183
+ function createReactiveResource(f) {
184
+ var refetch;
185
+ const memo = solidJs.createMemo(f);
186
+ solidJs.createEffect(solidJs.on(memo, () => refetch?.()));
187
+ const [get] = [, {
188
+ refetch
189
+ }] = solidJs.createResource(memo);
190
+ return get;
191
+ }
146
192
  function Nest(props) {
147
193
  const memo = memoProps(props, ["template", "children"]);
148
194
  const content = solidJs.mapArray(() => props.each, (x, i) => {
@@ -179,53 +225,14 @@
179
225
  function coalesceSignal([get, set], f) {
180
226
  return [() => get() ?? set(f), set];
181
227
  }
182
- function unwrapSignal(f) {
183
- return [() => f()[0](), (x) => f()[1](x)];
184
- }
185
- function untrackCall(f, ...args) {
186
- return solidJs.untrack(() => f.apply(this, args));
187
- }
188
- function refCall(ref, value) {
189
- if (!ref)
190
- return value;
191
- if (typeof ref !== "function")
192
- throw new ReferenceError('Il parametro "ref" deve essere stato compilato in una funzione a runtime');
193
- ref(value);
194
- return value;
195
- }
196
- function memoProps(obj, keys = Object.keys(obj)) {
197
- const out = {};
198
- for (const elm of keys)
199
- Object.defineProperty(out, elm, {
200
- enumerable: true,
201
- get: solidJs.createMemo(() => obj[elm])
202
- });
203
- return out;
204
- }
205
- function splitAndMemoProps(obj, keys) {
206
- const out = solidJs.splitProps(obj, keys);
207
- out[0] = memoProps(out[0], keys);
208
- return out;
209
- }
210
- function runWithContext(ctx2, value, f) {
211
- return solidJs.createRoot((d) => {
212
- try {
213
- (solidJs.getOwner().context ??= {})[ctx2.id] = value;
214
- return f(value === void 0 ? ctx2.defaultValue : value);
215
- } finally {
216
- d();
217
- }
218
- });
219
- }
220
- function createReactiveResource(f) {
221
- var refetch;
222
- const memo = solidJs.createMemo(f);
223
- solidJs.createEffect(solidJs.on(memo, () => refetch?.()));
224
- const [get] = [, {
225
- refetch
226
- }] = solidJs.createResource(memo);
227
- return get;
228
- }
228
+ const unwrap = (f) => new Proxy(f, HANDLER);
229
+ const HANDLER = new Proxy({}, {
230
+ get(cache, k) {
231
+ return cache[k] ??= (t, ...args) => {
232
+ return Reflect[k](t(), ...args);
233
+ };
234
+ }
235
+ });
229
236
  const debug = ReactiveContext.create(false, "debug-scope");
230
237
  exports2.Case = Case;
231
238
  exports2.Nest = Nest;
@@ -244,6 +251,6 @@
244
251
  exports2.toSetter = toSetter;
245
252
  exports2.toSignal = toSignal;
246
253
  exports2.untrackCall = untrackCall;
247
- exports2.unwrapSignal = unwrapSignal;
254
+ exports2.unwrap = unwrap;
248
255
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
249
256
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.37",
3
+ "version": "1.0.39",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -150,9 +150,6 @@ Functions that create bindings between `Signal`s
150
150
  >
151
151
  > #### `coalesceSignal()`
152
152
  > Creates a non nullable `Signal` from a nullable one
153
- >
154
- > #### `unwrapSignal()`
155
- > Calls an `Accessor` to a `Signal` maintaining its reactivity
156
153
 
157
154
  ### `ReactiveContext.create()`
158
155
  Method that creates a reactive version of a solid `Context` with some additional built-in functionalities
@@ -163,6 +160,12 @@ const valueAccessorForTheCurrentOwner = ctx.read;
163
160
  const value = ctx();
164
161
  ```
165
162
 
163
+ ### `runWithContext()`
164
+ Creates a context scope that persists for the duration of a function
165
+
166
+ ### `unwrap()`
167
+ Calls an `Accessor` maintaining its reactivity
168
+
166
169
  ### `debug()`
167
170
  Allows you to define ui section that are only visible in debug or NOT in debug
168
171
  ```tsx
@@ -189,8 +192,5 @@ Creates a partial version of an object with memoized remaining properties
189
192
  ### `splitAndMemoProps()`
190
193
  Like `splitProps()` but memoizes the whole local part
191
194
 
192
- ### `runWithContext()`
193
- Creates a context scope that persists for the duration of a function
194
-
195
195
  ### `createReactiveResource()`
196
196
  Like `createResource()` but the provided function will be reactive