Add support for custom streams list

Also, fix bug where unique stream ids were not being generated.
This commit is contained in:
Max Goodhart
2020-06-19 19:14:03 -07:00
parent e80b0075f6
commit 490b626a06
4 changed files with 144 additions and 46 deletions

View File

@@ -46,28 +46,34 @@ export async function* pollSpreadsheetData(creds, sheetId, tabName) {
}
}
export async function* processData(dataGen) {
// Give each stream a unique and recognizable short id.
const idMap = new Map()
for await (const data of dataGen) {
for (const stream of data) {
const { Link, Source } = stream
export class StreamIDGenerator {
constructor(parent) {
this.idMap = new Map(parent ? parent.idMap : null)
this.idSet = new Set(this.idMap.values())
}
process(streams) {
const { idMap, idSet } = this
for (const stream of streams) {
const { Link, Source, Label } = stream
if (!idMap.has(Link)) {
let counter = 0
let newId
const normalizedSource = Source.toLowerCase()
const normalizedText = (Source || Label || Link)
.toLowerCase()
.replace(/[^\w]/g, '')
.replace(/^the|^https?(www)?/, '')
do {
const sourcePart = normalizedSource.substr(0, 3).toLowerCase()
const counterPart = counter === 0 ? '' : counter
newId = `${sourcePart}${counterPart}`
const textPart = normalizedText.substr(0, 3).toLowerCase()
const counterPart = counter === 0 && textPart ? '' : counter
newId = `${textPart}${counterPart}`
counter++
} while (idMap.has(newId))
} while (idSet.has(newId))
idMap.set(Link, newId)
idSet.add(newId)
}
stream._id = idMap.get(Link)
}
yield data
return streams
}
}

View File

@@ -2,7 +2,7 @@ import fs from 'fs'
import yargs from 'yargs'
import { app, shell, BrowserWindow } from 'electron'
import { pollPublicData, pollSpreadsheetData, processData } from './data'
import { pollPublicData, pollSpreadsheetData, StreamIDGenerator } from './data'
import StreamWindow from './StreamWindow'
import initWebServer from './server'
@@ -49,12 +49,14 @@ async function main() {
})
.help().argv
const idGen = new StreamIDGenerator()
const streamWindow = new StreamWindow()
streamWindow.init()
let browseWindow = null
const clientState = {}
const clientState = { streams: [], customStreams: [], views: [] }
const getInitialState = () => clientState
let broadcastState = () => {}
const onMessage = (msg) => {
@@ -62,6 +64,11 @@ async function main() {
streamWindow.setViews(new Map(msg.views))
} else if (msg.type === 'set-listening-view') {
streamWindow.setListeningView(msg.viewIdx)
} else if (msg.type === 'set-custom-streams') {
const customIDGen = new StreamIDGenerator(idGen)
clientState.customStreams = customIDGen.process(msg.streams)
streamWindow.send('state', clientState)
broadcastState(clientState)
} else if (msg.type === 'reload-view') {
streamWindow.reloadView(msg.viewIdx)
} else if (msg.type === 'browse') {
@@ -90,8 +97,8 @@ async function main() {
}
streamWindow.on('state', (viewStates) => {
streamWindow.send('view-states', viewStates)
clientState.views = viewStates
streamWindow.send('state', clientState)
broadcastState(clientState)
})
@@ -102,9 +109,10 @@ async function main() {
dataGen = pollPublicData()
}
for await (const streams of processData(dataGen)) {
streamWindow.send('stream-data', streams)
for await (const rawStreams of dataGen) {
const streams = idGen.process(rawStreams)
clientState.streams = streams
streamWindow.send('state', clientState)
broadcastState(clientState)
}
}