-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inlined bridge.js, to avoid loading issues (#28)
- Loading branch information
Showing
3 changed files
with
79 additions
and
90 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -30,15 +30,86 @@ public class JustBridge: NSObject { | |
} | ||
|
||
/// The js that need inject to webView at dom start | ||
internal static let bridge_js: String = { | ||
guard let bundlePath = Bundle(for: JustBridge.self).path(forResource: "JSBridge", ofType: "bundle"), | ||
let bundle = Bundle(path: bundlePath), | ||
let jsPath = bundle.path(forResource: "bridge", ofType: "js"), | ||
let js = try? String(contentsOfFile: jsPath) else { | ||
fatalError("Could not find JSBridge.bundle or bridge.js") | ||
internal static let bridge_js: String = """ | ||
; | ||
(function() { | ||
// Copyright (c) 2018 Xiaoye220 <[email protected]> | ||
if (window.bridge) { | ||
return; | ||
} | ||
return js | ||
}() | ||
window.bridge = function() { | ||
var handlers = {}, | ||
callbacks = {}, | ||
errorCallbacks = {}, | ||
jsCallbackId = 0; | ||
function postMessage(name, data, swiftCallbackId, jsCallbackId, error) { | ||
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.bridge) { | ||
window.webkit.messageHandlers.bridge.postMessage({ | ||
"name": name, | ||
"data": data, | ||
"swiftCallbackId": swiftCallbackId, | ||
"jsCallbackId": jsCallbackId, | ||
"error": error, | ||
}); | ||
} | ||
} | ||
return { | ||
"register": function(name, handler) { | ||
handlers[name] = handler; | ||
}, | ||
"call": function(name, data, callback, errorCallback) { | ||
var id = (jsCallbackId++).toString(); | ||
callbacks[id] = callback; | ||
errorCallbacks[id] = errorCallback; | ||
postMessage(name, data, null, id, null); | ||
}, | ||
"receiveMessage": function(message) { | ||
var name = message.name; | ||
var data = message.data; | ||
var jsCallbackId = message.jsCallbackId; | ||
var swiftCallbackId = message.swiftCallbackId; | ||
var error = message.error; | ||
if (jsCallbackId) { | ||
if(error) { | ||
var jsErrorCallback = errorCallbacks[jsCallbackId]; | ||
if (jsErrorCallback) { | ||
jsErrorCallback(error); | ||
delete errorCallbacks[jsCallbackId]; | ||
} | ||
} else { | ||
var jsCallback = callbacks[jsCallbackId]; | ||
if (jsCallback) { | ||
jsCallback(data); | ||
delete callbacks[jsCallbackId]; | ||
} | ||
} | ||
} else { | ||
var swiftCallBack; | ||
if (swiftCallbackId) { | ||
swiftCallBack = function(data) { | ||
postMessage(name, data, swiftCallbackId); | ||
}; | ||
} | ||
var handler = handlers[name]; | ||
if (handler) { | ||
handler(data, swiftCallBack) | ||
} else { | ||
postMessage(name, data, swiftCallbackId, null, "HandlerNotExistError"); | ||
} | ||
} | ||
} | ||
} | ||
}() | ||
})(); | ||
""" | ||
|
||
public typealias ErrorCallback = (_ errorMessage: BridgeError) -> Void | ||
public typealias Callback = (_ responseData: BridgeData) -> Void | ||
|