Published
Edited
Oct 26, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class IdGenerator {
static *idGenerator(start = 0) {
let id = start;
while (true) yield id++;
}
static id(prefix = "") {
const gens = IdGenerator.gens = (IdGenerator.gens || {});
const gen = gens[prefix]|| (gens[prefix] = IdGenerator.idGenerator());
const num = gen.next().value;
if (prefix) {
return `${prefix}-${num}`;
}
return num;
}
}
Insert cell
layoutMonitor = new RecentMonitor(4, new MultiMonitor());
Insert cell
Insert cell
Insert cell
styles = ({});
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class EventQueue {
constructor() {
this.done = false;
this.newPromise();
this.notifyCount = 0;
this.watcherCount = 0;
this.readCount = 0;
this.waitCount = 0;
}
newPromise() {
this.promise = new Promise(accept => this.accept = accept)
.then(v => {
this.newPromise();
return v;
});
}
async next() {
try {
this.waitCount++;
const v = this.last = await this.promise;
this.readCount++;
return {
value: v,
done: this.done
};
} finally {
--this.waitCount;
}
}
notify(v) {
this.lastNotify = v;
this.notifyCount++;
this.accept && this.accept(v);
}
end() {
this.done = true;
this.notify();
}
watcher() {
this.watcherCount++;
const eq = this;
const w = async function* () {
while (!eq.done) {
const v = await eq.next();
if (v.done) {
eq.done = true;
} else {
yield v.value;
}
}
}
return w();
}
}
Insert cell
class EventMonitor {
constructor() {
this.queue = new EventQueue();
this.done = false;
this.notifyCount = 0;
this.watcherCount = 0;
this.readCount = 0;
this.waitCount = 0;
}
notify(v) {
this.notifyCount++;
return this.queue.notify(v);
}
async transform(v) {
return v;
}
async next() {
try {
this.waitCount++;
let n = await this.queue.next();
this.done = this.done || n.done;
const v = this.last = await n.value;
this.readCount++;
return {
value: !n.done && (this.lastX = await this.transform(v)),
done: n.done
};
} finally {
--this.waitCount;
}
}

end() {
this.done = true;
this.queue.end();
}

watcher() {
this.watcherCount++;
const em = this;
const w = async function* () {
while (!em.done) {
let n = await em.next();
if (n.done) {
return n.value;
}
yield await n.value;
}
};
return w();
}
}
Insert cell
class LayoutMonitor extends EventMonitor {
async transform(l) {
return html`
<div class='layout-record'>
<div class='layout-name'>
${l.name} ${l.getStartTime()}
</div>
<div class='layout-line'>
Iterations: ${l.iterationCount}, time: ${l.runtime / 1000.0}s
</div>
<div class='layout-line ${l.status === 'error' ? 'layout-error' : ''}'>
Status: ${l.status} ${l.error || ''}
</div>
</div>
`;
}
}
Insert cell
class MultiMonitor {
constructor(...monitors) {
this.queue = new EventQueue();
this.ended = false;
monitors.forEach(m => this.add(m));
console.log("MultiMonitor created.");
}
add(monitor) {
Promise.resolve().then(async () => {
const w = monitor.watcher();
let done = false;
while (!done && !this.ended) {
const v = await w.next();
if (v.done) {
done = true;
} else {
this.queue.notify({monitor: monitor, value: v.value});
}
}
if (done) {
this.queue.notify({monitor: monitor, done: true});
} else {
this.queue.notify({monitor: this, terminated: true});
}
});
return monitor;
}
watcher() {
return this.queue.watcher();
}
end() {
this.ended = true;
}
}
Insert cell
class RecentMonitor {
constructor(max, ...monitors) {
this.max = max;
this.nextId = 0;
monitors.forEach(m => m.monitorId = this.nextId++);
this.queue = new MultiMonitor(...monitors);
this._history = new Map();
this._done = new Map();
this.results = new EventQueue();
const watcher = this.queue.watcher();
Promise.resolve()
.then(async () => {
while (!this.ended) {
const v = await watcher.next();
if (v.value && v.value.terminated) {
this.ended = true;
this.results.end();
} else if (v.value && v.value.done) {
this._done.set(v.value.monitor.monitorId, v.value);
if (this._done.size > this.max) {
[...this._done.keys()].slice(0, this._done.size - this.max)
.forEach(k => {
this._done.delete(k);
this._history.delete(k);
});
}
this.results.notify(this._history);
} else {
this._history.delete(v.value.monitor.monitorId);
this._history.set(v.value.monitor.monitorId, {...v.value, id: v.value.monitor.monitorId});
const historyList = this._history.values();
if (this._history.length > this.max) {
const nhistory = new Map();
let ndone = 0;
for (const h of this._history) {
if (!h.done && !h.terminated) {
nhistory.set(h.monitor.monitorId, h);
} else if (++ndone <= this.max) {
nhistory.set(h.monitor.monitorId, h);
}
}
this._history = nhistory;
}
this.results.notify(this._history);
}
}
});
}
add(monitor) {
monitor.monitorId = this.nextId++;
return this.queue.add(monitor);
}
history() {
return [...this._history.values()];
}
watcher() {
return this.results.watcher();
}
end() {
this.queue.end();
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`## Imports`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rm = new RecentMonitor(3, em, em2, em3, em4);
Insert cell
rm.history();
Insert cell
em = mm.add(new EventMonitor());
Insert cell
em2 = mm.add(new EventMonitor())
Insert cell
em3 = mm.add(new EventMonitor())
Insert cell
em4 = mm.add(new EventMonitor())
Insert cell
em.notify(458732)
Insert cell
em2.notify('em2x')
Insert cell
em3.notify('em3')
Insert cell
em4.notify("em4")
Insert cell
em4.end()
Insert cell
em3.end()
Insert cell
em2.end()
Insert cell
em.end()
Insert cell
rm._done.size
Insert cell
[...rm._done.keys()]
//rm._done.keys().slice(rm.max - rm._done.size)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more