Skip to content

Commit 45eacf7

Browse files
committed
add 'async' option so async can be disabled
1 parent 0405ab2 commit 45eacf7

4 files changed

Lines changed: 101 additions & 18 deletions

File tree

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ const LogsContainer = () => {
8181

8282
// run once!
8383
useEffect(() => {
84-
Hook(
85-
window.console,
86-
(log) => setLogs((currLogs) => [...currLogs, log]),
87-
false
88-
)
84+
Hook(window.console, (log) => setLogs((currLogs) => [...currLogs, log]), {
85+
encode: false,
86+
})
8987
return () => Unhook(window.console)
9088
}, [])
9189

@@ -169,7 +167,7 @@ Hook(
169167
(log) => {
170168
this.setState(({ logs }) => ({ logs: [...logs, log] }))
171169
},
172-
false
170+
{ encode: false }
173171
)
174172
```
175173

src/Hook/__tests__/Hook.spec.tsx

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Log from './Log'
44
import { Decode } from '../..'
55

66
it('hooks the console', () => {
7-
Hook(console, log => {
7+
Hook(console, (log) => {
88
console.logs.push(log)
99
})
1010
expect(console.feed).toBeTruthy()
@@ -44,7 +44,7 @@ it('correctly encodes nested values', async () => {
4444
function: function myFunc() {},
4545
document: document.documentElement,
4646
nested: [[[new Promise(() => {})]]],
47-
recursive: null
47+
recursive: null,
4848
}
4949
input.recursive = input
5050

@@ -58,7 +58,28 @@ it('correctly encodes nested values', async () => {
5858
it('disables encoding with a flag', async () => {
5959
Hook(
6060
console,
61-
log => {
61+
(log) => {
62+
console.logs.push(log)
63+
},
64+
{ encode: false }
65+
)
66+
const input = {
67+
function: function myFunc() {},
68+
document: document.documentElement,
69+
nested: [[[new Promise(() => {})]]],
70+
recursive: null,
71+
}
72+
input.recursive = input
73+
74+
const result: any = await Log('debug', input)
75+
76+
expect(result.data).toMatchSnapshot()
77+
})
78+
79+
it('disables encoding with a flag (old-style)', async () => {
80+
Hook(
81+
console,
82+
(log) => {
6283
console.logs.push(log)
6384
},
6485
false
@@ -67,11 +88,33 @@ it('disables encoding with a flag', async () => {
6788
function: function myFunc() {},
6889
document: document.documentElement,
6990
nested: [[[new Promise(() => {})]]],
70-
recursive: null
91+
recursive: null,
7192
}
7293
input.recursive = input
7394

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

7697
expect(result.data).toMatchSnapshot()
7798
})
99+
100+
it('disables async with a flag', async () => {
101+
Hook(
102+
console,
103+
(log) => {
104+
console.logs.push(log)
105+
},
106+
{ async: false }
107+
)
108+
109+
let arr = []
110+
console.log(arr)
111+
arr.push(100)
112+
console.log(arr)
113+
114+
const decoded1 = Decode(console.logs[console.logs.length - 2])
115+
const decoded2 = Decode(console.logs[console.logs.length - 1])
116+
expect(decoded1.method).toEqual('log')
117+
expect(decoded1.data).toEqual([[]])
118+
expect(decoded2.method).toEqual('log')
119+
expect(decoded2.data).toEqual([[100]])
120+
})

src/Hook/__tests__/__snapshots__/Hook.spec.tsx.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ Array [
4545
]
4646
`;
4747

48+
exports[`disables encoding with a flag (old-style) 1`] = `
49+
Array [
50+
Object {
51+
"document": <html>
52+
<head />
53+
<body />
54+
</html>,
55+
"function": [Function],
56+
"nested": Array [
57+
Array [
58+
Array [
59+
Promise {},
60+
],
61+
],
62+
],
63+
"recursive": [Circular],
64+
},
65+
]
66+
`;
67+
4868
exports[`disables encoding with a flag 1`] = `
4969
Array [
5070
Object {

src/Hook/index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
Callback,
44
Storage,
55
Methods as ConsoleMethods,
6-
Message
6+
Message,
77
} from '../definitions/Console'
88
import Methods from '../definitions/Methods'
99

@@ -12,6 +12,17 @@ import Unhook from '../Unhook'
1212
import { Encode } from '../Transform'
1313
// import Construct from './construct'
1414

15+
export interface HookOptions {
16+
encode?: boolean
17+
async?: boolean
18+
}
19+
20+
const optionsDefault: HookOptions = { encode: true, async: true }
21+
22+
function runImmediately(f: () => void): void {
23+
f()
24+
}
25+
1526
/**
1627
* Hook a console constructor and forward messages to a callback
1728
* @argument console The Console constructor to Hook
@@ -20,35 +31,46 @@ import { Encode } from '../Transform'
2031
export default function Hook(
2132
console: Console,
2233
callback: Callback,
23-
encode = true
34+
optionsIn: boolean | HookOptions = true
2435
) {
36+
const options: HookOptions = (() => {
37+
// Support old call style, where third argument is just `encode`
38+
if (typeof optionsIn === 'boolean') {
39+
optionsIn = { encode: optionsIn }
40+
}
41+
// Set defaults
42+
optionsIn = Object.assign({}, optionsDefault, optionsIn)
43+
return optionsIn
44+
})()
45+
2546
const TargetConsole = console as HookedConsole
2647
const Storage: Storage = {
2748
pointers: {},
2849
src: {
2950
npm: 'https://npmjs.com/package/console-feed',
30-
github: 'https://github.com/samdenty99/console-feed'
31-
}
51+
github: 'https://github.com/samdenty99/console-feed',
52+
},
3253
}
3354

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

3859
// Override
39-
TargetConsole[method] = function() {
60+
TargetConsole[method] = function () {
4061
// Pass back to native method
4162
NativeMethod.apply(this, arguments)
4263

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

46-
// setTimeout to prevent lag
47-
setTimeout(() => {
67+
// setTimeout to prevent lag, unless disabled
68+
const maybeSetTimeout = options.async ? setTimeout : runImmediately
69+
maybeSetTimeout(() => {
4870
const parsed = Parse(method as ConsoleMethods, args)
4971
if (parsed) {
5072
let encoded: Message = parsed as Message
51-
if (encode) {
73+
if (options.encode) {
5274
encoded = Encode(parsed) as Message
5375
}
5476
callback(encoded, parsed)

0 commit comments

Comments
 (0)