@solid-primitives/utils 5.4.0-beta.0 → 5.5.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Solid Primitives Working Group
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Solid Primitives Working Group
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,19 +1,19 @@
1
- <p>
2
- <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Utils" alt="Solid Primitives Utils">
3
- </p>
4
-
5
- # @solid-primitives/utils
6
-
7
- Solid Primitives Utilities is a support and helper package for a number of primitives in our library. Please free to augment or centralize useful utilities and methods in this package for sharing.
8
-
9
- ## Installation
10
-
11
- ```bash
12
- npm install @solid-primitives/utils
13
- # or
14
- yarn add @solid-primitives/utils
15
- ```
16
-
17
- ## Changelog
18
-
19
- See [CHANGELOG.md](./CHANGELOG.md)
1
+ <p>
2
+ <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Utils" alt="Solid Primitives Utils">
3
+ </p>
4
+
5
+ # @solid-primitives/utils
6
+
7
+ Solid Primitives Utilities is a support and helper package for a number of primitives in our library. Please free to augment or centralize useful utilities and methods in this package for sharing.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @solid-primitives/utils
13
+ # or
14
+ yarn add @solid-primitives/utils
15
+ ```
16
+
17
+ ## Changelog
18
+
19
+ See [CHANGELOG.md](./CHANGELOG.md)
package/dist/index.cjs CHANGED
@@ -5,8 +5,9 @@ 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;
8
+ exports.trueFn = () => true;
9
+ exports.falseFn = () => false;
10
+ exports.isClient = !web.isServer;
10
11
  exports.isDev = solidJs.DEV && exports.isClient;
11
12
  exports.isProd = !exports.isDev;
12
13
  exports.ofClass = (v, c) => v instanceof c || v && v.constructor === c;
@@ -86,16 +87,28 @@ function createMicrotask(fn) {
86
87
  queueMicrotask(() => --calls === 0 && fn(...args));
87
88
  };
88
89
  }
90
+ function createHydratableSignal(serverValue, update, options) {
91
+ if (web.isServer) {
92
+ return solidJs.createSignal(serverValue, options);
93
+ }
94
+ if (solidJs.sharedConfig.context) {
95
+ const [state, setState] = solidJs.createSignal(serverValue, options);
96
+ solidJs.onMount(() => setState(() => update()));
97
+ return [state, setState];
98
+ }
99
+ return solidJs.createSignal(update(), options);
100
+ }
101
+ exports.createHydrateSignal = createHydratableSignal;
89
102
  function createStaticStore(init) {
90
103
  const copy = { ...init };
91
- const store = {};
104
+ const store = { ...init };
92
105
  const cache = /* @__PURE__ */ new Map();
93
106
  const getValue = (key) => {
94
107
  const saved = cache.get(key);
95
108
  if (saved)
96
109
  return saved[0]();
97
110
  const signal = solidJs.createSignal(copy[key], {
98
- name: typeof key === "string" ? key : void 0
111
+ internal: true
99
112
  });
100
113
  cache.set(key, signal);
101
114
  delete copy[key];
@@ -108,26 +121,34 @@ function createStaticStore(init) {
108
121
  if (key in copy)
109
122
  copy[key] = accessWith(value, [copy[key]]);
110
123
  };
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
- });
124
+ for (const key in init) {
125
+ Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
116
126
  }
117
127
  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
- });
128
+ if (isObject(a)) {
129
+ const entries2 = solidJs.untrack(
130
+ () => Object.entries(accessWith(a, store))
131
+ );
132
+ solidJs.batch(() => {
133
+ for (const [key, value] of entries2)
134
+ setValue(key, () => value);
124
135
  });
125
- else
136
+ } else
126
137
  setValue(a, b);
127
138
  return store;
128
139
  };
129
140
  return [store, setter];
130
141
  }
142
+ function createHydratableStaticStore(serverValue, update) {
143
+ if (web.isServer)
144
+ return createStaticStore(serverValue);
145
+ if (solidJs.sharedConfig.context) {
146
+ const [state, setState] = createStaticStore(serverValue);
147
+ solidJs.onMount(() => setState(update()));
148
+ return [state, setState];
149
+ }
150
+ return createStaticStore(update());
151
+ }
131
152
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
132
153
  const currLength = current.length;
133
154
  const prevLength = prev.length;
@@ -159,18 +180,15 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
159
180
  handleAdded(currEl);
160
181
  }
161
182
  }
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
183
 
184
+ Object.defineProperty(exports, 'isServer', {
185
+ enumerable: true,
186
+ get: function () { return web.isServer; }
187
+ });
171
188
  exports.accessWith = accessWith;
172
189
  exports.chain = chain;
173
- exports.createHydrateSignal = createHydrateSignal;
190
+ exports.createHydratableSignal = createHydratableSignal;
191
+ exports.createHydratableStaticStore = createHydratableStaticStore;
174
192
  exports.createMicrotask = createMicrotask;
175
193
  exports.createStaticStore = createStaticStore;
176
194
  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 { 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,14 @@ 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;
89
+ declare const trueFn: () => boolean;
90
+ declare const falseFn: () => boolean;
91
+
92
+ declare const isClient: boolean;
90
93
  /** development environment */
91
- declare const isDev = true;
94
+ declare const isDev: boolean;
92
95
  /** production environment */
93
- declare const isProd = false;
96
+ declare const isProd: boolean;
94
97
  /**
95
98
  * Check if the value is an instance of ___
96
99
  */
@@ -171,6 +174,21 @@ declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>(
171
174
  * @returns `fn`
172
175
  */
173
176
  declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
177
+ /**
178
+ * 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.
179
+ *
180
+ * @param serverValue initial value of the state on the server
181
+ * @param update called once on the client or on hydration to initialize the value
182
+ * @param options {@link SignalOptions}
183
+ * @returns
184
+ * ```ts
185
+ * [state: Accessor<T>, setState: Setter<T>]
186
+ * ```
187
+ * @see {@link createSignal}
188
+ */
189
+ declare function createHydratableSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): ReturnType<typeof createSignal<T>>;
190
+ /** @deprecated use {@link createHydratableSignal} instead */
191
+ declare const createHydrateSignal: typeof createHydratableSignal;
174
192
  type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
175
193
  (setter: (prev: T) => Partial<T>): T;
176
194
  (state: Partial<T>): T;
@@ -185,6 +203,17 @@ type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
185
203
  * ```
