Skip to content

Commit a42e548

Browse files
committed
fix(license): replace fbemitter with built-in bara's useEmitter
1 parent 40d49b7 commit a42e548

File tree

9 files changed

+147
-150
lines changed

9 files changed

+147
-150
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
"src/lib"
1717
],
1818
"dependencies": {
19-
"bara": "2.2.0",
20-
"fbemitter": "2.1.1",
19+
"bara": "2.3.0",
2120
"react-native-web": "0.11.2"
2221
},
2322
"devDependencies": {
2423
"@semantic-release/changelog": "3.0.2",
2524
"@semantic-release/git": "7.0.8",
26-
"@types/fbemitter": "2.0.32",
2725
"@types/jest": "24.0.11",
2826
"@types/node": "11.13.5",
2927
"@types/react": "16.8.12",

src/examples/features/welcome.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
import { useInit, useBarn, useTimerElapsed } from 'bara'
1+
import { setBarnState, useBarn, useInit, useTimerElapsed } from 'bara'
22

33
import {
4+
nameOfText,
45
nameOfTouchable,
56
nameOfTouchableOpacity,
6-
useTouchablePress,
7-
useTouchableOpacityPress,
87
useTextPress,
9-
nameOfText,
8+
useTouchableOpacityPress,
9+
useTouchablePress,
1010
} from '../../lib'
1111

12-
export function welcomeTrigger(setState: (key: string, value: any) => void) {
12+
export function welcomeTrigger() {
1313
useInit(() => {
14-
setState('welcome', `Loading...`)
14+
setBarnState('welcome', `Loading...`)
1515
useBarn('welcome', newMessage => {
16+
// tslint:disable-next-line
17+
console.log('Welcoming', newMessage)
18+
return
1619
})
1720
})
1821

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

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

3840
useTimerElapsed(5, () => {
39-
setState('welcome', `Who are you?`)
41+
setBarnState('welcome', `Who are you?`)
4042
})
4143

4244
useBarn('welcome', newMessage => {
45+
// tslint:disable-next-line
4346
console.log(`Barn welcome changed to: ${newMessage}`)
47+
return
4448
})
4549
}

src/index.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { register, useBarnStream } from 'bara'
1+
import { register, useBarnStream, useInitStream } from 'bara'
22
import App from './App'
33
import './index.css'
44
import {
5-
mapBarnWithReact,
65
useReactApp,
76
useTextStream,
87
useTouchableOpacityStream,
@@ -13,23 +12,23 @@ import {
1312
import { welcomeTrigger } from './examples/features/welcome'
1413

1514
const BaraApp = () => {
16-
const [setState] = useBarnStream({
15+
useInitStream()
16+
useBarnStream({
1717
version: '1.0.0',
1818
welcome: 'Welcome to Bara React App!',
1919
})
2020
useReactApp({ name: 'bara-app', App })
21-
mapBarnWithReact(setState)
2221
useViewStream()
2322
useTouchableStream()
2423
useTouchableOpacityStream()
2524
useTextStream()
26-
welcomeTrigger(setState)
25+
welcomeTrigger()
2726
}
2827

2928
const bara = register(BaraApp)
3029

3130
if (process.env.NODE_ENV === 'development' || __DEV__) {
3231
if (window) {
33-
(window as any).__BARA__ = bara
32+
;(window as any).__BARA__ = bara
3433
}
3534
}

src/lib/exports/Text/stream.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
1-
import { useStream } from 'bara'
2-
import { EventEmitter, EventSubscription } from 'fbemitter'
1+
import { createEmitter, useEmitter, useStream } from 'bara'
32

4-
import { BaraReactText, ON_TEXT_PRESS, ON_TEXT_LONG_PRESS } from './event'
3+
import { BaraReactText, ON_TEXT_LONG_PRESS, ON_TEXT_PRESS } from './event'
54

65
export interface BaraTextContext {
76
onPress: (data: BaraReactText) => void
87
onLongPress: (data: BaraReactText) => void
98
}
109

11-
const emitter = new EventEmitter()
12-
13-
export const textContext: BaraTextContext = {
14-
onPress: data => {
15-
emitter.emit(ON_TEXT_PRESS(), data)
16-
},
17-
onLongPress: data => {
18-
emitter.emit(ON_TEXT_LONG_PRESS(), data)
19-
},
20-
}
21-
2210
export function useTextStream() {
11+
const emitter = createEmitter(({ setName, addEventType }) => {
12+
setName('dev.barajs.react.text.emitter')
13+
addEventType(ON_TEXT_PRESS)
14+
addEventType(ON_TEXT_LONG_PRESS)
15+
})
16+
2317
return useStream<BaraReactText>(({ setName, emit, addEventTypes }) => {
2418
setName('dev.barajs.react.text')
2519
addEventTypes([ON_TEXT_PRESS, ON_TEXT_LONG_PRESS])
2620

2721
const onPressListener = emitter.addListener(
28-
ON_TEXT_PRESS(),
22+
ON_TEXT_PRESS,
2923
(data: BaraReactText) => {
3024
emit(ON_TEXT_PRESS, data)
3125
},
3226
)
3327

3428
const onLongPressListener = emitter.addListener(
35-
ON_TEXT_LONG_PRESS(),
29+
ON_TEXT_LONG_PRESS,
3630
(data: BaraReactText) => {
3731
emit(ON_TEXT_LONG_PRESS, data)
3832
},
@@ -41,7 +35,18 @@ export function useTextStream() {
4135
return () => {
4236
onPressListener.remove()
4337
onLongPressListener.remove()
44-
emitter.removeAllListeners()
4538
}
4639
})
4740
}
41+
42+
export const textContext: BaraTextContext = {
43+
onPress: data => {
44+
const emit = useEmitter(ON_TEXT_PRESS)
45+
emit!(data)
46+
},
47+
onLongPress: data => {
48+
const emit = useEmitter(ON_TEXT_LONG_PRESS)
49+
emit!(data)
50+
},
51+
}
52+
Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useStream } from 'bara'
2-
import { EventEmitter, EventSubscription } from 'fbemitter'
1+
import { createEmitter, useEmitter, useStream } from 'bara'
32
import React from 'react'
43

54
import {
@@ -17,27 +16,15 @@ export interface BaraTouchableContext {
1716
onLongPress: (data: BaraReactTouchable) => void
1817
}
1918

20-
// Emitter handle the reference with React Component via Context API
21-
const emitter = new EventEmitter()
22-
23-
// Export and being consumed by any Touchable component
24-
export const touchableContext: BaraTouchableContext = {
25-
onPress: data => {
26-
emitter.emit(ON_TOUCHABLE_PRESS(), data)
27-
},
28-
onPressIn: data => {
29-
emitter.emit(ON_TOUCHABLE_PRESS_IN(), data)
30-
},
31-
onPressOut: data => {
32-
emitter.emit(ON_TOUCHABLE_PRESS_OUT(), data)
33-
},
34-
onLongPress: data => {
35-
emitter.emit(ON_TOUCHABLE_LONG_PRESS(), data)
36-
},
37-
}
38-
39-
// BaraJS Stream Register
4019
export function useTouchableStream() {
20+
const emitter = createEmitter(({ setName, addEventType }) => {
21+
setName('dev.barajs.react.touchable.emitter')
22+
addEventType(ON_TOUCHABLE_LONG_PRESS)
23+
addEventType(ON_TOUCHABLE_PRESS)
24+
addEventType(ON_TOUCHABLE_PRESS_IN)
25+
addEventType(ON_TOUCHABLE_PRESS_OUT)
26+
})
27+
4128
return useStream<BaraReactTouchable>(({ setName, emit, addEventTypes }) => {
4229
setName('dev.barajs.react.touchable')
4330
addEventTypes([
@@ -48,28 +35,28 @@ export function useTouchableStream() {
4835
])
4936

5037
const onPressListener = emitter.addListener(
51-
ON_TOUCHABLE_PRESS(),
38+
ON_TOUCHABLE_PRESS,
5239
(data: BaraReactTouchable) => {
5340
emit(ON_TOUCHABLE_PRESS, data)
5441
},
5542
)
5643

5744
const onPressInListener = emitter.addListener(
58-
ON_TOUCHABLE_PRESS_IN(),
45+
ON_TOUCHABLE_PRESS_IN,
5946
(data: BaraReactTouchable) => {
6047
emit(ON_TOUCHABLE_PRESS_IN, data)
6148
},
6249
)
6350

6451
const onPressOutListener = emitter.addListener(
65-
ON_TOUCHABLE_PRESS_OUT(),
52+
ON_TOUCHABLE_PRESS_OUT,
6653
(data: BaraReactTouchable) => {
6754
emit(ON_TOUCHABLE_PRESS_OUT, data)
6855
},
6956
)
7057

7158
const onLongPressListener = emitter.addListener(
72-
ON_TOUCHABLE_LONG_PRESS(),
59+
ON_TOUCHABLE_LONG_PRESS,
7360
(data: BaraReactTouchable) => {
7461
emit(ON_TOUCHABLE_LONG_PRESS, data)
7562
},
@@ -80,7 +67,26 @@ export function useTouchableStream() {
8067
onPressInListener.remove()
8168
onPressOutListener.remove()
8269
onLongPressListener.remove()
83-
emitter.removeAllListeners()
8470
}
8571
})
8672
}
73+
74+
// Export and being consumed by any Touchable component
75+
export const touchableContext: BaraTouchableContext = {
76+
onPress: data => {
77+
const emit = useEmitter(ON_TOUCHABLE_PRESS)
78+
emit!(data)
79+
},
80+
onPressIn: data => {
81+
const emit = useEmitter(ON_TOUCHABLE_PRESS_IN)
82+
emit!(data)
83+
},
84+
onPressOut: data => {
85+
const emit = useEmitter(ON_TOUCHABLE_PRESS_OUT)
86+
emit!(data)
87+
},
88+
onLongPress: data => {
89+
const emit = useEmitter(ON_TOUCHABLE_LONG_PRESS)
90+
emit!(data)
91+
},
92+
}
Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useStream } from 'bara'
2-
import { EventEmitter } from 'fbemitter'
1+
import { createEmitter, useEmitter, useStream } from 'bara'
32

43
import {
54
BaraReactTouchableOpacity,
@@ -16,27 +15,17 @@ export interface BaraTouchableOpacityContext {
1615
onLongPress: (data: BaraReactTouchableOpacity) => void
1716
}
1817

19-
// Emitter handle the reference with React Component via Context API
20-
const emitter = new EventEmitter()
21-
22-
// Export and being consumed by any Touchable component
23-
export const touchableOpacityContext: BaraTouchableOpacityContext = {
24-
onPress: data => {
25-
emitter.emit(ON_TOUCHABLE_OPACITY_PRESS(), data)
26-
},
27-
onPressIn: data => {
28-
emitter.emit(ON_TOUCHABLE_OPACITY_PRESS_IN(), data)
29-
},
30-
onPressOut: data => {
31-
emitter.emit(ON_TOUCHABLE_OPACITY_PRESS_OUT(), data)
32-
},
33-
onLongPress: data => {
34-
emitter.emit(ON_TOUCHABLE_OPACITY_LONG_PRESS(), data)
35-
},
36-
}
37-
3818
// BaraJS Stream Register
3919
export function useTouchableOpacityStream() {
20+
const emitter = createEmitter(({ setName, addEventType }) => {
21+
setName('dev.barajs.react.touchable-opacity.emitter')
22+
23+
addEventType(ON_TOUCHABLE_OPACITY_LONG_PRESS)
24+
addEventType(ON_TOUCHABLE_OPACITY_PRESS)
25+
addEventType(ON_TOUCHABLE_OPACITY_PRESS_IN)
26+
addEventType(ON_TOUCHABLE_OPACITY_PRESS_OUT)
27+
})
28+
4029
return useStream<BaraReactTouchableOpacity>(
4130
({ setName, emit, addEventTypes }) => {
4231
setName('dev.barajs.react.touchableOpacity')
@@ -48,28 +37,28 @@ export function useTouchableOpacityStream() {
4837
])
4938

5039
const onPressListener = emitter.addListener(
51-
ON_TOUCHABLE_OPACITY_PRESS(),
40+
ON_TOUCHABLE_OPACITY_PRESS,
5241
(data: BaraReactTouchableOpacity) => {
5342
emit(ON_TOUCHABLE_OPACITY_PRESS, data)
5443
},
5544
)
5645

5746
const onPressInListener = emitter.addListener(
58-
ON_TOUCHABLE_OPACITY_PRESS_IN(),
47+
ON_TOUCHABLE_OPACITY_PRESS_IN,
5948
(data: BaraReactTouchableOpacity) => {
6049
emit(ON_TOUCHABLE_OPACITY_PRESS_IN, data)
6150
},
6251
)
6352

6453
const onPressOutListener = emitter.addListener(
65-
ON_TOUCHABLE_OPACITY_PRESS_OUT(),
54+
ON_TOUCHABLE_OPACITY_PRESS_OUT,
6655
(data: BaraReactTouchableOpacity) => {
6756
emit(ON_TOUCHABLE_OPACITY_PRESS_OUT, data)
6857
},
6958
)
7059

7160
const onLongPressListener = emitter.addListener(
72-
ON_TOUCHABLE_OPACITY_LONG_PRESS(),
61+
ON_TOUCHABLE_OPACITY_LONG_PRESS,
7362
(data: BaraReactTouchableOpacity) => {
7463
emit(ON_TOUCHABLE_OPACITY_LONG_PRESS, data)
7564
},
@@ -80,8 +69,27 @@ export function useTouchableOpacityStream() {
8069
onPressInListener.remove()
8170
onPressOutListener.remove()
8271
onLongPressListener.remove()
83-
emitter.removeAllListeners()
8472
}
8573
},
8674
)
8775
}
76+
77+
// Export and being consumed by any Touchable component
78+
export const touchableOpacityContext: BaraTouchableOpacityContext = {
79+
onPress: data => {
80+
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS)
81+
emit!(data)
82+
},
83+
onPressIn: data => {
84+
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_IN)
85+
emit!(data)
86+
},
87+
onPressOut: data => {
88+
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_OUT)
89+
emit!(data)
90+
},
91+
onLongPress: data => {
92+
const emit = useEmitter(ON_TOUCHABLE_OPACITY_LONG_PRESS)
93+
emit!(data)
94+
},
95+
}

0 commit comments

Comments
 (0)