@solid-primitives/utils 4.0.1 → 5.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.cjs CHANGED
@@ -35,9 +35,31 @@ exports.asAccessor = (v) => typeof v === "function" ? v : () => v;
35
35
  function accessWith(valueOrFn, ...args) {
36
36
  return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
37
37
  }
38
+ function defer(deps, fn, initialValue) {
39
+ const isArray = Array.isArray(deps);
40
+ let prevInput;
41
+ let shouldDefer = true;
42
+ return (prevValue) => {
43
+ let input;
44
+ if (isArray) {
45
+ input = Array(deps.length);
46
+ for (let i = 0; i < deps.length; i++)
47
+ input[i] = deps[i]();
48
+ } else
49
+ input = deps();
50
+ if (shouldDefer) {
51
+ shouldDefer = false;
52
+ prevInput = input;
53
+ return initialValue;
54
+ }
55
+ const result = solidJs.untrack(() => fn(input, prevInput, prevValue));
56
+ prevInput = input;
57
+ return result;
58
+ };
59
+ }
38
60
  exports.entries = Object.entries;
39
61
  exports.keys = Object.keys;
40
- exports.onRootCleanup = (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn;
62
+ exports.tryOnCleanup = (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn;
41
63
  exports.createCallbackStack = () => {
42
64
  let stack = [];
43
65
  const clear = () => stack = [];
@@ -58,18 +80,6 @@ function createMicrotask(fn) {
58
80
  queueMicrotask(() => --calls === 0 && fn(...args));
59
81
  };
60
82
  }
61
- function createProxy(traps) {
62
- return new Proxy(
63
- {},
64
- {
65
- get: (_, k) => traps.get(k),
66
- set: (_, k, v) => {
67
- traps.set?.(k, v);
68
- return false;
69
- }
70
- }
71
- );
72
- }
73
83
  function createStaticStore(init) {
74
84
  const copy = { ...init };
75
85
  const store = {};
@@ -143,12 +153,11 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
143
153
  handleAdded(currEl);
144
154
  }
145
155
  }
146
- exports.forEachEntry = (obj, fn) => Object.entries(obj).forEach(([key, value]) => fn(key, value));
147
156
 
148
157
  exports.accessWith = accessWith;
149
158
  exports.chain = chain;
150
159
  exports.createMicrotask = createMicrotask;
151
- exports.createProxy = createProxy;
152
160
  exports.createStaticStore = createStaticStore;
161
+ exports.defer = defer;
153
162
  exports.handleDiffArray = handleDiffArray;
154
163
  exports.isObject = isObject;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Accessor, Setter, onCleanup } from 'solid-js';
1
+ import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup } from 'solid-js';
2
+ export { EffectOptions, MemoOptions, OnOptions, SignalOptions } from 'solid-js/types/reactive/signal';
2
3
 
3
4
  /**
4
5
  * Can be single or in an array
@@ -12,7 +13,7 @@ type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
12
13
  */
13
14
  type ItemsOf<T> = T extends (infer E)[] ? E : never;
14
15
  type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
15
- type SetterValue<T> = Parameters<Setter<T>>[0];
16
+ type SetterParam<T> = Parameters<Setter<T>>[0];
16
17
  /**
17
18
  * T or a reactive/non-reactive function returning T
18
19
  */
@@ -77,6 +78,9 @@ type Position = {
77
78
  x: number;
78
79
  y: number;
79
80
  };
81
+ type Narrow<T> = (T extends [] ? [] : never) | (T extends string | number | bigint | boolean ? T : never) | {
82
+ [K in keyof T]: T[K] extends Function ? T[K] : Narrow<T[K]>;
83
+ };
80
84
 
81
85
  /** no operation */
82
86
  declare const noop: Noop;