186
204
  */
187
205
  declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
206
+ /**
207
+ * 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.
208
+ *
209
+ * @param serverValue initial value of the state on the server
210
+ * @param update called once on the client or on hydration to initialize the value
211
+ * @returns
212
+ * ```ts
213
+ * [access: Readonly<T>, write: StaticStoreSetter<T>]
214
+ * ```
215
+ */
216
+ declare function createHydratableStaticStore<T extends Readonly<AnyStatic>>(serverValue: T, update: () => T): ReturnType<typeof createStaticStore<T>>;
188
217
  /**
189
218
  * Handle items removed and added to the array by diffing it by refference.
190
219
  *
@@ -194,17 +223,5 @@ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [acc
194
223
  * @param handleRemoved called once for every removed from array
195
224
  */
196
225
  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
226
 
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 };
227
+ 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, isDev, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
package/dist/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount, batch } from 'solid-js';
2
- import { isServer as isServer$1 } from 'solid-js/web';
2
+ import { isServer } from 'solid-js/web';
3
+ export { 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
10
  var isDev = DEV && isClient;
9
11
  var isProd = !isDev;
@@ -84,16 +86,28 @@ function createMicrotask(fn) {
84
86
  queueMicrotask(() => --calls === 0 && fn(...args));
85
87
  };
86
88
  }
89
+ function createHydratableSignal(serverValue, update, options) {
90
+ if (isServer) {
91
+ return createSignal(serverValue, options);
92
+ }
93
+ if (sharedConfig.context) {
94
+ const [state, setState] = createSignal(serverValue, options);
95
+ onMount(() => setState(() => update()));
96
+ return [state, setState];
97
+ }
98
+ return createSignal(update(), options);
99
+ }
100
+ var createHydrateSignal = createHydratableSignal;
87
101
  function createStaticStore(init) {
88
102
  const copy = { ...init };
89
- const store = {};
103
+ const store = { ...init };
90
104
  const cache = /* @__PURE__ */ new Map();
91
105
  const getValue = (key) => {
92
106
  const saved = cache.get(key);
93
107
  if (saved)
94
108
  return saved[0]();
95
109
  const signal = createSignal(copy[key], {
96
- name: typeof key === "string" ? key : void 0
110
+ internal: true
97
111
  });
98
112
  cache.set(key, signal);
99
113
  delete copy[key];
@@ -106,26 +120,34 @@ function createStaticStore(init) {
106
120
  if (key in copy)
107
121
  copy[key] = accessWith(value, [copy[key]]);
108
122
  };
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
- });
123
+ for (const key in init) {
124
+ Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
114
125
  }
115
126
  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
- });
127
+ if (isObject(a)) {
128
+ const entries2 = untrack(
129
+ () => Object.entries(accessWith(a, store))
130
+ );
131
+ batch(() => {
132
+ for (const [key, value] of entries2)
133
+ setValue(key, () => value);
122
134
  });
123
- else
135
+ } else
124
136
  setValue(a, b);
125
137
  return store;
126
138
  };
127
139
  return [store, setter];
128
140
  }
141
+ function createHydratableStaticStore(serverValue, update) {
142
+ if (isServer)
143
+ return createStaticStore(serverValue);
144
+ if (sharedConfig.context) {
145
+ const [state, setState] = createStaticStore(serverValue);
146
+ onMount(() => setState(update()));
147
+ return [state, setState];
148
+ }
149
+ return createStaticStore(update());
150
+ }
129
151
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
130
152
  const currLength = current.length;
131
153
  const prevLength = prev.length;
@@ -157,13 +179,5 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
157
179
  handleAdded(currEl);
158
180
  }
159
181
  }
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
182
 
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 };
183
+ export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydratableStaticStore, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, falseFn, handleDiffArray, isClient, isDev, 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-beta.0",
3
+ "version": "5.5.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
  "contributors": [