Skip to content

Commit b7a33e2

Browse files
committed
expose internal pubsub creator as uPlot.sync(key). close #354, #430.
1 parent 117dcb2 commit b7a33e2

9 files changed

Lines changed: 158 additions & 79 deletions

File tree

demos/sync-cursor.html

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<script src="../dist/uPlot.iife.js"></script>
1818
<h2 id="wait">Loading lib....</h2>
1919

20+
<button id="sync">Disable Sync</button>
21+
22+
<br>
23+
2024
<script>
2125
function round2(val) {
2226
return Math.round(val * 100) / 100;
@@ -79,13 +83,15 @@ <h2 id="wait">Loading lib....</h2>
7983
function makeChart(data) {
8084
console.time('chart');
8185

86+
let mooSync = uPlot.sync("moo");
87+
8288
const cursorOpts = {
8389
lock: true,
8490
focus: {
8591
prox: 16,
8692
},
8793
sync: {
88-
key: "moo",
94+
key: mooSync.key,
8995
setSeries: true,
9096
},
9197
};
@@ -194,6 +200,28 @@ <h2 id="wait">Loading lib....</h2>
194200

195201
wait.textContent = "Done!";
196202
console.timeEnd('chart');
203+
204+
let synced = true;
205+
let syncBtn = document.getElementById('sync');
206+
207+
let fooSync = uPlot.sync("foo");
208+
209+
syncBtn.onclick = () => {
210+
synced = !synced;
211+
212+
if (synced) {
213+
mooSync.sub(uplot1);
214+
mooSync.sub(uplot2);
215+
mooSync.sub(uplot3);
216+
syncBtn.textContent = 'Disable Sync';
217+
}
218+
else {
219+
mooSync.unsub(uplot1);
220+
mooSync.unsub(uplot2);
221+
mooSync.unsub(uplot3);
222+
syncBtn.textContent = 'Enable Sync';
223+
}
224+
}
197225
/*
198226
setTimeout(() => {
199227
uplot1.destroy();

dist/uPlot.cjs.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,24 +1305,31 @@ const yScaleOpts = assign({}, xScaleOpts, {
13051305

13061306
const syncs = {};
13071307

1308-
function _sync(opts) {
1309-
let clients = [];
1310-
1311-
return {
1312-
sub(client) {
1313-
clients.push(client);
1314-
},
1315-
unsub(client) {
1316-
clients = clients.filter(c => c != client);
1317-
},
1318-
pub(type, self, x, y, w, h, i) {
1319-
if (clients.length > 1) {
1320-
clients.forEach(client => {
1321-
client != self && client.pub(type, self, x, y, w, h, i);
1322-
});
1308+
function _sync(key, opts) {
1309+
let s = syncs[key];
1310+
1311+
if (!s) {
1312+
let clients = [];
1313+
1314+
s = {
1315+
key,
1316+
sub(client) {
1317+
clients.push(client);
1318+
},
1319+
unsub(client) {
1320+
clients = clients.filter(c => c != client);
1321+
},
1322+
pub(type, self, x, y, w, h, i) {
1323+
for (let i = 0; i < clients.length; i++)
1324+
clients[i] != self && clients[i].pub(type, self, x, y, w, h, i);
13231325
}
1324-
}
1325-
};
1326+
};
1327+
1328+
if (key != null)
1329+
syncs[key] = s;
1330+
}
1331+
1332+
return s;
13261333
}
13271334

13281335
function orient(u, seriesIdx, cb) {
@@ -4319,7 +4326,7 @@ function uPlot(opts, data, then) {
43194326

43204327
const syncKey = syncOpts.key;
43214328

4322-
const sync = (syncKey != null ? (syncs[syncKey] = syncs[syncKey] || _sync()) : _sync());
4329+
const sync = _sync(syncKey);
43234330

43244331
sync.sub(self);
43254332

@@ -4383,6 +4390,10 @@ uPlot.orient = orient;
43834390
uPlot.tzDate = tzDate;
43844391
}
43854392

4393+
{
4394+
uPlot.sync = _sync;
4395+
}
4396+
43864397
{
43874398
uPlot.addGap = addGap;
43884399
uPlot.clipGaps = clipGaps;

dist/uPlot.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ declare class uPlot {
130130

131131
/** helper function for grabbing proper drawing orientation vars and fns for a plot instance (all dims in canvas pixels) */
132132
static orient: (u: uPlot, seriesIdx: number, callback: uPlot.OrientCallback) => any;
133+
134+
/** returns a pub/sub instance shared by all plots usng the provided key */
135+
static sync: (key: string) => uPlot.SyncPubSub;
133136
}
134137

135138
export = uPlot;
@@ -346,6 +349,13 @@ declare namespace uPlot {
346349
over?: boolean; // true
347350
}
348351

352+
export interface SyncPubSub {
353+
key: string;
354+
sub: (client: uPlot) => void;
355+
unsub: (client: uPlot) => void;
356+
pub: (type: string, client: uPlot, x: number, y: number, w: number, h: number, i: number) => void;
357+
}
358+
349359
export namespace Cursor {
350360
export type LeftTop = [left: number, top: number];
351361

dist/uPlot.esm.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,24 +1303,31 @@ const yScaleOpts = assign({}, xScaleOpts, {
13031303

13041304
const syncs = {};
13051305

1306-
function _sync(opts) {
1307-
let clients = [];
1308-
1309-
return {
1310-
sub(client) {
1311-
clients.push(client);
1312-
},
1313-
unsub(client) {
1314-
clients = clients.filter(c => c != client);
1315-
},
1316-
pub(type, self, x, y, w, h, i) {
1317-
if (clients.length > 1) {
1318-
clients.forEach(client => {
1319-
client != self && client.pub(type, self, x, y, w, h, i);
1320-
});
1306+
function _sync(key, opts) {
1307+
let s = syncs[key];
1308+
1309+
if (!s) {
1310+
let clients = [];
1311+
1312+
s = {
1313+
key,
1314+
sub(client) {
1315+
clients.push(client);
1316+
},
1317+
unsub(client) {
1318+
clients = clients.filter(c => c != client);
1319+
},
1320+
pub(type, self, x, y, w, h, i) {
1321+
for (let i = 0; i < clients.length; i++)
1322+
clients[i] != self && clients[i].pub(type, self, x, y, w, h, i);
13211323
}
1322-
}
1323-
};
1324+
};
1325+
1326+
if (key != null)
1327+
syncs[key] = s;
1328+
}
1329+
1330+
return s;
13241331
}
13251332

13261333
function orient(u, seriesIdx, cb) {
@@ -4317,7 +4324,7 @@ function uPlot(opts, data, then) {
43174324

43184325
const syncKey = syncOpts.key;
43194326

4320-
const sync = (syncKey != null ? (syncs[syncKey] = syncs[syncKey] || _sync()) : _sync());
4327+
const sync = _sync(syncKey);
43214328

43224329
sync.sub(self);
43234330

@@ -4381,6 +4388,10 @@ uPlot.orient = orient;
43814388
uPlot.tzDate = tzDate;
43824389
}
43834390

4391+
{
4392+
uPlot.sync = _sync;
4393+
}
4394+
43844395
{
43854396
uPlot.addGap = addGap;
43864397
uPlot.clipGaps = clipGaps;

dist/uPlot.iife.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,24 +1306,31 @@ var uPlot = (function () {
13061306

13071307
var syncs = {};
13081308

1309-
function _sync(opts) {
1310-
var clients = [];
1311-
1312-
return {
1313-
sub: function sub(client) {
1314-
clients.push(client);
1315-
},
1316-
unsub: function unsub(client) {
1317-
clients = clients.filter(c => c != client);
1318-
},
1319-
pub: function pub(type, self, x, y, w, h, i) {
1320-
if (clients.length > 1) {
1321-
clients.forEach(client => {
1322-
client != self && client.pub(type, self, x, y, w, h, i);
1323-
});
1309+
function _sync(key, opts) {
1310+
var s = syncs[key];
1311+
1312+
if (!s) {
1313+
var clients = [];
1314+
1315+
s = {
1316+
key: key,
1317+
sub: function sub(client) {
1318+
clients.push(client);
1319+
},
1320+
unsub: function unsub(client) {
1321+
clients = clients.filter(c => c != client);
1322+
},
1323+
pub: function pub(type, self, x, y, w, h, i) {
1324+
for (var i$1 = 0; i$1 < clients.length; i$1++)
1325+
{ clients[i$1] != self && clients[i$1].pub(type, self, x, y, w, h, i$1); }
13241326
}
1325-
}
1326-
};
1327+
};
1328+
1329+
if (key != null)
1330+
{ syncs[key] = s; }
1331+
}
1332+
1333+
return s;
13271334
}
13281335

13291336
function orient(u, seriesIdx, cb) {
@@ -4345,7 +4352,7 @@ var uPlot = (function () {
43454352

43464353
var syncKey = syncOpts.key;
43474354

4348-
var sync = (syncKey != null ? (syncs[syncKey] = syncs[syncKey] || _sync()) : _sync());
4355+
var sync = _sync(syncKey);
43494356

43504357
sync.sub(self);
43514358

@@ -4409,6 +4416,10 @@ var uPlot = (function () {
44094416
uPlot.tzDate = tzDate;
44104417
}
44114418

4419+
{
4420+
uPlot.sync = _sync;
4421+
}
4422+
44124423
{
44134424
uPlot.addGap = addGap;
44144425
uPlot.clipGaps = clipGaps;

dist/uPlot.iife.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"homepage": "https://github.com/leeoniya/uPlot#readme",
4141
"devDependencies": {
4242
"@rollup/plugin-buble": "^0.21.3",
43-
"rollup": "^2.38.5",
43+
"rollup": "^2.39.0",
4444
"rollup-plugin-terser": "^7.0.2"
4545
}
4646
}

src/sync.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
export const syncs = {};
22

3-
export function _sync(opts) {
4-
let clients = [];
3+
export function _sync(key, opts) {
4+
let s = syncs[key];
55

6-
return {
7-
sub(client) {
8-
clients.push(client);
9-
},
10-
unsub(client) {
11-
clients = clients.filter(c => c != client);
12-
},
13-
pub(type, self, x, y, w, h, i) {
14-
if (clients.length > 1) {
15-
clients.forEach(client => {
16-
client != self && client.pub(type, self, x, y, w, h, i);
17-
});
6+
if (!s) {
7+
let clients = [];
8+
9+
s = {
10+
key,
11+
sub(client) {
12+
clients.push(client);
13+
},
14+
unsub(client) {
15+
clients = clients.filter(c => c != client);
16+
},
17+
pub(type, self, x, y, w, h, i) {
18+
for (let i = 0; i < clients.length; i++)
19+
clients[i] != self && clients[i].pub(type, self, x, y, w, h, i);
1820
}
19-
}
20-
};
21+
};
22+
23+
if (key != null)
24+
syncs[key] = s;
25+
}
26+
27+
return s;
2128
}

src/uPlot.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ import {
165165
legendFill,
166166
} from './opts';
167167

168-
import {
169-
_sync,
170-
syncs,
171-
} from './sync';
168+
import { _sync } from './sync';
172169

173170
import { linear } from './paths/linear';
174171
import { spline } from './paths/spline';
@@ -2521,7 +2518,7 @@ export default function uPlot(opts, data, then) {
25212518

25222519
const syncKey = FEAT_CURSOR && syncOpts.key;
25232520

2524-
const sync = FEAT_CURSOR && (syncKey != null ? (syncs[syncKey] = syncs[syncKey] || _sync()) : _sync());
2521+
const sync = FEAT_CURSOR && _sync(syncKey);
25252522

25262523
FEAT_CURSOR && sync.sub(self);
25272524

@@ -2585,6 +2582,10 @@ if (FEAT_TIME) {
25852582
uPlot.tzDate = tzDate;
25862583
}
25872584

2585+
if (FEAT_CURSOR) {
2586+
uPlot.sync = _sync;
2587+
}
2588+
25882589
if (FEAT_PATHS) {
25892590
uPlot.addGap = addGap;
25902591
uPlot.clipGaps = clipGaps;
@@ -2595,4 +2596,4 @@ if (FEAT_PATHS) {
25952596
FEAT_PATHS_SPLINE && (paths.spline = spline);
25962597
FEAT_PATHS_STEPPED && (paths.stepped = stepped);
25972598
FEAT_PATHS_BARS && (paths.bars = bars);
2598-
}
2599+
}

0 commit comments

Comments
 (0)