-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |