Skip to content

Commit

Permalink
Inlined bridge.js, to avoid loading issues (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroloux authored Jul 24, 2024
1 parent f7bb753 commit ef72225
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 90 deletions.
4 changes: 0 additions & 4 deletions seatsio-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
15859824B00D0EB49C0C6913 /* Pods_seatsio_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 908BD004A0647EAEFBFB7746 /* Pods_seatsio_ios.framework */; };
3B40BA3822A7D49C00B9A507 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3B40BA3722A7D49C00B9A507 /* Assets.xcassets */; };
3BB8A4D82C2B1BFE009D913A /* JustBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB8A4D72C2B1BFE009D913A /* JustBridge.swift */; };
3BB8A4DF2C2B2046009D913A /* JSBridge.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 3BB8A4DE2C2B2046009D913A /* JSBridge.bundle */; };
4EA4AD6622A86F590030E989 /* SeatsioWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EA4AD6522A86F590030E989 /* SeatsioWebView.swift */; };
4EDEA15676C3C508BB4BBE4B /* SeatingChartSample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EDEAECDF76F62994037BC78 /* SeatingChartSample.swift */; };
4EDEA2B7249C0734B9AEE860 /* TicketType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EDEA64B09FF840D72C49C61 /* TicketType.swift */; };
Expand Down Expand Up @@ -49,7 +48,6 @@
3B40BA3722A7D49C00B9A507 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
3B40BA3C22A7D49C00B9A507 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3BB8A4D72C2B1BFE009D913A /* JustBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JustBridge.swift; sourceTree = "<group>"; };
3BB8A4DE2C2B2046009D913A /* JSBridge.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = JSBridge.bundle; sourceTree = "<group>"; };
4EA4AD6522A86F590030E989 /* SeatsioWebView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SeatsioWebView.swift; sourceTree = "<group>"; };
4EDEA0763144FD24206F519B /* CategoryFilter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoryFilter.swift; sourceTree = "<group>"; };
4EDEA099F1693CFC065B3A62 /* Pricing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Pricing.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -129,7 +127,6 @@
3B40BA2F22A7D49C00B9A507 /* seatsio-ios */ = {
isa = PBXGroup;
children = (
3BB8A4DE2C2B2046009D913A /* JSBridge.bundle */,
4EA4AD6522A86F590030E989 /* SeatsioWebView.swift */,
3B40BA3722A7D49C00B9A507 /* Assets.xcassets */,
3B40BA3C22A7D49C00B9A507 /* Info.plist */,
Expand Down Expand Up @@ -235,7 +232,6 @@
buildActionMask = 2147483647;
files = (
3B40BA3822A7D49C00B9A507 /* Assets.xcassets in Resources */,
3BB8A4DF2C2B2046009D913A /* JSBridge.bundle in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
78 changes: 0 additions & 78 deletions seatsio-ios/JSBridge.bundle/bridge.js

This file was deleted.

87 changes: 79 additions & 8 deletions seatsio-ios/JustBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ef72225

Please sign in to comment.