-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create
Dispatcher
- a Platform specific interface for awaitab…
…le UI or Background Threading dispatches (#45)
- Loading branch information
Showing
20 changed files
with
300 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
package/android/src/main/cpp/java-bindings/JDispatcher.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Created by Marc Rousavy on 12.03.24. | ||
// | ||
|
||
#include "JDispatcher.h" | ||
|
||
namespace margelo { | ||
|
||
JDispatcher::~JDispatcher() {} | ||
|
||
JDispatcher::JDispatcher(const jni::alias_ref<jhybridobject>& javaThis) : _javaPart(jni::make_global(javaThis)) {} | ||
|
||
jni::local_ref<JDispatcher::jhybriddata> JDispatcher::initHybrid(jni::alias_ref<jhybridobject> javaThis) { | ||
return makeCxxInstance(javaThis); | ||
} | ||
|
||
void JDispatcher::registerNatives() { | ||
registerHybrid({makeNativeMethod("initHybrid", JDispatcher::initHybrid), makeNativeMethod("trigger", JDispatcher::triggerParent)}); | ||
} | ||
|
||
void JDispatcher::triggerParent() { | ||
trigger(); | ||
} | ||
|
||
void JDispatcher::scheduleTrigger() { | ||
static const auto method = javaClassLocal()->getMethod<void()>("scheduleTrigger"); | ||
method(_javaPart); | ||
} | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Created by Marc Rousavy on 12.03.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "Dispatcher.h" | ||
#include <fbjni/fbjni.h> | ||
|
||
namespace margelo { | ||
|
||
using namespace facebook; | ||
|
||
class JDispatcher : public jni::HybridClass<JDispatcher>, public Dispatcher { | ||
public: | ||
~JDispatcher(); | ||
static void registerNatives(); | ||
|
||
public: | ||
void scheduleTrigger() override; | ||
|
||
private: | ||
void triggerParent(); | ||
|
||
private: | ||
friend HybridBase; | ||
jni::global_ref<JDispatcher::javaobject> _javaPart; | ||
|
||
private: | ||
static auto constexpr TAG = "JDispatcher"; | ||
static auto constexpr kJavaDescriptor = "Lcom/margelo/filament/Dispatcher;"; | ||
|
||
private: | ||
explicit JDispatcher(const jni::alias_ref<jhybridobject>& javaThis); | ||
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> javaThis); | ||
}; | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
package/android/src/main/java/com/margelo/filament/Dispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.margelo.filament; | ||
|
||
import androidx.annotation.Keep; | ||
import androidx.annotation.NonNull; | ||
|
||
import com.facebook.jni.HybridData; | ||
import com.facebook.proguard.annotations.DoNotStrip; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
/** @noinspection JavaJniMissingFunction*/ | ||
public class Dispatcher { | ||
private final Executor executor; | ||
/** @noinspection unused, FieldCanBeLocal */ | ||
@DoNotStrip | ||
@Keep | ||
private final HybridData mHybridData; | ||
|
||
public Dispatcher(@NonNull Executor executor) { | ||
this.executor = executor; | ||
this.mHybridData = initHybrid(); | ||
} | ||
|
||
/** @noinspection unused */ | ||
@DoNotStrip | ||
@Keep | ||
private void scheduleTrigger() { | ||
executor.execute(this::trigger); | ||
} | ||
|
||
private native void trigger(); | ||
private native HybridData initHybrid(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Created by Marc Rousavy on 12.03.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <future> | ||
#include <queue> | ||
|
||
namespace margelo { | ||
|
||
class Dispatcher { | ||
private: | ||
std::queue<std::function<void()>> _jobs; | ||
std::mutex _mutex; | ||
|
||
private: | ||
// Schedule trigger will be implemented by Android/iOS to schedule a call to `trigger()` on the target Thread. | ||
virtual void scheduleTrigger() = 0; | ||
|
||
protected: | ||
// Trigger will run the latest job that has been added to the queue. | ||
void trigger() { | ||
std::unique_lock lock(_mutex); | ||
auto job = _jobs.front(); | ||
job(); | ||
_jobs.pop(); | ||
} | ||
|
||
public: | ||
template <typename T> std::future<T> runAsync(std::function<T()>&& function) { | ||
// 1. Create Promise that can be shared between this and dispatcher thread | ||
auto promise = std::make_shared<std::promise<T>>(); | ||
std::future<T> future = promise->get_future(); | ||
|
||
std::unique_lock lock(_mutex); | ||
_jobs.push([function = std::move(function), promise]() { | ||
try { | ||
// 4. Call the actual function on the new Thread | ||
T result = function(); | ||
// 5.a. Resolve the Promise if we succeeded | ||
promise->set_value(std::move(result)); | ||
} catch (...) { | ||
// 5.b. Reject the Promise if the call failed | ||
promise->set_exception(std::current_exception()); | ||
} | ||
}); | ||
scheduleTrigger(); | ||
|
||
// 3. Return an open future that gets resolved later by the dispatcher Thread | ||
return future; | ||
} | ||
}; | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.