Skip to content

Commit

Permalink
add 'async' option so async can be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahhh committed Feb 11, 2021
1 parent 0405ab2 commit 45eacf7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 18 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ const LogsContainer = () => {

// run once!
useEffect(() => {
Hook(
window.console,
(log) => setLogs((currLogs) => [...currLogs, log]),
false
)
Hook(window.console, (log) => setLogs((currLogs) => [...currLogs, log]), {
encode: false,
})
return () => Unhook(window.console)
}, [])

Expand Down Expand Up @@ -169,7 +167,7 @@ Hook(
(log) => {
this.setState(({ logs }) => ({ logs: [...logs, log] }))
},
false
{ encode: false }
)
```

Expand Down
51 changes: 47 additions & 4 deletions src/Hook/__tests__/Hook.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Log from './Log'
import { Decode } from '../..'

it('hooks the console', () => {
Hook(console, log => {
Hook(console, (log) => {
console.logs.push(log)
})
expect(console.feed).toBeTruthy()
Expand Down Expand Up @@ -44,7 +44,7 @@ it('correctly encodes nested values', async () => {
function: function myFunc() {},
document: document.documentElement,
nested: [[[new Promise(() => {})]]],
recursive: null
recursive: null,
}
input.recursive = input

Expand All @@ -58,7 +58,28 @@ it('correctly encodes nested values', async () => {
it('disables encoding with a flag', async () => {
Hook(
console,
log => {
(log) => {
console.logs.push(log)
},
{ encode: false }
)
const input = {
function: function myFunc() {},
document: document.documentElement,
nested: [[[new Promise(() => {})]]],
recursive: null,
}
input.recursive = input

const result: any = await Log('debug', input)

expect(result.data).toMatchSnapshot()
})

it('disables encoding with a flag (old-style)', async () => {
Hook(
console,
(log) => {
console.logs.push(log)
},
false
Expand All @@ -67,11 +88,33 @@ it('disables encoding with a flag', async () => {
function: function myFunc() {},
document: document.documentElement,
nested: [[[new Promise(() => {})]]],
recursive: null
recursive: null,
}
input.recursive = input

const result: any = await Log('debug', input)

expect(result.data).toMatchSnapshot()
})

it('disables async with a flag', async () => {
Hook(
console,
(log) => {
console.logs.push(log)
},
{ async: false }
)

let arr = []
console.log(arr)
arr.push(100)
console.log(arr)

const decoded1 = Decode(console.logs[console.logs.length - 2])
const decoded2 = Decode(console.logs[console.logs.length - 1])
expect(decoded1.method).toEqual('log')
expect(decoded1.data).toEqual([[]])
expect(decoded2.method).toEqual('log')
expect(decoded2.data).toEqual([[100]])
})
20 changes: 20 additions & 0 deletions src/Hook/__tests__/__snapshots__/Hook.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ Array [
]
`;

exports[`disables encoding with a flag (old-style) 1`] = `
Array [
Object {
"document": <html>
<head />
<body />
</html>,
"function": [Function],
"nested": Array [
Array [
Array [
Promise {},
],
],
],
"recursive": [Circular],
},
]
`;

exports[`disables encoding with a flag 1`] = `
Array [
Object {
Expand Down
38 changes: 30 additions & 8 deletions src/Hook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Callback,
Storage,
Methods as ConsoleMethods,
Message
Message,
} from '../definitions/Console'
import Methods from '../definitions/Methods'

Expand All @@ -12,6 +12,17 @@ import Unhook from '../Unhook'
import { Encode } from '../Transform'
// import Construct from './construct'

export interface HookOptions {
encode?: boolean
async?: boolean
}

const optionsDefault: HookOptions = { encode: true, async: true }

function runImmediately(f: () => void): void {
f()
}

/**
* Hook a console constructor and forward messages to a callback
* @argument console The Console constructor to Hook
Expand All @@ -20,35 +31,46 @@ import { Encode } from '../Transform'
export default function Hook(
console: Console,
callback: Callback,
encode = true
optionsIn: boolean | HookOptions = true
) {
const options: HookOptions = (() => {
// Support old call style, where third argument is just `encode`
if (typeof optionsIn === 'boolean') {
optionsIn = { encode: optionsIn }
}
// Set defaults
optionsIn = Object.assign({}, optionsDefault, optionsIn)
return optionsIn
})()

const TargetConsole = console as HookedConsole
const Storage: Storage = {
pointers: {},
src: {
npm: 'https://npmjs.com/package/console-feed',
github: 'https://github.com/samdenty99/console-feed'
}
github: 'https://github.com/samdenty99/console-feed',
},
}

// Override console methods
for (let method of Methods) {
const NativeMethod = TargetConsole[method]

// Override
TargetConsole[method] = function() {
TargetConsole[method] = function () {
// Pass back to native method
NativeMethod.apply(this, arguments)

// Parse arguments and send to transport
const args = [].slice.call(arguments)

// setTimeout to prevent lag
setTimeout(() => {
// setTimeout to prevent lag, unless disabled
const maybeSetTimeout = options.async ? setTimeout : runImmediately
maybeSetTimeout(() => {
const parsed = Parse(method as ConsoleMethods, args)
if (parsed) {
let encoded: Message = parsed as Message
if (encode) {
if (options.encode) {
encoded = Encode(parsed) as Message
}
callback(encoded, parsed)
Expand Down

0 comments on commit 45eacf7

Please sign in to comment.