Skip to content

Commit

Permalink
feat(basics): add useBasicsStream for convenient usage
Browse files Browse the repository at this point in the history
by default it will register all available basics stream

closes #44
  • Loading branch information
nampdn committed Apr 24, 2019
1 parent 8d8f8c7 commit 6029ff7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/basics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './features'
export * from './operators'
export * from './use-basics-stream'
26 changes: 26 additions & 0 deletions packages/basics/src/use-basics-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as ExportedStreams from './features'

export interface UseBasicsStreamConfig {
Init?: boolean
Timer?: boolean
}

const defaultConfig: UseBasicsStreamConfig = {
Init: true,
Timer: true,
}

/**
* Register all components as Bara stream
*/
export function useBasicsStream(config?: UseBasicsStreamConfig) {
const combinedConfig = { ...defaultConfig, ...config }
for (const component in combinedConfig) {
if ((combinedConfig as any)[component] === true) {
const methodName = `use${component}Stream`
if (methodName in ExportedStreams) {
;(ExportedStreams as any)[methodName]()
}
}
}
}
3 changes: 2 additions & 1 deletion packages/basics/test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
useStream,
useTrigger,
} from '@bara/core'
import { useInit, useInitStream } from '../src/features/init'

import { useInit, useInitStream } from '../src'

describe('basics/init tests', () => {
it('should emit initialization event', done => {
Expand Down
26 changes: 26 additions & 0 deletions packages/basics/test/use-basics-stream.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
register,
useAction,
useCondition,
useEvent,
useStream,
useTrigger,
} from '@bara/core'

import { useBasicsStream, useInit } from '../src'

describe('basics/use-basics-stream tests', () => {
it('not emit init when disable from useBasicsStream', done => {
const handler = jest.fn()

const app = register(() => {
useBasicsStream({ Init: false })
useInit(handler)
})

setTimeout(() => {
expect(handler).not.toHaveBeenCalled()
done()
}, 100)
})
})

0 comments on commit 6029ff7

Please sign in to comment.