@solid-primitives/utils 0.2.2 → 0.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/README.md CHANGED
@@ -10,26 +10,6 @@ npm install @solid-primitives/utils
10
10
  yarn add @solid-primitives/utils
11
11
  ```
12
12
 
13
- ## Package Entries
14
-
15
- - [`*` - `@solid-primitives/utils`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/modules/index.md) - General utility functions and types for authoring primitives.
16
- - [`/fp` - `@solid-primitives/utils/fp`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/modules/fp.md) - Helpers for making changes to immutable data.
17
- - [`/setter` - `@solid-primitives/utils/setter`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/modules/setter.md) - Modified helpers from the `/fp`, but optimized for usage with signal setters.
18
-
19
- Import accordingly.
20
-
21
- ```ts
22
- import { access, promiseTimeout, MaybeAccessor } from "@solid-primitives/utils";
23
-
24
- // between these two some function names will overlap.
25
- import { pick, omit, filter } from "@solid-primitives/utils/fp";
26
- import { push, filter, drop } from "@solid-primitives/utils/setter";
27
- ```
28
-
29
- ## >>> [Modules documentation](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/README.md) <<<
30
-
31
- Documentation auto-generated from the [Typescript source files](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/src) using [typedoc](https://typedoc.org) + [markdown plugin](https://www.npmjs.com/package/typedoc-plugin-markdown).
32
-
33
13
  ## Changelog
34
14
 
35
15
  <details>
package/dist/index.cjs CHANGED
@@ -31,7 +31,11 @@ __export(src_exports, {
31
31
  asArray: () => asArray,
32
32
  compare: () => compare,
33
33
  createCallbackStack: () => createCallbackStack,
34
+ createMicrotask: () => createMicrotask,
34
35
  createProxy: () => createProxy,
36
+ createTrigger: () => createTrigger,
37
+ createTriggerCache: () => createTriggerCache,
38
+ definite: () => definite,
35
39
  destore: () => destore,
36
40
  entries: () => entries,
37
41
  forEach: () => forEach,
@@ -57,7 +61,9 @@ __export(src_exports, {
57
61
  raceTimeout: () => raceTimeout,
58
62
  tuple: () => tuple,
59
63
  warn: () => warn,
60
- withAccess: () => withAccess
64
+ withAccess: () => withAccess,
65
+ withFallback: () => withFallback,
66
+ withFallbackFn: () => withFallbackFn
61
67
  });
62
68
  var import_solid_js = require("solid-js");
63
69
  var import_web = require("solid-js/web");
@@ -76,6 +82,13 @@ var isArray = Array.isArray;
76
82
  var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
77
83
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
78
84
  var tuple = (input) => input;
85
+ var definite = (v) => v;
86
+ function withFallback(v, fallback) {
87
+ return isDefined(v) ? v : fallback;
88
+ }
89
+ function withFallbackFn(v, fallback) {
90
+ return isDefined(v) ? v : fallback();
91
+ }
79
92
  var includes = (arr, ...items) => {
80
93
  for (const item of arr) {
81
94
  if (items.includes(item))
@@ -133,6 +146,14 @@ var createCallbackStack = () => {
133
146
  clear
134
147
  };
135
148
  };
149
+ function createMicrotask(fn) {
150
+ let calls = 0;
151
+ let args;
152
+ return (...a) => {
153
+ args = a, calls++;
154
+ queueMicrotask(() => --calls === 0 && fn(...args));
155
+ };
156
+ }
136
157
  function createProxy(traps) {
137
158
  return new Proxy({}, {
138
159
  get: (_, k) => traps.get(k),
@@ -143,6 +164,27 @@ function createProxy(traps) {
143
164
  }
144
165
  });
145
166
  }
167
+ function createTrigger() {
168
+ return (0, import_solid_js.createSignal)(void 0, { equals: false });
169
+ }
170
+ function createTriggerCache() {
171
+ const cache = /* @__PURE__ */ new Map();
172
+ return {
173
+ dirty: (key) => {
174
+ var _a;
175
+ return (_a = cache.get(key)) == null ? void 0 : _a[1]();
176
+ },
177
+ dirtyAll: () => cache.forEach((s) => s[1]()),
178
+ track(key) {
179
+ let trigger = cache.get(key);
180
+ if (!trigger) {
181
+ trigger = createTrigger();
182
+ cache.set(key, trigger);
183
+ }
184
+ trigger[0]();
185
+ }
186
+ };
187
+ }
146
188
  module.exports = __toCommonJS(src_exports);
147
189
  // Annotate the CommonJS export names for ESM import in node:
148
190
  0 && (module.exports = {
@@ -153,7 +195,11 @@ module.exports = __toCommonJS(src_exports);
153
195
  asArray,
154
196
  compare,
155
197
  createCallbackStack,
198
+ createMicrotask,
156
199
  createProxy,
200
+ createTrigger,
201
+ createTriggerCache,
202
+ definite,
157
203
  destore,
158
204
  entries,
159
205
  forEach,
@@ -179,5 +225,7 @@ module.exports = __toCommonJS(src_exports);
179
225
  raceTimeout,
180
226
  tuple,
181
227
  warn,
182
- withAccess
228
+ withAccess,
229
+ withFallback,
230
+ withFallbackFn
183
231
  });
package/dist/index.d.ts CHANGED
@@ -64,12 +64,19 @@ declare type PrimitiveValue = string | boolean | number | bigint | symbol | null
64
64
  declare type FalsyValue = false | 0 | "" | null | undefined;
65
65
  declare type Truthy<T> = Exclude<T, FalsyValue>;
66
66
  declare type Falsy<T> = Extract<T, FalsyValue>;
67
+ declare type Fallback<T, F = NonNullable<T>> = NonNullable<T> | F;
67
68
  /**
68
69
  * Destructible store object, with values changed to accessors
69
70
  */
70
71
  declare type Destore<T extends Object> = {
71
72
  [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
72
73
  };
74
+ declare type TriggerCache<T> = {
75
+ track: Get<T>;
76
+ dirty: Get<T>;
77
+ dirtyAll: Fn;
78
+ };
79
+ declare type Trigger = [track: Fn, dirty: Fn];
73
80
  declare type Position = {
74
81
  x: number;
75
82
  y: number;
@@ -95,15 +102,37 @@ declare const isNumber: (val: any) => val is number;
95
102
  declare const isString: (val: unknown) => val is string;
96
103
  declare const isObject: (val: any) => val is object;
97
104
  declare const isArray: (val: any) => val is any[];
105
+ /**
106
+ * Check if the value is an instance of ___
107
+ */
98
108
  declare const ofClass: (v: any, c: AnyClass) => boolean;
99
109
  declare const compare: (a: any, b: any) => number;
100
110
  /**
101
111
  * for creating tuples by inferring type
102
112
  * @example
103
113
  * const users = tuple(["John", "Jeff", "Joe"]);
104
- * // users: [string, string, string]
114
+ * users // T: [string, string, string]
105
115
  */
106
116
  declare const tuple: <T extends any[] | []>(input: T) => T;
117
+ /**
118
+ * Removes the `null` and `undefined` from the type.
119
+ * @warning **Obviously use with caution**
120
+ */
121
+ declare const definite: <T>(v: T) => NonNullable<T>;
122
+ /**
123
+ * Get the value if it is defined, or get the fallback otherwise
124
+ * @param v the value to check
125
+ * @param fallback the fallback value
126
+ */
127
+ declare function withFallback<T>(v: T, fallback: NonNullable<T>): Fallback<T>;
128
+ declare function withFallback<T, F>(v: T, fallback: F): Fallback<T, F>;
129
+ /**
130
+ * Get the value if it is defined, or get the fallback otherwise
131
+ * @param v the value to check
132
+ * @param fallback function returning a fallback value
133
+ */
134
+ declare function withFallbackFn<T>(v: T, fallback: () => NonNullable<T>): Fallback<T>;
135
+ declare function withFallbackFn<T, F>(v: T, fallback: () => F): Fallback<T, F>;
107
136
  /** `Array.prototype.includes()` without so strict types. Also allows for checking for multiple items */
108
137
  declare const includes: (arr: any[], ...items: any) => boolean;
109
138
  /**
@@ -228,6 +257,12 @@ declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>(
228
257
  execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
229
258
  clear: Fn;
230
259
  };
260
+ /**
261
+ * Group synchronous function calls.
262
+ * @param fn
263
+ * @returns `fn`
264
+ */
265
+ declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
231
266
  /** WIP: an easier to setup and type Proxy */
232
267
  declare function createProxy<T extends Record<string | symbol, any>>(traps: {
233
268
  get: <K extends keyof T>(key: K) => T[K];
@@ -237,5 +272,34 @@ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
237
272
  get: <K extends keyof T>(key: K) => T[K];
238
273
  set?: undefined;
239
274
  }): Readonly<T>;
275
+ /**
276
+ * Set listeners in reactive computations and then trigger them when you want.
277
+ * @returns `[track function, dirty function]`
278
+ * @example
279
+ * const [track, dirty] = createTrigger()
280
+ * createEffect(() => {
281
+ * track()
282
+ * ...
283
+ * })
284
+ * // later
285
+ * dirty()
286
+ */
287
+ declare function createTrigger(): Trigger;
288
+ /**
289
+ * Set listeners in reactive computations and then trigger them when you want. Cache trackers by a `key`.
290
+ * @returns `{ track, dirty, dirtyAll }` functions
291
+ * `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.
292
+ * @example
293
+ * const { track, dirty } = createTriggerCache()
294
+ * createEffect(() => {
295
+ * track(1)
296
+ * ...
297
+ * })
298
+ * // later
299
+ * dirty(1)
300
+ * // this won't cause an update:
301
+ * dirty(2)
302
+ */
303
+ declare function createTriggerCache<T>(): TriggerCache<T>;
240
304
 
241
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, Clear, DeepPartialAny, Destore, Directive, ExtractIfPossible, Falsy, FalsyValue, Fn, Get, ItemsOf, ItemsOfMany, Keys, LiteralKey, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Noop, OnAccessEffectFunction, Position, PrimitiveValue, Truthy, UnionToIntersection, Values, access, accessArray, accessAsArray, asAccessor, asArray, compare, createCallbackStack, createProxy, destore, entries, forEach, forEachEntry, includes, isArray, isBoolean, isClient, isDefined, isDev, isFunction, isNumber, isObject, isProd, isString, keys, noop, ofClass, onAccess, onRootCleanup, promiseTimeout, raceTimeout, tuple, warn, withAccess };
305
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, Clear, DeepPartialAny, Destore, Directive, ExtractIfPossible, Fallback, Falsy, FalsyValue, Fn, Get, ItemsOf, ItemsOfMany, Keys, LiteralKey, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Noop, OnAccessEffectFunction, Position, PrimitiveValue, Trigger, TriggerCache, Truthy, UnionToIntersection, Values, access, accessArray, accessAsArray, asAccessor, asArray, compare, createCallbackStack, createMicrotask, createProxy, createTrigger, createTriggerCache, definite, destore, entries, forEach, forEachEntry, includes, isArray, isBoolean, isClient, isDefined, isDev, isFunction, isNumber, isObject, isProd, isString, keys, noop, ofClass, onAccess, onRootCleanup, promiseTimeout, raceTimeout, tuple, warn, withAccess, withFallback, withFallbackFn };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { getOwner, onCleanup, on, DEV } from "solid-js";
2
+ import { getOwner, onCleanup, on, createSignal, DEV } from "solid-js";
3
3
  import { isServer } from "solid-js/web";
