@solid-primitives/utils 1.3.0 → 1.4.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
@@ -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,12 @@ __export(src_exports, {
24
38
  accessWith: () => accessWith,
25
39
  asAccessor: () => asAccessor,
26
40
  asArray: () => asArray,
41
+ clamp: () => clamp,
27
42
  compare: () => compare,
28
43
  createCallbackStack: () => createCallbackStack,
29
44
  createMicrotask: () => createMicrotask,
30
45
  createProxy: () => createProxy,
46
+ createStaticStore: () => createStaticStore,
31
47
  createTrigger: () => createTrigger,
32
48
  createTriggerCache: () => createTriggerCache,
33
49
  entries: () => entries,
@@ -59,6 +75,7 @@ function isObject(value) {
59
75
  return value !== null && (typeof value === "object" || typeof value === "function");
60
76
  }
61
77
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
78
+ var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
62
79
  var access = (v) => typeof v === "function" && !v.length ? v() : v;
63
80
  var asArray = (value) => Array.isArray(value) ? value : [value];
64
81
  var accessArray = (list) => list.map((v) => access(v));
@@ -73,7 +90,7 @@ function accessWith(valueOrFn, ...args) {
73
90
  function forEachEntry(object, iterator) {
74
91
  Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
75
92
  }
76
- var entries = (object) => Object.entries(access(object));
93
+ var entries = Object.entries;
77
94
  var keys = Object.keys;
78
95
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
79
96
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
@@ -136,6 +153,48 @@ function createTriggerCache(options) {
136
153
  }
137
154
  };
138
155
  }
156
+ function createStaticStore(init) {
157
+ const copy = __spreadValues({}, init);
158
+ const store = {};
159
+ const cache = /* @__PURE__ */ new Map();
160
+ const getValue = (key) => {
161
+ const saved = cache.get(key);
162
+ if (saved)
163
+ return saved[0]();
164
+ const signal = (0, import_solid_js.createSignal)(copy[key], {
165
+ name: typeof key === "string" ? key : void 0
166
+ });
167
+ cache.set(key, signal);
168
+ delete copy[key];
169
+ return signal[0]();
170
+ };
171
+ const setValue = (key, value) => {
172
+ const saved = cache.get(key);
173
+ if (saved)
174
+ return saved[1](value);
175
+ if (key in copy)
176
+ copy[key] = accessWith(value, [copy[key]]);
177
+ };
178
+ for (const key of keys(init)) {
179
+ store[key] = void 0;
180
+ Object.defineProperty(store, key, {
181
+ get: getValue.bind(void 0, key)
182
+ });
183
+ }
184
+ const setter = (a, b) => {
185
+ if (isObject(a))
186
+ (0, import_solid_js.untrack)(() => {
187
+ (0, import_solid_js.batch)(() => {
188
+ for (const [key, value] of entries(accessWith(a, store)))
189
+ setValue(key, () => value);
190
+ });
191
+ });
192
+ else
193
+ setValue(a, b);
194
+ return store;
195
+ };
196
+ return [store, setter];
197
+ }
139
198
  // Annotate the CommonJS export names for ESM import in node:
140
199
  0 && (module.exports = {
141
200
  access,
@@ -143,10 +202,12 @@ function createTriggerCache(options) {
143
202
  accessWith,
144
203
  asAccessor,
145
204
  asArray,
205
+ clamp,
146
206
  compare,
147
207
  createCallbackStack,
148
208
  createMicrotask,
149
209
  createProxy,
210
+ createStaticStore,
150
211
  createTrigger,
151
212
  createTriggerCache,
152
213
  entries,
package/dist/index.d.ts CHANGED
@@ -93,6 +93,7 @@ declare const ofClass: (v: any, c: AnyClass) => boolean;
93
93
  /** Check if value is typeof "object" or "function" */
94
94
  declare function isObject(value: any): value is AnyObject;
95
95
  declare const compare: (a: any, b: any) => number;
96
+ declare const clamp: (n: number, min: number, max: number) => number;
96
97
  /**
97
98
  * Accesses the value of a MaybeAccessor
98
99
  * @example
@@ -124,9 +125,9 @@ declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Pa
124
125
  */
125
126
  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
127
  /**
127
- * Get `Object.entries()` of an MaybeAccessor<object>
128
+ * Get entries of an object
128
129
  */
129
- declare const entries: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A) => [keyof O, Values<O>][];
130
+ declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
130
131
  /**
131
132
  * Get keys of an object
132
133
  */
@@ -220,5 +221,19 @@ declare function createTrigger(options?: BaseOptions): Trigger;
220
221
  * dirty(2)
221
222
  */
222
223
  declare function createTriggerCache<T>(options?: BaseOptions): TriggerCache<T>;
224
+ declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
225
+ (setter: (prev: T) => Partial<T>): T;
226
+ (state: Partial<T>): T;
227
+ <K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
228
+ };
229
+ /**
230
+ * 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.
231
+ * @param init initial value of the store
232
+ * @returns
233
+ * ```ts
234
+ * [access: Readonly<T>, write: StaticStoreSetter<T>]
235
+ * ```
236
+ */
237
+ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
223
238
 
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 };
239
+ 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, StaticStoreSetter, Trigger, TriggerCache, Truthy, UnionToIntersection, Values, access, accessArray, accessWith, asAccessor, asArray, 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,7 @@ 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
+ var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
14
32
  var access = (v) => typeof v === "function" && !v.length ? v() : v;
15
33
  var asArray = (value) => Array.isArray(value) ? value : [value];
16
34
  var accessArray = (list) => list.map((v) => access(v));
@@ -25,7 +43,7 @@ function accessWith(valueOrFn, ...args) {
25
43
  function forEachEntry(object, iterator) {
26
44
  Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
27
45
  }
28
- var entries = (object) => Object.entries(access(object));
46
+ var entries = Object.entries;
29
47
  var keys = Object.keys;
30
48
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
31
49
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
@@ -88,16 +106,60 @@ function createTriggerCache(options) {
88
106
  }
89
107
  };
90
108
  }
