@solid-primitives/utils 5.4.0 → 5.5.1

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
@@ -5,10 +5,10 @@ var web = require('solid-js/web');
5
5
 
6
6
  // src/index.ts
7
7
  exports.noop = () => void 0;
8
- exports.isServer = web.isServer;
9
- exports.isClient = !exports.isServer;
10
- exports.isDev = solidJs.DEV && exports.isClient;
11
- exports.isProd = !exports.isDev;
8
+ exports.trueFn = () => true;
9
+ exports.falseFn = () => false;
10
+ exports.isClient = !web.isServer;
11
+ exports.isProd = !web.isDev;
12
12
  exports.ofClass = (v, c) => v instanceof c || v && v.constructor === c;
13
13
  function isObject(value) {
14
14
  return value !== null && (typeof value === "object" || typeof value === "function");
@@ -86,16 +86,28 @@ function createMicrotask(fn) {
86
86
  queueMicrotask(() => --calls === 0 && fn(...args));
87
87
  };
88
88
  }
89
+ function createHydratableSignal(serverValue, update, options) {
90
+ if (web.isServer) {
91
+ return solidJs.createSignal(serverValue, options);
92
+ }
93
+ if (solidJs.sharedConfig.context) {
94
+ const [state, setState] = solidJs.createSignal(serverValue, options);
95
+ solidJs.onMount(() => setState(() => update()));
96
+ return [state, setState];
97
+ }
98
+ return solidJs.createSignal(update(), options);
99
+ }
100
+ exports.createHydrateSignal = createHydratableSignal;
89
101
  function createStaticStore(init) {
90
102
  const copy = { ...init };
91
- const store = {};
103
+ const store = { ...init };
92
104
  const cache = /* @__PURE__ */ new Map();
93
105
  const getValue = (key) => {
94
106
  const saved = cache.get(key);
95
107
  if (saved)
96
108
  return saved[0]();
97
109
  const signal = solidJs.createSignal(copy[key], {
98
- name: typeof key === "string" ? key : void 0
110
+ internal: true
99
111
  });
100
112
  cache.set(key, signal);
101
113
  delete copy[key];
@@ -108,26 +120,34 @@ function createStaticStore(init) {
108
120
  if (key in copy)
109
121
  copy[key] = accessWith(value, [copy[key]]);
110
122
  };
111
- for (const key of exports.keys(init)) {
112
- store[key] = void 0;
113
- Object.defineProperty(store, key, {
114
- get: getValue.bind(void 0, key)
115
- });
123
+ for (const key in init) {
124
+ Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
116
125
  }
117
126
  const setter = (a, b) => {
118
- if (isObject(a))
119
- solidJs.untrack(() => {
120
- solidJs.batch(() => {
121
- for (const [key, value] of exports.entries(accessWith(a, store)))
122
- setValue(key, () => value);
123
- });
127
+ if (isObject(a)) {
128
+ const entries2 = solidJs.untrack(
129
+ () => Object.entries(accessWith(a, store))
130
+ );
131
+ solidJs.batch(() => {
132
+ for (const [key, value] of entries2)
133
+ setValue(key, () => value);
124
134
  });
125
- else
135
+ } else
126
136
  setValue(a, b);
127
137
  return store;
128
138
  };
129
139
  return [store, setter];
130
140
  }
141
+ function createHydratableStaticStore(serverValue, update) {
142
+ if (web.isServer)
143
+ return createStaticStore(serverValue);
144
+ if (solidJs.sharedConfig.context) {
145
+ const [state, setState] = createStaticStore(serverValue);
146
+ solidJs.onMount(() => setState(update()));
147
+ return [state, setState];
148
+ }
149
+ return createStaticStore(update());
150
+ }
131
151
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
132
152
  const currLength = current.length;
133
153
  const prevLength = prev.length;
@@ -159,18 +179,19 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
159
179
  handleAdded(currEl);
160
180
  }
161
181
  }
162
- function createHydrateSignal(serverValue, update, options) {
163
- if (exports.isServer) {
164
- return solidJs.createSignal(serverValue);
165
- }
166
- const [state, setState] = solidJs.createSignal(solidJs.sharedConfig.context ? serverValue : update(), options);
167
- solidJs.sharedConfig.context && solidJs.onMount(() => setState(() => update()));
168
- return [state, setState];
169
- }
170
182
 
183
+ Object.defineProperty(exports, 'isDev', {
184
+ enumerable: true,
185
+ get: function () { return web.isDev; }
186
+ });
187
+ Object.defineProperty(exports, 'isServer', {
188
+ enumerable: true,
189
+ get: function () { return web.isServer; }
190
+ });
171
191
  exports.accessWith = accessWith;
172
192
  exports.chain = chain;
173
- exports.createHydrateSignal = createHydrateSignal;
193
+ exports.createHydratableSignal = createHydratableSignal;
194
+ exports.createHydratableStaticStore = createHydratableStaticStore;
174
195
  exports.createMicrotask = createMicrotask;
175
196
  exports.createStaticStore = createStaticStore;
176
197
  exports.defer = defer;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, Signal } from 'solid-js';
1
+ import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, createSignal } from 'solid-js';
2
+ export { isDev, isServer } from 'solid-js/web';
2
3
  export { EffectOptions, OnOptions, ResolvedChildren, ResolvedJSXElement } from 'solid-js/types/reactive/signal';
