本节提供 postmessage-duplex 的完整 API 文档。
import { IframeChannel } from 'postmessage-duplex'
const channel = new IframeChannel(iframe, options?)
const channel = new IframeChannel(parentOrigin, options?)
await channel.publish(cmdname, data?, options?)
await channel.call(methodName, params)
channel.subscribe(cmdname, callback)
channel.once(cmdname, callback)
channel.unSubscribe(cmdname)
channel.destroy()
channel.isReady
channel.isSon
channel.isDestroyed
import { ServiceWorkerChannel } from 'postmessage-duplex'
const channel = await ServiceWorkerChannel.createFromPage(options?)
const channel = ServiceWorkerChannel.createFromEvent(event, options?)
const channel = ServiceWorkerChannel.createFromWorker(clientId, options?)
interface ChannelOption {
timeout?: number
log?: Console
subscribeMap?: Record<string, Function>
maxMessageSize?: number
rateLimit?: number
}
interface PublishOptions {
timeout?: number
transferables?: Transferable[]
}
enum ReturnCode {
Success = 0,
ReceiverCallbackError = -1,
SendCallbackError = -2,
NoSubscribe = -3,
TimeOut = -99
}
import {
IframeChannel,
ServiceWorkerChannel,
ChannelError,
ReturnCode,
ErrorCode,
createConnectionDestroyedError,
createTimeoutError,
createHandlerError
} from 'postmessage-duplex'
import type {
PostResponse,
PostRequest,
PostCallback,
ChannelOption,
PublishOptions,
Communicator,
Methods,
MethodParams,
MethodReturn,
TypedPostResponse
} from 'postmessage-duplex'