Skip to content
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
3 changes: 0 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ jobs:
name: Publish
steps:
- uses: holepunchto/actions/node-base@v1
- run: npm install --global bare-make
- run: bare-make generate
- run: bare-make install --prefix .
- uses: holepunchto/actions/publish@v1
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
addons/
build/
node_modules/
package-lock.json
17 changes: 0 additions & 17 deletions CMakeLists.txt

This file was deleted.

65 changes: 27 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
npm i react-native-bare-kit
```

Bare Kit isn't included. The application brings it along with the addons and assets of its worklets, so that other code, such as a notification service extension, can share them. See [Linking addons and assets](#linking-addons-and-assets).

## Usage

```js
Expand All @@ -15,10 +17,8 @@ import b4a from 'b4a'
const worklet = new Worklet()

const source = `\
const { IPC } = BareKit

IPC.on('data', (data) => console.log(data.toString()))
IPC.write(Buffer.from('Hello from Bare!'))
Bare.IPC.on('data', (data) => console.log(data.toString()))
Bare.IPC.write(Buffer.from('Hello from Bare!'))
`

worklet.start('/app.js', source)
Expand All @@ -29,56 +29,45 @@ IPC.on('data', (data) => console.log(b4a.toString(data)))
IPC.write(b4a.from('Hello from React Native!'))
```

Alternatively to load from a bundle:
Refer to <https://github.com/holepunchto/bare-expo> for an example of using the library in an Expo application.

```js
import { Worklet } from 'react-native-bare-kit'
### Packing workers

// Bundle output by `bare-pack`
// Extension can be .bundle, .js, .cjs, .mjs
import bundle from './my.bundle.js'
Pack a worker and its dependencies into a package with <https://github.com/holepunchto/bare-stow> and <https://github.com/holepunchto/bare-stow-target-react-native>. The worker exports a `start()` function that receives the IPC stream:

const worklet = new Worklet()
// First arg (filename)'s extension *must* be .bundle
worklet.start('/app.bundle', source)
```js
module.exports = function start(ipc) {
ipc.on('data', (data) => ipc.write(data))
}
```
Comment thread
tony-go marked this conversation as resolved.
Resolved

// [...]
```console
bare-stow pack --target react-native --out ./worker worker.js
```

Refer to <https://github.com/holepunchto/bare-expo> for an example of using the library in an Expo application.
Add the package as a dependency of the application and start the worker in a worklet:

