heya-defer

    1.1.0 • Public • Published

    defer

    Build status Dependencies devDependencies NPM version

    This module provides a functionality similar to process.nextTick() of node.js, but in browser. It tries to use setImmediate(), if available, reverting to postMessage(), and falling back on setTimeout(). In general, the module's implementation follows setImmediate polyfill outline.

    The module provides a foundation for running micro tasks, yet giving a chance for a browser to run its internal tasks as well including layout, rendering, and garbage collection.

    Additionally a batching is provided for "read" and "write" operations.

    "Read" and "write" tasks are used for DOM operations to prevent an excessive DOM thrashing. "Read" operation, such as reading of layout-related style properties, or node geometry measuring of any sort, can delay everything synchronously until a browser finishes a layout and assigns all sizes, and offesets. "Write" operations can mutate DOM invalidating everyting, and triggering re-application of CSS rules, and re-layout. Interleaving such operations slows down everything considerably. Batching them together by kind speeds things up.

    This module can be used with AMD and globals. In the latter case it can be accessed as window.heya.defer.

    The main API

    • nextTick() — runs a micro task during a next tick.
    • asap() — adds a micro task to the end of a current task queue.
    • submitRead() — submits a "read" task. All "read" tasks run as a single batch.
    • submitWrite() — submits a "write" task. All "write" tasks run as a single batch.

    First scheduled task of a certain type will run before the next scheduled task of the same type.

    nextTick()

    This procedure takes a single argument: a micro task as a procedure without arguments. It is queued, and scheduled to be run at the next time slice.

    asap()

    This procedure takes a single argument: a micro task as a procedure without arguments. "Asap" tasks are meant to be run as soon as possible, but if the current time slice cannot be scheduled at the moment, an "asap" tasks is scheduled as a normal "next tick" task. There is no inherent precendence order between "asap" and "next tick" tasks scheduled for the same time slice — they can be run in any relative order.

    submitRead()

    This procedure takes a single argument: a micro task as a procedure without arguments. It queues "read" tasks to be run as a single batch this time slice or the next one. "Read" tasks are run before general and "write" tasks.

    submitWrite()

    This procedure takes a single argument: a micro task as a procedure without arguments. It queues "write" tasks to be run as a single batch this time slice, or the next one. "Write" tasks are run after "read" tasks and general tasks.

    defer-promise

    This module is based on defer module. It exposes three events as promises:

    • When asap() is run.
    • When nextTick() is run.
    • When a "read" butch is run.
    • When a "write" batch as run.

    An application has a chance to add event handlers to run, when a promise is fullfilled using the usual means: then() and done() methods, and combine them with other promises conditionally.

    The main API

    The main API is represented by three functions that take up to one argument, and return corresponding promises.

    The only argument is a Deferred compatible constructor similar to provided by heya-async module (Deferred or FastDeferred). If not specified, the standard Promise is used.

    whenAsap()

    It takes no arguments and returns a promise, which is resolved when defer.asap() tasks are run.

    whenNext()

    It takes no arguments and returns a promise, which is resolved when defer.nextTick() tasks are run.

    whenRead()

    It takes no arguments and returns a promise, which is resolved when defer.submitRead() tasks are run.

    whenWrite()

    It takes no arguments and returns a promise, which is resolved when defer.submitWrite() tasks are run.

    Versions

    • 1.1.0 — Reworked defer-promise module.
    • 1.0.1 — Added globals-based versions of modules.
    • 1.0.0 — The initial public release.

    License

    BSD

    Install

    npm i heya-defer

    DownloadsWeekly Downloads

    3

    Version

    1.1.0

    License

    BSD-3-Clause

    Last publish

    Collaborators

    • elazutkin