Skip to content

Commit

Permalink
[iOS] add requestHandle to download js
Browse files Browse the repository at this point in the history
  • Loading branch information
jianhan-he committed Nov 13, 2018
1 parent d306455 commit 9b3acb4
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 35 deletions.
18 changes: 11 additions & 7 deletions ios/sdk/WeexSDK/Sources/Bridge/WXCoreBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -718,15 +718,19 @@ + (void)createDataRenderInstance:(NSString *)pageId template:(NSString *)jsBundl
auto node_manager = weex::core::data_render::VNodeRenderManager::GetInstance();
NSString *optionsString = [WXUtility JSONString:options];
NSString *dataString = [WXUtility JSONString:data];
weex::core::data_render::HttpModule downloadJS;
downloadJS.set_func([=](const char* url){
NSURL* ns_url = [NSURL URLWithString:NSSTRING(url)];
WXPerformBlockOnMainThread(^{
[[WXSDKManager bridgeMgr] DownloadJS:pageId scriptUrl:ns_url];

node_manager->CreatePage([jsBundleString UTF8String] ?: "", [pageId UTF8String] ?: "", [optionsString UTF8String] ?: "", [dataString UTF8String] ?: "", [=](const char* javascript){
if (!javascript) {
return;
}
[[WXSDKManager bridgeMgr] createInstanceForJS:pageId template:NSSTRING(javascript) options:nil data:data];
WXPerformBlockOnComponentThread(^{
auto root = node_manager->GetRootVNode([pageId UTF8String] ?: "");
if (root && root->IsVirtualComponent()) {
static_cast<weex::core::data_render::VComponent*>(root)->DispatchCreated();
}
});
});

node_manager->CreatePage([jsBundleString UTF8String] ?: "", [pageId UTF8String] ?: "", [optionsString UTF8String] ?: "", [dataString UTF8String] ?: "", &downloadJS);
}

+ (void)createDataRenderInstance:(NSString *)pageId contents:(NSData *)contents options:(NSDictionary *)options data:(id)data
Expand Down
14 changes: 13 additions & 1 deletion ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ extern void WXPerformBlockOnBridgeThread(void (^block)(void));
**/
@property (nonatomic, weak, readonly) WXSDKInstance *topInstance;

/**
* Create Instance Method
* @param instance : instance id
* @param temp : template data
* @param options : parameters
* @param data : external data
**/
- (void)createInstanceForJS:(NSString *)instance
template:(NSString *)temp
options:(NSDictionary *)options
data:(id)data;

/**
* Create Instance Method
* @param instance : instance id
Expand Down Expand Up @@ -101,7 +113,7 @@ extern void WXPerformBlockOnBridgeThread(void (^block)(void));
+ * download JS Script
+ * @param scriptUrl : script url
+ **/
- (void)DownloadJS:(NSString *)instance scriptUrl:(NSURL *)scriptUrl;
- (void)DownloadJS:(NSURL *)scriptUrl completion:(void (^)(NSString *script))complection;

/**
* Register JS service Script
Expand Down
24 changes: 18 additions & 6 deletions ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ + (void)_performBlockSyncOnBridgeThread:(void (^)(void))block
}

#pragma mark JSBridge Management
- (void)createInstanceForJS:(NSString *)instance
template:(NSString *)temp
options:(NSDictionary *)options
data:(id)data {
if (!instance || !temp) return;
__weak typeof(self) weakSelf = self;
WXPerformBlockOnBridgeThread(^(){
[weakSelf.bridgeCtx createInstance:instance
template:temp
options:options
data:data];
});
}

- (void)createInstance:(NSString *)instance
template:(NSString *)temp
Expand Down Expand Up @@ -289,22 +302,21 @@ - (JSValue *)callJSMethodWithResult:(WXCallJSMethod *)method
return value;
}

- (void)DownloadJS:(NSString *)instance scriptUrl:(NSURL *)scriptUrl
- (void)DownloadJS:(NSURL *)scriptUrl completion:(void (^)(NSString *script))complection;
{
if (!instance || !scriptUrl) {
if (!scriptUrl) {
complection(nil);
return;
}
__weak typeof(self) weakSelf = self;
WXResourceRequest* request = [WXResourceRequest requestWithURL:scriptUrl];
WXResourceLoader* jsLoader = [[WXResourceLoader alloc] initWithRequest:request];
jsLoader.onFinished = ^(WXResourceResponse *response, NSData *data) {
NSString* jsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
WXPerformBlockOnBridgeThread(^(){
[weakSelf createInstance:instance template:jsString options:nil data:nil];
});
complection(jsString);
};
jsLoader.onFailed = ^(NSError *loadError) {
WXLogError(@"No js URL found");
complection(nil);
};

[jsLoader start];
Expand Down
21 changes: 9 additions & 12 deletions weex_core/Source/core/data_render/vnode/vnode_render_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ void VNodeRenderManager::InitVM() {
}
}

void VNodeRenderManager::CreatePage(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, HttpModule *downloadJS) {
std::string err = CreatePageImpl(input, page_id, options, init_data, downloadJS);
void VNodeRenderManager::CreatePage(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, std::function<void(const char*)> js_exec) {
std::string err = CreatePageImpl(input, page_id, options, init_data, js_exec);
if (!err.empty()) {
WeexCore::WeexCoreManager::Instance()->getPlatformBridge()->platform_side()->ReportException(page_id.c_str(), nullptr, err.c_str());
}
}


std::string VNodeRenderManager::CreatePageImpl(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, HttpModule *downloadJS) {
std::string VNodeRenderManager::CreatePageImpl(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, std::function<void(const char*)> exec_js) {
InitVM();
auto start = std::chrono::steady_clock::now();
ExecState *exec_state = new ExecState(g_vm);
Expand Down Expand Up @@ -227,18 +227,15 @@ std::string VNodeRenderManager::CreatePageImpl(const std::string &input, const s
return err;
}

const json11::Json& javascript_obj = exec_state->context()->raw_json()["javascript"];
std::string javacript_url = javascript_obj.string_value();
if (!javacript_url.empty()) {
downloadJS->Send(javacript_url.c_str());
}

CreatePageInternal(page_id, exec_state->context()->root());
auto duration_post = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
LOGD("DATA_RENDER, All time %lld\n", duration_post.count());
auto root = vnode_trees_[page_id];
if (root->IsVirtualComponent()) {
static_cast<VComponent*>(root)->DispatchCreated();

const json11::Json& javascript_obj = exec_state->context()->raw_json()["javascript"];
std::string javacript_url = javascript_obj.string_value();
if (!javacript_url.empty()) {
HttpModule http_module;
http_module.Send(javacript_url.c_str(), exec_js);
}
return err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class VNodeRenderManager {
~VNodeRenderManager() {}

public:
void CreatePage(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, HttpModule *downloadJS);
void CreatePage(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, std::function<void(const char*)> exec_js);

void CreatePage(const char* contents, unsigned long length, const std::string& page_id, const std::string& options, const std::string& init_data);

Expand Down Expand Up @@ -78,7 +78,7 @@ class VNodeRenderManager {
bool RefreshPageInternal(const std::string &page_id, VNode *new_node);
bool ClosePageInternal(const std::string &page_id);
std::string CreatePageWithOpcode(const std::string& page_id, const std::string& options, const std::string& init_data);
std::string CreatePageImpl(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, HttpModule *downloadJS);
std::string CreatePageImpl(const std::string &input, const std::string &page_id, const std::string &options, const std::string &init_data, std::function<void(const char*)> exec_js);

static VM *g_vm;
static VNodeRenderManager *g_instance;
Expand Down
37 changes: 37 additions & 0 deletions weex_core/Source/core/network/default_request_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef DEFAULT_REQUEST_HANDLER_H
#define DEFAULT_REQUEST_HANDLER_H

#include "core/network/request_handler.h"

namespace weex {
namespace core {
namespace data_render {

class DefaultRequestHandler : public RequestHandler{
public:
void Send(const char* url, std::function<void(const char*)> callback) override;
};

}
}
}
#endif
18 changes: 14 additions & 4 deletions weex_core/Source/core/network/http_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@
* under the License.
*/
#include "core/network/http_module.h"
#include "core/network/request_handler.h"
#include "core/network/default_request_handler.h"

namespace weex {
namespace core {
namespace data_render {
void HttpModule::set_func(std::function<void(const char*)> func) {
func_ = func;

HttpModule::HttpModule() : request_handler_(new DefaultRequestHandler) {
}

HttpModule::HttpModule(RequestHandler* request_handler) {
if (!request_handler) {
request_handler_.reset(new DefaultRequestHandler);
} else {
request_handler_.reset(request_handler);
}
}

void HttpModule::Send(const char* url) {
func_(url);
void HttpModule::Send(const char* url, FUNC callback) {
request_handler_->Send(url, callback);
}

}
Expand Down
10 changes: 7 additions & 3 deletions weex_core/Source/core/network/http_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@
#ifndef HTTP_MODULE_
#define HTTP_MODULE_
#include <functional>
#include <memory>

namespace weex {
namespace core {
namespace data_render {
typedef std::function<void(const char*)> FUNC;
class RequestHandler;

class HttpModule {
public:
void set_func(std::function<void(const char*)> func);
void Send(const char* url);
HttpModule();
HttpModule(RequestHandler* request_handler);
void Send(const char* url, FUNC callback);
private:
std::function<void(const char*)> func_;
std::auto_ptr<RequestHandler> request_handler_;
};

}
Expand Down
34 changes: 34 additions & 0 deletions weex_core/Source/core/network/ios/default_request_handler.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "core/network/default_request_handler.h"
#import "WXConvertUtility.h"
#import "WXSDKManager.h"

namespace weex {
namespace core {
namespace data_render {
void DefaultRequestHandler::Send(const char* url, std::function<void(const char*)> callback) {
NSURL* ns_url = [NSURL URLWithString:NSSTRING(url)];
[[WXSDKManager bridgeMgr] DownloadJS:ns_url completion:^(NSString *script) {
callback([script UTF8String] ? : nullptr);
}];
}
}
}
}
36 changes: 36 additions & 0 deletions weex_core/Source/core/network/request_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef REQUEST_HANDLER_H
#define REQUEST_HANDLER_H
#include <functional>

namespace weex {
namespace core {
namespace data_render {

class RequestHandler {
public:
virtual void Send(const char* url, std::function<void(const char*)> callback) = 0;
};

}
}
}
#endif

0 comments on commit 9b3acb4

Please sign in to comment.