David Piepgrass
05/09/2020, 3:13 AMRyan King
05/09/2020, 3:27 AMChet Corcos
05/09/2020, 4:09 AMRyan King
05/09/2020, 4:36 AMEdward de Jong / Beads Project
05/09/2020, 6:42 AMDoug Moen
05/09/2020, 11:46 AMIvan Reese
Dan Cook
05/09/2020, 3:25 PMDavid Piepgrass
05/09/2020, 4:21 PMDoug Moen
05/09/2020, 5:20 PMChet Corcos
05/09/2020, 6:38 PMtype Paper = {
id: string
subject: string
date: string // ISO
}
const collection: Record<string, Paper> = {}
// This is the query I want to index:
// Filter for nutrition items this year, range 20-40.
Object.values(collection)
.filter((item) => item.subject === "Nutrition" && item.date > "2020-01-01")
.slice(20, 40)
// First, lets translate this into a composite index.
const filterIndex: Array<[string, string, string]> = [] // [subject, date, id]
for (const item of Object.values(collection)) {
// uses binary search to insert in sorted order.
addToIndex(filterIndex, [item.subject, item.date])
}
// Translate your query into subscriptions.
const subscriptions = [
[
"date",
"2020-01-01",
() => {
/* Update callback */
},
],
[
"filterIndex",
20,
40,
() => {
/* Update callback */
},
],
[
"subject",
"Nutrition",
() => {
/* Update callback */
},
],
]
function updateItem(id: string, update: Partial<Paper>) {
// Emit on the old key-value because this will be removed from result set.
for (const key in update) {
subscriptions
.filter(([a, b]) => a === key && b === collection[id][key])
.forEach(([_a, _b, callback]) => callback())
}
const beforeIndex = removeFromIndex(filterIndex, collection[id])
Object.assign(collection[id], update)
const afterIndex = addToIndex(filterIndex, collection[id])
// Emit on the new key value because this will be added to result set.
for (const key in update) {
subscriptions
.filter(([a, b]) => a === key && b === collection[id][key])
.forEach(([_a, _b, callback]) => callback())
}
if (beforeIndex !== afterIndex) {
// Emit an update for all listeners on filterIndex between before and after.
}
}
Chet Corcos
05/09/2020, 6:39 PMChet Corcos
05/09/2020, 6:39 PMChet Corcos
05/10/2020, 1:04 AMEdward de Jong / Beads Project
05/10/2020, 2:53 AMjamii
05/10/2020, 3:54 AMjamii
05/10/2020, 3:56 AMIvan Reese
wtaysom
05/10/2020, 12:03 PMIan Rumac
05/13/2020, 2:23 PMIan Rumac
05/13/2020, 2:24 PMDavid Piepgrass
05/13/2020, 3:34 PMIan Rumac
05/14/2020, 1:41 PM