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

Develop #4

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 6 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

DSBridge-Swift is a [DSBridge-iOS](https://github.com/wendux/DSBridge-IOS) fork in Swift. It allows developers to send method calls back and forth between Swift and JavaScript.

# Installation
# Usage

Check out [wiki](https://github.com/EdgarDegas/DSBridge-Swift/wiki) for docs.

## Installation

DSBridge is available on both iOS and Android.

Expand All @@ -34,7 +38,7 @@ Or install with npm:
npm install [email protected]
```

# Usage
#

## Brief

Expand Down Expand Up @@ -165,70 +169,6 @@ bridge.call('asyncFunction', 1, function(v) { console.log(v) });
// 4
```

# Declaration Rules
## Allowed Interface Types
You can declare your interface as these types:
- class
- enum
- struct

> actors are not supported yet. Please file up your ideas about it.
## Allowed Data Types
You can receive or send the following types:
- String
- Int, Double (types toll-free bridged to NSNumber)
- Bool
- Standard JSON top-level objects:

- Dictionary that's encodable

- Array that's encodable


## Allowed Function Declarations
DSBridge-Swift ignores argument labels and parameter names of your functions. Thus you can name your parameters whatever you want.

#### Synchronous Functions

About parameters, synchronous functions can have:

- 1 parameter, which is one of the above-mentioned *Allowed Data Types*
- no parameter

About return value, synchronous functions can have:

- return value that's one of the above-mentioned *Allowed Data Types*
- no return value

For simplicity, we use `Allowed` to represent the before-mentioned Allowed Data Types.

```swift
func name()
func name(Allowed)
func name(Allowed) -> Allowed
```
#### Asynchronous Functions

Asynchronous functions are allowed to have 1 or 2 parameters and no return value.

If there are 2 parameters, the first one must be one of the above-mentioned *Allowed Data Types*.

The last parameter has to be a closure that returns nothing (i.e., `Void`). For parameters, the closure can have:

- 1 parameter, one of the above-mentioned *Allowed Data Types*
- 2 parameters, the first one is one of the above-mentioned *Allowed Data Types* and the second one is a `Bool`

```swift
typealias Completion = (Allowed) -> Void
typealias RepeatableCompletion = (Allowed, Bool) -> Void

func name(Completion)
func name(RepeatableCompletion)
func name(Allowed, Completion)
func name(Allowed, RepeatableCompletion)
```
Attribute your closure with `@ecaping` if needed. Otherwise, keep in mind that your functions run on the main thread and try not to block it.

# Differences with DSBridge-iOS

## Seamless `WKWebView` Experience
Expand Down
74 changes: 6 additions & 68 deletions README.zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

DSBridge-Swift 是 [DSBridge-iOS](https://github.com/wendux/DSBridge-IOS) 的一个 Swift 版 fork。它允许开发者在原生和 JavaScript 之间调用彼此的方法。

# 集成方式
# 使用

在 [wiki](https://github.com/EdgarDegas/DSBridge-Swift/wiki) 中查看详细文档。

## 集成

DSBridge 是一个三端可用的 JavaScript Bridge。

Expand All @@ -20,6 +24,7 @@ DSBridge 是一个三端可用的 JavaScript Bridge。
Android 端集成方式见 [DSBridge-Android](https://github.com/wendux/DSBridge-Android)。

你可以通过 CDN 引入 JavaScript 代码(或下载 JS 文件并添加到工程中以避免网络问题):

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/dsbridge.js"></script>
```
Expand All @@ -30,8 +35,6 @@ Android 端集成方式见 [DSBridge-Android](https://github.com/wendux/DSBridge
npm install [email protected]
```

# 使用

## 简介

首先,在你的视图中使用 `DSBridge.WebView` 而非 `WKWebView`:
Expand Down Expand Up @@ -168,71 +171,6 @@ bridge.call('asyncFunction', 1, function(v) { console.log(v) });
// 4
```

## `Interface` 声明规则

### 支持的 `Interface` 类型

你可以将 `Interface` 声明为 `class`、`struct` 或 `enum`。暂未支持 `actor`,欢迎大家的想法。

### 支持的数据类型

你可以发送或接收这些类型的数据:

- String
- Int, Double 等(与 NSNumber 无缝转换的类型)
- Bool
- 标准的 JSON 顶层对象:
- Dictionary,必须可编码为 JSON
- Array,必须可编码为 JSON

### 支持的方法声明

DSBridge-Swift 无视 `Interface` 中的方法的参数名,无论调用名还是内部名,因此你可以使用任意的参数名。

#### 同步方法

关于参数,同步方法只能:

- 有 1 个参数,类型符合上述”支持的数据类型“

- 没有参数

关于返回值,同步方法可以:

- 有返回值,类型符合上述”支持的数据类型“
- 没有返回值

为了简便,使用 `Allowed` 代指上面说的”支持的数据类型“:

```swift
func name()
func name(Allowed)
func name(Allowed) -> Allowed
```

#### 异步方法

异步方法可以有 1 个或 2 个参数,不允许有返回值。

如果有 2 个参数,第 1 个参数类型必须符合上述”支持的数据类型“。

方法的最后一个参数必须是闭包,返回 `Void`。关于参数,闭包只能:

- 有 1 个参数,类型符合上述”支持的数据类型“
- 有 2 个参数,第 1 个类型符合上述”支持的数据类型“,第 2 个必须是 `Bool` 类型

```swift
typealias Completion = (Allowed) -> Void
typealias RepeatableCompletion = (Allowed, Bool) -> Void

func name(Completion)
func name(RepeatableCompletion)
func name(Allowed, Completion)
func name(Allowed, RepeatableCompletion)
```

闭包可以是 `@escaping` 的;如果不是的话,请注意,你的方法应当快速执行、立即返回,否则将会阻塞主线程。

# 与 DSBridge-iOS 的不同

## 无感的 `WKWebView` 体验
Expand Down
Loading