Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import faker from 'faker';
import {
graph,
link,
page,
merge,
unwrap,
makeStream,
setVersion,
} from '@graffy/common';
// import { debug } from '@graffy/testing';
const TARGET = 2000;
const state = graph({ visitors: {}, visitorsByTime: page({}) });
const freeIds = new Set();
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const listeners = new Set();
export default function(g) {
g.onGet(() => {
// console.log('Get: Returning', debug(state));
ts = Date.now();
setVersion(state, ts);
return state;
});
g.onSub(() =>
makeStream((push, _end) => {
listeners.add(push);
function visitorInfo() {
return {
name: faker.internet.userName(),
avatar: faker.internet.avatar(),
pageviews: page({
[ts]: faker.internet.url(),
}),
};
}
function finalize(value) {
if (!value || typeof value !== 'object') return value;
const obj = {};
if (Array.isArray(value)) {
value.forEach((v, i) => (obj[encodeKey(i)] = finalize(v)));
return page(obj);
}
for (const i in value) obj[i] = finalize(value[i]);
return obj;
}