@solid-primitives/utils 2.0.3 → 2.2.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,21 +1,8 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
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
- };
19
6
  var __export = (target, all) => {
20
7
  for (var name in all)
21
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -36,6 +23,7 @@ __export(src_exports, {
36
23
  access: () => access,
37
24
  accessArray: () => accessArray,
38
25
  accessWith: () => accessWith,
26
+ arrayEquals: () => arrayEquals,
39
27
  asAccessor: () => asAccessor,
40
28
  asArray: () => asArray,
41
29
  chain: () => chain,
@@ -47,8 +35,10 @@ __export(src_exports, {
47
35
  createStaticStore: () => createStaticStore,
48
36
  createTrigger: () => createTrigger,
49
37
  createTriggerCache: () => createTriggerCache,
38
+ createWeakTriggerCache: () => createWeakTriggerCache,
50
39
  entries: () => entries,
51
40
  forEachEntry: () => forEachEntry,
41
+ handleDiffArray: () => handleDiffArray,
52
42
  isClient: () => isClient,
53
43
  isDev: () => isDev,
54
44
  isObject: () => isObject,
@@ -77,6 +67,7 @@ function isObject(value) {
77
67
  return value !== null && (typeof value === "object" || typeof value === "function");
78
68
  }
79
69
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
70
+ var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
80
71
  function chain(callbacks) {
81
72
  return (...args) => {
82
73
  for (const callback of callbacks) {
@@ -142,29 +133,42 @@ function createProxy(traps) {
142
133
  }
143
134
  });
144
135
  }
145
- function createTrigger(options) {
146
- return (0, import_solid_js.createSignal)(void 0, { equals: false, name: options == null ? void 0 : options.name });
136
+ var createTrigger = isDev ? () => (0, import_solid_js.createSignal)(void 0, { equals: false, name: "trigger" }) : () => (0, import_solid_js.createSignal)(void 0, { equals: false });
137
+ function dirtyTriggerCache(key) {
138
+ const trigger = this.get(key);
139
+ if (trigger)
140
+ trigger[1]();
141
+ }
142
+ function dirtyAllTriggerCache() {
143
+ this.forEach((s) => s[1]());
144
+ }
145
+ function trackTriggerCache(key) {
146
+ if (!(0, import_solid_js.getListener)())
147
+ return;
148
+ let trigger = this.get(key);
149
+ if (!trigger) {
150
+ trigger = createTrigger();
151
+ this.set(key, trigger);
152
+ }
153
+ trigger[0]();
147
154
  }
148
- function createTriggerCache(options) {
155
+ function createTriggerCache() {
149
156
  const cache = /* @__PURE__ */ new Map();
150
157
  return {
151
- dirty: (key) => {
152
- var _a;
153
- return (_a = cache.get(key)) == null ? void 0 : _a[1]();
154
- },
155
- dirtyAll: () => cache.forEach((s) => s[1]()),
156
- track(key) {
157
- let trigger = cache.get(key);
158
- if (!trigger) {
159
- trigger = createTrigger(options);
160
- cache.set(key, trigger);
161
- }
162
- trigger[0]();
163
- }
158
+ dirty: dirtyTriggerCache.bind(cache),
159
+ dirtyAll: dirtyAllTriggerCache.bind(cache),
160
+ track: trackTriggerCache.bind(cache)
161
+ };
162
+ }
163
+ function createWeakTriggerCache() {
164
+ const cache = /* @__PURE__ */ new WeakMap();
165
+ return {
166
+ dirty: dirtyTriggerCache.bind(cache),
167
+ track: trackTriggerCache.bind(cache)
164
168
  };
165
169
  }
166
170
  function createStaticStore(init) {
167
- const copy = __spreadValues({}, init);
171
+ const copy = { ...init };
168
172
  const store = {};
169
173
  const cache = /* @__PURE__ */ new Map();
170
174
  const getValue = (key) => {
@@ -205,11 +209,43 @@ function createStaticStore(init) {
205
209
  };
206
210
  return [store, setter];
207
211
  }
212
+ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
213
+ const currLength = current.length;
214
+ const prevLength = prev.length;
215
+ let i = 0;
216
+ if (!prevLength) {
217
+ for (; i < currLength; i++)
218
+ handleAdded(current[i]);
219
+ return;
220
+ }
221
+ if (!currLength) {
222
+ for (; i < prevLength; i++)
223
+ handleRemoved(prev[i]);
224
+ return;
225
+ }
226
+ for (; i < prevLength; i++) {
227
+ if (prev[i] !== current[i])
228
+ break;
229
+ }
230
+ let prevEl;
231
+ let currEl;
232
+ prev = prev.slice(i);
233
+ current = current.slice(i);
234
+ for (prevEl of prev) {
235
+ if (!current.includes(prevEl))
236
+ handleRemoved(prevEl);
237
+ }
238
+ for (currEl of current) {
239
+ if (!prev.includes(currEl))
240
+ handleAdded(currEl);
241
+ }
242
+ }
208
243
  // Annotate the CommonJS export names for ESM import in node:
209
244
  0 && (module.exports = {
210
245
  access,
211
246
  accessArray,
212
247
  accessWith,
248
+ arrayEquals,
213
249
  asAccessor,
214
250
  asArray,
215
251
  chain,
@@ -221,8 +257,10 @@ function createStaticStore(init) {
221
257
  createStaticStore,
222
258
  createTrigger,
223
259
  createTriggerCache,
260
+ createWeakTriggerCache,
224
261
  entries,
225
262
  forEachEntry,
263
+ handleDiffArray,
226
264
  isClient,
227
265
  isDev,
228
266
  isObject,
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Accessor, Setter, onCleanup } from 'solid-js';
2
- import { BaseOptions } from 'solid-js/types/reactive/signal';
3
2
 
4
3
  /**
5
4
  * Can be single or in an array
@@ -82,12 +81,6 @@ declare type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;
82
81
  declare type FalsyValue = false | 0 | "" | null | undefined;
83
82
  declare type Truthy<T> = Exclude<T, FalsyValue>;
84
83
  declare type Falsy<T> = Extract<T, FalsyValue>;
85
- declare type TriggerCache<T> = {
86
- track: (v: T) => void;
87
- dirty: (v: T) => void;
88
- dirtyAll: VoidFunction;
89
- };
90
- declare type Trigger = [track: VoidFunction, dirty: VoidFunction];
91
84
  declare type Position = {
92
85
  x: number;
93
86
  y: number;
@@ -110,6 +103,10 @@ declare const ofClass: (v: any, c: AnyClass) => boolean;
110
103
  /** Check if value is typeof "object" or "function" */
111
104
  declare function isObject(value: any): value is AnyObject;
112
105
  declare const compare: (a: any, b: any) => number;
106
+ /**
107
+ * Check shallow array equality
108
+ */
109
+ declare const arrayEquals: (a: readonly unknown[], b: readonly unknown[]) => boolean;
113
110
  /**
114
111
  * Returns a function that will call all functions in the order they were chained with the same arguments.
115
112
  */
@@ -215,6 +212,7 @@ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
215
212
  get: <K extends keyof T>(key: K) => T[K];
216
213
  set?: undefined;
217
214
  }): Readonly<T>;
215
+ declare type Trigger = [track: VoidFunction, dirty: VoidFunction];
218
216
  /**
219
217
  * Set listeners in reactive computations and then trigger them when you want.
220
218
  * @returns `[track function, dirty function]`
@@ -227,7 +225,12 @@ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
227
225
  * // later
228
226
  * dirty()
229
227
  */
230
- declare function createTrigger(options?: BaseOptions): Trigger;
228
+ declare const createTrigger: () => Trigger;
229
+ declare type TriggerCache<T> = {
230
+ track: (v: T) => void;
231
+ dirty: (v: T) => void;
232
+ dirtyAll: VoidFunction;
233
+ };
231
234
  /**
232
235
  * Set listeners in reactive computations and then trigger them when you want. Cache trackers by a `key`.
233
236
  * @returns `{ track, dirty, dirtyAll }` functions
@@ -243,14 +246,34 @@ declare function createTrigger(options?: BaseOptions): Trigger;
243
246
  * // this won't cause an update:
244
247
  * dirty(2)
245
248
  */
246
- declare function createTriggerCache<T>(options?: BaseOptions): TriggerCache<T>;
249
+ declare function createTriggerCache<T>(): TriggerCache<T>;
250
+ declare type WeakTriggerCache<T extends object> = {
251
+ track: (v: T) => void;
252
+ dirty: (v: T) => void;
253
+ };
254
+ /**
255
+ * Set listeners in reactive computations and then trigger them when you want. Cache trackers by a `key`.
256
+ * @returns `{ track, dirty }` functions
257
+ * `track` and `dirty` are called with a `key` so that each tracker will trigger an update only when his individual `key` would get marked as dirty.
258
+ * @example
259
+ * const { track, dirty } = createWeakTriggerCache()
260
+ * createEffect(() => {
261
+ * track(1)
262
+ * ...
263
+ * })
264
+ * // later
265
+ * dirty(1)
266
+ * // this won't cause an update:
267
+ * dirty(2)
268
+ */
269
+ declare function createWeakTriggerCache<T extends object>(): WeakTriggerCache<T>;
247
270
  declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
248
271
  (setter: (prev: T) => Partial<T>): T;
249
272
  (state: Partial<T>): T;
250
273
  <K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
251
274
  };
252
275
  /**
253
- * 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.
276
+ * A shallowly wrapped reactive store object. It behaves similarly to the createStore, 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.
254
277
  * @param init initial value of the store
255
278
  * @returns
256
279
  * ```ts
@@ -258,5 +281,14 @@ declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
258
281
  * ```
259
282
  */
260
283
  declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
284
+ /**
285
+ * Handle items removed and added to the array by diffing it by refference.
286
+ *
287
+ * @param current new array instance
288
+ * @param prev previous array copy
289
+ * @param handleAdded called once for every added item to array
290
+ * @param handleRemoved called once for every removed from array
291
+ */
292
+ declare function handleDiffArray<T>(current: T[], prev: T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
261
293
 
262
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Mutable, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterValue, Simplify, StaticStoreSetter, Tail, 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, isServer, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
294
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Mutable, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterValue, Simplify, StaticStoreSetter, Tail, Trigger, TriggerCache, Truthy, UnboxLazy, UnionToIntersection, Values, WeakTriggerCache, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, createTrigger, createTriggerCache, createWeakTriggerCache, entries, forEachEntry, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
package/dist/index.js CHANGED
@@ -1,22 +1,15 @@
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
- };
1
+ "use strict";
17
2
 
18
3
  // src/index.ts
19
- import { getOwner, onCleanup, createSignal, DEV, untrack, batch } from "solid-js";
4
+ import {
5
+ getOwner,
6
+ onCleanup,
7
+ createSignal,
8
+ DEV,
9
+ untrack,
10
+ batch,
11
+ getListener
12
+ } from "solid-js";
20
13
  import { isServer as _isServer } from "solid-js/web";
21
14
  var noop = () => void 0;
22
15
  var isServer = _isServer;
@@ -29,6 +22,7 @@ function isObject(value) {
29
22
  return value !== null && (typeof value === "object" || typeof value === "function");
30
23
  }
31
24
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
25
+ var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
32
26
  function chain(callbacks) {
33
27
  return (...args) => {
34
28
  for (const callback of callbacks) {
@@ -94,29 +88,42 @@ function createProxy(traps) {
94
88
  }
95
89
  });
96
90
  }
97
- function createTrigger(options) {
98
- return createSignal(void 0, { equals: false, name: options == null ? void 0 : options.name });
91
+ var createTrigger = isDev ? () => createSignal(void 0, { equals: false, name: "trigger" }) : () => createSignal(void 0, { equals: false });
92
+ function dirtyTriggerCache(key) {
93
+ const trigger = this.get(key);
94
+ if (trigger)
95
+ trigger[1]();
96
+ }
97
+ function dirtyAllTriggerCache() {
98
+ this.forEach((s) => s[1]());
99
+ }
100
+ function trackTriggerCache(key) {
101
+ if (!getListener())
102
+ return;
103
+ let trigger = this.get(key);
104
+ if (!trigger) {
105
+ trigger = createTrigger();
106
+ this.set(key, trigger);
107
+ }
108
+ trigger[0]();
99
109
  }
100
- function createTriggerCache(options) {
110
+ function createTriggerCache() {
101
111
  const cache = /* @__PURE__ */ new Map();
102
112
  return {
103
- dirty: (key) => {
104
- var _a;
105
- return (_a = cache.get(key)) == null ? void 0 : _a[1]();
106
- },
107
- dirtyAll: () => cache.forEach((s) => s[1]()),
108
- track(key) {
109
- let trigger = cache.get(key);
110
- if (!trigger) {
111
- trigger = createTrigger(options);
112
- cache.set(key, trigger);
113
- }
114
- trigger[0]();
115
- }
113
+ dirty: dirtyTriggerCache.bind(cache),
114
+ dirtyAll: dirtyAllTriggerCache.bind(cache),
115
+ track: trackTriggerCache.bind(cache)
116
+ };
117
+ }
118
+ function createWeakTriggerCache() {
119
+ const cache = /* @__PURE__ */ new WeakMap();
120
+ return {
121
+ dirty: dirtyTriggerCache.bind(cache),
122
+ track: trackTriggerCache.bind(cache)
116
123
  };
117
124
  }
118
125
  function createStaticStore(init) {
119
- const copy = __spreadValues({}, init);
126
+ const copy = { ...init };
120
127
  const store = {};
121
128
  const cache = /* @__PURE__ */ new Map();
122
129
  const getValue = (key) => {
@@ -157,10 +164,42 @@ function createStaticStore(init) {
157
164
  };
158
165
  return [store, setter];
159
166
  }
167
+ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
168
+ const currLength = current.length;
169
+ const prevLength = prev.length;
170
+ let i = 0;
171
+ if (!prevLength) {
172
+ for (; i < currLength; i++)
173
+ handleAdded(current[i]);
174
+ return;
175
+ }
176
+ if (!currLength) {
177
+ for (; i < prevLength; i++)
178
+ handleRemoved(prev[i]);
179
+ return;
180
+ }
181
+ for (; i < prevLength; i++) {
182
+ if (prev[i] !== current[i])
183
+ break;
184
+ }
185
+ let prevEl;
186
+ let currEl;
187
+ prev = prev.slice(i);
188
+ current = current.slice(i);
189
+ for (prevEl of prev) {
190
+ if (!current.includes(prevEl))
191
+ handleRemoved(prevEl);
192
+ }
193
+ for (currEl of current) {
194
+ if (!prev.includes(currEl))
195
+ handleAdded(currEl);
196
+ }
197
+ }
160
198
  export {
161
199
  access,
162
200
  accessArray,
163
201
  accessWith,
202
+ arrayEquals,
164
203
  asAccessor,
165
204
  asArray,
166
205
  chain,
@@ -172,8 +211,10 @@ export {
172
211
  createStaticStore,
173
212
  createTrigger,
174
213
  createTriggerCache,
214
+ createWeakTriggerCache,
175
215
  entries,
176
216
  forEachEntry,
217
+ handleDiffArray,
177
218
  isClient,
178
219
  isDev,
179
220
  isObject,
package/package.json CHANGED
@@ -1,47 +1,47 @@
1
- {
2
- "name": "@solid-primitives/utils",
3
- "version": "2.0.3",
4
- "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
- "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
- "license": "MIT",
7
- "homepage": "https://github.com/solidjs-community/solid-primitives/tree/main/packages/utils#readme",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https://github.com/solidjs-community/solid-primitives.git"
11
- },
12
- "bugs": {
13
- "url": "https://github.com/solidjs-community/solid-primitives/issues"
14
- },
15
- "private": false,
16
- "sideEffects": false,
17
- "type": "module",
18
- "main": "dist/index.cjs",
19
- "module": "dist/index.js",
20
- "types": "dist/index.d.ts",
21
- "files": [
22
- "dist"
23
- ],
24
- "scripts": {
25
- "build": "tsup",
26
- "test": "uvu -r solid-register",
27
- "test:watch": "watchlist src test -- npm test"
28
- },
29
- "keywords": [
30
- "utilities",
31
- "reactivity",
32
- "solid",
33
- "primitives"
34
- ],
35
- "devDependencies": {
36
- "jsdom": "^19.0.0",
37
- "prettier": "^2.5.1",
38
- "solid-register": "^0.2.5",
39
- "tslib": "^2.3.1",
40
- "tsup": "^5.11.0",
41
- "uvu": "^0.5.2",
42
- "watchlist": "^0.3.1"
43
- },
44
- "peerDependencies": {
45
- "solid-js": "^1.4.1"
46
- }
47
- }
1
+ {
2
+ "name": "@solid-primitives/utils",
3
+ "version": "2.2.1",
4
+ "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
+ "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/solidjs-community/solid-primitives/tree/main/packages/utils#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/solidjs-community/solid-primitives.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/solidjs-community/solid-primitives/issues"
14
+ },
15
+ "private": false,
16
+ "sideEffects": false,
17
+ "type": "module",
18
+ "main": "dist/index.cjs",
19
+ "module": "dist/index.js",
20
+ "types": "dist/index.d.ts",
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsup",
26
+ "test": "uvu -r solid-register",
27
+ "test:watch": "watchlist src test -- npm test"
28
+ },
29
+ "keywords": [
30
+ "utilities",
31
+ "reactivity",
32
+ "solid",
33
+ "primitives"
34
+ ],
35
+ "devDependencies": {
36
+ "jsdom": "^19.0.0",
37
+ "prettier": "^2.7.1",
38
+ "solid-register": "^0.2.5",
39
+ "tslib": "^2.4.0",
40
+ "tsup": "^6.1.2",
41
+ "uvu": "^0.5.4",
42
+ "watchlist": "^0.3.1"
43
+ },
44
+ "peerDependencies": {
45
+ "solid-js": "^1.4.1"
46
+ }
47
+ }