This repository has been archived by the owner on Feb 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Fix test --coverage errors related to TS #8
Merged
brentvatne
merged 16 commits into
react-navigation:master
from
horacioh:feature/typescript
Oct 25, 2018
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
edbd479
fix ts test errors + remove formatters
3d5836e
add husky again
75aa6a0
fix ts test errors + remove formatters
22236bb
add husky again
092c037
Merge branch 'feature/typescript' of github.com:horacioh/react-naviga…
833c07b
yarn install update
383b713
change new ts file for a simpler one
80a8a0f
add another ts file
4baa953
Merge branch 'master' into feature/typescript
1665c14
add tsconfig to git (it was ignored)
8392a50
update gitignore
ce562bf
remove tsconfig.json comments
5bdc868
Merge branch 'master' into feature/typescript
ef2ee0c
fix merge with master
449d080
Merge branch 'master' into feature/typescript
f67807b
remove getChildEventSubscriber.ts file bc of conflicts with updates
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
dist/ | ||
jest-setup.js | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* This is used to extract one children's worth of events from a stream of navigation action events | ||
* | ||
* Based on the 'action' events that get fired for this navigation state, this utility will fire | ||
* focus and blur events for this child | ||
*/ | ||
export default function getChildEventSubscriber( | ||
addListener: ( | ||
eventName: string, | ||
eventHandler: EventHandler | ||
) => { remove: () => void }, | ||
key: string | ||
) { | ||
const actionSubscribers = new Set<EventHandler>(); | ||
const willFocusSubscribers = new Set<EventHandler>(); | ||
const didFocusSubscribers = new Set<EventHandler>(); | ||
const willBlurSubscribers = new Set<EventHandler>(); | ||
const didBlurSubscribers = new Set<EventHandler>(); | ||
|
||
const removeAll = () => { | ||
[ | ||
actionSubscribers, | ||
willFocusSubscribers, | ||
didFocusSubscribers, | ||
willBlurSubscribers, | ||
didBlurSubscribers, | ||
].forEach(set => set.clear()); | ||
|
||
upstreamSubscribers.forEach(subs => subs && subs.remove()); | ||
}; | ||
|
||
const getChildSubscribers = (evtName: string) => { | ||
switch (evtName) { | ||
case 'action': | ||
return actionSubscribers; | ||
case 'willFocus': | ||
return willFocusSubscribers; | ||
case 'didFocus': | ||
return didFocusSubscribers; | ||
case 'willBlur': | ||
return willBlurSubscribers; | ||
case 'didBlur': | ||
return didBlurSubscribers; | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
const emit = (type: string, payload: IPayload) => { | ||
const payloadWithType = { ...payload, type }; | ||
const subscribers = getChildSubscribers(type); | ||
if (subscribers) { | ||
subscribers.forEach(subs => { | ||
subs(payloadWithType); | ||
}); | ||
} | ||
}; | ||
|
||
// lastEmittedEvent keeps track of focus state for one route. First we assume | ||
// we are blurred. If we are focused on initialization, the first 'action' | ||
// event will cause onFocus+willFocus events because we had previously been | ||
// considered blurred | ||
let lastEmittedEvent = 'didBlur'; | ||
|
||
const upstreamEvents = [ | ||
'willFocus', | ||
'didFocus', | ||
'willBlur', | ||
'didBlur', | ||
'action', | ||
]; | ||
|
||
const upstreamSubscribers = upstreamEvents.map((eventName: string) => | ||
addListener(eventName, payload => { | ||
const { state, lastState, action } = payload; | ||
const lastRoutes = lastState && lastState.routes; | ||
const routes = state && state.routes; | ||
|
||
// const lastFocusKey = | ||
// lastState && lastState.routes && lastState.routes[lastState.index].key; | ||
const focusKey = routes && routes[state.index].key; | ||
|
||
const isChildFocused = focusKey === key; | ||
const lastRoute = | ||
lastRoutes && lastRoutes.find(route => route.key === key); | ||
const newRoute = routes && routes.find(route => route.key === key); | ||
const childPayload: IPayload = { | ||
context: `${key}:${action.type}_${payload.context || 'Root'}`, | ||
state: newRoute, | ||
lastState: lastRoute, | ||
action, | ||
type: eventName, | ||
}; | ||
const isTransitioning = !!state && state.isTransitioning; | ||
|
||
const previouslyLastEmittedEvent = lastEmittedEvent; | ||
|
||
if (lastEmittedEvent === 'didBlur') { | ||
// The child is currently blurred. Look for willFocus conditions | ||
if (eventName === 'willFocus' && isChildFocused) { | ||
emit((lastEmittedEvent = 'willFocus'), childPayload); | ||
} else if (eventName === 'action' && isChildFocused) { | ||
emit((lastEmittedEvent = 'willFocus'), childPayload); | ||
} | ||
} | ||
if (lastEmittedEvent === 'willFocus') { | ||
// We are currently mid-focus. Look for didFocus conditions. | ||
// If state.isTransitioning is false, this child event happens immediately after willFocus | ||
if (eventName === 'didFocus' && isChildFocused && !isTransitioning) { | ||
emit((lastEmittedEvent = 'didFocus'), childPayload); | ||
} else if ( | ||
eventName === 'action' && | ||
isChildFocused && | ||
!isTransitioning | ||
) { | ||
emit((lastEmittedEvent = 'didFocus'), childPayload); | ||
} | ||
} | ||
|
||
if (lastEmittedEvent === 'didFocus') { | ||
// The child is currently focused. Look for blurring events | ||
if (!isChildFocused) { | ||
// The child is no longer focused within this navigation state | ||
emit((lastEmittedEvent = 'willBlur'), childPayload); | ||
} else if (eventName === 'willBlur') { | ||
// The parent is getting a willBlur event | ||
emit((lastEmittedEvent = 'willBlur'), childPayload); | ||
} else if ( | ||
eventName === 'action' && | ||
previouslyLastEmittedEvent === 'didFocus' | ||
) { | ||
// While focused, pass action events to children for grandchildren focus | ||
emit('action', childPayload); | ||
} | ||
} | ||
|
||
if (lastEmittedEvent === 'willBlur') { | ||
// The child is mid-blur. Wait for transition to end | ||
if (eventName === 'action' && !isChildFocused && !isTransitioning) { | ||
// The child is done blurring because transitioning is over, or isTransitioning | ||
// never began and didBlur fires immediately after willBlur | ||
emit((lastEmittedEvent = 'didBlur'), childPayload); | ||
} else if (eventName === 'didBlur') { | ||
// Pass through the parent didBlur event if it happens | ||
emit((lastEmittedEvent = 'didBlur'), childPayload); | ||
} | ||
} | ||
|
||
if (lastEmittedEvent === 'didBlur' && !newRoute) { | ||
removeAll(); | ||
} | ||
}) | ||
); | ||
|
||
return { | ||
addListener(eventName: string, eventHandler: EventHandler) { | ||
const subscribers = getChildSubscribers(eventName); | ||
if (!subscribers) { | ||
throw new Error(`Invalid event name "${eventName}"`); | ||
} | ||
subscribers.add(eventHandler); | ||
const remove = () => { | ||
subscribers.delete(eventHandler); | ||
}; | ||
return { remove }; | ||
}, | ||
}; | ||
} | ||
|
||
export type EventHandler = (payload: IPayload) => void; | ||
|
||
export interface IPayload { | ||
action: { type: string }; | ||
context?: string; | ||
lastState: IRoute; | ||
state: IRoute; | ||
type: string; | ||
} | ||
|
||
export interface IRoute { | ||
index: number; | ||
isTransitioning: boolean; | ||
key?: string; | ||
routeName?: string; | ||
routes: any[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
let uniqueBaseId: string = `id-${Date.now()}`; | ||
let uuidCount: number = 0; | ||
|
||
export function _TESTING_ONLY_normalize_keys(): void { | ||
uniqueBaseId = 'id'; | ||
uuidCount = 0; | ||
} | ||
|
||
export function generateKey(): string { | ||
return `${uniqueBaseId}-${uuidCount++}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"sourceMap": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "tslint:recommended", | ||
"rules": { | ||
"arrow-parens": [true, "ban-single-arg-parens"], | ||
"arrow-return-shorthand": [true, "multiline"], | ||
"trailing-comma": false, | ||
"quotemark": { | ||
"options": [ | ||
"single", | ||
"avoid-escape" | ||
] | ||
}, | ||
"object-literal-sort-keys": false | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.