File tree Expand file tree Collapse file tree 4 files changed +95
-1
lines changed Expand file tree Collapse file tree 4 files changed +95
-1
lines changed Original file line number Diff line number Diff line change 1
- import { kv } from "../../ utils/db .ts" ;
1
+ import { kv } from "../utils/kv .ts" ;
2
2
import { createTaskQueue } from "../../utils/queue.ts" ;
3
3
4
4
type StoreOptions = {
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Inspired by Deno Saaskit
3
+ * See: https://github.com/denoland/saaskit/blob/main/utils/db.ts
4
+ */
5
+ const DENO_KV_PATH_KEY = "DENO_KV_PATH" ;
6
+ let path = undefined ;
7
+ if (
8
+ /**
9
+ * Resolves to the current status of a permission.
10
+ */
11
+ ( await Deno . permissions . query ( { name : "env" , variable : DENO_KV_PATH_KEY } ) )
12
+ . state === "granted"
13
+ ) {
14
+ path = Deno . env . get ( DENO_KV_PATH_KEY ) ;
15
+ }
16
+
17
+ /**
18
+ * Create Deno.Kv Singleton
19
+ */
20
+ let instance : Deno . Kv ;
21
+ async function getKvInstance ( path ?: string ) : Promise < Deno . Kv > {
22
+ if ( ! instance ) {
23
+ instance = await Deno . openKv ( path ) ;
24
+ }
25
+ return instance ;
26
+ }
27
+
28
+ export const kv = await getKvInstance ( path ) ;
29
+
30
+ export async function collectValues < T > ( iter : Deno . KvListIterator < T > ) {
31
+ return await Array . fromAsync ( iter , ( { value } ) => value ) ;
32
+ }
33
+
34
+ export async function reset ( ) {
35
+ const iter = kv . list ( { prefix : [ ] } ) ;
36
+ const promises = [ ] ;
37
+ for await ( const res of iter ) promises . push ( kv . delete ( res . key ) ) ;
38
+ await Promise . all ( promises ) ;
39
+ }
Original file line number Diff line number Diff line change
1
+ import { assertEquals } from "../../core/server/deps.ts" ;
2
+ import { createTaskQueue } from "../../utils/queue.ts" ;
3
+
4
+ Deno . test ( "Queue: create message" , async ( ) => {
5
+ const q = createTaskQueue ( ) ;
6
+ const x = await q . process ( ( ) => "x" ) ;
7
+ const y = await q . process ( ( ) => 1 ) ;
8
+ const z = await q . process ( ( ) => true ) ;
9
+ const o = await q . process ( ( ) => ( { } ) ) ;
10
+ const v = await q . process ( ( ) => { } ) ;
11
+ const p = await q . process ( async ( ) => {
12
+ const r = new Promise < string > ( ( resolve ) => resolve ( "hello" ) ) ;
13
+ return await r ;
14
+ } ) ;
15
+ assertEquals ( x , "x" ) ;
16
+ assertEquals ( y , 1 ) ;
17
+ assertEquals ( z , true ) ;
18
+ assertEquals ( o , { } ) ;
19
+ assertEquals ( v , undefined ) ;
20
+ assertEquals ( p , "hello" ) ;
21
+ } ) ;
Original file line number Diff line number Diff line change
1
+ export function createTaskQueue ( ) {
2
+ // deno-lint-ignore no-explicit-any
3
+ const array : any = [ ] ;
4
+
5
+ let isExecuting = false ;
6
+ async function exec ( ) {
7
+ while ( array . length > 0 ) {
8
+ const { fn, args, resolve } = array . shift ( ) ;
9
+ try {
10
+ const result = await fn ( ...args ) ;
11
+ resolve ( result ) ;
12
+ } catch ( error ) {
13
+ console . error ( "Error executing function:" , error ) ;
14
+ }
15
+ }
16
+ isExecuting = false ;
17
+ }
18
+
19
+ // deno-lint-ignore no-explicit-any
20
+ function process < T extends any [ ] , R > (
21
+ fn : ( ...args : T ) => Promise < R > | R ,
22
+ ...args : T
23
+ ) : Promise < R > {
24
+ return new Promise ( ( resolve ) => {
25
+ array . push ( { fn, args, resolve } ) ;
26
+ if ( ! isExecuting ) {
27
+ isExecuting = true ;
28
+ exec ( ) ;
29
+ }
30
+ } ) ;
31
+ }
32
+
33
+ return { process } ;
34
+ }
You can’t perform that action at this time.
0 commit comments