New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@solid-primitives/scheduled

Package Overview
Dependencies
Maintainers
3
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solid-primitives/scheduled - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

35

dist/index.d.ts

@@ -0,1 +1,3 @@

import { Accessor } from 'solid-js';
type ScheduleCallback = <Args extends unknown[]>(callback: (...args: Args) => void, wait?: number) => Scheduled<Args>;

@@ -15,2 +17,4 @@ interface Scheduled<Args extends unknown[]> {

*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#debounce
*
* @example

@@ -33,2 +37,4 @@ * ```ts

*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#throttle
*
* @example

@@ -53,2 +59,4 @@ * ```ts

*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#scheduleidle
*
* @example

@@ -72,2 +80,4 @@ * ```ts

*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#leading
*
* @example

@@ -81,3 +91,26 @@ * ```ts

declare function leading<Args extends unknown[]>(schedule: ScheduleCallback, callback: (...args: Args) => void, wait?: number): Scheduled<Args>;
/**
* Creates a signal used for scheduling execution of solid computations by tracking.
*
* @param schedule Schedule the invalidate function (can be {@link debounce} or {@link throttle})
* @returns A function used to track the signal. It returns `true` if the signal is dirty *(callback should be called)* and `false` otherwise.
*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#createScheduled
*
* @example
* ```ts
* const debounced = createScheduled(fn => debounce(fn, 250));
*
* createEffect(() => {
* // track source signal
* const value = count();
* // track the debounced signal and check if it's dirty
* if (debounced()) {
* console.log('count', value);
* }
* });
* ```
*/
declare function createScheduled(schedule: (callback: VoidFunction) => VoidFunction): Accessor<boolean>;
export { ScheduleCallback, Scheduled, debounce, leading, scheduleIdle, throttle };
export { ScheduleCallback, Scheduled, createScheduled, debounce, leading, scheduleIdle, throttle };

78

dist/index.js

@@ -1,2 +0,2 @@

import { getOwner, onCleanup } from 'solid-js';
import { getOwner, onCleanup, createSignal, getListener } from 'solid-js';

@@ -36,25 +36,31 @@ // src/index.ts

};
var scheduleIdle = window.requestIdleCallback ? (callback, maxWait) => {
let isDeferred = false, id, lastArgs;
const deferred = (...args) => {
lastArgs = args;
if (isDeferred)
return;
isDeferred = true;
id = requestIdleCallback(
() => {
callback(...lastArgs);
isDeferred = false;
},
{ timeout: maxWait }
);
};
const clear = () => {
cancelIdleCallback(id);
isDeferred = false;
};
if (getOwner())
onCleanup(clear);
return Object.assign(deferred, { clear });
} : (callback) => throttle(callback);
var scheduleIdle = (
// requestIdleCallback is not supported in Safari
window.requestIdleCallback ? (callback, maxWait) => {
let isDeferred = false, id, lastArgs;
const deferred = (...args) => {
lastArgs = args;
if (isDeferred)
return;
isDeferred = true;
id = requestIdleCallback(
() => {
callback(...lastArgs);
isDeferred = false;
},
{ timeout: maxWait }
);
};
const clear = () => {
cancelIdleCallback(id);
isDeferred = false;
};
if (getOwner())
onCleanup(clear);
return Object.assign(deferred, { clear });
} : (
// fallback to setTimeout (throttle)
(callback) => throttle(callback)
)
);
function leading(schedule, callback, wait) {

@@ -78,3 +84,25 @@ let isScheduled = false;

}
function createScheduled(schedule) {
let listeners = 0;
let isDirty = false;
const [track, dirty] = createSignal(void 0, { equals: false });
const call = schedule(() => {
isDirty = true;
dirty();
});
return () => {
if (!isDirty)
call(), track();
if (isDirty) {
isDirty = !!listeners;
return true;
}
if (getListener()) {
listeners++;
onCleanup(() => listeners--);
}
return false;
};
}
export { debounce, leading, scheduleIdle, throttle };
export { createScheduled, debounce, leading, scheduleIdle, throttle };

@@ -1,2 +0,2 @@

import 'solid-js';
import { createSignal, getListener, onCleanup } from 'solid-js';

@@ -27,3 +27,25 @@ // src/index.ts

}
function createScheduled(schedule) {
let listeners = 0;
let isDirty = false;
const [track, dirty] = createSignal(void 0, { equals: false });
const call = schedule(() => {
isDirty = true;
dirty();
});
return () => {
if (!isDirty)
call(), track();
if (isDirty) {
isDirty = !!listeners;
return true;
}
if (getListener()) {
listeners++;
onCleanup(() => listeners--);
}
return false;
};
}
export { debounce, leading, scheduleIdle, throttle };
export { createScheduled, debounce, leading, scheduleIdle, throttle };
{
"name": "@solid-primitives/scheduled",
"version": "1.2.1",
"version": "1.3.0",
"description": "Primitives for creating scheduled — throttled or debounced — callbacks.",

@@ -20,3 +20,3 @@ "contributors": [

"name": "scheduled",
"stage": 2,
"stage": 3,
"list": [

@@ -26,3 +26,4 @@ "debounce",

"scheduleIdle",
"leading"
"leading",
"createSchedule"
],

@@ -87,3 +88,4 @@ "category": "Utilities"

"devDependencies": {
"@solid-primitives/timer": "^1.3.5"
"@solid-primitives/timer": "^1.3.5",
"@solidjs/router": "^0.7.0"
},

@@ -90,0 +92,0 @@ "peerDependencies": {

@@ -18,2 +18,4 @@ <p>

- [`leading`](#leading) - Creates a scheduled and cancellable callback that will be called on **leading** edge.
- [`createScheduled`](#createscheduled) - Creates a signal used for scheduling execution of solid computations by tracking.
- [Scheduling explanation](#scheduling-explanation)

@@ -108,2 +110,48 @@ ## Installation

## `createScheduled`
Creates a signal used for scheduling execution of solid computations by tracking.
### How to use it
`createScheduled` takes only one parameter - a `schedule` function. This function is called with a callback that should be scheduled. It should return a function for triggering the timeout.
```ts
// e.g. with debounce
createScheduled(fn => debounce(fn, 1000));
// e.g. with throttle
createScheduled(fn => throttle(fn, 1000));
// e.g. with leading debounce
createScheduled(fn => leading(debounce, fn, 1000));
// e.g. with leading throttle
createScheduled(fn => leading(throttle, fn, 1000));
```
It returns a signal that can be used to schedule execution of a solid computation. The signal returns `true` if it's dirty _(callback should be called)_ and `false` otherwise.
```ts
import { createScheduled, debounce } from "@solid-primitives/scheduled";
const scheduled = createScheduled(fn => debounce(fn, 1000));
const [count, setCount] = createSignal(0);
createEffect(() => {
// track source signal
const value = count();
// track the debounced signal and check if it's dirty
if (scheduled()) {
console.log("count", value);
}
});
// or with createMemo
const debouncedCount = createMemo((p: number = 0) => {
// track source signal
const value = count();
// track the debounced signal and check if it's dirty
return scheduled() ? value : p;
});
```
## Scheduling explanation

@@ -110,0 +158,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc