Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unity] c#回调js触发promise resolve时,promise后续调用晚了一帧,和v8版有区别 #68

Open
zxt5105515 opened this issue Aug 22, 2024 · 1 comment

Comments

@zxt5105515
Copy link

c#调用js回调后,如果触发promise的resolve,promise的后续会晚一帧,如果c#在回调后清理数据,js promise拿到的就是错误数据。
有什么好的办法吗

//这部分是C#代码

class TestData{
    public int value;
}

class TestClass{
    private Action<TestData> _onDone;
   
    void SetOnDone(Action<TestData> v){
        _onDone = v;
        //通过协程或者update 实现1s后调用WhenDone
    }
 
    void WhenDone(){
        TestData data = MyPool.get<TestData>(); //从对象池获取一个TestData
        data.value = 999; //设置想要的值
        _onDone(data);

        //归还给对象池
        data.value = 0;
        MyPool.despawn(data);
    }
}
//这部分是js代码
class JsClass {

   getData() {
      let testClass = new TestClass() // get TestClass
      return new Promise((resolve, reject) => {
         testClass.SetOnDone((ret) => {
            resolve(ret)
         })
      })
   }

   async testData() {
      let data = await this.getData()
      // 下面这行 puerts-webgl下会输出0 ,puets v8版 会输出999
      console.log("data.value:", data.value)
   }

}

(new JsClass).testData()
@zombieyang
Copy link
Owner

如果其中一方是浏览器,理论上应该以浏览器为准。应该从puerts native层面修

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants