-
Notifications
You must be signed in to change notification settings - Fork 15
considering strongly-typed bus events. #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
32bitkid
wants to merge
1
commit into
fabienjuif:master
Choose a base branch
from
32bitkid:feature/strongly-typed-buses
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 hidden or 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,14 +1,32 @@ | ||
declare module "use-bus" { | ||
export interface EventAction { | ||
type: string; | ||
export interface EventAction<T extends string = string> { | ||
type: T; | ||
[key: string]: any; | ||
} | ||
|
||
export function dispatch(name: string): void; | ||
export function dispatch(event: EventAction): void; | ||
interface DispatchFn<T extends EventAction = EventAction> { | ||
(name: keyof T): void; | ||
(event: T): void; | ||
} | ||
|
||
type FilterActionType< | ||
A extends EventAction, | ||
ActionType extends A["type"] | ||
> = A extends any ? ActionType extends A['type'] ? A : never : never; | ||
|
||
interface UseBus< | ||
T extends EventAction = EventAction | ||
> { | ||
<TName extends T['type'] = T['type']>(name: TName, callback: (event: FilterActionType<T, TName>) => void, deps: any[]): DispatchFn<T>; | ||
<TName extends T['type'] = T['type']>(name: TName[], callback: (event: FilterActionType<T, TName>) => void, deps: any[]): DispatchFn<T>; | ||
(name: RegExp, callback: (event: T) => void, deps: any[]): DispatchFn<T>; | ||
<TEvent extends T>(filter: (event: T) => event is TEvent, callback: (event:TEvent) => void, deps: any[]): DispatchFn<T>; | ||
} | ||
|
||
export const dispatch: DispatchFn; | ||
|
||
const useBus: UseBus; | ||
export default useBus; | ||
|
||
export default function useBus(name: string, callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
export default function useBus(name: string[], callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
export default function useBus(name: RegExp, callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
export default function useBus(filter: (event: EventAction) => boolean, callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
export function createBus<TEvents extends EventAction = EventAction>(): [UseBus<TEvents>, DispatchFn<TEvents>]; | ||
} |
This file contains hidden or 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,43 +1,50 @@ | ||
import { useEffect } from 'react' | ||
|
||
let subscribers = [] | ||
export const createBus = () => { | ||
let subscribers = [] | ||
|
||
const subscribe = (filter, callback) => { | ||
if (filter === undefined || filter === null) return undefined | ||
if (callback === undefined || callback === null) return undefined | ||
const subscribe = (filter, callback) => { | ||
if (filter === undefined || filter === null) return undefined | ||
if (callback === undefined || callback === null) return undefined | ||
|
||
subscribers = [ | ||
...subscribers, | ||
[filter, callback], | ||
] | ||
subscribers = [ | ||
...subscribers, | ||
[filter, callback], | ||
] | ||
|
||
return () => { | ||
subscribers = subscribers.filter((subscriber) => subscriber[1] !== callback) | ||
return () => { | ||
subscribers = subscribers.filter((subscriber) => subscriber[1] !== callback) | ||
} | ||
} | ||
} | ||
|
||
export const dispatch = (event) => { | ||
let { type } = event | ||
if (typeof event === 'string') type = event | ||
const dispatch = (event) => { | ||
let { type } = event | ||
if (typeof event === 'string') type = event | ||
|
||
const args = [] | ||
if (typeof event === 'string') args.push({ type }) | ||
else args.push(event) | ||
const args = [] | ||
if (typeof event === 'string') args.push({ type }) | ||
else args.push(event) | ||
|
||
subscribers.forEach(([filter, callback]) => { | ||
if (typeof filter === 'string' && filter !== type) return | ||
if (Array.isArray(filter) && !filter.includes(type)) return | ||
if (filter instanceof RegExp && !filter.test(type)) return | ||
if (typeof filter === 'function' && !filter(...args)) return | ||
subscribers.forEach(([filter, callback]) => { | ||
if (typeof filter === 'string' && filter !== type) return | ||
if (Array.isArray(filter) && !filter.includes(type)) return | ||
if (filter instanceof RegExp && !filter.test(type)) return | ||
if (typeof filter === 'function' && !filter(...args)) return | ||
|
||
callback(...args) | ||
}) | ||
} | ||
callback(...args) | ||
}) | ||
} | ||
|
||
const useBus = (type, callback, deps = []) => { | ||
useEffect(() => subscribe(type, callback), deps) | ||
const useBus = (type, callback, deps = []) => { | ||
useEffect(() => subscribe(type, callback), deps) | ||
|
||
return dispatch | ||
return dispatch | ||
} | ||
|
||
return [useBus, dispatch] | ||
} | ||
|
||
const [useBus, dispatch] = createBus(); | ||
|
||
export { dispatch } | ||
export default useBus |
This file contains hidden or 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about adding a second optional generic type for the value of
[key: string]
instead ofany
and default that new generic type toany