index.ts 3.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { ApiOptions, ApiProtocols } from '../../protocols/type'
import { API_TYPE_ON_PROTOCOLS, validateProtocols } from '../protocol'
3
import {
fxy060608's avatar
fxy060608 已提交
4
  invokeCallback,
5 6 7
  createAsyncApiCallback,
  createKeepAliveApiCallback,
} from './callback'
fxy060608's avatar
fxy060608 已提交
8
import { promisify } from './promise'
9 10

export const API_TYPE_ON = 0
fxy060608's avatar
fxy060608 已提交
11 12 13
export const API_TYPE_TASK = 1
export const API_TYPE_SYNC = 2
export const API_TYPE_ASYNC = 3
14 15 16

type API_TYPES =
  | typeof API_TYPE_ON
fxy060608's avatar
fxy060608 已提交
17
  | typeof API_TYPE_TASK
18 19 20 21 22 23 24 25 26 27 28 29
  | typeof API_TYPE_SYNC
  | typeof API_TYPE_ASYNC

function formatApiArgs(args: any[], options?: ApiOptions) {
  return args
}

function wrapperOnApi(name: string, fn: Function) {
  return (callback: Function) =>
    fn.apply(null, createKeepAliveApiCallback(name, callback))
}

fxy060608's avatar
fxy060608 已提交
30 31 32 33 34
function wrapperTaskApi(name: string, fn: Function, options?: ApiOptions) {
  return (args: Record<string, any>) =>
    fn.apply(null, [args, createAsyncApiCallback(name, args, options)])
}

35 36 37 38 39 40 41
function wrapperSyncApi(fn: Function) {
  return (...args: any[]) => fn.apply(null, args)
}

function wrapperAsyncApi(name: string, fn: Function, options?: ApiOptions) {
  return (args: Record<string, any>) => {
    const callbackId = createAsyncApiCallback(name, args, options)
fxy060608's avatar
fxy060608 已提交
42 43 44 45
    const res = fn.apply(null, [args, callbackId])
    if (res) {
      invokeCallback(callbackId, res)
    }
46 47 48 49 50 51 52 53 54 55
  }
}

function wrapperApi<T extends Function>(
  fn: Function,
  name?: string,
  protocol?: ApiProtocols,
  options?: ApiOptions
) {
  return (function (...args: any[]) {
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60
    if (__DEV__) {
      const errMsg = validateProtocols(name!, args, protocol)
      if (errMsg) {
        return errMsg
      }
61
    }
fxy060608's avatar
fxy060608 已提交
62
    return fn.apply(null, formatApiArgs(args, options))
63 64 65 66 67 68
  } as unknown) as T
}

export function createOnApi<T extends Function>(
  name: string,
  fn: T,
fxy060608's avatar
fxy060608 已提交
69
  options?: ApiOptions
70
) {
fxy060608's avatar
fxy060608 已提交
71 72 73 74
  return createApi(
    API_TYPE_ON,
    name,
    fn,
fxy060608's avatar
fxy060608 已提交
75
    __DEV__ ? API_TYPE_ON_PROTOCOLS : undefined,
fxy060608's avatar
fxy060608 已提交
76 77
    options
  )
78 79
}

fxy060608's avatar
fxy060608 已提交
80
export function createTaskApi<T extends Function>(
81 82 83 84 85
  name: string,
  fn: T,
  protocol?: ApiProtocols,
  options?: ApiOptions
) {
fxy060608's avatar
fxy060608 已提交
86 87 88 89 90 91 92
  return createApi(
    API_TYPE_TASK,
    name,
    fn,
    __DEV__ ? protocol : undefined,
    options
  )
93 94
}

fxy060608's avatar
fxy060608 已提交
95
export function createSyncApi<T extends Function>(
96 97 98 99 100
  name: string,
  fn: T,
  protocol?: ApiProtocols,
  options?: ApiOptions
) {
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107
  return createApi(
    API_TYPE_SYNC,
    name,
    fn,
    __DEV__ ? protocol : undefined,
    options
  )
108 109
}

fxy060608's avatar
fxy060608 已提交
110
export function createAsyncApi<T extends Function>(
111 112 113 114 115
  name: string,
  fn: T,
  protocol?: ApiProtocols,
  options?: ApiOptions
) {
fxy060608's avatar
fxy060608 已提交
116 117 118
  return promisify(
    createApi(API_TYPE_ASYNC, name, fn, __DEV__ ? protocol : undefined, options)
  )
119 120 121 122 123 124 125 126 127 128 129 130
}

function createApi<T extends Function>(
  type: API_TYPES,
  name: string,
  fn: T,
  protocol?: ApiProtocols,
  options?: ApiOptions
) {
  switch (type) {
    case API_TYPE_ON:
      return wrapperApi<T>(wrapperOnApi(name, fn), name, protocol, options)
fxy060608's avatar
fxy060608 已提交
131 132
    case API_TYPE_TASK:
      return wrapperApi<T>(wrapperTaskApi(name, fn), name, protocol, options)
133 134 135 136 137 138 139 140 141 142 143
    case API_TYPE_SYNC:
      return wrapperApi<T>(wrapperSyncApi(fn), name, protocol, options)
    case API_TYPE_ASYNC:
      return wrapperApi<T>(
        wrapperAsyncApi(name, fn, options),
        name,
        protocol,
        options
      )
  }
}