Skip to content

Commit

Permalink
feat(core): new operate addDebugListener for debugging
Browse files Browse the repository at this point in the history
You can use this operater to debug all the stream of the application

closes #52
  • Loading branch information
nampdn committed Apr 24, 2019
1 parent ca6df1e commit 8d8f8c7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/bara.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ const bara = (() => {
app()
return { appStream, emitterMap, streamRegistry, triggerRegistry }
},
addDebugListener(whichStream: string, callback: (...args: any[]) => void) {
const listener = {
next: (value: any) => {
callback(value)
},
}
if (whichStream === 'app') {
appStream.setDebugListener(listener)
} else {
const stream = streamRegistry.find(s => s.name === whichStream)
if (stream) {
stream._$.setDebugListener(listener)
} else {
throw new Error(
`[Bara Debugger] Not found any stream registered with name ${whichStream}. You can specify stream 'app' for all stream event.`,
)
}
}
},
useStream<T>(streamSetup: BaraStreamSetup<T>) {
let stream
let duplicateIndex = -1
Expand Down Expand Up @@ -194,6 +213,7 @@ const bara = (() => {
})()

const {
addDebugListener,
register,
useStream,
useTrigger,
Expand All @@ -206,6 +226,7 @@ const {
} = bara

export {
addDebugListener,
register,
useStream,
useTrigger,
Expand Down
69 changes: 69 additions & 0 deletions packages/core/test/debug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
addDebugListener,
createEventType,
register,
useAction,
useEvent,
useStream,
useTrigger,
} from '../src'

describe('use debugger', () => {
it('should attach console log to main app stream', done => {
const handler = jest.fn()
const handlerReal = jest.fn()
const handlerNotFoundStream = jest.fn()
const handlerSpecificStream = jest.fn()

register(() => {
const SAMPLE_EVENT = createEventType('SAMPLE_EVENT')
const STREAM_ID = 'dev.barajs.debugger'
useStream<number>(({ setName, addEventType, emit }) => {
setName(STREAM_ID)
addEventType(SAMPLE_EVENT)

let counter = 0
const timer = setInterval(() => {
emit(SAMPLE_EVENT, counter)
counter += 1
if (counter > 9) {
clearInterval(timer)
}
}, 500)

return () => {
clearInterval(timer)
}
})

// tslint:disable-next-line
addDebugListener('app', (data: any) => {
handler(data)
})

// Add specific stream debugger by attaching to its name
addDebugListener(STREAM_ID, handlerSpecificStream)

// Add not found stream and expect a handling error
expect(() => {
addDebugListener(STREAM_ID + '-diff', handlerNotFoundStream)
}).toThrow(
`[Bara Debugger] Not found any stream registered with name ${STREAM_ID}-diff. You can specify stream 'app' for all stream event.`,
)

useTrigger(() => {
const event = useEvent(SAMPLE_EVENT)
const action = useAction(handlerReal)
return { event, action }
})
})

setTimeout(() => {
expect(handler).toHaveBeenCalledTimes(9)
expect(handlerSpecificStream).toHaveBeenCalledTimes(9)
expect(handlerNotFoundStream).not.toHaveBeenCalled()
expect(handler).toHaveBeenCalled()
done()
}, 5000)
})
})

0 comments on commit 8d8f8c7

Please sign in to comment.