Skip to content

Commit 9714851

Browse files
committed
refactor: make ContentContext::new synchronous and infallible across all bindings
1 parent 673e3eb commit 9714851

12 files changed

Lines changed: 31 additions & 63 deletions

File tree

c/src/content_context/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub unsafe extern "C" fn lb_content_context_new(
3333
std::ptr::null_mut::<c_void>(),
3434
userdata,
3535
async move {
36-
let ctx = ContentContext::try_new(config)?;
36+
let ctx = ContentContext::new(config);
3737
let arc_ctx = Arc::new(CContentContext { ctx });
3838
let ctx = Arc::into_raw(arc_ctx);
3939
Ok(CAsyncResult {

java/javasrc/src/main/java/com/longbridge/SdkNative.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static native void oauthBuild(String clientId, int callbackPort,
5757

5858
public static native void freeOAuth(long oauth);
5959

60-
public static native void newContentContext(long config, AsyncCallback callback);
60+
public static native long newContentContext(long config);
6161

6262
public static native void freeContentContext(long context);
6363

java/javasrc/src/main/java/com/longbridge/content/ContentContext.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ public class ContentContext implements AutoCloseable {
1414
* Create a ContentContext object
1515
*
1616
* @param config Config object
17-
* @return A Future representing the result of the operation
18-
* @throws OpenApiException If an error occurs
17+
* @return A ContentContext object
1918
*/
20-
public static CompletableFuture<ContentContext> create(Config config)
21-
throws OpenApiException {
22-
return AsyncCallback.executeTask((callback) -> {
23-
SdkNative.newContentContext(config.getRaw(), callback);
24-
});
19+
public static ContentContext create(Config config) {
20+
ContentContext ctx = new ContentContext();
21+
ctx.raw = SdkNative.newContentContext(config.getRaw());
22+
return ctx;
2523
}
2624

2725
@Override

java/src/content_context.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ use std::sync::Arc;
22

33
use jni::{
44
JNIEnv,
5-
errors::Result,
6-
objects::{JClass, JObject, JValueOwned},
5+
objects::{JClass, JObject},
76
};
87
use longbridge::{Config, content::ContentContext};
98

109
use crate::{
1110
async_util,
1211
error::jni_result,
13-
init::CONTENT_CONTEXT_CLASS,
14-
types::{FromJValue, IntoJValue, ObjectArray, set_field},
12+
types::{FromJValue, ObjectArray},
1513
};
1614

1715
struct ContextObj {
@@ -23,29 +21,11 @@ pub unsafe extern "system" fn Java_com_longbridge_SdkNative_newContentContext(
2321
mut env: JNIEnv,
2422
_class: JClass,
2523
config: i64,
26-
callback: JObject,
27-
) {
28-
struct ContextObjRef(i64);
29-
30-
impl IntoJValue for ContextObjRef {
31-
fn into_jvalue<'a>(self, env: &mut JNIEnv<'a>) -> Result<JValueOwned<'a>> {
32-
let ctx_obj = env.new_object(CONTENT_CONTEXT_CLASS.get().unwrap(), "()V", &[])?;
33-
set_field(env, &ctx_obj, "raw", self.0)?;
34-
Ok(JValueOwned::from(ctx_obj))
35-
}
36-
}
37-
38-
jni_result(&mut env, (), |env| {
24+
) -> i64 {
25+
jni_result(&mut env, 0i64, |_env| {
3926
let config = Arc::new((*(config as *const Config)).clone());
40-
41-
async_util::execute(env, callback, async move {
42-
let ctx = ContentContext::try_new(config)?;
43-
Ok(ContextObjRef(
44-
Box::into_raw(Box::new(ContextObj { ctx })) as i64
45-
))
46-
})?;
47-
48-
Ok(())
27+
let ctx = ContentContext::new(config);
28+
Ok(Box::into_raw(Box::new(ContextObj { ctx })) as i64)
4929
})
5030
}
5131

java/src/init.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) static TIME_LOCALDATETIME_CLASS: OnceLock<GlobalRef> = OnceLock::new(
2020
pub(crate) static TIME_ZONE_ID: OnceLock<GlobalRef> = OnceLock::new();
2121
pub(crate) static QUOTE_CONTEXT_CLASS: OnceLock<GlobalRef> = OnceLock::new();
2222
pub(crate) static TRADE_CONTEXT_CLASS: OnceLock<GlobalRef> = OnceLock::new();
23-
pub(crate) static CONTENT_CONTEXT_CLASS: OnceLock<GlobalRef> = OnceLock::new();
23+
2424
pub(crate) static DERIVATIVE_TYPE_CLASS: OnceLock<GlobalRef> = OnceLock::new();
2525
pub(crate) static OPENAPI_EXCEPTION_CLASS: OnceLock<GlobalRef> = OnceLock::new();
2626

@@ -73,11 +73,7 @@ pub extern "system" fn Java_com_longbridge_SdkNative_init<'a>(
7373
(DERIVATIVE_TYPE_CLASS, "com/longbridge/quote/DerivativeType"),
7474
(OPENAPI_EXCEPTION_CLASS, "com/longbridge/OpenApiException"),
7575
(QUOTE_CONTEXT_CLASS, "com/longbridge/quote/QuoteContext"),
76-
(TRADE_CONTEXT_CLASS, "com/longbridge/trade/TradeContext"),
77-
(
78-
CONTENT_CONTEXT_CLASS,
79-
"com/longbridge/content/ContentContext"
80-
)
76+
(TRADE_CONTEXT_CLASS, "com/longbridge/trade/TradeContext")
8177
);
8278

8379
init_timezone_id(&mut env);

mcp/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4949
);
5050
let (quote_context, _) = QuoteContext::new(config.clone());
5151
let (trade_context, _) = TradeContext::new(config.clone());
52-
let content_context = ContentContext::try_new(config.clone())?;
52+
let content_context = ContentContext::new(config.clone());
5353
let readonly = cli.readonly;
5454

5555
if !cli.http {

nodejs/src/content/context.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ pub struct ContentContext {
1919
impl ContentContext {
2020
/// Create a new `ContentContext`
2121
#[napi]
22-
pub async fn new(config: &Config) -> napi::Result<ContentContext> {
23-
Ok(Self {
24-
ctx: longbridge::content::ContentContext::try_new(Arc::new(config.0.clone()))
25-
.map_err(ErrorNewType)?,
26-
})
22+
pub fn new(config: &Config) -> ContentContext {
23+
Self {
24+
ctx: longbridge::content::ContentContext::new(Arc::new(config.0.clone())),
25+
}
2726
}
2827

2928
/// Get discussion topics list

python/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
- [AsyncContentContext](reference_all.md#longbridge.openapi.AsyncContentContext)
3232

33-
Async content API for use with asyncio; create via `AsyncContentContext.create(config)` and await in asyncio.
33+
Async content API for use with asyncio; create via `AsyncContentContext.create(config)` (synchronous, no await needed at construction).
3434

3535
## Quickstart
3636

python/src/content/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl ContentContext {
1919
#[new]
2020
fn new(config: &Config) -> PyResult<Self> {
2121
Ok(Self {
22-
ctx: ContentContextSync::try_new(Arc::new(config.0.clone())).map_err(ErrorNewType)?,
22+
ctx: ContentContextSync::new(Arc::new(config.0.clone())).map_err(ErrorNewType)?,
2323
})
2424
}
2525

python/src/content/context_async.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@ pub(crate) struct AsyncContentContext {
1919
impl AsyncContentContext {
2020
/// Create an async content context.
2121
#[classmethod]
22-
fn create(cls: &Bound<PyType>, config: &Config) -> PyResult<Py<PyAny>> {
23-
let py = cls.py();
24-
let config = Arc::new(config.0.clone());
25-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
26-
Ok(AsyncContentContext {
27-
ctx: Arc::new(ContentContext::try_new(config).map_err(ErrorNewType)?),
28-
})
29-
})
30-
.map(|b| b.unbind())
22+
fn create(_cls: &Bound<PyType>, config: &Config) -> Self {
23+
AsyncContentContext {
24+
ctx: Arc::new(ContentContext::new(Arc::new(config.0.clone()))),
25+
}
3126
}
3227

3328
/// Get discussion topics list. Returns awaitable.

0 commit comments

Comments
 (0)