Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const ref = db.child(path);
return new Promise((resolve) => {
ref.once('value', snapshot => resolve(snapshot.val())); // tslint:disable-line
})
}
const fetchItem = async (id): Promise => {
const item = await fetch(`item/${id}`);
if (item && item.kids) item.kids = await Promise.all(item.kids.map(async kid => {
return typeof kid === 'number' ?
await fetchItem(kid) : kid
}));
return item;
}
app.on('get-list', async (type, list) => {
const fetchListItems = async ({ items, min, max }) => {
await Promise.all(items.map(async (id, idx) => {
if (idx >= min && idx < max && (typeof id === 'number')) {
items[idx] = await fetch(`item/${id}`)
}
}));
app.run('render');
}
if (list.items.length) {
await fetchListItems(list);
} else {
const ref = db.child(`${type}stories`);
ref.on('value', async snapshot => {
list.items = snapshot.val();
await fetchListItems(list);
});
}
}));
app.run('render');
}
if (list.items.length) {
await fetchListItems(list);
} else {
const ref = db.child(`${type}stories`);
ref.on('value', async snapshot => {
list.items = snapshot.val();
await fetchListItems(list);
});
}
});
app.on('get-item', (id, state) => {
if (!id) return;
const ref = db.child(`item/${id}`);
ref.on('value', async snapshot => {
state[id] = snapshot.val();
if (state[id].kids) {
state[id].kids = await Promise.all(state[id].kids.map(async kid => {
return typeof kid === 'number' ?
await fetchItem(kid) : kid
}));
}
app.run('render');
});
});
component.run(t.id);
stopMeasure();
} else if (t.matches('.remove')) {
e.preventDefault();
startMeasure('remove');
component.run('remove', getId(t));
stopMeasure();
} else if (t.matches('.lbl')) {
e.preventDefault();
startMeasure('select');
component.run('select', getId(t));
stopMeasure();
}
});
app.on('//', _ => { })
app.on('#', _ => { })
let component = new Component(store, view, update);
component.start(document.getElementById('main'));
stopMeasure();
} else if (t.matches('.remove')) {
e.preventDefault();
startMeasure('remove');
component.run('remove', getId(t));
stopMeasure();
} else if (t.matches('.lbl')) {
e.preventDefault();
startMeasure('select');
component.run('select', getId(t));
stopMeasure();
}
});
app.on('//', _ => { })
app.on('#', _ => { })
let component = new Component(store, view, update);
component.start(document.getElementById('main'));
component.run(t.id);
stopMeasure();
} else if (t.matches('.remove')) {
e.preventDefault();
startMeasure('remove');
component.run('remove', getId(t));
stopMeasure();
} else if (t.matches('.lbl')) {
e.preventDefault();
startMeasure('select');
component.run('select', getId(t));
stopMeasure();
}
});
app.on('//', _ => { })
app.on('#', _ => { })
let component = new Component(store, view, update);
component.start(document.getElementById('main'));
stopMeasure();
} else if (t.matches('.remove')) {
e.preventDefault();
startMeasure('remove');
component.run('remove', getId(t));
stopMeasure();
} else if (t.matches('.lbl')) {
e.preventDefault();
startMeasure('select');
component.run('select', getId(t));
stopMeasure();
}
});
app.on('//', _ => { })
app.on('#', _ => { })
let component = new Component(store, view, update);
component.start(document.getElementById('main'));
'#': (state, type, id) => {
type = type || 'top';
state.type = type;
state[type] = state[type] || { min: 0, max: page_size, items: [] };
state.id = id;
if (type === 'item')
app.run('get-item', id, state);
else
app.run('get-list', type, state[type]);
},
'render': state => state,
const fetchListItems = async ({ items, min, max }) => {
await Promise.all(items.map(async (id, idx) => {
if (idx >= min && idx < max && (typeof id === 'number')) {
items[idx] = await fetch(`item/${id}`)
}
}));
app.run('render');
}
if (list.items.length) {
'#': (state, type, id) => {
type = type || 'top';
state.type = type;
state[type] = state[type] || { min: 0, max: page_size, items: [] };
state.id = id;
if (type === 'item')
app.run('get-item', id, state);
else
app.run('get-list', type, state[type]);
},
'render': state => state,
'more': state => {
const list = state[state.type];
list.max = Math.min(list.max + page_size, list.items.length)
app.run('get-list', state.type, list);
},
}