Skip to content

Instantly share code, notes, and snippets.

@danrasmuson
Created September 30, 2016 20:24
Show Gist options
  • Save danrasmuson/cb7b7adbdc9d8947199456d534041040 to your computer and use it in GitHub Desktop.
Save danrasmuson/cb7b7adbdc9d8947199456d534041040 to your computer and use it in GitHub Desktop.

Codigo is going to send us a json string that represents the methods of an object

{
  "Marker":{
    "getLatLng":{"tunnelFuncId":1378085958112},
    "unproject":{"tunnelFuncId":255555924361.6694},
    "project":{"tunnelFuncId":110443166753.5969},
    "addMarker":{"tunnelFuncId":367293096637.76416}
  }
}

We will translate that json object into a javascript object.

const Marker = {
  getLatLng: ()=>{...},
  unproject: ()=>{...},
  project: ()=>{...},
  addMarker: ()=>{...}
}

When the javascript calls a function, it will return an observable and send a message to Codigo asking for that action to be invoked and for the response to be delievered (callId).

getLatLng: () => {
  var callId = getRandomId();
  callObjectiveC(1378085958112, callId);
  return {
    subscribe: (next, error, complete) =>
     callFunctionWhenCallIdIsResolved(next, error, complete, callId)
  }

}

Now Codigo got a message from the javascript. "Please call the function with the id 1378085958112 and return the response to callId"

invoke(1378085958112).then((response) => sendResponseToJavascript(callId, response))

Back to javascript

newMessages(function(callId, response){
  var sendResponseToGetLatLng = findCallId(callId)
  sendResponseToGetLatLng(response)
})

Here is what the end call should loook like

marker.getLatLng().subscribe((latLng) => console.log(latLng))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment