@solid-primitives/utils 5.1.1 → 5.2.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
@@ -3,20 +3,18 @@
3
3
  var solidJs = require('solid-js');
4
4
  var web = require('solid-js/web');
5
5
 
6
- var __defProp = Object.defineProperty;
7
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
- exports.noop = /* @__PURE__ */ __name(() => void 0, "noop");
6
+ // src/index.ts
7
+ exports.noop = () => void 0;
9
8
  exports.isServer = web.isServer;
10
9
  exports.isClient = !exports.isServer;
11
10
  exports.isDev = solidJs.DEV && exports.isClient;
12
11
  exports.isProd = !exports.isDev;
13
- exports.ofClass = /* @__PURE__ */ __name((v, c) => v instanceof c || v && v.constructor === c, "ofClass");
12
+ exports.ofClass = (v, c) => v instanceof c || v && v.constructor === c;
14
13
  function isObject(value) {
15
14
  return value !== null && (typeof value === "object" || typeof value === "function");
16
15
  }
17
- __name(isObject, "isObject");
18
- exports.compare = /* @__PURE__ */ __name((a, b) => a < b ? -1 : a > b ? 1 : 0, "compare");
19
- exports.arrayEquals = /* @__PURE__ */ __name((a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]), "arrayEquals");
16
+ exports.compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
17
+ exports.arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
20
18
  function chain(callbacks) {
21
19
  return (...args) => {
22
20
  for (const callback of callbacks) {
@@ -25,20 +23,18 @@ function chain(callbacks) {
25
23
  }
26
24
  };
27
25
  }
28
- __name(chain, "chain");
29
- exports.clamp = /* @__PURE__ */ __name((n, min, max) => Math.min(Math.max(n, min), max), "clamp");
30
- exports.access = /* @__PURE__ */ __name((v) => typeof v === "function" && !v.length ? v() : v, "access");
31
- exports.asArray = /* @__PURE__ */ __name((value) => Array.isArray(value) ? value : [value], "asArray");
32
- exports.accessArray = /* @__PURE__ */ __name((list) => list.map((v) => exports.access(v)), "accessArray");
33
- exports.withAccess = /* @__PURE__ */ __name((value, fn) => {
26
+ exports.clamp = (n, min, max) => Math.min(Math.max(n, min), max);
27
+ exports.access = (v) => typeof v === "function" && !v.length ? v() : v;
28
+ exports.asArray = (value) => Array.isArray(value) ? value : [value];
29
+ exports.accessArray = (list) => list.map((v) => exports.access(v));
30
+ exports.withAccess = (value, fn) => {
34
31
  const _value = exports.access(value);
35
32
  typeof _value !== "undefined" && _value !== null && fn(_value);
36
- }, "withAccess");
37
- exports.asAccessor = /* @__PURE__ */ __name((v) => typeof v === "function" ? v : () => v, "asAccessor");
33
+ };
34
+ exports.asAccessor = (v) => typeof v === "function" ? v : () => v;
38
35
  function accessWith(valueOrFn, ...args) {
39
36
  return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
40
37
  }
41
- __name(accessWith, "accessWith");
42
38
  function defer(deps, fn, initialValue) {
43
39
  const isArray = Array.isArray(deps);
44
40
  let prevInput;
@@ -61,13 +57,12 @@ function defer(deps, fn, initialValue) {
61
57
  return result;
62
58
  };
63
59
  }
64
- __name(defer, "defer");
65
60
  exports.entries = Object.entries;
66
61
  exports.keys = Object.keys;
67
- exports.tryOnCleanup = /* @__PURE__ */ __name((fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn, "tryOnCleanup");
68
- exports.createCallbackStack = /* @__PURE__ */ __name(() => {
62
+ exports.tryOnCleanup = (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn;
63
+ exports.createCallbackStack = () => {
69
64
  let stack = [];
70
- const clear = /* @__PURE__ */ __name(() => stack = [], "clear");
65
+ const clear = () => stack = [];
71
66
  return {
72
67
  push: (...callbacks) => stack.push(...callbacks),
73
68
  execute(arg0, arg1, arg2, arg3) {
@@ -76,7 +71,7 @@ exports.createCallbackStack = /* @__PURE__ */ __name(() => {
76
71
  },
77
72
  clear
78
73
  };
79
- }, "createCallbackStack");
74
+ };
80
75
  function createMicrotask(fn) {
81
76
  let calls = 0;
82
77
  let args;
@@ -85,12 +80,11 @@ function createMicrotask(fn) {
85
80
  queueMicrotask(() => --calls === 0 && fn(...args));
86
81
  };
87
82
  }
88
- __name(createMicrotask, "createMicrotask");
89
83
  function createStaticStore(init) {
90
84
  const copy = { ...init };
91
85
  const store = {};
92
86
  const cache = /* @__PURE__ */ new Map();
93
- const getValue = /* @__PURE__ */ __name((key) => {
87
+ const getValue = (key) => {
94
88
  const saved = cache.get(key);
95
89
  if (saved)
96
90
  return saved[0]();
@@ -100,21 +94,21 @@ function createStaticStore(init) {
100
94
  cache.set(key, signal);
101
95
  delete copy[key];
102
96
  return signal[0]();
103
- }, "getValue");
104
- const setValue = /* @__PURE__ */ __name((key, value) => {
97
+ };
98
+ const setValue = (key, value) => {
105
99
  const saved = cache.get(key);
106
100
  if (saved)
107
101
  return saved[1](value);
108
102
  if (key in copy)
109
103
  copy[key] = accessWith(value, [copy[key]]);
110
- }, "setValue");
104
+ };
111
105
  for (const key of exports.keys(init)) {
112
106
  store[key] = void 0;
113
107
  Object.defineProperty(store, key, {
114
108
  get: getValue.bind(void 0, key)
115
109
  });
116
110
  }
117
- const setter = /* @__PURE__ */ __name((a, b) => {
111
+ const setter = (a, b) => {
118
112
  if (isObject(a))
119
113
  solidJs.untrack(() => {
120
114
  solidJs.batch(() => {
@@ -125,10 +119,9 @@ function createStaticStore(init) {
125
119
  else
126
120
  setValue(a, b);
127
121
  return store;
128
- }, "setter");
122
+ };
129
123
  return [store, setter];
130
124
  }
131
- __name(createStaticStore, "createStaticStore");
132
125
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
133
126
  const currLength = current.length;
134
127
  const prevLength = prev.length;
@@ -160,10 +153,19 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
160
153
  handleAdded(currEl);
161
154
  }
162
155
  }
163
- __name(handleDiffArray, "handleDiffArray");
156
+ function createHydrateSignal(serverValue, update, options) {
157
+ if (exports.isServer) {
158
+ return solidJs.createSignal(serverValue);
159
+ }
160
+ let init = !!solidJs.sharedConfig.context;
161
+ const [state, setState] = solidJs.createSignal(init ? serverValue : update(), options);
162
+ init && solidJs.onMount(() => setState(() => update()));
163
+ return [state, setState];
164
+ }
164
165
 
165
166
  exports.accessWith = accessWith;
166
167
  exports.chain = chain;
168
+ exports.createHydrateSignal = createHydrateSignal;
167
169
  exports.createMicrotask = createMicrotask;
168
170
  exports.createStaticStore = createStaticStore;
169
171
  exports.defer = defer;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup } from 'solid-js';
1
+ import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, Signal } from 'solid-js';
2
2
  export { EffectOptions, OnOptions } from 'solid-js/types/reactive/signal';
3
3
 
4
4
  /**
@@ -190,5 +190,17 @@ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [acc
190
190
  * @param handleRemoved called once for every removed from array
191
191
  */
192
192
  declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
193
+ /**
194
+ * A signal object that handle hydration.
195
+ * @param serverValue initial value of the state on the server
196
+ * @param update called once on the client or on hydration to initialise the value
197
+ * @param options {@link SignalOptions}
198
+ * @returns
199
+ * ```ts
200
+ * [state: Accessor<T>, setState: Setter<T>]
201
+ * ```
202
+ * @see {@link createSignal}
203
+ */
204
+ declare function createHydrateSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): Signal<T>;
193
205
 
194
- 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 };
206
+ 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, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, tryOnCleanup, withAccess };
package/dist/index.js CHANGED
@@ -1,20 +1,18 @@
1
- import { DEV, untrack, getOwner, onCleanup, createSignal, batch } from 'solid-js';
1
+ import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount, batch } from 'solid-js';
2
2
  import { isServer as isServer$1 } from 'solid-js/web';
3
3
 
4
- var __defProp = Object.defineProperty;
5
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
- var noop = /* @__PURE__ */ __name(() => void 0, "noop");
4
+ // src/index.ts
5
+ var noop = () => void 0;
7
6
  var isServer = isServer$1;
8
7
  var isClient = !isServer;
9
8
  var isDev = DEV && isClient;
10
9
  var isProd = !isDev;
11
- var ofClass = /* @__PURE__ */ __name((v, c) => v instanceof c || v && v.constructor === c, "ofClass");
10
+ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
12
11
  function isObject(value) {
13
12
  return value !== null && (typeof value === "object" || typeof value === "function");
14
13
  }
15
- __name(isObject, "isObject");
16
- var compare = /* @__PURE__ */ __name((a, b) => a < b ? -1 : a > b ? 1 : 0, "compare");
17
- var arrayEquals = /* @__PURE__ */ __name((a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]), "arrayEquals");
14
+ var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
15
+ var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
18
16
  function chain(callbacks) {
19
17
  return (...args) => {
20
18
  for (const callback of callbacks) {
@@ -23,20 +21,18 @@ function chain(callbacks) {
23
21
  }
24
22
  };
25
23
  }
26
- __name(chain, "chain");
27
- var clamp = /* @__PURE__ */ __name((n, min, max) => Math.min(Math.max(n, min), max), "clamp");
28
- var access = /* @__PURE__ */ __name((v) => typeof v === "function" && !v.length ? v() : v, "access");
29
- var asArray = /* @__PURE__ */ __name((value) => Array.isArray(value) ? value : [value], "asArray");
30
- var accessArray = /* @__PURE__ */ __name((list) => list.map((v) => access(v)), "accessArray");
31
- var withAccess = /* @__PURE__ */ __name((value, fn) => {
24
+ var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
25
+ var access = (v) => typeof v === "function" && !v.length ? v() : v;
26
+ var asArray = (value) => Array.isArray(value) ? value : [value];
27
+ var accessArray = (list) => list.map((v) => access(v));
28
+ var withAccess = (value, fn) => {
32
29
  const _value = access(value);
33
30
  typeof _value !== "undefined" && _value !== null && fn(_value);
34
- }, "withAccess");
35
- var asAccessor = /* @__PURE__ */ __name((v) => typeof v === "function" ? v : () => v, "asAccessor");
31
+ };
32
+ var asAccessor = (v) => typeof v === "function" ? v : () => v;
36
33
  function accessWith(valueOrFn, ...args) {
37
34
  return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
38
35
  }
39
- __name(accessWith, "accessWith");
40
36
  function defer(deps, fn, initialValue) {
41
37
  const isArray = Array.isArray(deps);
42
38
  let prevInput;
@@ -59,13 +55,12 @@ function defer(deps, fn, initialValue) {
59
55
  return result;
60
56
  };
61
57
  }
62
- __name(defer, "defer");
63
58
  var entries = Object.entries;
64
59
  var keys = Object.keys;
65
- var tryOnCleanup = /* @__PURE__ */ __name((fn) => getOwner() ? onCleanup(fn) : fn, "tryOnCleanup");
66
- var createCallbackStack = /* @__PURE__ */ __name(() => {
60
+ var tryOnCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
61
+ var createCallbackStack = () => {
67
62
  let stack = [];
68
- const clear = /* @__PURE__ */ __name(() => stack = [], "clear");
63
+ const clear = () => stack = [];
69
64
  return {
70
65
  push: (...callbacks) => stack.push(...callbacks),
71
66
  execute(arg0, arg1, arg2, arg3) {
@@ -74,7 +69,7 @@ var createCallbackStack = /* @__PURE__ */ __name(() => {
74
69
  },
75
70
  clear
76
71
  };
77
- }, "createCallbackStack");
72
+ };
78
73
  function createMicrotask(fn) {
79
74
  let calls = 0;
80
75
  let args;
@@ -83,12 +78,11 @@ function createMicrotask(fn) {
83
78
  queueMicrotask(() => --calls === 0 && fn(...args));
84
79
  };
85
80
  }
86
- __name(createMicrotask, "createMicrotask");
87
81
  function createStaticStore(init) {
88
82
  const copy = { ...init };
89
83
  const store = {};
90
84
  const cache = /* @__PURE__ */ new Map();
91
- const getValue = /* @__PURE__ */ __name((key) => {
85
+ const getValue = (key) => {
92
86
  const saved = cache.get(key);
93
87
  if (saved)
94
88
  return saved[0]();
@@ -98,21 +92,21 @@ function createStaticStore(init) {
98
92
  cache.set(key, signal);
99
93
  delete copy[key];
100
94
  return signal[0]();
101
- }, "getValue");
102
- const setValue = /* @__PURE__ */ __name((key, value) => {
95
+ };
96
+ const setValue = (key, value) => {
103
97
  const saved = cache.get(key);
104
98
  if (saved)
105
99
  return saved[1](value);
106
100
  if (key in copy)
107
101
  copy[key] = accessWith(value, [copy[key]]);
108
- }, "setValue");
102
+ };
109
103
  for (const key of keys(init)) {
110
104
  store[key] = void 0;
111
105
  Object.defineProperty(store, key, {
112
106
  get: getValue.bind(void 0, key)
113
107
  });
114
108
  }
115
- const setter = /* @__PURE__ */ __name((a, b) => {
109
+ const setter = (a, b) => {
116
110
  if (isObject(a))
117
111
  untrack(() => {
118
112
  batch(() => {
@@ -123,10 +117,9 @@ function createStaticStore(init) {
123
117
  else
124
118
  setValue(a, b);
125
119
  return store;
126
- }, "setter");
120
+ };
127
121
  return [store, setter];
128
122
  }
129
- __name(createStaticStore, "createStaticStore");
130
123
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
131
124
  const currLength = current.length;
132
125
  const prevLength = prev.length;
@@ -158,6 +151,14 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
158
151
  handleAdded(currEl);
159
152
  }
160
153
  }
161
- __name(handleDiffArray, "handleDiffArray");
154
+ function createHydrateSignal(serverValue, update, options) {
155
+ if (isServer) {
156
+ return createSignal(serverValue);
157
+ }
158
+ let init = !!sharedConfig.context;
159
+ const [state, setState] = createSignal(init ? serverValue : update(), options);
160
+ init && onMount(() => setState(() => update()));
161
+ return [state, setState];
162
+ }
162
163
 
163
- 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 };
164
+ export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, tryOnCleanup, withAccess };
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "5.1.1",
3
+ "version": "5.2.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
+ "contributors": [
7
+ "Tom Pichaud <dev.tompichaud@icloud.com>"
8
+ ],
6
9
  "license": "MIT",
7
10
  "homepage": "https://github.com/solidjs-community/solid-primitives/tree/main/packages/utils#readme",
8
11
  "repository": {