Explorar el Código

use `ToIPCSymbol` to define the special function

amrbashir hace 10 meses
padre
commit
77fce45201

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
crates/tauri/scripts/bundle.global.js


+ 11 - 6
crates/tauri/scripts/ipc.js

@@ -92,17 +92,22 @@
     // unlike `JSON.stringify`, we can't extend a type with a method similar to `toJSON`
     // so that `structuredClone` would use, so until https://github.com/whatwg/html/issues/7428
     // we manually call `toIPC`
-    function recursiveCallToIPC(data) {
+    function processToIPCSymbol(data) {
+      // if this value changes, make sure to update it in:
+      // 1. process-ipc-message-fn.js
+      // 2. core.ts
+      const ToIPCSymbol = '__TAURI_TO_IPC__'
+
       if (
         typeof data === 'object' &&
         'constructor' in data &&
         data.constructor === Array
       ) {
-        return data.map((v) => recursiveCallToIPC(v))
+        return data.map((v) => processToIPCSymbol(v))
       }
 
-      if (typeof data === 'object' && 'toIPC' in data) {
-        return data.toIPC()
+      if (typeof data === 'object' && ToIPCSymbol in data) {
+        return data[ToIPCSymbol]()
       }
 
       if (
@@ -112,7 +117,7 @@
       ) {
         const acc = {}
         Object.entries(data).forEach(([k, v]) => {
-          acc[k] = recursiveCallToIPC(v)
+          acc[k] = processToIPCSymbol(v)
         })
         return acc
       }
@@ -120,7 +125,7 @@
       return data
     }
 
-    data.payload = recursiveCallToIPC(data.payload)
+    data.payload = processToIPCSymbol(data.payload)
 
     isolation.frame.contentWindow.postMessage(
       data,

+ 7 - 2
crates/tauri/scripts/process-ipc-message-fn.js

@@ -16,6 +16,11 @@
     }
   } else {
     const data = JSON.stringify(message, (_k, val) => {
+      // if this value changes, make sure to update it in:
+      // 1. ipc.js
+      // 2. core.ts
+      const ToIPCSymbol = '__TAURI_TO_IPC__'
+
       if (val instanceof Map) {
         return Object.fromEntries(val.entries())
       } else if (val instanceof Uint8Array) {
@@ -28,8 +33,8 @@
         typeof val.id === 'number'
       ) {
         return `__CHANNEL__:${val.id}`
-      } else if (typeof val === "object" && 'toIPC' in val) {
-        return val.toIPC()
+      } else if (typeof val === "object" && ToIPCSymbol in val) {
+        return val[ToIPCSymbol]()
       } else {
         return val
       }

+ 50 - 0
packages/api/src/core.ts

@@ -258,6 +258,56 @@ export class Resource {
   }
 }
 
+/**
+ * Symbol to be used to implement a special function
+ * on your types that define how your type should be serialized
+ * when passing across the IPC.
+ * @example
+ * Given a type in Rust that looks like this
+ * ```rs
+ * #[derive(serde::Serialize, serde::Deserialize)
+ * enum UserId {
+ *   String(String),
+ *   Number(u32),
+ * }
+ * ```
+ * `UserId::String("id")` would be serialized into `{ String: "id" }`
+ * and so we need to pass the same structure back to Rust
+ * ```ts
+ * import { ToIPCSymbol } from "@tauri-apps/api/core"
+ *
+ * class UserIdString {
+ *   id
+ *   constructor(id) {
+ *     this.id = id
+ *   }
+ *
+ *   [ToIPCSymbol]() {
+ *     return { String: this.id }
+ *   }
+ * }
+ *
+ * class UserIdNumber {
+ *   id
+ *   constructor(id) {
+ *     this.id = id
+ *   }
+ *
+ *   [ToIPCSymbol]() {
+ *     return { Number: this.id }
+ *   }
+ * }
+ *
+ *
+ * type UserId = UserIdString | UserIdNumber
+ * ```
+ *
+ */
+// if this value changes, make sure to update it in:
+// 1. ipc.js
+// 2. process-ipc-message-fn.js
+export const ToIPCSymbol = '__TAURI_TO_IPC__'
+
 function isTauri(): boolean {
   return 'isTauri' in window && !!window.isTauri
 }

+ 10 - 8
packages/api/src/dpi.ts

@@ -2,6 +2,8 @@
 // SPDX-License-Identifier: Apache-2.0
 // SPDX-License-Identifier: MIT
 
+import { ToIPCSymbol } from './core'
+
 /**
  * A size represented in logical pixels.
  *
@@ -68,7 +70,7 @@ class LogicalSize {
    *
    * @since 2.0.0
    */
-  toIPC() {
+  [ToIPCSymbol]() {
     return {
       Logical: {
         width: this.width,
@@ -92,7 +94,7 @@ class LogicalSize {
    * @since 2.0.0
    */
   toJSON() {
-    return this.toIPC()
+    return this[ToIPCSymbol]()
   }
 }
 
@@ -158,7 +160,7 @@ class PhysicalSize {
    *
    * @since 2.0.0
    */
-  toIPC() {
+  [ToIPCSymbol]() {
     return {
       Physical: {
         width: this.width,
@@ -182,7 +184,7 @@ class PhysicalSize {
    * @since 2.0.0
    */
   toJSON() {
-    return this.toIPC()
+    return this[ToIPCSymbol]()
   }
 }
 
@@ -252,7 +254,7 @@ class LogicalPosition {
    *
    * @since 2.0.0
    */
-  toIPC() {
+  [ToIPCSymbol]() {
     return {
       Logical: {
         x: this.x,
@@ -276,7 +278,7 @@ class LogicalPosition {
    * @since 2.0.0
    */
   toJSON() {
-    return this.toIPC()
+    return this[ToIPCSymbol]()
   }
 }
 
@@ -327,7 +329,7 @@ class PhysicalPosition {
    *
    * @since 2.0.0
    */
-  toIPC() {
+  [ToIPCSymbol]() {
     return {
       Physical: {
         x: this.x,
@@ -351,7 +353,7 @@ class PhysicalPosition {
    * @since 2.0.0
    */
   toJSON() {
-    return this.toIPC()
+    return this[ToIPCSymbol]()
   }
 }
 

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio