-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bba0f1
commit 265d2e2
Showing
3 changed files
with
39 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect,it , vi} from "vitest"; | ||
|
||
import { createDebug, registerDebugLogger } from "./debug"; | ||
|
||
describe("Debug", () => { | ||
it("Registers hook on log creation", () =>{ | ||
const mock = vi.fn() | ||
|
||
registerDebugLogger((args) => mock(args)) | ||
const debug= createDebug("foo"); | ||
|
||
debug.log("message %s", "injected"); | ||
|
||
expect(mock).toHaveBeenCalledWith("") | ||
|
||
}) | ||
}) |
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,3 +1,23 @@ | ||
import debugPkg from "debug"; | ||
|
||
export const createDebug = (namespace: string) => debugPkg.debug(`app-sdk:${namespace}`); | ||
const debugObservables = new Set<Function>(); | ||
|
||
export const registerDebugLogger = (loggerHook: (message: string) => void) => { | ||
debugObservables.add(loggerHook); | ||
|
||
return () => debugObservables.delete(loggerHook); | ||
}; | ||
|
||
export const createDebug = (namespace: string) => { | ||
const debugInstance = debugPkg.debug(`app-sdk:${namespace}`); | ||
console.log(debugInstance) | ||
|
||
const originalDebugLog = debugInstance.log.bind(debugInstance); | ||
|
||
debugInstance.log = (args) => { | ||
const log = originalDebugLog(args); | ||
debugObservables.forEach((d) => d(log as string)); | ||
}; | ||
|
||
return debugInstance | ||
}; |
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