@solid-primitives/utils 4.0.1 → 5.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.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
  */
@@ -113,7 +114,7 @@ declare const clamp: (n: number, min: number, max: number) => number;
113
114
  * ```
114
115
  */
115
116
  declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
116
- declare const asArray: <T>(value: T) => T extends any[] ? T : T[];
117
+ declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : T)[];
117
118
  /**
118
119
  * Access an array of MaybeAccessors
119
120
  * @example
@@ -130,6 +131,15 @@ declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue
130
131
  declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
131
132
  /** If value is a function – call it with a given arguments – otherwise get the value as is */
132
133
  declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
134
+ /**
135
+ * Solid's `on` helper, but always defers and returns a provided initial value when if does instead of `undefined`.
136
+ *
137
+ * @param deps
138
+ * @param fn
139
+ * @param initialValue
140
+ */
141
+ 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>>;
142
+ 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
143
  /**
134
144
  * Get entries of an object
135
145
  */
@@ -141,7 +151,7 @@ declare const keys: <T extends object>(object: T) => (keyof T)[];
141
151
  /**
142
152
  * Solid's `onCleanup` that is registered only if there is a root.
143
153
  */
144
- declare const onRootCleanup: typeof onCleanup;
154
+ declare const tryOnCleanup: typeof onCleanup;
145
155
  declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
146
156
  push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
147
157
  execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
@@ -153,19 +163,10 @@ declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>(
153
163
  * @returns `fn`
154
164
  */
155
165
  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
166
  type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
166
167
  (setter: (prev: T) => Partial<T>): T;
167
168
  (state: Partial<T>): T;
168
- <K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
169
+ <K extends keyof T>(key: K, state: SetterParam<T[K]>): T;
169
170
  };
170
171
  /**
171
172
  * 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 +186,5 @@ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [acc
185
186
  * @param handleRemoved called once for every removed from array
186
187
  */
187
188
  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
189
 
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 };
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, 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.0.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",