Improve types of watch Obz

This commit is contained in:
Andre Staltz 2024-02-06 14:42:40 +02:00
parent 8983ba29f0
commit 3accb5e3b0
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
4 changed files with 21 additions and 8 deletions

2
.gitignore vendored
View File

@ -3,7 +3,7 @@ node_modules
pnpm-lock.yaml
package-lock.json
coverage
**/*.d.ts
lib/**/*.d.ts
*~
# For misc scripts and experiments:

10
declarations/obz.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
declare module 'obz' {
type Remove = () => void
export interface Obz<X> {
(listener: (value: X) => void): Remove
set(value: X): unknown
value: X
}
function createObz(): Obz
export = createObz
}

View File

@ -1,4 +1,3 @@
// @ts-ignore
const Obz = require('obz')
const MsgV4 = require('ppppp-db/msg-v4')
@ -24,9 +23,7 @@ const PREFIX = 'set_v1__'
* del: Array<string>,
* supersedes: Array<MsgID>,
* }} SetMsgData
* @typedef {{
* set: (ev: { event: 'add' | 'del', subdomain: string, value: string }) => void
* }} ObzType
* @typedef {{ event: 'add' | 'del', subdomain: string, value: string }} Event
* @typedef {{
* set?: {
* ghostSpan?: number
@ -39,6 +36,11 @@ const PREFIX = 'set_v1__'
* @typedef {import('ppppp-db/msg-v4').Msg<T>} Msg<T>
*/
/**
* @template T
* @typedef {import('obz').Obz<T>} ObzType
*/
/**
* @template T
* @typedef {T extends void ?
@ -84,7 +86,7 @@ function initSet(peer, config) {
let loadedAccountID = /** @type {string | null} */ (null)
let loadPromise = /** @type {Promise<void> | null} */ (null)
let cancelOnRecordAdded = /** @type {CallableFunction | null} */ (null)
const watch = /**@type {ObzType}*/ (Obz())
const watch = /**@type {ObzType<Event>}*/ (Obz())
const tangles = /** @type {Map<Subdomain, MsgV4.Tangle>} */ (new Map())
const itemRoots = {

View File

@ -1,5 +1,5 @@
{
"include": ["lib/**/*.js"],
"include": ["declarations", "lib/**/*.js"],
"exclude": ["coverage/", "node_modules/", "test/"],
"compilerOptions": {
"checkJs": true,
@ -11,6 +11,7 @@
"module": "node16",
"skipLibCheck": true,
"strict": true,
"target": "es2022"
"target": "es2022",
"typeRoots": ["node_modules/@types", "declarations"]
}
}