4
4
  var noop = () => void 0;
5
5
  var isClient = !isServer;
@@ -16,6 +16,13 @@ var isArray = Array.isArray;
16
16
  var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
17
17
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
18
18
  var tuple = (input) => input;
19
+ var definite = (v) => v;
20
+ function withFallback(v, fallback) {
21
+ return isDefined(v) ? v : fallback;
22
+ }
23
+ function withFallbackFn(v, fallback) {
24
+ return isDefined(v) ? v : fallback();
25
+ }
19
26
  var includes = (arr, ...items) => {
20
27
  for (const item of arr) {
21
28
  if (items.includes(item))
@@ -73,6 +80,14 @@ var createCallbackStack = () => {
73
80
  clear
74
81
  };
75
82
  };
83
+ function createMicrotask(fn) {
84
+ let calls = 0;
85
+ let args;
86
+ return (...a) => {
87
+ args = a, calls++;
88
+ queueMicrotask(() => --calls === 0 && fn(...args));
89
+ };
90
+ }
76
91
  function createProxy(traps) {
77
92
  return new Proxy({}, {
78
93
  get: (_, k) => traps.get(k),
@@ -83,6 +98,27 @@ function createProxy(traps) {
83
98
  }
84
99
  });
85
100
  }
101
+ function createTrigger() {
102
+ return createSignal(void 0, { equals: false });
103
+ }
104
+ function createTriggerCache() {
105
+ const cache = /* @__PURE__ */ new Map();
106
+ return {
107
+ dirty: (key) => {
108
+ var _a;
109
+ return (_a = cache.get(key)) == null ? void 0 : _a[1]();
110
+ },
111
+ dirtyAll: () => cache.forEach((s) => s[1]()),
112
+ track(key) {
113
+ let trigger = cache.get(key);
114
+ if (!trigger) {
115
+ trigger = createTrigger();
116
+ cache.set(key, trigger);
117
+ }
118
+ trigger[0]();
119
+ }
120
+ };
121
+ }
86
122
  export {
87
123
  access,
88
124
  accessArray,
@@ -91,7 +127,11 @@ export {
91
127
  asArray,
92
128
  compare,
93
129
  createCallbackStack,
130
+ createMicrotask,
94
131
  createProxy,
132
+ createTrigger,
133
+ createTriggerCache,
134
+ definite,
95
135
  destore,
96
136
  entries,
97
137
  forEach,
@@ -117,5 +157,7 @@ export {
117
157
  raceTimeout,
118
158
  tuple,
119
159
  warn,
120
- withAccess
160
+ withAccess,
161
+ withFallback,
162
+ withFallbackFn
121
163
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "0.2.2",
3
+ "version": "0.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
  "license": "MIT",
@@ -24,8 +24,7 @@
24
24
  "scripts": {
25
25
  "build": "tsup",
26
26
  "test": "uvu -r solid-register",
27
- "watch-test": "watchlist src test -- npm test",
28
- "docs": "typedoc"
27
+ "watch-test": "watchlist src test -- npm test"
29
28
  },
30
29
  "keywords": [
31
30
  "utilities",
@@ -39,8 +38,6 @@
39
38
  "solid-register": "^0.1.1",
40
39
  "tslib": "^2.3.1",
41
40
  "tsup": "^5.11.0",
42
- "typedoc": "^0.22.10",
43
- "typedoc-plugin-markdown": "^3.11.8",
44
41
  "uvu": "^0.5.2",
45
42
  "watchlist": "^0.3.1"
46
43
  },