From ded704d6c0af0fd27ea72ac66ea1b48903199a65 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 17 May 2022 23:07:23 +0800 Subject: [PATCH] chore: deprecate driver error catcher --- src/analytics.ts | 14 ----- src/content.tsx | 4 -- src/driver/connect.ts | 119 ------------------------------------------ 3 files changed, 137 deletions(-) delete mode 100644 src/driver/connect.ts diff --git a/src/analytics.ts b/src/analytics.ts index 341a71e..1ad233f 100644 --- a/src/analytics.ts +++ b/src/analytics.ts @@ -1,5 +1,4 @@ import * as Sentry from '@sentry/browser' -import { Middleware } from 'driver/connect.js' import { IN_PRODUCTION_MODE, VERSION } from 'env' import { platform } from 'platforms' import { forOf } from 'utils/general' @@ -57,19 +56,6 @@ const sentryOptions: Sentry.BrowserOptions = { } Sentry.init(sentryOptions) -export const withErrorLog: Middleware = function withErrorLog(method, args) { - return [ - async function (...args) { - try { - await method(...args) - } catch (error) { - if (error instanceof Error) raiseError(error) - } - } as typeof method, - args, - ] -} - export function raiseError(error: Error, extra?: unknown) { if (!IN_PRODUCTION_MODE || platform.isEnterprise()) { // ignore errors from enterprise to get less noise on Sentry diff --git a/src/content.tsx b/src/content.tsx index cc11087..ebf54ca 100644 --- a/src/content.tsx +++ b/src/content.tsx @@ -1,14 +1,10 @@ -import { withErrorLog } from 'analytics' import { Gitako } from 'components/Gitako' -import { addMiddleware } from 'driver/connect' import { platform } from 'platforms' import * as React from 'react' import * as ReactDOM from 'react-dom' import './content.scss' if (platform.resolvePartialMetaData()) { - addMiddleware(withErrorLog) - if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init) } else { diff --git a/src/driver/connect.ts b/src/driver/connect.ts deleted file mode 100644 index f710bcc..0000000 --- a/src/driver/connect.ts +++ /dev/null @@ -1,119 +0,0 @@ -import * as React from 'react' - -export type Method = (...args: Args) => void | Promise -export type Middleware = ( - method: M, - args: Parameters, -) => [MM | M, Parameters] - -const middlewares: Middleware[] = [] - -export function addMiddleware(middleware: Middleware) { - if (typeof middleware !== 'function') return null - const m = middleware.bind(null) - middlewares.push(m) - return function removeMiddleware() { - const index = middlewares.indexOf(m) - if (index === -1) return - middlewares.splice(index, 1) - } -} - -const applyMiddlewares: Middleware = function applyMiddlewares(method, args) { - for (const middleware of middlewares) { - ;[method = method, args = args] = middleware(method, args) - } - return [method, args] -} - -function run([method, args]: [M, Parameters]) { - method.apply(null, args) -} - -export type DispatchState = React.Component['setState'] -export type GetState = () => { state: State; props: Props } -export type TriggerOtherMethod = ( - methodCreator: MethodCreator, - ...args: Parameters>> -) => void - -export type Dispatch = { - set: DispatchState - get: GetState - call: TriggerOtherMethod -} - -export type MethodCreator = ( - dispatch: Dispatch, -) => Method - -export type Sources = { - [key: string]: MethodCreator -} -type WrappedMethods = { - [key: string]: Method -} - -function link(instance: React.Component, sources: Sources): WrappedMethods { - const wrappedMethods: WrappedMethods = { - /* [keyof sources] -> wrappedMethods.method */ - } - const map = new Map< - MethodCreator, - Method - >(/* sources.creator -> wrappedMethods.method */) - - const dispatchCall: TriggerOtherMethod = (createMethod, ...otherArgs) => { - const isFromSource = sourcesValues.includes(createMethod) - if (isFromSource) { - const method = map.get(createMethod) - if (!method) throw new Error('Method not found') - const runnable = applyMiddlewares(method, otherArgs) - run(runnable) - } - } - const dispatchState: DispatchState = (updater, callback) => { - instance.setState(updater, callback) - } - const prepareState: GetState = () => ({ state: instance.state, props: instance.props }) - const dispatch: Dispatch = { - call: dispatchCall, - get: prepareState, - set: dispatchState, - } - - Object.entries(sources).forEach(([key, createMethod]) => { - const method = createMethod(dispatch) - wrappedMethods[key] = method - map.set(createMethod, method) - }) - - const sourcesValues = Object.values(sources) - - return wrappedMethods -} - -export function connect(mapping: Sources) { - return function linkComponent>( - Component: ComponentType, - ) { - return class ConnectedComponent extends React.PureComponent { - static displayName = `Connected(${Component.displayName || Component.name})` - static defaultProps = Component.defaultProps - - state: ExtraP = {} as ExtraP - connectedMethods: WrappedMethods = link(this, mapping) - - render() { - const props = Object.assign({}, this.props, this.connectedMethods, this.state) - return React.createElement(Component, props) - } - } - } -} - -export type GetCreatedMethod = MC extends MethodCreator - ? Args extends any[] - ? (...args: Args) => void - : never - : never