3
4
 
4
5
  /**
@@ -85,12 +86,11 @@ type Narrow<T extends any> = T extends [] ? T : RawNarrow<T>;
85
86
 
86
87
  /** no operation */
87
88
  declare const noop: Noop;
88
- declare const isServer: boolean;
89
- declare const isClient = true;
90
- /** development environment */
91
- declare const isDev = true;
92
- /** production environment */
93
- declare const isProd = false;
89
+ declare const trueFn: () => boolean;
90
+ declare const falseFn: () => boolean;
91
+
92
+ declare const isClient: boolean;
93
+ declare const isProd: boolean;
94
94
  /**
95
95
  * Check if the value is an instance of ___
96
96
  */
@@ -171,6 +171,21 @@ declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>(
171
171
  * @returns `fn`
172
172
  */
173
173
  declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
174
+ /**
175
+ * A hydratable version of the {@link createSignal}. It will use the serverValue on the server and the update function on the client. If initialized during hydration it will use serverValue as the initial value and update it once hydration is complete.
176
+ *
177
+ * @param serverValue initial value of the state on the server
178
+ * @param update called once on the client or on hydration to initialize the value
179
+ * @param options {@link SignalOptions}
180
+ * @returns
181
+ * ```ts
182
+ * [state: Accessor<T>, setState: Setter<T>]
183
+ * ```
184
+ * @see {@link createSignal}
185
+ */
186
+ declare function createHydratableSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): ReturnType<typeof createSignal<T>>;
187
+ /** @deprecated use {@link createHydratableSignal} instead */
188
+ declare const createHydrateSignal: typeof createHydratableSignal;
174
189
  type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
175
190
  (setter: (prev: T) => Partial<T>): T;
176
191
  (state: Partial<T>): T;