109
+ function createStaticStore(init) {
110
+ const copy = __spreadValues({}, init);
111
+ const store = {};
112
+ const cache = /* @__PURE__ */ new Map();
113
+ const getValue = (key) => {
114
+ const saved = cache.get(key);
115
+ if (saved)
116
+ return saved[0]();
117
+ const signal = createSignal(copy[key], {
118
+ name: typeof key === "string" ? key : void 0
119
+ });
120
+ cache.set(key, signal);
121
+ delete copy[key];
122
+ return signal[0]();
123
+ };
124
+ const setValue = (key, value) => {
125
+ const saved = cache.get(key);
126
+ if (saved)
127
+ return saved[1](value);
128
+ if (key in copy)
129
+ copy[key] = accessWith(value, [copy[key]]);
130
+ };
131
+ for (const key of keys(init)) {
132
+ store[key] = void 0;
133
+ Object.defineProperty(store, key, {
134
+ get: getValue.bind(void 0, key)
135
+ });
136
+ }
137
+ const setter = (a, b) => {
138
+ if (isObject(a))
139
+ untrack(() => {
140
+ batch(() => {
141
+ for (const [key, value] of entries(accessWith(a, store)))
142
+ setValue(key, () => value);
143
+ });
144
+ });
145
+ else
146
+ setValue(a, b);
147
+ return store;
148
+ };
149
+ return [store, setter];
150
+ }
91
151
  export {
92
152
  access,
93
153
  accessArray,
94
154
  accessWith,
95
155
  asAccessor,
96
156
  asArray,
157
+ clamp,
97
158
  compare,
98
159
  createCallbackStack,
99
160
  createMicrotask,
100
161
  createProxy,
162
+ createStaticStore,
101
163
  createTrigger,
102
164
  createTriggerCache,
103
165
  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.4.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",
@@ -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
+ }