KennethHung
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Dart js_interop 初探 (Dart 3.4) [TOC] ## 💡 起因動機 ### 專案內的套件 原先,專案內使用了一個名為 `flutter_web3` 的套件,我需要針對套件內的class `Log` 做一系列的邏輯處理,我先是寫了一個test ,後來要mock 該class 時,發現 ![image](https://hackmd.io/_uploads/SJe0t9p4A.png) `Log` 具有某個 private class 的泛型,並且繼承於 Interop class (如上圖),而該 private class (如下圖) 是一個和JS 互動的class ![image](https://hackmd.io/_uploads/SyJK99aVR.png) 這裡有個問題: `Log` class 可以被mock 嗎? - 可以,但 `_LogImpl` 會被略過,沒有辦法被測試到 有甚麼方法是,**既可以測試到這個與JS 互動的介面,又可以測試到Dart 端的class model 的呢?** ### 官方文件內的技術內容 我參考了[js interop 的dart 官方文件](https://dart.dev/interop/js-interop),裡面的[Mock 章節](https://dart.dev/interop/js-interop/mock)提到了 - 使用 extension type,加上 - 使用 `@JSExport()` - 使用`createJSInteropWrapper()` 的組合拳,就可以達到測試 JS 介面和Dart class model 的需求。 ```java= // The Dart class must have `@JSExport` on it or at least one of its instance // members. @JSExport() class FakeCounter { int value = 0; @JSExport('increment') void renamedIncrement() { value++; } void decrement() { value--; } } extension type Counter(JSObject _) implements JSObject { external int value; external void increment(); void decrement() { value -= 2; } } void main() { var fakeCounter = FakeCounter(); // Returns a JS object whose properties call the relevant instance members in // `fakeCounter`. var counter = createJSInteropWrapper<FakeCounter>(fakeCounter) as Counter; // Calls `FakeCounter.value`. expect(counter.value, 0); // `FakeCounter.renamedIncrement` is renamed to `increment`, so it gets // called. counter.increment(); expect(counter.value, 1); expect(fakeCounter.value, 1); // Changes in the fake affect the wrapper and vice-versa. fakeCounter.value = 0; expect(counter.value, 0); counter.decrement(); // Because `Counter.decrement` is non-`external`, we never called // `FakeCounter.decrement`. expect(counter.value, -2); } ``` 問題越來越多,甚麼是extension type,然後這些 annotaion 跟 method 是怎麼回事? ## 🔬 研究 Dart JS Interoperability 最根本的問題,Dart 是如何和 JS 互相溝通的呢?這裡就以[Usage](https://dart.dev/interop/js-interop/usage)的解釋來說明: > To call and receive JS values from this API, you use external interop members. In order to construct and provide types for JS values, you use and declare interop types, which also contain interop members. To pass Dart values like Lists or Function to interop members or convert from JS values to Dart values, you use conversion functions unless the interop member contains a primitive type. 1. 要call JS Function 或是取得JS 的值的話,用 `external` 關鍵字 2. 要建構和提供JS 值的型別,用 interop type 來宣告 3. 要從Dart 傳List 或是Function 給JS,而其型別為原始型別(int, String, bool ...) 的話,用conversion function ### 基礎型別 對於基礎型別,`external` 關鍵字的用法如下,用法不難 ```javascript // javascript // 等同於 var name = 'global' globalThis.name = 'global'; // 等同於 // function isNameEmpty() { // return name.length == 0; // } globalThis.isNameEmpty = function() { return globalThis.name.length == 0; } ``` ```java // Dart @JS() external String get name; @JS('name') // 對應到javascript 的變數名稱 external String get jsName; @JS() external set name(String value); @JS() external bool isNameEmpty(); ``` ### Async Function 若要JS 使用非同步Function,特別注意JS 的型別是interop type,在Dart 操作時需要轉換成dart type ```javascript // javascript function resolveAfter2Seconds() { return new Promise((resolve) => { setTimeout(() => { resolve('resolved after 2 sec'); }, 2000); }); } async function asyncCall() { const result = await resolveAfter2Seconds(); return result; } ``` ```java //dart @JS() external JSPromise<JSString> asyncCall(); final jsResult = await asyncCall(); print(jsResult.toDart) // resolved after 2 sec ``` ### Callback Function、Function w/ arguments 若需要使用callback,例如當 JS 處理完後,對Dart 呼叫callback 並放入參數。 ```javascript // javascript function heavyWork(onWorkDone){ var status = 0; // ... do some heavy things status = 1; console.log('Heavy work finished on JS'); onWorkDone.call(this, status); } ``` ```java // dart @JS('heavyWork') external void jsHeavyWork(ExternalDartReference onDone); void onDone(int statusCode){ print('JS callback'); print('status code: $statusCode'); } jsHeavyWork(onDone.toJS.toExternalReference); // Heavy work finished on JS // JS callback // status code: 1 ``` Dart 的Callback function 需要被包裝成 `ExternalDartReference` 物件,而這個物件就只是Dart 的一個參照,但是要成為參照之前,需要 1. 先把function 變成 `JSExportedDartFunction` (`.toJS`) 2. 再轉換成`ExternalDartReference` (`.toExternalReference`) ::: info 如果 function 沒有先轉成`JSExportedDartFunction` 直接轉成 `ExternalDartReference` ,而被JS 調用的話 會出現下面的錯誤 Dart function requires `allowInterop` to be passed to JavaScript. ::: --- ### Interop Type & Extension type 但如果今天要取得的不是原始型別,我們可以用 JS type,或是用 extension type 來包裝型別。 JS Type (正式名稱為 interop type) 是 Dart 用來宣告 JS 來的型別,下面是和 Dart type 的對應表 ![image](https://hackmd.io/_uploads/BypOViC40.png) 而 extension type 則是除此之外、自定義的type或是class。對我而言,extension type 很像是Dart 用來描述型別的interface,而正好他在[文件](https://dart.dev/language/extension-types)的第一行也是這麼描述的 > An extension type is a compile-time abstraction that "wraps" an existing type with a different, static-only interface. 既然extension type 是拿來描述型別的介面,那也可以很自然的拿來描述JS interop 的型別。可以從原始檔內找到官方的定義方式。 ![image](https://hackmd.io/_uploads/ryYtYi0NC.png) ![image](https://hackmd.io/_uploads/SJ2TYoREA.png) ![image](https://hackmd.io/_uploads/Hy-19oAVR.png) --- ### Extension type 實作 所以話說回來,當我們需要自定義一個來自JS 的結構,我們可以用 extension type 來實現。例如有個名為Empolyee 的JS class: ```javascript // javascript class Employee{ constructor(name, id, department){ this._name = name; this._id = id; this._department = department; this._label = `Name: ${name}, id: ${id}`; this._greeting = `${name} says hello from JS` } // ...setter getter of the class members } class Department{ constructor(name){ this._name = name; } // ...setter getter of the class members } const depRD = new Department('RD'); const employee = new Employee('John', 1, depSE); ``` ```java // dart extension type Employee._(JSObject _) implements JSObject{ external Employee(String name, int id, Department department); external String name; external int id; @JS('label') external String get jsLabel; external Department department; @JS('greeting') // @JS() 可以用來區別JavaScript 和Dart 的Function external String get greetingJS; String get greeting => "$name says hello from Dart"; } extension type Department._(JSObject _) implements JSObject{ external String name; } @JS('employee') external Employee employeeJS; print(employeeJS.name) // John print(employeeJS.id) // 1 print(employeeJS.department.name) // RD print(employeeJS.label) // Name: John, id: 1 print(employeeJS.greeting) // John says hello from Dart print(employeeJS.greetingJS) // John says hello from JS ``` ## 🕵🏼‍♂️ 檢視問題 ### 問題重述 研究的 extension type 和 annotation 已經解釋的差不多了,再回來看看問題 **如何既可以測試到這個與JS 互動的介面,又可以測試到Dart 端的class model 的呢?** 這個問題經上述的研究後,可以知道 extension type 就是Dart 和JS 互動的介面結構,可以把問題換成: **如何 mock JS 的 extension type?** 很幸運的,官方也提供了這樣的[文件](https://dart.dev/interop/js-interop/mock),文件中提到 > Mocking classes in Dart is usually done through overriding instance members. However, since extension types are used to declare interop types, all extension type members are dispatched statically and therefore overriding can't be used. This limitation is true for extension members as well, and therefore instance extension type or extension members can't be mocked. > > While this applies to any non-external extension type member, external interop members are special as they invoke members on a JS value. > ... > Therefore, by using a different JSObject, a different implementation of getDay can be called. 簡單翻譯一下 大概就是說 extension type 是靜態分配的(在 compile time 就被定義,所以才能做型別的靜態分析),所以沒辦法使用Override,且這個規則也套用於 extension type 的 member,也就是沒辦法被mock。 但是很湊巧的,因為 external member 會調用 JS 的值,所以當我提供了不一樣的`JSObject`,不一樣的 function 就會被呼叫,也就自然的達到mock 的目的。 所以我們可以再把問題重新敘述成: **如何讓 extension type 可以使用被 mock 出的 JS 物件?** 文件中說有兩個關鍵的元件,`@JSExport()` 和 `createJSInteropWrapper()` ### @JSExport() `@JSExport()` 的功能是讓 `createJSInteropWrapper()` 把 Dart class 或是其member 包裝成 `JSObject`,也就是標記讓哪個 class 或是 member 變成 JS 的物件。 ### createJSInteropWrapper() 將Dart class 或是其member 包裝成 JS 物件的方法。就這麼簡單。 ```java // dart @JSExport() // 把 FakeCounter 整個class 包裝成JSObject class FakeCounter { int value = 0; @JSExport('increment') // 對應到 extension type 內的 member 名稱 void renamedIncrement() { value++; } void decrement() { value--; } } var fakeCounter = FakeCounter(); // counter 是 var counter = createJSInteropWrapper<FakeCounter>(fakeCounter) as Counter; expect(counter.value, 0); counter.increment(); expect(counter.value, 1); ``` ## 🏁 最後 研究出來後,`flutter_web3` 可以測試成功嗎?答案是 短時間內不行,原因是 - 執行測試時,該套件內有多處 import `dart:js`,而Dart VM 內並沒有 js 套件可以使用,會出現 `Dart library 'dart:js_interop' is not available on this platform` 的錯誤。 - 若把 `dart:js` 換成 `dart:js_interop`,就得改寫套件內所有舊API 寫法,要改非常多地方...

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully