Skip to content

Commit

Permalink
feat: remove unecessary context
Browse files Browse the repository at this point in the history
  • Loading branch information
macoley committed Apr 13, 2022
1 parent 911d5a4 commit fa95498
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 85 deletions.
78 changes: 0 additions & 78 deletions src/context.ts

This file was deleted.

23 changes: 17 additions & 6 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as React from 'react'
import React from 'react'
import {
unstable_NormalPriority as NormalPriority,
unstable_runWithPriority as runWithPriority
} from 'scheduler'

import { isDev, useIsomorphicLayoutEffect } from './common'
import { createContext } from './context'
import { ContextListener } from './types'

/**
* Types
*/
type ContextListener<Value> = (payload: Readonly<Value>) => void

type EventKey = string | number | symbol

Expand Down Expand Up @@ -49,7 +53,6 @@ export default function events<
current: EVENT_DISPATCHED_NULL
}
const contextListeners: ContextListener<EventState>[] = []
const context = createContext<EventState>(contextListeners, EVENT_STATE_NULL)

/**
* Dispatcher
Expand Down Expand Up @@ -78,12 +81,20 @@ export default function events<
/**
* Provider
*/
const BindProvider: React.FC = ({ children }) => {
const BindProvider: React.FC = (props) => {
// setEvent is never updated
const [event, setEvent] = React.useState<EventState>(EVENT_STATE_NULL)
dispatcher.current = setEvent

return React.createElement(context.Provider, { value: event }, children)
useIsomorphicLayoutEffect(() => {
runWithPriority(NormalPriority, () => {
contextListeners.forEach((listener) => {
listener(event)
})
})
}, [event])

return React.createElement(React.Fragment, props)
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/types.ts

This file was deleted.

0 comments on commit fa95498

Please sign in to comment.