diff --git a/packages/uni-api/src/protocols/ui/showActionSheet.ts b/packages/uni-api/src/protocols/ui/showActionSheet.ts index f96d2057f8382c1724d05cae516bd2e7322f7e3a..fee30c342282eef61af5885146cf4ca4cd839e6f 100644 --- a/packages/uni-api/src/protocols/ui/showActionSheet.ts +++ b/packages/uni-api/src/protocols/ui/showActionSheet.ts @@ -7,7 +7,7 @@ export const ShowActionSheetProtocol: ApiProtocol = required: true, }, itemColor: String, - // popover: Object, + popover: Object, } export const ShowActionSheetOptions: ApiOptions = { formatArgs: { diff --git a/packages/uni-api/src/service/base/eventBus.ts b/packages/uni-api/src/service/base/eventBus.ts index 75ace632ca30f48fce4b38a90d25b6449fd86d3a..da7b917fcf6629eea0b44023c7bc846b07b454e3 100644 --- a/packages/uni-api/src/service/base/eventBus.ts +++ b/packages/uni-api/src/service/base/eventBus.ts @@ -13,94 +13,46 @@ import { EmitProtocol, OnceProtocol, } from '../../protocols/base/eventBus' +import { Emitter } from '@dcloudio/uni-core' -type EventName = string | number | symbol -type EventCallback = (...args: any) => any type EventStopHandler = () => void -class Emitter { - private eventMap: Map> - - constructor() { - this.eventMap = new Map() - } - - on = (name: EventName, callback: EventCallback): EventStopHandler => { - if (!this.eventMap.has(name)) { - this.eventMap.set(name, []) - } - - this.eventMap.get(name)!.push(callback) - - return () => this.off(name, callback) - } - - once = (name: EventName, callback: EventCallback): EventStopHandler => { - const listener = (...args: any[]) => { - this.off(name, listener) - callback(...args) - } - - this.on(name, listener) - - return () => this.off(name, listener) - } - - emit = (name: EventName, ...args: any[]): void => { - const cbs = this.eventMap.get(name) - if (cbs instanceof Array) { - cbs.forEach((cb) => { - typeof cb === 'function' && cb(...args) - }) - } - } - - off = (names?: EventName | EventName[], callback?: EventCallback): void => { - if (!names) { - this.eventMap.clear() - return - } - if (!(names instanceof Array)) { - names = [names] - } - if (typeof callback === 'function') { - names.forEach((name) => { - if (this.eventMap.has(name)) { - this.eventMap.set( - name, - this.eventMap.get(name)!.filter((cb) => cb !== callback) - ) - } - }) - } else { - names.forEach((name) => { - this.eventMap.delete(name) - }) - } - } -} - const emitter = new Emitter() export const $on = defineSyncApi( API_ON, - (type, callback): EventStopHandler => emitter.on(type, callback), + (name, callback): EventStopHandler => { + emitter.on(name, callback) + + return () => emitter.off(name, callback) + }, OnProtocol ) export const $once = defineSyncApi( API_ONCE, - (type, callback): EventStopHandler => emitter.once(type, callback), + (name, callback): EventStopHandler => { + emitter.once(name, callback) + + return () => emitter.off(name, callback) + }, OnceProtocol ) export const $off = defineSyncApi( API_OFF, - (type, callback) => { - emitter.off(type, callback) + (name, callback) => { + if (!name) { + emitter.e = {} + return + } + if (!Array.isArray(name)) name = [name] + name.forEach((n) => emitter.off(n, callback)) }, OffProtocol ) export const $emit = defineSyncApi( API_EMIT, - emitter.emit, + (name, ...args: any[]) => { + emitter.emit(name, ...args) + }, EmitProtocol ) diff --git a/packages/uni-api/src/service/context/canvas.ts b/packages/uni-api/src/service/context/canvas.ts index 05ab5d7494e9aba36eebd3664a9189d62a0011d1..ccc2bbc19ecb34885ecdf94ad2bb3f8c4d4b3c1b 100644 --- a/packages/uni-api/src/service/context/canvas.ts +++ b/packages/uni-api/src/service/context/canvas.ts @@ -795,20 +795,21 @@ export class CanvasContext implements UniApp.CanvasContext { ;[...methods1, ...methods2].forEach(function (method) { function get(method: string) { - // @ts-ignore - let _this = this as CanvasContext switch (method) { case 'fill': case 'stroke': return function () { - _this.actions.push({ + // @ts-ignore + this.actions.push({ method: method + 'Path', - data: [..._this.path], + // @ts-ignore + data: [...this.path], }) } case 'fillRect': return function (x: number, y: number, width: number, height: number) { - _this.actions.push({ + // @ts-ignore + this.actions.push({ method: 'fillPath', data: [ { @@ -820,7 +821,8 @@ export class CanvasContext implements UniApp.CanvasContext { } case 'strokeRect': return function (x: number, y: number, width: number, height: number) { - _this.actions.push({ + // @ts-ignore + this.actions.push({ method: 'strokePath', data: [ { @@ -837,7 +839,8 @@ export class CanvasContext implements UniApp.CanvasContext { if (typeof maxWidth === 'number') { data.push(maxWidth) } - _this.actions.push({ + // @ts-ignore + this.actions.push({ method, data, }) @@ -864,7 +867,7 @@ export class CanvasContext implements UniApp.CanvasContext { dWidth = undefined dHeight = undefined } - let data: ActionsItemData + var data function isNumber(e: any) { return typeof e === 'number' @@ -888,14 +891,16 @@ export class CanvasContext implements UniApp.CanvasContext { : isNumber(sWidth) && isNumber(sHeight) ? [imageResource, sx, sy, sWidth, sHeight] : [imageResource, sx, sy] - _this.actions.push({ + // @ts-ignore + this.actions.push({ method, data, }) } default: - return function (...data: ActionsItemData) { - _this.actions.push({ + return function (...data: any) { + // @ts-ignore + this.actions.push({ method, data, }) @@ -906,32 +911,29 @@ export class CanvasContext implements UniApp.CanvasContext { }) methods3.forEach(function (method) { function get(method: string) { - // @ts-ignore - let _this = this as CanvasContext switch (method) { case 'setFillStyle': case 'setStrokeStyle': return function (color: string | Data) { if (typeof color !== 'object') { - _this.actions.push({ + // @ts-ignore + this.actions.push({ method, data: ['normal', checkColor(color)], }) } else { - _this.actions.push({ + // @ts-ignore + this.actions.push({ method, - data: [ - color.type as ActionsItemType, - color.data as ActionsItemType, - color.colorStop as ActionsItemType, - ], + data: [color.type, color.data, color.colorStop], }) } } case 'setGlobalAlpha': return function (alpha: number) { alpha = Math.floor(255 * parseFloat(alpha as unknown as string)) - _this.actions.push({ + // @ts-ignore + this.actions.push({ method, data: [alpha], }) @@ -943,41 +945,52 @@ methods3.forEach(function (method) { blur: number, color: string ) { - let _color = checkColor(color) - _this.actions.push({ + color = checkColor(color) as any + // @ts-ignore + this.actions.push({ method, data: [offsetX, offsetY, blur, color], }) - _this.state.shadowBlur = blur - _this.state.shadowColor = _color - _this.state.shadowOffsetX = offsetX - _this.state.shadowOffsetY = offsetY + // @ts-ignore + this.state.shadowBlur = blur + // @ts-ignore + this.state.shadowColor = color + // @ts-ignore + this.state.shadowOffsetX = offsetX + // @ts-ignore + this.state.shadowOffsetY = offsetY } case 'setLineDash': return function (pattern: Array | undefined, offset: number) { pattern = pattern || [0, 0] offset = offset || 0 - _this.actions.push({ + // @ts-ignore + this.actions.push({ method, data: [pattern, offset], }) - _this.state.lineDash = pattern + // @ts-ignore + this.state.lineDash = pattern } case 'setFontSize': return function (fontSize: number) { - _this.state.font = _this.state.font.replace( + // @ts-ignore + this.state.font = this.state.font.replace( /\d+\.?\d*px/, fontSize + 'px' ) - _this.state.fontSize = fontSize - _this.actions.push({ + // @ts-ignore + this.state.fontSize = fontSize + // @ts-ignore + this.actions.push({ method, data: [fontSize], }) } default: - return function (...data: ActionsItemData) { - _this.actions.push({ + return function (...data: any) { + // @ts-ignore + this.actions.push({ method, data, }) diff --git a/packages/uni-core/src/helpers/TinyEmitter.js b/packages/uni-core/src/helpers/TinyEmitter.ts similarity index 66% rename from packages/uni-core/src/helpers/TinyEmitter.js rename to packages/uni-core/src/helpers/TinyEmitter.ts index c59aa445710e17e89499b5822f5d74c9c7af1b6f..3543b64b7a202ab58370d5f46879c6e506aec2cf 100644 --- a/packages/uni-core/src/helpers/TinyEmitter.js +++ b/packages/uni-core/src/helpers/TinyEmitter.ts @@ -1,10 +1,22 @@ -function E() { +interface E { + e: Data + on: (name: EventName, callback: EventCallback, ctx?: any) => this + once: (name: EventName, callback: EventCallback, ctx?: any) => this + emit: (name: EventName, ...args: any[]) => this + off: (name: EventName, callback?: EventCallback) => this +} + +const E = function () { // Keep this empty so it's easier to inherit from // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) -} +} as unknown as { new (): E } + +// export type EventName = string | number | symbol +export type EventName = string +export type EventCallback = Function E.prototype = { - on: function (name, callback, ctx) { + on: function (name: EventName, callback: EventCallback, ctx?: any) { var e = this.e || (this.e = {}) ;(e[name] || (e[name] = [])).push({ @@ -15,7 +27,7 @@ E.prototype = { return this }, - once: function (name, callback, ctx) { + once: function (name: EventName, callback: EventCallback, ctx?: any) { var self = this function listener() { self.off(name, listener) @@ -26,7 +38,7 @@ E.prototype = { return this.on(name, listener, ctx) }, - emit: function (name) { + emit: function (name: EventName) { var data = [].slice.call(arguments, 1) var evtArr = ((this.e || (this.e = {}))[name] || []).slice() var i = 0 @@ -39,7 +51,7 @@ E.prototype = { return this }, - off: function (name, callback) { + off: function (name: EventName, callback?: EventCallback) { var e = this.e || (this.e = {}) var evts = e[name] var liveEvents = [] diff --git a/packages/uni-core/src/helpers/index.ts b/packages/uni-core/src/helpers/index.ts index d52c8ec9899057e3a09db2b118ceb71a97201366..63d591b8bdd438b77133d8eb51266bad2dab845b 100644 --- a/packages/uni-core/src/helpers/index.ts +++ b/packages/uni-core/src/helpers/index.ts @@ -5,3 +5,4 @@ export * from './page' export * from './scroll' export * from './getRealRoute' export * from './callbacks' +export { default as Emitter } from './TinyEmitter' diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index 0dcd7984ea66acedd6fa9a26d3d6772094f222d2..5c8d3a9757c2d35e379142b438d9c3c4634ac528 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -115,8 +115,8 @@ const initI18nVideoMsgsOnce = /* @__PURE__ */ uniShared.once(() => { i18n.add(uniI18n.LOCALE_ZH_HANT, normalizeMessages(name, {danmu: "\u5F48\u5E55", volume: "\u97F3\u91CF"})); } }); -function E() { -} +const E = function() { +}; E.prototype = { on: function(name, callback2, ctx) { var e2 = this.e || (this.e = {}); diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index bdeeb06cf4c11fa52d1af18e4cf68ea2c2797771..525ca5eef0fd88e8abce27c62f8a61d2e6e37d23 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -188,8 +188,8 @@ const initI18nVideoMsgsOnce = /* @__PURE__ */ once(() => { i18n.add(LOCALE_ZH_HANT, normalizeMessages(name, {danmu: "\u5F48\u5E55", volume: "\u97F3\u91CF"})); } }); -function E() { -} +const E = function() { +}; E.prototype = { on: function(name, callback2, ctx) { var e2 = this.e || (this.e = {}); @@ -2010,61 +2010,27 @@ const EmitProtocol = [ required: true } ]; -class Emitter { - constructor() { - this.on = (name, callback2) => { - if (!this.eventMap.has(name)) { - this.eventMap.set(name, []); - } - this.eventMap.get(name).push(callback2); - return () => this.off(name, callback2); - }; - this.once = (name, callback2) => { - const listener2 = (...args) => { - this.off(name, listener2); - callback2(...args); - }; - this.on(name, listener2); - return () => this.off(name, listener2); - }; - this.emit = (name, ...args) => { - const cbs = this.eventMap.get(name); - if (cbs instanceof Array) { - cbs.forEach((cb) => { - typeof cb === "function" && cb(...args); - }); - } - }; - this.off = (names, callback2) => { - if (!names) { - this.eventMap.clear(); - return; - } - if (!(names instanceof Array)) { - names = [names]; - } - if (typeof callback2 === "function") { - names.forEach((name) => { - if (this.eventMap.has(name)) { - this.eventMap.set(name, this.eventMap.get(name).filter((cb) => cb !== callback2)); - } - }); - } else { - names.forEach((name) => { - this.eventMap.delete(name); - }); - } - }; - this.eventMap = new Map(); +const emitter = new E(); +const $on = /* @__PURE__ */ defineSyncApi(API_ON, (name, callback2) => { + emitter.on(name, callback2); + return () => emitter.off(name, callback2); +}, OnProtocol); +const $once = /* @__PURE__ */ defineSyncApi(API_ONCE, (name, callback2) => { + emitter.once(name, callback2); + return () => emitter.off(name, callback2); +}, OnceProtocol); +const $off = /* @__PURE__ */ defineSyncApi(API_OFF, (name, callback2) => { + if (!name) { + emitter.e = {}; + return; } -} -const emitter = new Emitter(); -const $on = /* @__PURE__ */ defineSyncApi(API_ON, (type, callback2) => emitter.on(type, callback2), OnProtocol); -const $once = /* @__PURE__ */ defineSyncApi(API_ONCE, (type, callback2) => emitter.once(type, callback2), OnceProtocol); -const $off = /* @__PURE__ */ defineSyncApi(API_OFF, (type, callback2) => { - emitter.off(type, callback2); + if (!Array.isArray(name)) + name = [name]; + name.forEach((n) => emitter.off(n, callback2)); }, OffProtocol); -const $emit = /* @__PURE__ */ defineSyncApi(API_EMIT, emitter.emit, EmitProtocol); +const $emit = /* @__PURE__ */ defineSyncApi(API_EMIT, (name, ...args) => { + emitter.emit(name, ...args); +}, EmitProtocol); const validator = [ { name: "id", @@ -2853,19 +2819,18 @@ class CanvasContext { } [...methods1, ...methods2].forEach(function(method) { function get(method2) { - let _this = this; switch (method2) { case "fill": case "stroke": return function() { - _this.actions.push({ + this.actions.push({ method: method2 + "Path", - data: [..._this.path] + data: [...this.path] }); }; case "fillRect": return function(x, y, width, height) { - _this.actions.push({ + this.actions.push({ method: "fillPath", data: [ { @@ -2877,7 +2842,7 @@ class CanvasContext { }; case "strokeRect": return function(x, y, width, height) { - _this.actions.push({ + this.actions.push({ method: "strokePath", data: [ { @@ -2894,7 +2859,7 @@ class CanvasContext { if (typeof maxWidth === "number") { data.push(maxWidth); } - _this.actions.push({ + this.actions.push({ method: method2, data }); @@ -2911,7 +2876,7 @@ class CanvasContext { dWidth = void 0; dHeight = void 0; } - let data; + var data; function isNumber(e2) { return typeof e2 === "number"; } @@ -2926,14 +2891,14 @@ class CanvasContext { dWidth, dHeight ] : isNumber(sWidth) && isNumber(sHeight) ? [imageResource, sx, sy, sWidth, sHeight] : [imageResource, sx, sy]; - _this.actions.push({ + this.actions.push({ method: method2, data }); }; default: return function(...data) { - _this.actions.push({ + this.actions.push({ method: method2, data }); @@ -2944,69 +2909,64 @@ class CanvasContext { }); methods3.forEach(function(method) { function get(method2) { - let _this = this; switch (method2) { case "setFillStyle": case "setStrokeStyle": return function(color) { if (typeof color !== "object") { - _this.actions.push({ + this.actions.push({ method: method2, data: ["normal", checkColor(color)] }); } else { - _this.actions.push({ + this.actions.push({ method: method2, - data: [ - color.type, - color.data, - color.colorStop - ] + data: [color.type, color.data, color.colorStop] }); } }; case "setGlobalAlpha": return function(alpha) { alpha = Math.floor(255 * parseFloat(alpha)); - _this.actions.push({ + this.actions.push({ method: method2, data: [alpha] }); }; case "setShadow": return function(offsetX, offsetY, blur, color) { - let _color = checkColor(color); - _this.actions.push({ + color = checkColor(color); + this.actions.push({ method: method2, data: [offsetX, offsetY, blur, color] }); - _this.state.shadowBlur = blur; - _this.state.shadowColor = _color; - _this.state.shadowOffsetX = offsetX; - _this.state.shadowOffsetY = offsetY; + this.state.shadowBlur = blur; + this.state.shadowColor = color; + this.state.shadowOffsetX = offsetX; + this.state.shadowOffsetY = offsetY; }; case "setLineDash": return function(pattern, offset) { pattern = pattern || [0, 0]; offset = offset || 0; - _this.actions.push({ + this.actions.push({ method: method2, data: [pattern, offset] }); - _this.state.lineDash = pattern; + this.state.lineDash = pattern; }; case "setFontSize": return function(fontSize) { - _this.state.font = _this.state.font.replace(/\d+\.?\d*px/, fontSize + "px"); - _this.state.fontSize = fontSize; - _this.actions.push({ + this.state.font = this.state.font.replace(/\d+\.?\d*px/, fontSize + "px"); + this.state.fontSize = fontSize; + this.actions.push({ method: method2, data: [fontSize] }); }; default: return function(...data) { - _this.actions.push({ + this.actions.push({ method: method2, data }); @@ -3846,7 +3806,8 @@ const ShowActionSheetProtocol = { type: Array, required: true }, - itemColor: String + itemColor: String, + popover: Object }; const ShowActionSheetOptions = { formatArgs: {