@solid-primitives/utils 1.3.0 → 1.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
@@ -1,7 +1,21 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
5
19
  var __export = (target, all) => {
6
20
  for (var name in all)
7
21
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -24,10 +38,13 @@ __export(src_exports, {
24
38
  accessWith: () => accessWith,
25
39
  asAccessor: () => asAccessor,
26
40
  asArray: () => asArray,
41
+ chain: () => chain,
42
+ clamp: () => clamp,
27
43
  compare: () => compare,
28
44
  createCallbackStack: () => createCallbackStack,
29
45
  createMicrotask: () => createMicrotask,
30
46
  createProxy: () => createProxy,
47
+ createStaticStore: () => createStaticStore,
31
48
  createTrigger: () => createTrigger,
32
49
  createTriggerCache: () => createTriggerCache,
33
50
  entries: () => entries,
@@ -59,6 +76,13 @@ function isObject(value) {
59
76
  return value !== null && (typeof value === "object" || typeof value === "function");
60
77
  }
61
78
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
79
+ function chain(...callbacks) {
80
+ return (...args) => {
81
+ for (const callback of callbacks)
82
+ callback(...args);
83
+ };
84
+ }
85
+ var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
62
86
  var access = (v) => typeof v === "function" && !v.length ? v() : v;
63
87
  var asArray = (value) => Array.isArray(value) ? value : [value];
64
88
  var accessArray = (list) => list.map((v) => access(v));
@@ -73,7 +97,7 @@ function accessWith(valueOrFn, ...args) {
73
97
  function forEachEntry(object, iterator) {
74
98
  Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
75
99
  }
76
- var entries = (object) => Object.entries(access(object));
100
+ var entries = Object.entries;
77
101
  var keys = Object.keys;
78
102
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
79
103
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
@@ -136,6 +160,48 @@ function createTriggerCache(options) {
136
160
  }
137
161
  };
138
162
  }
163
+ function createStaticStore(init) {
164
+ const copy = __spreadValues({}, init);
165
+ const store = {};
166
+ const cache = /* @__PURE__ */ new Map();
167
+ const getValue = (key) => {
168
+ const saved = cache.get(key);
169
+ if (saved)
170
+ return saved[0]();
171
+ const signal = (0, import_solid_js.createSignal)(copy[key], {
172
+ name: typeof key === "string" ? key : void 0
173
+ });
174
+ cache.set(key, signal);
175
+ delete copy[key];
176
+ return signal[0]();
177
+ };
178
+ const setValue = (key, value) => {
179
+ const saved = cache.get(key);
180
+ if (saved)
181
+ return saved[1](value);
182
+ if (key in copy)
183
+ copy[key] = accessWith(value, [copy[key]]);
184
+ };
185
+ for (const key of keys(init)) {
186
+ store[key] = void 0;
187
+ Object.defineProperty(store, key, {
188
+ get: getValue.bind(void 0, key)
189
+ });
190
+ }
191
+ const setter = (a, b) => {
192
+ if (isObject(a))
193
+ (0, import_solid_js.untrack)(() => {
194
+ (0, import_solid_js.batch)(() => {
195
+ for (const [key, value] of entries(accessWith(a, store)))
196
+ setValue(key, () => value);
197
+ });
198
+ });
199
+ else
200
+ setValue(a, b);
201
+ return store;
202
+ };
203
+ return [store, setter];
204
+ }
139
205
  // Annotate the CommonJS export names for ESM import in node:
140
206
  0 && (module.exports = {
141
207
  access,
@@ -143,10 +209,13 @@ function createTriggerCache(options) {
143
209
  accessWith,
144
210
  asAccessor,
145
211
  asArray,
212
+ chain,
213
+ clamp,
146
214
  compare,
147
215
  createCallbackStack,
148
216
  createMicrotask,
149
217
  createProxy,
218
+ createStaticStore,
150
219
  createTrigger,
151
220
  createTriggerCache,
152
221
  entries,
package/dist/index.d.ts CHANGED
@@ -54,6 +54,18 @@ declare type NonIterable<T> = T & {
54
54
  declare type Definite<T> = {
55
55
  [K in keyof T]-?: T[K];
56
56
  };
57
+ /** Unwraps the type definition of an object, making it more readable */
58
+ declare type Simplify<T> = T extends object ? {
59
+ [K in keyof T]: T[K];
60
+ } : T;
61
+ /** Unboxes type definition, making it more readable */
62
+ declare type UnboxLazy<T> = T extends () => infer U ? U : T;
63
+ /** Get the required keys of an object */
64
+ declare type RequiredKeys<T> = keyof {
65
+ [K in keyof T as T extends {
66
+ [_ in K]: unknown;
67
+ } ? K : never]: 0;
68
+ };
57
69
  /** `A | B => A & B` */
58
70
  declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
59
71
  declare type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
@@ -93,6 +105,11 @@ declare const ofClass: (v: any, c: AnyClass) => boolean;
93
105
  /** Check if value is typeof "object" or "function" */
94
106
  declare function isObject(value: any): value is AnyObject;
95
107
  declare const compare: (a: any, b: any) => number;
108
+ /**
109
+ * Returns a function that will call all functions in the order they were chained with the same arguments.
110
+ */
111
+ declare function chain<Args extends [] | any[]>(...callbacks: ((...args: Args) => any)[]): (...args: Args) => void;
112
+ declare const clamp: (n: number, min: number, max: number) => number;
96
113
  /**
97
114
  * Accesses the value of a MaybeAccessor
98
115
  * @example
@@ -124,9 +141,9 @@ declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Pa
124
141
  */
125
142
  declare function forEachEntry<O extends AnyObject>(object: O, iterator: (key: keyof O, item: Values<O>, index: number, pairs: [keyof O, Values<O>][], object: O) => void): void;
126
143
  /**
127
- * Get `Object.entries()` of an MaybeAccessor<object>
144
+ * Get entries of an object
128
145
  */
129
- declare const entries: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A) => [keyof O, Values<O>][];
146
+ declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
130
147
  /**
131
148
  * Get keys of an object
132
149
  */
@@ -220,5 +237,19 @@ declare function createTrigger(options?: BaseOptions): Trigger;
220
237
  * dirty(2)
221
238
  */
222
239
  declare function createTriggerCache<T>(options?: BaseOptions): TriggerCache<T>;
240
+ declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
241
+ (setter: (prev: T) => Partial<T>): T;
242
+ (state: Partial<T>): T;
243
+ <K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
244
+ };
245
+ /**
246
+ * A shallowly wrapped reactive store object. It behaves similarly to the creatStore, 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.
247
+ * @param init initial value of the store
248
+ * @returns
249
+ * ```ts
250
+ * [access: Readonly<T>, write: StaticStoreSetter<T>]
251
+ * ```
252
+ */
253
+ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
223
254
 
224
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, SetterValue, Trigger, TriggerCache, Truthy, UnionToIntersection, Values, access, accessArray, accessWith, asAccessor, asArray, compare, createCallbackStack, createMicrotask, createProxy, createTrigger, createTriggerCache, entries, forEachEntry, isClient, isDev, isObject, isProd, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
255
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterValue, Simplify, StaticStoreSetter, Trigger, TriggerCache, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, createTrigger, createTriggerCache, entries, forEachEntry, isClient, isDev, isObject, isProd, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
package/dist/index.js CHANGED
@@ -1,5 +1,22 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
1
18
  // src/index.ts
2
- import { getOwner, onCleanup, createSignal, DEV } from "solid-js";
19
+ import { getOwner, onCleanup, createSignal, DEV, untrack, batch } from "solid-js";
3
20
  import { isServer } from "solid-js/web";
4
21
  var noop = () => void 0;
5
22
  var isClient = !isServer;
@@ -11,6 +28,13 @@ function isObject(value) {
11
28
  return value !== null && (typeof value === "object" || typeof value === "function");
12
29
  }
13
30
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
31
+ function chain(...callbacks) {
32
+ return (...args) => {
33
+ for (const callback of callbacks)
34
+ callback(...args);
35
+ };
36
+ }
37
+ var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
14
38
  var access = (v) => typeof v === "function" && !v.length ? v() : v;
15
39
  var asArray = (value) => Array.isArray(value) ? value : [value];
16
40
  var accessArray = (list) => list.map((v) => access(v));
@@ -25,7 +49,7 @@ function accessWith(valueOrFn, ...args) {
25
49
  function forEachEntry(object, iterator) {
26
50
  Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
27
51
  }
28
- var entries = (object) => Object.entries(access(object));
52
+ var entries = Object.entries;
29
53
  var keys = Object.keys;
30
54
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
31
55
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
@@ -88,16 +112,61 @@ function createTriggerCache(options) {
88
112
  }
89
113
  };
90
114
  }
115
+ function createStaticStore(init) {
116
+ const copy = __spreadValues({}, init);
117
+ const store = {};
118
+ const cache = /* @__PURE__ */ new Map();
119
+ const getValue = (key) => {
120
+ const saved = cache.get(key);
121
+ if (saved)
122
+ return saved[0]();
123
+ const signal = createSignal(copy[key], {
124
+ name: typeof key === "string" ? key : void 0
125
+ });
126
+ cache.set(key, signal);
127
+ delete copy[key];
128
+ return signal[0]();
129
+ };
130
+ const setValue = (key, value) => {
131
+ const saved = cache.get(key);
132
+ if (saved)
133
+ return saved[1](value);
134
+ if (key in copy)
135
+ copy[key] = accessWith(value, [copy[key]]);
136
+ };
137
+ for (const key of keys(init)) {
138
+ store[key] = void 0;
139
+ Object.defineProperty(store, key, {
140
+ get: getValue.bind(void 0, key)
141
+ });
142
+ }
143
+ const setter = (a, b) => {
144
+ if (isObject(a))
145
+ untrack(() => {
146
+ batch(() => {
147
+ for (const [key, value] of entries(accessWith(a, store)))
148
+ setValue(key, () => value);
149
+ });
150
+ });
151
+ else
152
+ setValue(a, b);
153
+ return store;
154
+ };
155
+ return [store, setter];
156
+ }
91
157
  export {
92
158
  access,
93
159
  accessArray,
94
160
  accessWith,
95
161
  asAccessor,
96
162
  asArray,
163
+ chain,
164
+ clamp,
97
165
  compare,
98
166
  createCallbackStack,
99
167
  createMicrotask,
100
168
  createProxy,
169
+ createStaticStore,
101
170
  createTrigger,
102
171
  createTriggerCache,
103
172
  entries,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "1.3.0",
3
+ "version": "1.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
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  "scripts": {
25
25
  "build": "tsup",
26
26
  "test": "uvu -r solid-register",
27
- "watch-test": "watchlist src test -- npm test"
27
+ "test:watch": "watchlist src test -- npm test"
28
28
  },
29
29
  "keywords": [
30
30
  "utilities",
@@ -44,4 +44,4 @@
44
44
  "peerDependencies": {
45
45
  "solid-js": "^1.3.1"
46
46
  }
47
- }
47
+ }