Skip to content

Commit

Permalink
ALS tweaks (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez authored Oct 4, 2024
1 parent d455905 commit 415e7bd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": {
"url": "git+https://github.com/Digital-Alchemy-TS/core"
},
"version": "24.10.2",
"version": "24.10.3",
"author": {
"url": "https://github.com/zoe-codez",
"name": "Zoe Codez"
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/wiring.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ export interface AsyncLocalData {
export type AlsExtension = {
asyncStorage: () => AsyncLocalStorage<AsyncLocalData>;
getStore: () => AsyncLocalData;
init(source: object, callback: () => TBlackHole): void;
run(data: AsyncLocalData, callback: () => TBlackHole): void;
enterWith(data: AsyncLocalData): void;
getLogData: () => AsyncLogData;
// register(callback: AlsHook): void;
};

export type AlsHook = () => object;

// #MARK: TServiceParams
Expand Down
15 changes: 8 additions & 7 deletions src/services/als.extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { AsyncLocalStorage } from "async_hooks";
import { v4 } from "uuid";

import { AlsExtension, AsyncLocalData, AsyncLogData, TBlackHole, TServiceParams } from "../helpers";
import { AlsExtension, AsyncLocalData, AsyncLogData, TBlackHole } from "../helpers";

export function ALS({ config }: TServiceParams): AlsExtension {
export function ALS(): AlsExtension {
const storage = new AsyncLocalStorage<AsyncLocalData>();
return {
asyncStorage: () => (config.boilerplate.NODE_ENV ? storage : undefined),
asyncStorage: () => storage,
enterWith(data) {
storage.enterWith(data);
},
getLogData: () => storage.getStore()?.logs ?? ({} as AsyncLogData),
getStore: () => storage.getStore(),
init(source: object, callback: () => TBlackHole) {
const data = { logs: { id: v4(), ...source } };
storage.run(data as AsyncLocalData, () => {
run(data: AsyncLocalData, callback: () => TBlackHole) {
storage.run(data, () => {
callback();
});
},
Expand Down

0 comments on commit 415e7bd

Please sign in to comment.