@@ -185,6 +200,17 @@ type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
185
200
  * ```
186
201
  */
187
202
  declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
203
+ /**
204
+ * A hydratable version of the {@link createStaticStore}. It will use the serverValue on the server and the update function on the client. If initialized during hydration it will use serverValue as the initial value and update it once hydration is complete.
205
+ *
206
+ * @param serverValue initial value of the state on the server
207
+ * @param update called once on the client or on hydration to initialize the value
208
+ * @returns
209
+ * ```ts
210
+ * [access: Readonly<T>, write: StaticStoreSetter<T>]
211
+ * ```
212
+ */
213
+ declare function createHydratableStaticStore<T extends Readonly<AnyStatic>>(serverValue: T, update: () => T): ReturnType<typeof createStaticStore<T>>;
188
214
  /**
189
215
  * Handle items removed and added to the array by diffing it by refference.
190
216
  *
@@ -194,17 +220,5 @@ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [acc
194
220
  * @param handleRemoved called once for every removed from array
195
221
  */
196
222
  declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
197
- /**
198
- * A signal object that handle hydration.
199
- * @param serverValue initial value of the state on the server
200
- * @param update called once on the client or on hydration to initialize the value
201
- * @param options {@link SignalOptions}
202
- * @returns
203
- * ```ts
204
- * [state: Accessor<T>, setState: Setter<T>]
205
- * ```
206
- * @see {@link createSignal}
207
- */
208
- declare function createHydrateSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): Signal<T>;
209
223
 
210
- 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, reverseChain, tryOnCleanup, withAccess };
224
+ 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, createHydratableSignal, createHydratableStaticStore, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, falseFn, handleDiffArray, isClient, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
package/dist/index.js CHANGED
@@ -1,11 +1,12 @@
1
- import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount, batch } from 'solid-js';
2
- import { isServer as isServer$1 } from 'solid-js/web';
1
+ import { untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount, batch } from 'solid-js';
2
+ import { isServer, isDev } from 'solid-js/web';
3
+ export { isDev, isServer } from 'solid-js/web';
3
4
 
4
5
  // src/index.ts
5
6
  var noop = () => void 0;
6
- var isServer = isServer$1;
7
+ var trueFn = () => true;
8
+ var falseFn = () => false;
7
9
  var isClient = !isServer;
8
- var isDev = DEV && isClient;
9
10
  var isProd = !isDev;
10
11
  var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
11
12
  function isObject(value) {
@@ -84,16 +85,28 @@ function createMicrotask(fn) {
84
85
  queueMicrotask(() => --calls === 0 && fn(...args));
85
86
  };
86
87
  }
88
+ function createHydratableSignal(serverValue, update, options) {
89
+ if (isServer) {
90
+ return createSignal(serverValue, options);
91
+ }
92
+ if (sharedConfig.context) {
93
+ const [state, setState] = createSignal(serverValue, options);
94
+ onMount(() => setState(() => update()));
95
+ return [state, setState];
96
+ }
97
+ return createSignal(update(), options);
98
+ }
99
+ var createHydrateSignal = createHydratableSignal;
87
100
  function createStaticStore(init) {
88
101
  const copy = { ...init };
89
- const store = {};
102
+ const store = { ...init };
90
103
  const cache = /* @__PURE__ */ new Map();
91
104
  const getValue = (key) => {
92
105
  const saved = cache.get(key);
93
106
  if (saved)
94
107
  return saved[0]();
95
108
  const signal = createSignal(copy[key], {
96
- name: typeof key === "string" ? key : void 0
109
+ internal: true
97
110
  });
98
111
  cache.set(key, signal);
99
112
  delete copy[key];
@@ -106,26 +119,34 @@ function createStaticStore(init) {
106
119
  if (key in copy)
107
120
  copy[key] = accessWith(value, [copy[key]]);
108
121
  };
109
- for (const key of keys(init)) {
110
- store[key] = void 0;
111
- Object.defineProperty(store, key, {
112
- get: getValue.bind(void 0, key)
113
- });
122
+ for (const key in init) {
123
+ Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
114
124
  }
115
125
  const setter = (a, b) => {
116
- if (isObject(a))
117
- untrack(() => {
118
- batch(() => {
119
- for (const [key, value] of entries(accessWith(a, store)))
120
- setValue(key, () => value);
121
- });
126
+ if (isObject(a)) {
127
+ const entries2 = untrack(
128
+ () => Object.entries(accessWith(a, store))
129
+ );
130
+ batch(() => {
131
+ for (const [key, value] of entries2)
132
+ setValue(key, () => value);
122
133
  });
123
- else
134
+ } else
124
135
  setValue(a, b);
125
136
  return store;
126
137
  };
127
138
  return [store, setter];
128
139
  }
140
+ function createHydratableStaticStore(serverValue, update) {
141
+ if (isServer)
142
+ return createStaticStore(serverValue);
143
+ if (sharedConfig.context) {
144
+ const [state, setState] = createStaticStore(serverValue);
145
+ onMount(() => setState(update()));
146
+ return [state, setState];
147
+ }
148
+ return createStaticStore(update());
149
+ }
129
150
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
130
151
  const currLength = current.length;
131
152
  const prevLength = prev.length;
@@ -157,13 +178,5 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
157
178
  handleAdded(currEl);
158
179
  }
159
180
  }
160
- function createHydrateSignal(serverValue, update, options) {
161
- if (isServer) {
162
- return createSignal(serverValue);
163
- }
164
- const [state, setState] = createSignal(sharedConfig.context ? serverValue : update(), options);
165
- sharedConfig.context && onMount(() => setState(() => update()));
166
- return [state, setState];
167
- }
168
181
 
169
- 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, reverseChain, tryOnCleanup, withAccess };
182
+ export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydratableStaticStore, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, falseFn, handleDiffArray, isClient, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "5.4.0",
3
+ "version": "5.5.1",
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
  "contributors": [
@@ -39,7 +39,7 @@
39
39
  "primitives"
40
40
  ],
41
41
  "peerDependencies": {
42
- "solid-js": "^1.6.0"
42
+ "solid-js": "^1.6.12"
43
43
  },
44
44
  "typesVersions": {},
45
45
  "scripts": {