Giter Club home page Giter Club logo

Comments (6)

minggo avatar minggo commented on July 19, 2024

Could i know how to reproduce it?

from cocos-engine.

a1076559139 avatar a1076559139 commented on July 19, 2024

我的设备是IPhone 11, 系统是13.6版本,大概4年前的系统。
D-Chat_20240702103526

报错原因是用了matchMedia,并且调用它的addEventListener方法,我抓它的报错了,确实没有addEventListener方法。

D-Chat_20240702103230
虽然addListener被标记为已启用,但一些系统钉子户,仍然不支持最新的addEventListener方法。

我目前的临时兼容处理(window上的addEventListener可用,new EventTarget不可用):

// 兼容某些旧机型,无法使用MediaQueryList.prototype.addEventListener
if ('function' == typeof window.matchMedia) {
    const matchMedia = window.matchMedia;
    window.matchMedia = function (query: string): MediaQueryList {
        const res = matchMedia.call(window, query) as MediaQueryList;
        if (!res.addEventListener && res.addListener) {
            const callback = function (this: MediaQueryList, ev: MediaQueryListEvent) {
                window.dispatchEvent(new Event('matchMedia:' + ev.type, {
                    bubbles: ev.bubbles,
                    cancelable: ev.cancelable,
                    composed: ev.composed
                }));
            };
            let listenerAdded = false;
            res.addEventListener = function (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) {
                window.addEventListener('matchMedia:' + type, listener, options);
                if (listenerAdded === false) {
                    listenerAdded = true;
                    res.addListener(callback);
                }
            };
            res.removeEventListener = function (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions) {
                window.removeEventListener('matchMedia:' + type, listener, options);
            };
        }

        return res;
    };
}

from cocos-engine.

minggo avatar minggo commented on July 19, 2024

我搜索了代码,screen-adapter.ts 里用到了 window.matchMedia

if (typeof window.matchMedia === 'function') {
            const updateDPRChangeListener = (): void => {
                const dpr = window.devicePixelRatio;
                // NOTE: some browsers especially on iPhone doesn't support MediaQueryList
                window.matchMedia(`(resolution: ${dpr}dppx)`)?.addEventListener?.('change', (): void => {
                    this.emit('window-resize', this.windowSize.width, this.windowSize.height);
                    updateDPRChangeListener();
                }, { once: true });
            };
    ...
}

这里对 addEventListener 是否存在做了判断,所以没太明白为什么还会有上面的代码。

from cocos-engine.

a1076559139 avatar a1076559139 commented on July 19, 2024

你再稍微往下看几行就能发现问题了,下面没有判断
D-Chat_20240702105804

from cocos-engine.

minggo avatar minggo commented on July 19, 2024

@bofeng-song 看一下,我看是你加的代码。

from cocos-engine.

a1076559139 avatar a1076559139 commented on July 19, 2024

建议最好可以兼容一下,addEventListener不存在就用addListener

from cocos-engine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.