mirror of
https://github.com/pockethost/pockethost.git
synced 2025-09-15 21:20:11 +00:00
feat(pockethost): unsubscribe from filters
This commit is contained in:
parent
35edc77a9a
commit
879845a831
5
.changeset/nice-roses-carry.md
Normal file
5
.changeset/nice-roses-carry.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'pockethost': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Enhancement: unsubscribe from filters
|
@ -32,14 +32,23 @@ const filters: {
|
|||||||
[key: string]: FilterEntry<any>[]
|
[key: string]: FilterEntry<any>[]
|
||||||
} = {}
|
} = {}
|
||||||
|
|
||||||
async function registerFilter<TCarry, TContext extends {} = {}>(
|
function registerFilter<TCarry, TContext extends {} = {}>(
|
||||||
filter: string,
|
filter: string,
|
||||||
handler: FilterHandler<TCarry, TContext>,
|
handler: FilterHandler<TCarry, TContext>,
|
||||||
priority = 10,
|
priority = 10,
|
||||||
) {
|
): () => void {
|
||||||
if (!(filter in filters)) filters[filter] = []
|
if (!(filter in filters)) filters[filter] = []
|
||||||
filters[filter]!.push({ priority, handler })
|
filters[filter]!.push({ priority, handler })
|
||||||
filters[filter]!.sort((a, b) => a.priority - b.priority)
|
filters[filter]!.sort((a, b) => a.priority - b.priority)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
const index = filters[filter]!.findIndex(
|
||||||
|
(entry) => entry.handler === handler,
|
||||||
|
)
|
||||||
|
if (index !== -1) {
|
||||||
|
filters[filter]!.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function filter<TCarry, TContext extends {} = {}>(
|
async function filter<TCarry, TContext extends {} = {}>(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user