Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
fix: more consistent caching of todoist API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
moranje committed Oct 7, 2018
1 parent 079d6fc commit f397593
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
24 changes: 17 additions & 7 deletions src/alfred-workflow-todoist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import '@babel/polyfill'

import { cache, serialize } from '@/todoist/cache'

import { AlfredError } from './workflow/error'
import { Notification } from './workflow/notifier'
import { TodoistWorkflow } from './workflow/todoist-workflow'
Expand All @@ -12,22 +14,30 @@ const todoistWorkflow = TodoistWorkflow()

function handleError(err: Error) {
let error = new AlfredError(err.message, err.name, err.stack)

return Notification(Object.assign(error, { query })).write()
}

function handleSerialization() {
serialize(cache.dump())
}

if (type === 'read') {
todoistWorkflow.read(query).catch(handleError)
todoistWorkflow.read(query).then(handleSerialization)
} else if (type === 'create') {
todoistWorkflow.create(query).catch(handleError)
todoistWorkflow.create(query).then(handleSerialization)
} else if (type === 'submit') {
todoistWorkflow.submit(JSON.parse(query)).catch(handleError)
todoistWorkflow.submit(JSON.parse(query)).then(handleSerialization)
} else if (type === 'remove') {
todoistWorkflow.remove(JSON.parse(query)).catch(handleError)
todoistWorkflow.remove(JSON.parse(query)).then(handleSerialization)
} else if (type === 'settings' && query.trim() !== '') {
let [key, value] = query.trim().split(' ')
todoistWorkflow.editSetting(key, value).catch(handleError)
todoistWorkflow.editSetting(key, value)
} else if (type === 'settings') {
todoistWorkflow.settings().catch(handleError)
todoistWorkflow.settings()
} else if (type === 'settings:store') {
todoistWorkflow.storeSetting(JSON.parse(query)).catch(handleError)
todoistWorkflow.storeSetting(JSON.parse(query))
}

process.on('uncaughtException', handleError)
process.on('unhandledRejection', handleError)
5 changes: 2 additions & 3 deletions src/todoist/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ const path = `${
process.env.HOME
}/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/com.alfred-workflow-todoist/cache.json`
const options = {
max: 500
// maxAge: 1000 * 60 * 60 * 24
// length: (n: string) => n.length
max: 500,
maxAge: 1000 * 60 * 60 * 24 // A day
}
const cache = LRU(options)

Expand Down
25 changes: 11 additions & 14 deletions src/todoist/rest-api-v8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ const ProjectAdapter = compose(
async find(this: Adapter, id: number) {
// @ts-ignore: cache type definition is incorrect
let cachedProjects: Project[] = cache.get('projects') || []
let project = find(cachedProjects, ['id', id])

if (find(cachedProjects, ['id', id])) {
return find(cachedProjects, ['id', id])
}
if (project) return project

let { body } = await this.got.get(`projects/${id}`)

Expand Down Expand Up @@ -144,10 +143,9 @@ const LabelAdapter = compose(
async find(this: Adapter, id: number) {
// @ts-ignore: cache type definition is incorrect
let cachedLabels: Label[] = cache.get('labels') || []
let label = find(cachedLabels, ['id', id])

if (find(cachedLabels, ['id', id])) {
return find(cachedLabels, ['id', id])
}
if (label) return label

let { body } = await this.got.get(`labels/${id}`)

Expand All @@ -159,7 +157,7 @@ const LabelAdapter = compose(

async findAll(this: Adapter) {
// @ts-ignore: cache type definition is incorrect
let cachedLabels: Label[] = cache.get('projects') || []
let cachedLabels: Label[] = cache.get('labels') || []

if (cachedLabels.length > 0) return cachedLabels

Expand Down Expand Up @@ -190,18 +188,17 @@ export const TaskAdapter = compose(
methods: {
async find(this: TaskAdapter, id: number): Promise<Task> {
// @ts-ignore: cache type definition is incorrect
let cachedTasks: anTasky[] = cache.get('tasks') || []
let cachedTasks: Task[] = cache.get('tasks') || []
let task = find(cachedTasks, ['id', id])

if (find(cachedTasks, ['id', id])) {
return find(cachedTasks, ['id', id])
}
if (task) return task

let { body } = await this.got.get(`tasks/${id}`)

cachedTasks.push(body)
cache.set('tasks', unionBy(cachedTasks, 'id'))

return Task(await this.getRelationships(body)) // tslint:disable-line
return Task(body) // tslint:disable-line
},

async findAll(this: TaskAdapter) {
Expand All @@ -213,8 +210,8 @@ export const TaskAdapter = compose(
let { body } = await this.got.get('tasks')

// Cache label and projects
ProjectAdapter({ token: this.token }).findAll()
LabelAdapter({ token: this.token }).findAll()
await ProjectAdapter({ token: this.token }).findAll()
await LabelAdapter({ token: this.token }).findAll()

let mapped = body.map(async (task: Task) => {
return Task(await this.getRelationships(task)) // tslint:disable-line
Expand Down

0 comments on commit f397593

Please sign in to comment.