Skip to content

Commit

Permalink
add fsrs-browser
Browse files Browse the repository at this point in the history
  • Loading branch information
ishiko732 committed Nov 13, 2024
1 parent de3b307 commit f406e5d
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/controller/fsrs-browser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Context } from 'koa'
import { project } from '../../services/optimizer/fsrs-browser/constrant'
import { FSRSBrowserService } from '../../services/optimizer/fsrs-browser'
import { INextRequest } from '../../services/optimizer/fsrs-browser/types'

class FSRSBrowserController {
intro = async (ctx: Context) => {
ctx.body = project
}

scheduler = async (ctx: Context) => {
const { d, s, r, ivl } = ctx.request.body as Omit<
INextRequest,
'card_id' | 'now' | 'timezone'
>
const svc = await new FSRSBrowserService().init()
const scheduler = svc.state(d, s, r, ivl)
ctx.body = scheduler
}
}

const controller = new FSRSBrowserController()

export default controller
18 changes: 18 additions & 0 deletions src/controller/fsrs-browser/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TRouter } from '../../bootstrap/types'
import fsrsBrowserController from './index'

const routers: TRouter = {
['browser/intro']: {
method: 'get',
path: '/intro',
endpoint: fsrsBrowserController.intro
},
['browser/scheduler']: {
method: 'post',
path: '/scheduler',
endpoint: fsrsBrowserController.scheduler
},

}

export default routers
4 changes: 3 additions & 1 deletion src/controller/router.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { TRouter } from '../bootstrap/types'
import tsfsrsRouters from '../controller/ts-fsrs/router'
import browserRouters from '../controller/fsrs-browser/router'

const root = {
...tsfsrsRouters
...tsfsrsRouters,
...browserRouters
} satisfies TRouter

export default root
6 changes: 6 additions & 0 deletions src/services/optimizer/fsrs-browser/constrant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const project = {
name: 'fsrs-browser',
version: 'v1.4.3',
repo: 'https://github.com/open-spaced-repetition/fsrs-browser',
npm: 'https://www.npmjs.com/package/fsrs-browser'
} as const
Binary file not shown.
48 changes: 48 additions & 0 deletions src/services/optimizer/fsrs-browser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { readFileSync } from 'fs'
import init, { Fsrs, InitOutput, ItemState } from 'fsrs-browser/fsrs_browser'
let initOutput: InitOutput | null = null

export class FSRSBrowserService {
constructor() {}

async init() {
if (!initOutput) {
const wasmBuffer = readFileSync(
new URL('fsrs_browser_bg.wasm', import.meta.url)
)
initOutput = await init(wasmBuffer.buffer)
}
return this
}

next(d: number, s: number, r: number) {
const fsrs = new Fsrs()
const result = fsrs.nextInterval(s, d, r)
fsrs.free()
return +result.toFixed(4)
}

state(d: number, s: number, r: number, interval: number) {
const fsrs = new Fsrs()
const result = fsrs.nextStates(s || undefined, d || undefined, r, interval)

const dataset = {
again: this._getState(result.again),
hard: this._getState(result.hard),
good: this._getState(result.good),
easy: this._getState(result.easy)
}

fsrs.free()
return dataset
}

_getState(info: ItemState) {
return {
memoryState: {
stability: +info.memory.stability.toFixed(4),
difficulty: +info.memory.difficulty.toFixed(4)
}
}
}
}
8 changes: 8 additions & 0 deletions src/services/optimizer/fsrs-browser/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IRequest } from '../../scheduler/ts-fsrs/types'

export interface INextRequest<T = string | number> extends IRequest<T> {
s: number
d: number
r: number
ivl: number
}

0 comments on commit f406e5d

Please sign in to comment.