@solid-primitives/utils 0.3.0 → 0.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
@@ -31,7 +31,10 @@ __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,
35
38
  definite: () => definite,
36
39
  destore: () => destore,
37
40
  entries: () => entries,
@@ -143,6 +146,14 @@ var createCallbackStack = () => {
143
146
  clear
144
147
  };
145
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
+ }
146
157
  function createProxy(traps) {
147
158
  return new Proxy({}, {
148
159
  get: (_, k) => traps.get(k),
@@ -153,6 +164,27 @@ function createProxy(traps) {
153
164
  }
154
165
  });
155
166
  }
167
+ function createTrigger(options) {
168
+ return (0, import_solid_js.createSignal)(void 0, { equals: false, name: options == null ? void 0 : options.name });
169
+ }
170
+ function createTriggerCache(options) {
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(options);
182
+ cache.set(key, trigger);
183
+ }
184
+ trigger[0]();
185
+ }
186
+ };
187
+ }
156
188
  module.exports = __toCommonJS(src_exports);
157
189
  // Annotate the CommonJS export names for ESM import in node:
158
190
  0 && (module.exports = {
@@ -163,7 +195,10 @@ module.exports = __toCommonJS(src_exports);
163
195
  asArray,
164
196
  compare,
165
197
  createCallbackStack,
198
+ createMicrotask,
166
199
  createProxy,
200
+ createTrigger,
201
+ createTriggerCache,
167
202
  definite,
168
203
  destore,
169
204
  entries,
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Accessor, onCleanup } from 'solid-js';
2
- import { OnOptions, EffectFunction, NoInfer } from 'solid-js/types/reactive/signal';
2
+ import { OnOptions, EffectFunction, NoInfer, BaseOptions } from 'solid-js/types/reactive/signal';
3
3
  import { Store } from 'solid-js/store';
4
4
  export { isServer } from 'solid-js/web';
5
5
 
@@ -71,6 +71,12 @@ declare type Fallback<T, F = NonNullable<T>> = NonNullable<T> | F;
71
71
  declare type Destore<T extends Object> = {
72
72
  [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
73
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];
74
80
  declare type Position = {
75
81
  x: number;
76
82
  y: number;
@@ -251,6 +257,12 @@ declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>(
251
257
  execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
252
258
  clear: Fn;
253
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;
254
266
  /** WIP: an easier to setup and type Proxy */
255
267
  declare function createProxy<T extends Record<string | symbol, any>>(traps: {
256
268
  get: <K extends keyof T>(key: K) => T[K];
@@ -260,5 +272,34 @@ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
260
272
  get: <K extends keyof T>(key: K) => T[K];
261
273
  set?: undefined;
262
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(options?: BaseOptions): 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>(options?: BaseOptions): TriggerCache<T>;
263
304
 
264
- 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, Truthy, UnionToIntersection, Values, access, accessArray, accessAsArray, asAccessor, asArray, compare, createCallbackStack, createProxy, 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 };
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;
@@ -80,6 +80,14 @@ var createCallbackStack = () => {
80
80
  clear
81
81
  };
82
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
+ }
83
91
  function createProxy(traps) {
84
92
  return new Proxy({}, {
85
93
  get: (_, k) => traps.get(k),
@@ -90,6 +98,27 @@ function createProxy(traps) {
90
98
  }
91
99
  });
92
100
  }
101
+ function createTrigger(options) {
102
+ return createSignal(void 0, { equals: false, name: options == null ? void 0 : options.name });
103
+ }
104
+ function createTriggerCache(options) {
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(options);
116
+ cache.set(key, trigger);
117
+ }
118
+ trigger[0]();
119
+ }
120
+ };
121
+ }
93
122
  export {
94
123
  access,
95
124
  accessArray,
@@ -98,7 +127,10 @@ export {
98
127
  asArray,
99
128
  compare,
100
129
  createCallbackStack,
130
+ createMicrotask,
101
131
  createProxy,
132
+ createTrigger,
133
+ createTriggerCache,
102
134
  definite,
103
135
  destore,
104
136
  entries,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "0.3.0",
3
+ "version": "0.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",