### Linking native addons
```js
import worker from 'worker'

Native addons used by the worklet are not linked automatically. Applications must link them into their native projects themselves, such as by using <https://github.com/holepunchto/bare-link>. Given the entry point of the worklet, `bare-link` links the addons loaded by its module graph:
const { ipc } = await worker.start()

```console
bare-link --preset ios --out ios/addons worklet.js
bare-link --preset android --out android/app/src/main/jniLibs worklet.js
ipc.on('data', (data) => console.log(b4a.toString(data)))
ipc.write(b4a.from('Hello from React Native!'))
```

On iOS, the resulting `.xcframework` bundles must be embedded in the application target. On Android, the resulting `.so` libraries must be included in the `jniLibs` of the application and the resulting `.jar` libraries, if any, added as dependencies.
The package leaves out the addons and assets of the worker, so link them as described below.

### Linking addons and assets

As a convenience, the library includes designated directories that are already wired into its native projects. Any `.xcframework` bundle in `ios/addons` and any `.so` or `.jar` library in `android/src/main/addons` of the library will be included in the build without further configuration:
Link the addons and assets of all of the worklets of the application once with <https://github.com/holepunchto/bare-stow>. It writes a Swift package and a Gradle module, which also bring along Bare Kit:

```console
bare-link --preset ios --out node_modules/react-native-bare-kit/ios/addons worklet.js
bare-link --preset android --out node_modules/react-native-bare-kit/android/src/main/addons worklet.js
bare-stow link --target bare-kit --language swift --out ios/BareKitResources worklet.js
bare-stow link --target bare-kit --language kotlin --language-option package=com.example.addons --out android/bare-kit-resources worklet.js
```

As these directories live within `node_modules`, they are cleared whenever the library is reinstalled. Consider running the commands from a `postinstall` script of the application:

```json
{
"scripts": {
"postinstall": "npm run link:ios && npm run link:android",
"link:ios": "bare-link --preset ios --out node_modules/react-native-bare-kit/ios/addons worklet.js",
"link:android": "bare-link --preset android --out node_modules/react-native-bare-kit/android/src/main/addons worklet.js"
},
"devDependencies": {
"bare-link": "^4.0.1"
}
}
```
Add the Swift package and its `bare-stow` folder to the application target, and include the Gradle module in the application. In an Expo application, list the workers in the `workers` option of <https://github.com/holepunchto/expo-bare-kit> instead.

### Logging

Expand Down
1 change: 0 additions & 1 deletion android/.npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.cxx/
build/
addons/
libc++_shared.so
11 changes: 2 additions & 9 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ cmake_minimum_required(VERSION 3.22)

project(react-native-bare-kit)

add_library(bare-kit SHARED IMPORTED)

set_target_properties(
bare-kit
PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/libs/bare-kit/jni/${ANDROID_ABI}/libbare-kit.so"
)

add_library(react-native-bare-kit STATIC)

set_target_properties(
Expand All @@ -24,6 +16,7 @@ target_sources(
PRIVATE
../shared/BareKitModule.cc
../shared/BareKitModule.h
../shared/bare-kit.h
)

target_include_directories(
Expand All @@ -34,6 +27,6 @@ target_include_directories(

target_link_libraries(
react-native-bare-kit
bare-kit
fbjni::fbjni
react_codegen_BareKitSpec
)
15 changes: 1 addition & 14 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ android {
minSdk 29
targetSdk 34

externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
}
}
consumerProguardFiles "proguard-rules.pro"
}

sourceSets {
main {
manifest.srcFile "src/main/AndroidManifest.xml"
jniLibs.srcDirs "src/main/addons", "libs/bare-kit/jni"
}
}

Expand All @@ -39,12 +34,6 @@ android {
minifyEnabled false
}
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}

repositories {
Expand All @@ -53,8 +42,6 @@ repositories {
}

dependencies {
api fileTree(dir: "libs", include: ["bare-kit/classes.jar"])
api fileTree(dir: "src/main/addons", include: ["*.jar"])

implementation "com.facebook.react:react-native:+"
}
3 changes: 0 additions & 3 deletions android/libs/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion android/libs/.npmignore

This file was deleted.

3 changes: 3 additions & 0 deletions android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-keep class to.holepunch.bare.kit.react.BareKitProvider {
public static android.content.Context getApplicationContext();
}
9 changes: 8 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<provider
android:name="to.holepunch.bare.kit.react.BareKitProvider"
android:authorities="${applicationId}.bare-kit"
android:exported="false" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
public class BareKitPackage extends BaseReactPackage {
public static String NAME = "BareKit";

static {
// The library must be loaded from Java so JNI_OnLoad runs and captures the JVM.
System.loadLibrary("bare-kit");
}

@Override
public NativeModule
getModule(String name, ReactApplicationContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package to.holepunch.bare.kit.react;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

// Captures the application context at startup, as React Native never hands one
// to a C++ module, so that worklets can publish it to their native addons.
public class BareKitProvider extends ContentProvider {
private static Context applicationContext;

public static Context
getApplicationContext() {
return applicationContext;
}

@Override
public boolean
onCreate() {
applicationContext = getContext().getApplicationContext();

return false;
}

@Override
public Cursor
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}

@Override
public String
getType(Uri uri) {
return null;
}

@Override
public Uri
insert(Uri uri, ContentValues values) {
return null;
}

@Override
public int
delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}

@Override
public int
update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface WorkletEvents extends EventMap {

export interface WorkletOptions {
memoryLimit?: number
assets?: string
}

export class Worklet extends EventEmitter<WorkletEvents> {
Expand Down
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BareKitIPC extends Duplex {
const data = NativeBareKit.read(this._worklet._handle)

if (data) {
this.push(new Uint8Array(data))
this.push(data.byteLength === 0 ? null : new Uint8Array(data))
cb(null)
} else {
this._pendingRead = cb
Expand All @@ -71,12 +71,18 @@ class BareKitIPC extends Duplex {
)

if (written === data.byteLength) cb(null)
else if (written < 0) cb(new Error('IPC has been closed'))
else {
this._pendingWrite = [data.subarray(written), cb]
this._update()
}
}

_final(cb) {
NativeBareKit.end(this._worklet._handle)
cb(null)
}

_continueOpen(err) {
if (this._pendingOpen === null) {
if (err) this.destroy(err)
Expand Down Expand Up @@ -117,7 +123,7 @@ class BareKitWorklet extends EventEmitter {
throw new TypeError('ID must be a string. Received type ' + typeof id + ' (' + id + ')')
}

const { memoryLimit = 0, assets = null } = opts
const { memoryLimit = 0 } = opts

if (typeof memoryLimit !== 'number') {
throw new TypeError(
Expand All @@ -129,12 +135,6 @@ class BareKitWorklet extends EventEmitter {
)
}

if (typeof assets !== 'string' && assets !== null) {
throw new TypeError(
'Asset path must be a string. Received type ' + typeof assets + ' (' + assets + ')'
)
}

super()

this._state = 0
Expand All @@ -145,7 +145,6 @@ class BareKitWorklet extends EventEmitter {
this._handle = NativeBareKit.init(
id,
memoryLimit,
assets,
terminate,
this._ipc._poll,
this._onsuspend.bind(this),
Expand Down Expand Up @@ -280,7 +279,7 @@ class BareKitWorklet extends EventEmitter {

static wakeup(deadline) {
for (const worklet of this._worklets) {
worklet.deadline(deadline)
worklet.wakeup(deadline)
}
}

Expand Down
1 change: 0 additions & 1 deletion ios/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion ios/.npmignore

This file was deleted.

19 changes: 0 additions & 19 deletions ios/BareKit.podspec

This file was deleted.

Loading
Loading