@@ -113,7 +117,7 @@ declare const clamp: (n: number, min: number, max: number) => number;
113
117
  * ```
114
118
  */
115
119
  declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
116
- declare const asArray: <T>(value: T) => T extends any[] ? T : T[];
120
+ declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : T)[];
117
121
  /**
118
122
  * Access an array of MaybeAccessors
119
123
  * @example
@@ -130,6 +134,15 @@ declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue
130
134
  declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
131
135
  /** If value is a function – call it with a given arguments – otherwise get the value as is */
132
136
  declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
137
+ /**
138
+ * Solid's `on` helper, but always defers and returns a provided initial value when if does instead of `undefined`.
139
+ *
140
+ * @param deps
141
+ * @param fn
142
+ * @param initialValue
143
+ */
144
+ declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue: Next): EffectFunction<undefined | NoInfer<Next>, NoInfer<Next>>;
145
+ declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue?: undefined): EffectFunction<undefined | NoInfer<Next>>;
133
146
  /**
134
147
  * Get entries of an object
135
148
  */
@@ -141,7 +154,7 @@ declare const keys: <T extends object>(object: T) => (keyof T)[];
141
154
  /**
142
155
  * Solid's `onCleanup` that is registered only if there is a root.
143
156
  */
144
- declare const onRootCleanup: typeof onCleanup;
157
+ declare const tryOnCleanup: typeof onCleanup;
145
158
  declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
146
159
  push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
147
160
  execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
@@ -153,19 +166,10 @@ declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>(
153
166
  * @returns `fn`
154
167
  */
155
168
  declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
156
- /** WIP: an easier to setup and type Proxy */
157
- declare function createProxy<T extends Record<string | symbol, any>>(traps: {
158
- get: <K extends keyof T>(key: K) => T[K];
159
- set: <K extends keyof T>(key: K, value: T[K]) => void;
160
- }): T;
161
- declare function createProxy<T extends Record<string | symbol, any>>(traps: {
162
- get: <K extends keyof T>(key: K) => T[K];
163
- set?: undefined;
164
- }): Readonly<T>;
165
169
  type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
166
170
  (setter: (prev: T) => Partial<T>): T;
167
171
  (state: Partial<T>): T;
168
- <K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
172
+ <K extends keyof T>(key: K, state: SetterParam<T[K]>): T;
169
173
  };
170
174
  /**
171
175
  * A shallowly wrapped reactive store object. It behaves similarly to the createStore, but with limited features to keep it simple. Designed to be used for reactive objects with static keys, but dynamic values, like reactive Event State, location, etc.
@@ -185,6 +189,5 @@ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [acc
185
189
  * @param handleRemoved called once for every removed from array
186
190
  */
187
191
  declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
188
- declare const forEachEntry: <T>(obj: Record<string, T>, fn: (key: string, value: T) => void) => void;
189
192
 
190
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterValue, Simplify, StaticStoreSetter, Tail, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, entries, forEachEntry, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, onRootCleanup, withAccess };
193
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Narrow, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterParam, Simplify, StaticStoreSetter, Tail, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createStaticStore, defer, entries, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, tryOnCleanup, withAccess };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DEV, getOwner, onCleanup, createSignal, untrack, batch } from 'solid-js';
1
+ import { DEV, untrack, getOwner, onCleanup, createSignal, batch } from 'solid-js';
2
2
  import { isServer as isServer$1 } from 'solid-js/web';
3
3
 
4
4
  // src/index.ts
@@ -33,9 +33,31 @@ var asAccessor = (v) => typeof v === "function" ? v : () => v;
33
33
  function accessWith(valueOrFn, ...args) {
34
34
  return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
35
35
  }
36
+ function defer(deps, fn, initialValue) {
37
+ const isArray = Array.isArray(deps);
38
+ let prevInput;
39
+ let shouldDefer = true;
40
+ return (prevValue) => {
41
+ let input;
42
+ if (isArray) {
43
+ input = Array(deps.length);
44
+ for (let i = 0; i < deps.length; i++)
45
+ input[i] = deps[i]();
46
+ } else
47
+ input = deps();
48
+ if (shouldDefer) {
49
+ shouldDefer = false;
50
+ prevInput = input;
51
+ return initialValue;
52
+ }
53
+ const result = untrack(() => fn(input, prevInput, prevValue));
54
+ prevInput = input;
55
+ return result;
56
+ };
57
+ }
36
58
  var entries = Object.entries;
37
59
  var keys = Object.keys;
38
- var onRootCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
60
+ var tryOnCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
39
61
  var createCallbackStack = () => {
40
62
  let stack = [];
41
63
  const clear = () => stack = [];
@@ -56,18 +78,6 @@ function createMicrotask(fn) {
56
78
  queueMicrotask(() => --calls === 0 && fn(...args));
57
79
  };
58
80
  }
59
- function createProxy(traps) {
60
- return new Proxy(
61
- {},
62
- {
63
- get: (_, k) => traps.get(k),
64
- set: (_, k, v) => {
65
- traps.set?.(k, v);
66
- return false;
67
- }
68
- }
69
- );
70
- }
71
81
  function createStaticStore(init) {
72
82
  const copy = { ...init };
73
83
  const store = {};
@@ -141,6 +151,5 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
141
151
  handleAdded(currEl);
142
152
  }
143
153
  }
144
- var forEachEntry = (obj, fn) => Object.entries(obj).forEach(([key, value]) => fn(key, value));
145
154
 
146
- export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, entries, forEachEntry, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, onRootCleanup, withAccess };
155
+ export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createStaticStore, defer, entries, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, tryOnCleanup, withAccess };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "4.0.1",
3
+ "version": "5.1.0",
4
4
  "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
5
  "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
6
  "license": "MIT",