Last active
January 9, 2024 14:18
-
-
Save cowboyd/baa3f975e84c16b09110d519c36d8116 to your computer and use it in GitHub Desktop.
Effection console effect
This file contains hidden or 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
// deno-lint-ignore-file no-explicit-any | |
import { createContext, type Operation } from "./mod.ts"; | |
export interface Console { | |
// Logging | |
assert(condition?: boolean, ...data: any[]): Operation<void>; | |
clear(): Operation<void>; | |
debug(...data: any[]): Operation<void>; | |
error(...data: any[]): Operation<void>; | |
info(...data: any[]): Operation<void>; | |
log(...data: any[]): Operation<void>; | |
table(tabularData?: any, properties?: string[]): Operation<void>; | |
trace(...data: any[]): Operation<void>; | |
warn(...data: any[]): Operation<void>; | |
dir(item?: any, options?: any): Operation<void>; | |
dirxml(...data: any): Operation<void>; | |
// Counting | |
count(label?: string): Operation<void>; | |
countReset(label?: string): Operation<void>; | |
// Grouping | |
group(...data: any[]): Operation<void>; | |
groupCollapsed(...data: any[]): Operation<void>; | |
groupEnd(): Operation<void>; | |
// Timing | |
time(label?: string): Operation<void>; | |
timeLog(label?: string, ...data: any[]): Operation<void>; | |
timeEnd(label?: string): Operation<void>; | |
} | |
const $console = globalThis.console; | |
const builtin: Console = { | |
*assert(condition?: boolean, ...data: any[]) { | |
$console.assert(condition, ...data); | |
}, | |
*clear() { | |
$console.clear(); | |
}, | |
*debug(...data) { | |
$console.debug(...data); | |
}, | |
*error(...data) { | |
$console.error(...data); | |
}, | |
*info(...data) { | |
$console.info(...data); | |
}, | |
*log(...data) { | |
$console.log(...data); | |
}, | |
*table(...args) { | |
$console.table(...args); | |
}, | |
*warn(...args) { | |
$console.warn(...args); | |
}, | |
*trace(...args) { | |
$console.trace(...args); | |
}, | |
*dir(...args) { | |
$console.dir(...args); | |
}, | |
*dirxml(...args) { | |
$console.dirxml(...args); | |
}, | |
*count(label?: string) { | |
$console.count(label); | |
}, | |
*countReset(label?: string) { | |
$console.countReset(label); | |
}, | |
// Grouping | |
*group(...args) { | |
$console.group(...args); | |
}, | |
*groupCollapsed(...args) { | |
$console.groupCollapsed(...args); | |
}, | |
*groupEnd(...args) { | |
$console.groupEnd(...args); | |
}, | |
// Timing | |
*time(label?: string) { | |
$console.time(label); | |
}, | |
*timeLog(...args) { | |
$console.timeLog(...args); | |
}, | |
*timeEnd(label?: string) { | |
$console.timeEnd(label); | |
} | |
} | |
const ConsoleContext = createContext<Console>("console", builtin); | |
function delegate(methodName: keyof Console) { | |
return function*(...args: any[]): Operation<void> { | |
let context = yield* ConsoleContext; | |
let method = context[methodName]; | |
return yield* method(...args); | |
} | |
} | |
export const console: Console = { | |
assert: delegate("assert"), | |
clear: delegate("clear"), | |
debug: delegate("debug"), | |
error: delegate("error"), | |
info: delegate("info"), | |
log: delegate("log"), | |
table: delegate("table"), | |
warn: delegate("warn"), | |
trace: delegate("trace"), | |
dir: delegate("dir"), | |
dirxml: delegate("dirxml"), | |
count: delegate("count"), | |
countReset: delegate("countReset"), | |
// Grouping | |
group: delegate("group"), | |
groupCollapsed: delegate("groupCollapsed"), | |
groupEnd: delegate("groupEnd"), | |
// Timing | |
time: delegate("time"), | |
timeLog: delegate("timeLog"), | |
timeEnd: delegate("timeEnd"), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment