Skip to content

Commit

Permalink
allow deferring firing hooks in explicit u.batch()
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Jan 19, 2024
1 parent 9ee1c07 commit 86302f7
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 22 deletions.
30 changes: 25 additions & 5 deletions dist/uPlot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4539,6 +4539,17 @@ function uPlot(opts, data, then) {
}

let queuedCommit = false;
let deferHooks = false;
let hooksQueue = [];

function flushHooks() {
deferHooks = false;

for (let i = 0; i < hooksQueue.length; i++)
fire(...hooksQueue[i]);

hooksQueue.length = 0;
}

function commit() {
if (!queuedCommit) {
Expand All @@ -4548,10 +4559,15 @@ function uPlot(opts, data, then) {
}

// manual batching (aka immediate mode), skips microtask queue
function batch(fn) {
function batch(fn, _deferHooks = false) {
queuedCommit = true;
deferHooks = _deferHooks;

fn(self);
_commit();

if (_deferHooks && hooksQueue.length > 0)
queueMicrotask(flushHooks);
}

self.batch = batch;
Expand Down Expand Up @@ -5727,10 +5743,14 @@ function uPlot(opts, data, then) {
const hooks = self.hooks = opts.hooks || {};

function fire(evName, a1, a2) {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
if (deferHooks)
hooksQueue.push([evName, a1, a2]);
else {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/uPlot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ declare class uPlot {
redraw(rebuildPaths?: boolean, recalcAxes?: boolean): void;

/** manual batching of multiple ops (aka immediate mode that skips implicit microtask queue), ops, e.g. setScale('x', ...) && setScale('y', ...) */
batch(txn: Function): void;
batch(txn: Function, deferHooks?: boolean): void;

/** destroys DOM, removes resize & scroll listeners, etc. */
destroy(): void;
Expand Down
30 changes: 25 additions & 5 deletions dist/uPlot.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4537,6 +4537,17 @@ function uPlot(opts, data, then) {
}

let queuedCommit = false;
let deferHooks = false;
let hooksQueue = [];

function flushHooks() {
deferHooks = false;

for (let i = 0; i < hooksQueue.length; i++)
fire(...hooksQueue[i]);

hooksQueue.length = 0;
}

function commit() {
if (!queuedCommit) {
Expand All @@ -4546,10 +4557,15 @@ function uPlot(opts, data, then) {
}

// manual batching (aka immediate mode), skips microtask queue
function batch(fn) {
function batch(fn, _deferHooks = false) {
queuedCommit = true;
deferHooks = _deferHooks;

fn(self);
_commit();

if (_deferHooks && hooksQueue.length > 0)
queueMicrotask(flushHooks);
}

self.batch = batch;
Expand Down Expand Up @@ -5725,10 +5741,14 @@ function uPlot(opts, data, then) {
const hooks = self.hooks = opts.hooks || {};

function fire(evName, a1, a2) {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
if (deferHooks)
hooksQueue.push([evName, a1, a2]);
else {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
}
}
}

Expand Down
30 changes: 25 additions & 5 deletions dist/uPlot.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -4540,6 +4540,17 @@ var uPlot = (function () {
}

let queuedCommit = false;
let deferHooks = false;
let hooksQueue = [];

function flushHooks() {
deferHooks = false;

for (let i = 0; i < hooksQueue.length; i++)
fire(...hooksQueue[i]);

hooksQueue.length = 0;
}

function commit() {
if (!queuedCommit) {
Expand All @@ -4549,10 +4560,15 @@ var uPlot = (function () {
}

// manual batching (aka immediate mode), skips microtask queue
function batch(fn) {
function batch(fn, _deferHooks = false) {
queuedCommit = true;
deferHooks = _deferHooks;

fn(self);
_commit();

if (_deferHooks && hooksQueue.length > 0)
queueMicrotask(flushHooks);
}

self.batch = batch;
Expand Down Expand Up @@ -5728,10 +5744,14 @@ var uPlot = (function () {
const hooks = self.hooks = opts.hooks || {};

function fire(evName, a1, a2) {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
if (deferHooks)
hooksQueue.push([evName, a1, a2]);
else {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/uPlot.iife.min.js

Large diffs are not rendered by default.

30 changes: 25 additions & 5 deletions src/uPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,17 @@ export default function uPlot(opts, data, then) {
}

let queuedCommit = false;
let deferHooks = false;
let hooksQueue = [];

function flushHooks() {
deferHooks = false;

for (let i = 0; i < hooksQueue.length; i++)
fire(...hooksQueue[i])

hooksQueue.length = 0;
}

function commit() {
if (!queuedCommit) {
Expand All @@ -2031,10 +2042,15 @@ export default function uPlot(opts, data, then) {
}

// manual batching (aka immediate mode), skips microtask queue
function batch(fn) {
function batch(fn, _deferHooks = false) {
queuedCommit = true;
deferHooks = _deferHooks;

fn(self);
_commit();

if (_deferHooks && hooksQueue.length > 0)
queueMicrotask(flushHooks);
}

self.batch = batch;
Expand Down Expand Up @@ -3210,10 +3226,14 @@ export default function uPlot(opts, data, then) {
const hooks = self.hooks = opts.hooks || {};

function fire(evName, a1, a2) {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
if (deferHooks)
hooksQueue.push([evName, a1, a2]);
else {
if (evName in hooks) {
hooks[evName].forEach(fn => {
fn.call(null, self, a1, a2);
});
}
}
}

Expand Down

0 comments on commit 86302f7

Please sign in to comment.