Skip to content

Commit

Permalink
fix(license): replace fbemitter with built-in bara's useEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
nampdn committed Apr 19, 2019
1 parent 40d49b7 commit a42e548
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 150 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
"src/lib"
],
"dependencies": {
"bara": "2.2.0",
"fbemitter": "2.1.1",
"bara": "2.3.0",
"react-native-web": "0.11.2"
},
"devDependencies": {
"@semantic-release/changelog": "3.0.2",
"@semantic-release/git": "7.0.8",
"@types/fbemitter": "2.0.32",
"@types/jest": "24.0.11",
"@types/node": "11.13.5",
"@types/react": "16.8.12",
Expand Down
22 changes: 13 additions & 9 deletions src/examples/features/welcome.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { useInit, useBarn, useTimerElapsed } from 'bara'
import { setBarnState, useBarn, useInit, useTimerElapsed } from 'bara'

import {
nameOfText,
nameOfTouchable,
nameOfTouchableOpacity,
useTouchablePress,
useTouchableOpacityPress,
useTextPress,
nameOfText,
useTouchableOpacityPress,
useTouchablePress,
} from '../../lib'

export function welcomeTrigger(setState: (key: string, value: any) => void) {
export function welcomeTrigger() {
useInit(() => {
setState('welcome', `Loading...`)
setBarnState('welcome', `Loading...`)
useBarn('welcome', newMessage => {
// tslint:disable-next-line
console.log('Welcoming', newMessage)
return
})
})

Expand All @@ -21,7 +24,7 @@ export function welcomeTrigger(setState: (key: string, value: any) => void) {
nameOf: nameOfTouchable('welcome-button'),
},
({ name }) => {
setState('welcome', `You (${name}) are already welcomed!`)
setBarnState('welcome', `You (${name}) are already welcomed!`)
},
)

Expand All @@ -31,15 +34,16 @@ export function welcomeTrigger(setState: (key: string, value: any) => void) {
},
({ name }) => {
alert(`${name} is PRESSED! YAY !!!`)
console.log('hey, are you working?')
},
)

useTimerElapsed(5, () => {
setState('welcome', `Who are you?`)
setBarnState('welcome', `Who are you?`)
})

useBarn('welcome', newMessage => {
// tslint:disable-next-line
console.log(`Barn welcome changed to: ${newMessage}`)
return
})
}
11 changes: 5 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { register, useBarnStream } from 'bara'
import { register, useBarnStream, useInitStream } from 'bara'
import App from './App'
import './index.css'
import {
mapBarnWithReact,
useReactApp,
useTextStream,
useTouchableOpacityStream,
Expand All @@ -13,23 +12,23 @@ import {
import { welcomeTrigger } from './examples/features/welcome'

const BaraApp = () => {
const [setState] = useBarnStream({
useInitStream()
useBarnStream({
version: '1.0.0',
welcome: 'Welcome to Bara React App!',
})
useReactApp({ name: 'bara-app', App })
mapBarnWithReact(setState)
useViewStream()
useTouchableStream()
useTouchableOpacityStream()
useTextStream()
welcomeTrigger(setState)
welcomeTrigger()
}

const bara = register(BaraApp)

if (process.env.NODE_ENV === 'development' || __DEV__) {
if (window) {
(window as any).__BARA__ = bara
;(window as any).__BARA__ = bara
}
}
39 changes: 22 additions & 17 deletions src/lib/exports/Text/stream.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
import { useStream } from 'bara'
import { EventEmitter, EventSubscription } from 'fbemitter'
import { createEmitter, useEmitter, useStream } from 'bara'

import { BaraReactText, ON_TEXT_PRESS, ON_TEXT_LONG_PRESS } from './event'
import { BaraReactText, ON_TEXT_LONG_PRESS, ON_TEXT_PRESS } from './event'

export interface BaraTextContext {
onPress: (data: BaraReactText) => void
onLongPress: (data: BaraReactText) => void
}

const emitter = new EventEmitter()

export const textContext: BaraTextContext = {
onPress: data => {
emitter.emit(ON_TEXT_PRESS(), data)
},
onLongPress: data => {
emitter.emit(ON_TEXT_LONG_PRESS(), data)
},
}

export function useTextStream() {
const emitter = createEmitter(({ setName, addEventType }) => {
setName('dev.barajs.react.text.emitter')
addEventType(ON_TEXT_PRESS)
addEventType(ON_TEXT_LONG_PRESS)
})

return useStream<BaraReactText>(({ setName, emit, addEventTypes }) => {
setName('dev.barajs.react.text')
addEventTypes([ON_TEXT_PRESS, ON_TEXT_LONG_PRESS])

const onPressListener = emitter.addListener(
ON_TEXT_PRESS(),
ON_TEXT_PRESS,
(data: BaraReactText) => {
emit(ON_TEXT_PRESS, data)
},
)

const onLongPressListener = emitter.addListener(
ON_TEXT_LONG_PRESS(),
ON_TEXT_LONG_PRESS,
(data: BaraReactText) => {
emit(ON_TEXT_LONG_PRESS, data)
},
Expand All @@ -41,7 +35,18 @@ export function useTextStream() {
return () => {
onPressListener.remove()
onLongPressListener.remove()
emitter.removeAllListeners()
}
})
}

export const textContext: BaraTextContext = {
onPress: data => {
const emit = useEmitter(ON_TEXT_PRESS)
emit!(data)
},
onLongPress: data => {
const emit = useEmitter(ON_TEXT_LONG_PRESS)
emit!(data)
},
}

60 changes: 33 additions & 27 deletions src/lib/exports/Touchable/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useStream } from 'bara'
import { EventEmitter, EventSubscription } from 'fbemitter'
import { createEmitter, useEmitter, useStream } from 'bara'
import React from 'react'

import {
Expand All @@ -17,27 +16,15 @@ export interface BaraTouchableContext {
onLongPress: (data: BaraReactTouchable) => void
}

// Emitter handle the reference with React Component via Context API
const emitter = new EventEmitter()

// Export and being consumed by any Touchable component
export const touchableContext: BaraTouchableContext = {
onPress: data => {
emitter.emit(ON_TOUCHABLE_PRESS(), data)
},
onPressIn: data => {
emitter.emit(ON_TOUCHABLE_PRESS_IN(), data)
},
onPressOut: data => {
emitter.emit(ON_TOUCHABLE_PRESS_OUT(), data)
},
onLongPress: data => {
emitter.emit(ON_TOUCHABLE_LONG_PRESS(), data)
},
}

// BaraJS Stream Register
export function useTouchableStream() {
const emitter = createEmitter(({ setName, addEventType }) => {
setName('dev.barajs.react.touchable.emitter')
addEventType(ON_TOUCHABLE_LONG_PRESS)
addEventType(ON_TOUCHABLE_PRESS)
addEventType(ON_TOUCHABLE_PRESS_IN)
addEventType(ON_TOUCHABLE_PRESS_OUT)
})

return useStream<BaraReactTouchable>(({ setName, emit, addEventTypes }) => {
setName('dev.barajs.react.touchable')
addEventTypes([
Expand All @@ -48,28 +35,28 @@ export function useTouchableStream() {
])

const onPressListener = emitter.addListener(
ON_TOUCHABLE_PRESS(),
ON_TOUCHABLE_PRESS,
(data: BaraReactTouchable) => {
emit(ON_TOUCHABLE_PRESS, data)
},
)

const onPressInListener = emitter.addListener(
ON_TOUCHABLE_PRESS_IN(),
ON_TOUCHABLE_PRESS_IN,
(data: BaraReactTouchable) => {
emit(ON_TOUCHABLE_PRESS_IN, data)
},
)

const onPressOutListener = emitter.addListener(
ON_TOUCHABLE_PRESS_OUT(),
ON_TOUCHABLE_PRESS_OUT,
(data: BaraReactTouchable) => {
emit(ON_TOUCHABLE_PRESS_OUT, data)
},
)

const onLongPressListener = emitter.addListener(
ON_TOUCHABLE_LONG_PRESS(),
ON_TOUCHABLE_LONG_PRESS,
(data: BaraReactTouchable) => {
emit(ON_TOUCHABLE_LONG_PRESS, data)
},
Expand All @@ -80,7 +67,26 @@ export function useTouchableStream() {
onPressInListener.remove()
onPressOutListener.remove()
onLongPressListener.remove()
emitter.removeAllListeners()
}
})
}

// Export and being consumed by any Touchable component
export const touchableContext: BaraTouchableContext = {
onPress: data => {
const emit = useEmitter(ON_TOUCHABLE_PRESS)
emit!(data)
},
onPressIn: data => {
const emit = useEmitter(ON_TOUCHABLE_PRESS_IN)
emit!(data)
},
onPressOut: data => {
const emit = useEmitter(ON_TOUCHABLE_PRESS_OUT)
emit!(data)
},
onLongPress: data => {
const emit = useEmitter(ON_TOUCHABLE_LONG_PRESS)
emit!(data)
},
}
60 changes: 34 additions & 26 deletions src/lib/exports/TouchableOpacity/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useStream } from 'bara'
import { EventEmitter } from 'fbemitter'
import { createEmitter, useEmitter, useStream } from 'bara'

import {
BaraReactTouchableOpacity,
Expand All @@ -16,27 +15,17 @@ export interface BaraTouchableOpacityContext {
onLongPress: (data: BaraReactTouchableOpacity) => void
}

// Emitter handle the reference with React Component via Context API
const emitter = new EventEmitter()

// Export and being consumed by any Touchable component
export const touchableOpacityContext: BaraTouchableOpacityContext = {
onPress: data => {
emitter.emit(ON_TOUCHABLE_OPACITY_PRESS(), data)
},
onPressIn: data => {
emitter.emit(ON_TOUCHABLE_OPACITY_PRESS_IN(), data)
},
onPressOut: data => {
emitter.emit(ON_TOUCHABLE_OPACITY_PRESS_OUT(), data)
},
onLongPress: data => {
emitter.emit(ON_TOUCHABLE_OPACITY_LONG_PRESS(), data)
},
}

// BaraJS Stream Register
export function useTouchableOpacityStream() {
const emitter = createEmitter(({ setName, addEventType }) => {
setName('dev.barajs.react.touchable-opacity.emitter')

addEventType(ON_TOUCHABLE_OPACITY_LONG_PRESS)
addEventType(ON_TOUCHABLE_OPACITY_PRESS)
addEventType(ON_TOUCHABLE_OPACITY_PRESS_IN)
addEventType(ON_TOUCHABLE_OPACITY_PRESS_OUT)
})

return useStream<BaraReactTouchableOpacity>(
({ setName, emit, addEventTypes }) => {
setName('dev.barajs.react.touchableOpacity')
Expand All @@ -48,28 +37,28 @@ export function useTouchableOpacityStream() {
])

const onPressListener = emitter.addListener(
ON_TOUCHABLE_OPACITY_PRESS(),
ON_TOUCHABLE_OPACITY_PRESS,
(data: BaraReactTouchableOpacity) => {
emit(ON_TOUCHABLE_OPACITY_PRESS, data)
},
)

const onPressInListener = emitter.addListener(
ON_TOUCHABLE_OPACITY_PRESS_IN(),
ON_TOUCHABLE_OPACITY_PRESS_IN,
(data: BaraReactTouchableOpacity) => {
emit(ON_TOUCHABLE_OPACITY_PRESS_IN, data)
},
)

const onPressOutListener = emitter.addListener(
ON_TOUCHABLE_OPACITY_PRESS_OUT(),
ON_TOUCHABLE_OPACITY_PRESS_OUT,
(data: BaraReactTouchableOpacity) => {
emit(ON_TOUCHABLE_OPACITY_PRESS_OUT, data)
},
)

const onLongPressListener = emitter.addListener(
ON_TOUCHABLE_OPACITY_LONG_PRESS(),
ON_TOUCHABLE_OPACITY_LONG_PRESS,
(data: BaraReactTouchableOpacity) => {
emit(ON_TOUCHABLE_OPACITY_LONG_PRESS, data)
},
Expand All @@ -80,8 +69,27 @@ export function useTouchableOpacityStream() {
onPressInListener.remove()
onPressOutListener.remove()
onLongPressListener.remove()
emitter.removeAllListeners()
}
},
)
}

// Export and being consumed by any Touchable component
export const touchableOpacityContext: BaraTouchableOpacityContext = {
onPress: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS)
emit!(data)
},
onPressIn: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_IN)
emit!(data)
},
onPressOut: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_OUT)
emit!(data)
},
onLongPress: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_LONG_PRESS)
emit!(data)
},
}
Loading

0 comments on commit a42e548

Please sign in to comment.