-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from felanios/beta
Beta
- Loading branch information
Showing
14 changed files
with
499 additions
and
193 deletions.
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 |
---|---|---|
@@ -1,28 +1,70 @@ | ||
import { AsyncLocalStorage } from 'async_hooks'; | ||
import { AsyncStorageManagerException } from '../exceptions'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AsyncStorageManager<T> { | ||
private asyncLocalStorage = new AsyncLocalStorage<Map<string, T>>(); | ||
export class AsyncStorageManager<T> implements Map<string, T> { | ||
constructor(private readonly asyncLocalStorage: AsyncLocalStorage<Map<string, T>>) { } | ||
|
||
runWithNewContext<R>(fn: () => Promise<R>): Promise<R> { | ||
return this.asyncLocalStorage.run(new Map<string, T>(), fn); | ||
} | ||
|
||
set(key: string, value: T) { | ||
private getStore(): Map<string, T> { | ||
const store = this.asyncLocalStorage.getStore(); | ||
|
||
if (!store) { | ||
throw new AsyncStorageManagerException('No active store found'); | ||
} | ||
store[key] = value; | ||
|
||
return store; | ||
} | ||
|
||
register(): void { | ||
this.asyncLocalStorage.enterWith(new Map()); | ||
} | ||
|
||
runWithNewContext<R, TArgs extends any[]>(fn: (...args: TArgs) => R, ...args: TArgs): R { | ||
return this.asyncLocalStorage.run<R, TArgs>(new Map<string, T>(), fn, ...args); | ||
} | ||
|
||
set(key: string, value: T): this { | ||
this.getStore().set(key, value); | ||
return this; | ||
} | ||
|
||
get(key: string): T | undefined { | ||
const store = this.asyncLocalStorage.getStore(); | ||
if (!store) { | ||
throw new AsyncStorageManagerException('No active store found'); | ||
} | ||
return store?.[key]; | ||
return this.getStore().get(key); | ||
} | ||
|
||
clear(): void { | ||
return this.getStore().clear(); | ||
} | ||
|
||
delete(key: string): boolean { | ||
return this.getStore().delete(key); | ||
} | ||
|
||
forEach(callbackfn: (value: T, key: string, map: Map<string, T>) => void, thisArg?: any): void { | ||
return this.getStore().forEach(callbackfn, thisArg); | ||
} | ||
has(key: string): boolean { | ||
return this.getStore().has(key); | ||
} | ||
|
||
get size(): number { | ||
return this.getStore().size; | ||
} | ||
|
||
entries(): IterableIterator<[string, T]> { | ||
return this.getStore().entries(); | ||
} | ||
|
||
keys(): IterableIterator<string> { | ||
return this.getStore().keys(); | ||
} | ||
|
||
values(): IterableIterator<T> { | ||
return this.getStore().values(); | ||
} | ||
|
||
[Symbol.iterator](): IterableIterator<[string, T]> { | ||
return this.getStore()[Symbol.iterator]() | ||
} | ||
|
||
[Symbol.toStringTag]: string = '[object AsyncContext]'; | ||
} |
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,15 @@ | ||
import { DynamicModule, Global, Module } from '@nestjs/common' | ||
import { AsyncStorageManager } from './als-manager'; | ||
import { AsyncStorageService } from './als.service'; | ||
|
||
@Global() | ||
@Module({}) | ||
export class AsyncStorageManagerModule { | ||
static forRoot(): DynamicModule { | ||
return { | ||
module: AsyncStorageManagerModule, | ||
providers: [AsyncStorageService, AsyncStorageManager], | ||
exports: [AsyncStorageService] | ||
} | ||
} | ||
} |
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,28 @@ | ||
import { Injectable, OnModuleInit } from "@nestjs/common"; | ||
import { AsyncStorageManager } from "./als-manager"; | ||
import { AsyncLocalStorage } from "async_hooks"; | ||
|
||
@Injectable() | ||
export class AsyncStorageService implements OnModuleInit { | ||
constructor(private asyncStorageManager: AsyncStorageManager<string>) { } | ||
|
||
onModuleInit() { | ||
this.asyncStorageManager = new AsyncStorageManager(new AsyncLocalStorage<any>()); | ||
} | ||
|
||
async runWithNewContext<R, TArgs extends any[]>(fn: (...args: TArgs) => R, ...args: TArgs): Promise<R> { | ||
return this.asyncStorageManager.runWithNewContext(fn, ...args); | ||
} | ||
|
||
registerContext(): void { | ||
this.asyncStorageManager.register(); | ||
} | ||
|
||
get(key: string): string { | ||
return this.asyncStorageManager.get(key); | ||
} | ||
|
||
setClientID(key: string, value: string): void { | ||
this.asyncStorageManager.set(key, value); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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 @@ | ||
export * from './uuid-generator'; |
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,11 @@ | ||
export function generateUuid(): string { | ||
let d = Date.now(); | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( | ||
/[xy]/g, | ||
(c: string) => { | ||
const r = (d + Math.random() * 16) % 16 | 0; | ||
d = Math.floor(d / 16); | ||
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16); | ||
} | ||
); | ||
} |
Oops, something went wrong.