<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kureev Alexey on Medium]]></title>
        <description><![CDATA[Stories by Kureev Alexey on Medium]]></description>
        <link>https://medium.com/@kureevalexey?source=rss-fef7a1c40734------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ft9JJQdqr409VsqkN5csgg.jpeg</url>
            <title>Stories by Kureev Alexey on Medium</title>
            <link>https://medium.com/@kureevalexey?source=rss-fef7a1c40734------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 05 Jun 2026 23:08:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kureevalexey/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[React Native Meets SwiftUI]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/better-programming/react-native-meets-swiftui-d1606a8e1681?source=rss-fef7a1c40734------2"><img src="https://cdn-images-1.medium.com/max/2401/1*WGHWviOG48L6VENjYLYXEA@2x.png" width="2401"></a></p><p class="medium-feed-snippet">Write a proxy that allows you to use SwiftUI in React Native apps</p><p class="medium-feed-link"><a href="https://medium.com/better-programming/react-native-meets-swiftui-d1606a8e1681?source=rss-fef7a1c40734------2">Continue reading on Better Programming »</a></p></div>]]></description>
            <link>https://medium.com/better-programming/react-native-meets-swiftui-d1606a8e1681?source=rss-fef7a1c40734------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1606a8e1681</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kureev Alexey]]></dc:creator>
            <pubDate>Wed, 12 Feb 2020 22:17:39 GMT</pubDate>
            <atom:updated>2020-02-14T00:15:43.906Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Query Components with Apollo]]></title>
            <link>https://medium.com/apollo-stack/query-components-with-apollo-ec603188c157?source=rss-fef7a1c40734------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec603188c157</guid>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Kureev Alexey]]></dc:creator>
            <pubDate>Wed, 13 Dec 2017 11:43:36 GMT</pubDate>
            <atom:updated>2017-12-23T20:57:52.195Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of months ago, we started to use Apollo Client (+React Native) at <a href="https://werkspot.nl">Werkspot</a>. From the very beginning, Apollo overwhelmed us by the simplicity and flexibility it provides. It inspired us to create our own vision about technology and experiment with various approaches to data fetching that we think are worth sharing.</p><p>Enjoy the ride! 🚀</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jWjWcYFrb7dw1PSyNssrhQ.jpeg" /><figcaption>Apollo + Query Components hype rocket!</figcaption></figure><h3>Anatomy of Query Components</h3><p>One of the very powerful concepts that we found for ourselves with Apollo is Query Components. These components are meant to express a domain-specific data logic in a clear, declarative way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2deaab5854bbd9986ab1229849c7da2/href">https://medium.com/media/e2deaab5854bbd9986ab1229849c7da2/href</a></iframe><p>CitiesQuery component is an oversimplified example of an average Query Component. It exports a Component, enhanced by a graphql wrapper for fetching data. After Apollo takes care of fetching and mapping props to query parameters, query component uses “function-as-a-child” approach to expose a child-independent API with injected data.</p><p>Comparing to the standard approach where you add a graphql wrapper to your higher-order components, query components have a set of advantages:</p><ul><li><strong>Testability</strong>. Unless you have a very good reason, you can test your components without any data management logic.</li><li><strong>Separation of concerns</strong>. From now on, higher-order components are no longer concerned about the way data is fetched/pushed to the server.</li><li><strong>Abstraction over data management layer</strong>. Frankly speaking, it doesn’t really matter which library you use under the hood. Query Components abstracts out your data management layer so you can focus on business logic and change underlying implementation if needed.</li></ul><p>Once a Query Component is defined, it can be used as any other component in your application:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/082c73f70cdbb04d574681101cf52a79/href">https://medium.com/media/082c73f70cdbb04d574681101cf52a79/href</a></iframe><p>A nice side effect of decoupling higher-order components into Query Components is minimizing bloat. If the data management layer grows, it won’t affect presenter components unless they have to reflect new data properties.</p><h3>Mutations with Query Components</h3><p>Mutations are not that straightforward as queries. When a query component is mounted, Apollo immediately tries to fetch. However, when we talk about mutations, there is no need (in 99% of our cases) to perform any mutations at the mount time. This idea brings us to the point that we should have a way to control a moment when we execute our mutations. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80a745f18367b45380174e3c5a1b5b62/href">https://medium.com/media/80a745f18367b45380174e3c5a1b5b62/href</a></iframe><p>Therefore, you can use this component in a similar way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3599ffac47a0d9ec16a31083ae2dcddb/href">https://medium.com/media/3599ffac47a0d9ec16a31083ae2dcddb/href</a></iframe><p>So the concept is the same: Query Component receives a bunch of props that are later on used as parameters for the mutation query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AWcvWxN7S2ykK0bvKAVVVA.jpeg" /><figcaption>Testing Query Components like a pro 😎</figcaption></figure><h3>How to test Query Components?</h3><p>When it comes to testing, Apollo-based components have a bunch of features that we need to take into account. The most complicated one is related to React context and Apollo Client reference in particular. All components that intent to use Apollo should be put into the subtree of &lt;ApolloProvider /&gt; which gives them a reference to the client through the React context. Hopefully, if we use Enzyme and shallow rendering, we can supply context object as one of the options.</p><h4>Assertions</h4><p>One of the very first things we would like to test is that every Query Component is able to produce variables, required by a query. To ensure that, we check a shape of resulting variables.</p><p>Another assertion that we would like to make is about response shape. In other words, we check if given data.x complies with the shape of the mock that was passed to the fake client during initialization.</p><p>And the last, but not least: query component should call a function that we pass as a child with the data pulled from Apollo. Altogether, the test suit looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91377746e241b22ca69545f4198a1a98/href">https://medium.com/media/91377746e241b22ca69545f4198a1a98/href</a></iframe><p>The very last uncovered function is makeFakeClient. It is a self-written helper that generates an Apollo Client with a fake link:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdb81af8234c176620b0add0997a5e21/href">https://medium.com/media/cdb81af8234c176620b0add0997a5e21/href</a></iframe><p>This should be more or less it. I didn’t cover mutation components testing, but it isn’t anyhow different. The only thing I can foresee is an additional assertion that execute handler triggers Apollo fetcher, but I’ll leave it to the reader.</p><h3>Where to go from here</h3><ul><li><a href="http://engineering.khanacademy.org/posts/creating-query-components-with-apollo.htm">A great blog post from Khan Academy about their approach writing Query and Mutation components</a></li></ul><p>Don’t hesitate to ask questions, challenge this approach and share your ideas about Apollo and GraphQL overall. You can always find me on <a href="https://twitter.com/kureevalexey">Twitter</a> or Discord (kureev on #reactiflux).</p><p>Oh yeah, and last but not least: if you like this or any other of my articles, don’t hesitate to <a href="https://www.patreon.com/kureevalexey">become a part of it</a>! Your support is greatly appreciated!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec603188c157" width="1" height="1" alt=""><hr><p><a href="https://medium.com/apollo-stack/query-components-with-apollo-ec603188c157">Query Components with Apollo</a> was originally published in <a href="https://medium.com/apollo-stack">Apollo GraphQL</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Network layer in React Native]]></title>
            <link>https://medium.com/dailyjs/network-layer-in-react-native-eec841f11861?source=rss-fef7a1c40734------2</link>
            <guid isPermaLink="false">https://medium.com/p/eec841f11861</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Kureev Alexey]]></dc:creator>
            <pubDate>Sun, 17 Sep 2017 21:37:08 GMT</pubDate>
            <atom:updated>2019-03-10T01:13:20.683Z</atom:updated>
            <content:encoded><![CDATA[<p>This September started for me with a great experience <a href="https://www.youtube.com/watch?v=CghyeXGNzig">giving a talk</a> about network layer in React Native at <a href="https://react-native.eu/">React Native Europe conference</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FIMN8bDmlfog%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIMN8bDmlfog&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FIMN8bDmlfog%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/abe6a9487aadca87a51b6b337c5b8ca5/href">https://medium.com/media/abe6a9487aadca87a51b6b337c5b8ca5/href</a></iframe><p>Unfortunately, not everyone was able to attend the conference and/or listen to my talk, so for those who are interested, I’d like to provide an article explaining the concept I shared.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XAHm_Ib6imdqQC8YqeuDPw.jpeg" /></figure><h3>Motivation</h3><p>Nowadays, it is hard to imagine a mobile application without any kind of network communications. Currently, my phone contains more than 40 applications, but there is only one that doesn’t use any of them — calculator. For me it is a solid indicator that it is almost inevitable to avoid writing network-specific code in an average React Native app.</p><p>In this article we’ll cover all networking primitives that React Native expose to JavaScript, make a round trip request and discuss most common network issues you may encounter using the platform.</p><h3>Meet our heroes!</h3><p>React Native ships with three network APIs available by default: <em>fetch, XMLHttpRequest </em>and <em>WebSocket. </em>There is one thing about all of them that some developers may not know: neither of these APIs is a part of ECMAScript standard which means that JavaScript doesn’t have any standardised ways of transmitting packages over the wire.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X2e4iIy7dMshdUwPyM3WFw.jpeg" /><figcaption>fetch, XMLHttpRequest and WebSocket APIs</figcaption></figure><p>Therefore, if we want to perform network requests from JavaScript, we need to extend its runtime by a custom functionality implemented by the underlying host platform (which is React Native in our case).</p><blockquote>There is a chance that you are not familiar with terms “runtime” and “host platform”: a runtime represents a complete JavaScript execution environment which means it contains all the APIs that are available for developer. Host platform is a platform that embeds JavaScript language and therefore can extend its runtime (by adding custom variables and functions to it).</blockquote><p>If we oversimplify JavaScript runtime, it may look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DxKoPpC0G156IWbT2CEeJg.jpeg" /><figcaption>Oversimplified JavaScript runtime. JS API consists of a custom host-platform API and primitives described in the ECMAScript standard (JS API)</figcaption></figure><p>So it turns out that React Native should supply <em>fetch</em>, <em>XMLHttpRequest</em> and <em>WebSocket</em> APIs to JS runtime. Although, there are no such libraries that would work in appliance with <a href="https://spec.whatwg.org/">WHATWG specifications</a> (e.g. mimic web behaviour) for React Native target platforms (iOS and Android). That implies React Native should’ve re-implement existing standards and it did.</p><h4>XMLHttpRequest</h4><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest">XMLHttpRequest</a> (XHR) is an object that is used to interact with remote resources. It was originally designed by Microsoft long long time ago and later on adapted by other browser vendors. Despite the naming, nowadays this object is capable of transmitting different data types (not only XML) and work over various protocols based on TCP.</p><p>In React Native, XMLHttpRequest consists of two parts: front-end and back-end. The front-end part is something that you can interact with in JavaScript. It is capable of sending appropriate request data to the XHR back-end which is responsible for processing network request and using underlying mobile platform.</p><p>The back-end part of XMLHttpRequest is called <a href="https://github.com/facebook/react-native/tree/master/Libraries/Network">Networking</a>. It is an abstraction on top of iOS and Android operating systems that provides a unified API layer that front-end can work with. Implementation of this layer is different for each platform. While on iOS Networking module takes advantage of built-in <a href="https://developer.apple.com/documentation/foundation/urlsession">URLSession</a>, Android version works on top of third-party <a href="http://square.github.io/okhttp/">OkHTTP</a> library that supplies a certain abstraction for <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/net/index.html">low-level Java networking API</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9aJejrZfWoAvSn6HukC-Vg.jpeg" /><figcaption>React Native’s “Networking” module takes care of request data</figcaption></figure><p>Therefore, once developer make a request, it goes through React Native’s Networking module and underlying operating system right to the requested resource.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*902H0B44anAsxloEsAve3A.jpeg" /><figcaption>Flow of the XMLHttpRequest in React Native</figcaption></figure><h4>Fetch</h4><p>Fetch is a modern concept equivalent to XMLHttpRequest. It offers a lot of the same functionality as the XMLHttpRequest, but is designed to be more extensible and efficient. It operates <a href="https://developer.mozilla.org/en-US/docs/Web/API/Request">Request</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/Response">Response</a> objects that can be re-used in the following requests.</p><p>In modern browsers, this API is a part of <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope">WindowOrWorkerGlobalScope</a> mixin that extends global JavaScript scope, however in older browsers and React Native (in order to not to re-implement a new API for both platforms) it uses <a href="https://github.com/github/fetch">GitHub’s whatwg-fetch polyfill</a> that relies on XMLHttpRequest implementation, so it implicitly uses React Native’s Networking module under the hood. That implies some nuances that we need to be aware of. For instance, one of the known Networking issues is impossibility of transferring any binary data over the wire that currently leads to workarounds (like working with base64 representation of binary data) and/or third-party solutions (like <a href="https://github.com/wkh237/react-native-fetch-blob">react-native-fetch-blob</a>). You can learn more about these and other implications in the second part of <a href="https://www.youtube.com/watch?v=CghyeXGNzig">my ReactNativeEU talk</a>.</p><h4>WebSocket</h4><p>WebSocket is a communications protocol, providing full-duplex communication channels over a single TCP connection.</p><p>You might already use WebSockets for developing your React Native applications. Every time you start an app in development mode, it creates a WebSocket connection between packager and the app itself, so every time you edit your JS files (or other static assets), it sends a patch signal to the app so it can react to the changes.</p><p>As far as WebSocket is somewhat different from a standalone request, there is a separated module called WebSockets that performs as a back-end for the technology. Not hard to guess, that by analogy with XMLHttpRequest, WebSocket also has two different implementations across operating systems. Android is being consistent using OkHTTP library while iOS uses a <a href="https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/Libraries/WebSocket/RCTSRWebSocket.m">self-written implementation</a> on top of <a href="https://developer.apple.com/documentation/foundation/nsstream">NSStream</a>.</p><h3>Networking issues</h3><p>The second part of this article is dedicated to cover the most common networking issues in React Native. Debugging network requests, sending binary data over the wire and credentials management — today we’ll unveil them all.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xA-oEwMq049sIu1_QKNT8A.jpeg" /><figcaption>Networking issues in React Native may be tricky to debug, but there is always a way ;)</figcaption></figure><h3>Debugging network requests</h3><p>If you ever developed a mobile application using React Native, you might notice that it exposes a great way of debugging JavaScript code — Chrome Developer Tools (CDT). By a couple of taps your JavaScript can be run in a Chrome service worker that provides a wide set of debugging tools. There is a way to log runtime data to console, profile memory, use breakpoints — almost everything you might need. The only one thing went missing — debugging network requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*teuVda3pI4KfDPawncoFHw.jpeg" /><figcaption>Trying to trace remote request using Chrome Developer Tools</figcaption></figure><p>Due to implementation specifics, CDT is not capable of handling network requests. If we consider web platform, XHR, fetch and WebSocket APIs are backed by the browser itself which gives it an advantage of having spies that makes it possible to trace network requests. However, React Native doesn’t use Chrome to make networking requests (all network requests goes through the Networking module no matter what), so it can’t trace them.</p><p>There is a way to avoid using React Native networking polyfill (so all requests in CDT will go through the Chrome platform and therefore will become traceable), but that would bring inconsistencies between your runtime environment in CDT and your app. For instance, fetch will become capable of sending binary data in CDT, but throw an error in RN app. This is a thin ice and therefore not recommended to use. Hopefully, there are many alternative ways you can use to debug network requests.</p><h4>Using TCP traffic monitoring tools</h4><p>This approach works if you develop using an emulator. All your requests will go through your PC/Mac which means they will be regular HTTP requests* and therefore, become traceable.</p><blockquote>By “regular HTTP requests” I mean HTTP requests that are similar to the requests we make in our browsers. HTTP protocol is based on TCP, so most of the traffic monitoring tools will be capable to trace them down.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T2ldPj_ZVwDlA4OSwAobPQ.jpeg" /><figcaption>Monitoring TCP connection</figcaption></figure><p>There is a way of using <a href="https://www.charlesproxy.com/">CharlesProxy</a>, <a href="https://www.wireshark.org/">Wireshark</a> or their analogs to get a list of outcoming TCP connections. One (or more) of these connections will be related to the app you’re trying to debug.</p><p>Monitoring TCP traffic becomes a little bit more complex when we start developing using real device. In order to monitor traffic from mobile, you need to configure a proxy (it is possible with tools like CharlesProxy (<a href="https://www.charlesproxy.com/documentation/faqs/using-charles-from-an-iphone/">docs</a>), mitmproxy (<a href="http://jasdev.me/intercepting-ios-traffic">docs</a>) and some others) and manually re-route application requests through it.</p><h4>Using XHRInterceptor / WebSocketInterceptor</h4><p>There is another way of tracing your remote requests: by using interceptors. React Native ships with two interceptors for XMLHttpRequest and WebSockets accordingly. Each one is meant to be used for tracing an appropriate API: <a href="https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/Libraries/Network/XHRInterceptor.js">XHRInterceptor</a> for XMLHttpRequest and fetch requests, <a href="https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/Libraries/WebSocket/WebSocketInterceptor.js">WebSocketInterceptor</a> for WebSocket connections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FY7V_aYZV0KuCfZKHAC_PQ.jpeg" /><figcaption>Using XHRInterceptor to trace XHR/fetch requests</figcaption></figure><p>These APIs are not documented or exposed as a part of the object, exported by react-native package. If you would like to use either of them, there is no other way than relying on current relative import path (which may change in upcoming versions of React Native).</p><p>Hopefully, you don’t have to do any of these by your own. There is a pretty great tool brought to us by <a href="https://infinite.red/">Infinite Red</a> called <a href="https://github.com/infinitered/reactotron">Reactotron</a>. It is a swiss knife for React(+Native) development that provides lots of helpful functionality like debugging network requests, redux actions, sagas and much much more. You can easily find a full list of features on <a href="https://infinite.red/reactotron">its website</a>.</p><p>At this moment, the only solution React Native provides by default is “Networking monitor” that you can enable by shaking your device in development mode. However, the functionality of this tool is very limited to a basic tracing of the request. Hopefully, there are lots of approaches in the community that may slightly improve your experience in this area.</p><h3>Sending binary data over the wire</h3><p>Another big topic related to networking issues is about transmitting binary data in React Native. It is not a secret that React Native never supported transferring files out of box. We use various workarounds like converting binary data to base64 strings or third-party solutions like <a href="https://github.com/wkh237/react-native-fetch-blob">react-native-fetch-blob</a>, but the underlying issue stays unsolved. This part will unveil some internals of the File/Blob management, consider workarounds and potential solutions we can apply to React Native to bridge the gap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6BoJbhmRXzH0g6E0PSRkuQ.jpeg" /></figure><blockquote>Default JavaScript runtime doesn’t contain File/Blob constructors which makes it impossible to represent binary data. However, all modern browsers enhance a default runtime environment by adding their custom implementations so you can work with file inputs and binary data straight from JS. With these implementations actual binary data is stored on the browser/OS side and the only part which is exposed to developer is a reference wrapped into File/Blob object. Unfortunately, JS environment (and Networking module that exposes provides backend for XHR and fetch) of React Native doesn’t support either of these objects, so there is no “default” way of getting a reference to the file from your code.</blockquote><h4>Convert binary to base64</h4><p>Base64 is a group of similar <a href="https://en.wikipedia.org/wiki/Binary-to-text_encoding">binary-to-text encoding</a> schemes that represent <a href="https://en.wikipedia.org/wiki/Binary_data">binary data</a> in an <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> string format by translating it into a <a href="https://en.wikipedia.org/wiki/Radix">radix</a>-64 representation.</p><p>As far as React Native is not capable of transferring non-primitive data over the bridge (so only serialisable data structures), the only way of getting there is to convert binary format to a string. Base64 sounds like a good solution until we consider a round-trip request:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lLSQi6PA4iylV9oAx96o5w.jpeg" /><figcaption>Round-trip of a base64 request</figcaption></figure><p>Imagine we request an image from our server. Not just a URI, but the file itself. Request goes from JS thread through React Native platform to underlying OS and, finally, to the requested resource. That resource (your API, for instance) should respond with a base64 string that will be returned to JavaScript by replaying the request flow in reverse order.</p><p>Once response hits the bridge, JavaScript thread should allocate memory, sufficient for representing it. That implies certain performance issues that we want to avoid writing our React Native applications. Most of these problems are outlined in the great article of <a href="https://medium.com/@talkol">Tal Kol</a> <a href="https://medium.com/@talkol/performance-limitations-of-react-native-and-how-to-overcome-them-947630d7f440">“Performance limitations of React Native and how to overcome them”</a>.</p><p>Furthermore, we also need to render an image. Therefore we supply base64 string as a URI parameter for React Native’s &lt;Image /&gt; tag. At the moment of runtime, JSX tree that React Native compose will be sent to the native thread where the native UI lives, therefore the base64 string we supply will be traveling over the bridge again causing memory spikes and additional performance issues.</p><p>Now, when we considered potential issues of using base64, I’d like to focus your attention on the alternative approaches we can use for transmitting binary data in React Native.</p><h4>Can we do better?</h4><p>Of course, the problem outlined above is not new and probably every developer who ever had to transmit binary data faced it. This problem inspired many developers to give birth to numerous third-party solutions we can use. Although, the underlying issue is still not solved.</p><p>The only solid solution for this problem would be to bridge the missing bits of the standard. Hopefully, some time ago <a href="https://twitter.com/satya164">Satyajit Sahoo</a> and <a href="https://twitter.com/grabbou">Mike Grabowski</a> from <a href="http://callstack.io">Callstack.io</a> <a href="https://github.com/facebook/react-native/pull/11417">created a PR</a> to that add these bits to the WebSocket module. After six months of discussions, rebases and reviews, it has been merged and from now on, the latest versions of React Native supports transmitting binary data over WebSocket protocol (🎉!). Although, it solves only a part of the problem: you still can’t send binaries using Networking module. This part of the issue is still under consideration, but <a href="https://github.com/facebook/react-native/pull/11573">you can keep an eye on the progress of the second PR here</a>. Once it is merged, the problem of transferring binary data in React Native will be closed for good.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZrJKJqBsksWd-8uKM9OvgA.png" /></figure><h3>Conclusion</h3><p>React Native is a complex platform in terms of composition of different patterns, libraries and architectural solutions. Network layer is only a one of many interesting topics that powers the platform, although for some reason not covered by the community at all. Hope this article shed some light on the network layer and pushed your knowledge of the platform a little bit further.</p><p>Have any questions left? You are very welcome to ask them on <a href="https://twitter.com/kureevalexey">Twitter</a> or by commenting on the article below.</p><p><em>All these amazing illustrations were drawn by Nicolas: </em><a href="mailto:shadow.x.q84@gmail.com"><em>shadow.x.q84@gmail.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eec841f11861" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dailyjs/network-layer-in-react-native-eec841f11861">Network layer in React Native</a> was originally published in <a href="https://medium.com/dailyjs">DailyJS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create your own native bridge]]></title>
            <link>https://medium.com/@kureevalexey/how-to-create-your-own-native-bridge-bfa050e708fc?source=rss-fef7a1c40734------2</link>
            <guid isPermaLink="false">https://medium.com/p/bfa050e708fc</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Kureev Alexey]]></dc:creator>
            <pubDate>Thu, 20 Oct 2016 10:41:28 GMT</pubDate>
            <atom:updated>2016-10-21T10:41:57.624Z</atom:updated>
            <content:encoded><![CDATA[<h4>Chapter 2: JSVM and the first adventure</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W-pdzx3uYzA8gDqNtoqBNw.png" /></figure><p><strong>Previous article:</strong> “<a href="https://medium.com/@kureevalexey/how-to-create-you-own-native-bridge-93a8d4a40bd2#.ql31jn4md">How to create your own native bridge. Chapter 1: Designing an architecture</a>”.</p><p>What do you know about JavaScript engines? Did you ever try to embed one? In this chapter I’m going to guide you through the dark spooky forest of hosted objects, virtual machines, interpreters and other evil spirits that we call JavaScript engines.</p><p>I know that it may look scary, but don’t forget that a journey of a thousand miles starts with a single step.<strong> </strong>In our case it’ll be a step into code parsing. At this stage our JavaScript source is getting converted to a structure called <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">abstract syntax tree (AST)</a>. There are many different techniques to parse your code (<a href="https://en.wikipedia.org/wiki/LL_parser">LL(k)</a>, <a href="https://en.wikipedia.org/wiki/LR_parser">LR(k)</a> etc) and convert it to AST, but for the sake of simplicity I want to keep it out of this article.</p><blockquote>Although, for those who are interested, I got all my knowledge about parsers (and compiler theory in general) from the <a href="https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools">Dragon Book</a>. I just can’t recommend it enough.</blockquote><p>However, I will tell you more about the abstract syntax tree concept. An AST is a structural representation of your code in a tree format where every node represents a language construct (e.g. expressions, statements, variables, literals etc). You can play with it using <a href="http://esprima.org/demo/parse.html">ESPrima praser demo page</a> or <a href="https://astexplorer.net/">ASTExplorer</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y-MLbkNfR9FXLJd8pEUNjA.png" /><figcaption>Abstract syntax tree inside ChakraCore forest</figcaption></figure><p>First of all, a JavaScript engine has to parse (<a href="https://en.wikipedia.org/wiki/Tokenization_%28lexical_analysis%29">tokenize</a>) a source code to produce an array of tokens. These tokens are supplied to a <a href="https://en.wikipedia.org/wiki/Syntax_%28programming_languages%29">syntactic analysis</a> tool<strong> </strong>that builds an AST based on a given <a href="https://en.wikibooks.org/wiki/Introduction_to_Programming_Languages/Grammars">language grammar</a>. Once an AST is built, JavaScript engine will compile it either to machine code directly (V8 behaves this way) or to <a href="https://en.wikipedia.org/wiki/Intermediate_representation">intermediate representation</a>, which is an another level of abstraction over machine code.</p><p>In this experiment I committed to use ChakraCore which uses a <a href="https://en.wikipedia.org/wiki/Bytecode">bytecode</a> as it’s intermediate language. But it can’t be executed straight away: our target machine doesn’t know how to process it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*faSobwhCQmFyV1ss3yYdOQ.png" /><figcaption>A bytecode river</figcaption></figure><p>In order to bridge the gap, ChakraCore includes a bytecode interpreter. On ChakraCore’s bytecode each instruction starts with a 1-byte bytecode that represents which operation should be executed (a.k.a. opcode), and therefore the interpreter may have up to 256 instructions. Some bytecodes may take multiple bytes, and may be arbitrarily complicated</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RKhgFyjsBzPfgy-a-s2gQA.png" /><figcaption>On the way to the “Interpreter” ship</figcaption></figure><p>That was a very short overview of the JS execution flow. Probably you noticed that in this article we don’t talk about inner code optimizations (like <a href="https://en.wikipedia.org/wiki/Just-in-time_compilation">JIT</a> or <a href="https://en.wikipedia.org/wiki/Ahead-of-time_compilation">AoT</a>). Although it’s a very interesting topic, I decided to omit it in order to make this article easier to grasp.</p><h3>Embedding ChakraCore</h3><p>Now, when we have some knowledge about ChakraCore, we can start embedding it into our application. So first of all we need to install ChakraCore dependencies:</p><ul><li>$ xcode-select --install</li><li>$ brew install cmake icu4c</li></ul><p>And ChakraCore itself. I will show how to include it as a submodule:</p><ul><li>$ mkdir modules &amp;&amp; $_</li><li>$ git submodule add <a href="https://github.com/Microsoft/ChakraCore">https://github.com/Microsoft/ChakraCore</a></li></ul><p>Then build it from the source:</p><ul><li>$ cd ChakraCore</li><li>$ ./build.sh --static --icu=/usr/local/Cellar/icu4c/<em>&lt;version&gt;</em>/include --test-build -j=2</li></ul><p>Once these steps are done, we can include it into our application:</p><ul><li>Open the project we created in the previous chapter</li><li>Select ExampleBridge project in the Project navigator and switch to the target:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dkn5FM1KLH2l1_Zz0ZIWfA.png" /><figcaption>ExampleBridge target is selected</figcaption></figure><p>Link your compiled ChakraCore files:</p><ul><li><em>libChakra.Pal.a</em></li><li><em>libChakra.Common.Core.a</em></li><li><em>libChakra.Jsrt.a</em></li></ul><p>And your icu4u files (from <em>/usr/local/Cellar/icu4c/&lt;version&gt;/include</em>):</p><ul><li><em>libicudata.a</em></li><li><em>libicui18n.a</em></li><li><em>libicuuc.a</em></li></ul><p>Your result should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*kMnzKlL0ASDeR9khPo_e2Q.png" /><figcaption>All libraries are linked properly</figcaption></figure><p><strong>Note: </strong><a href="https://github.com/Microsoft/ChakraCore/issues/1532#issuecomment-244887604">order of these dependencies is very important!</a></p><h3>Getting started with ChakraCore</h3><p>We come to the very interesting part of our journey. To the place where we need all our knowledge about JavaScript engines and the way they work. Yes, dear reader, you’re right! We’re about to start using ChakraCore!</p><h4>Bootstrapping ChakraCore</h4><p>First of all, open your ChakraProxy.m file and find the NSLog statement that we added in the previous chapter. Let’s replace it by something that makes more sense:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/307ed797a87c1a260927a46b0c6dbf07/href">https://medium.com/media/307ed797a87c1a260927a46b0c6dbf07/href</a></iframe><p>I don’t expect you to be familiar with Objective C, so let me guide you through this code:</p><ul><li>Line 6–8: Read a content of main.js file from the bundle. <a href="https://developer.apple.com/reference/foundation/bundle">More about bundles here</a>.</li><li>Line 14: ChakraCore is an engine, written in C++, so it doesn’t understand NSString format. However, we used Objective C approach to read a file content and now we have to deal with NSString to const char* conversion. So, that’s how we do it.</li><li>Line 24: That’s my custom C++ function. I use it in order to reduce a cognitive payload of run method and move a function description to the different section of this article.</li></ul><p>Hope it doesn’t look very complex to you. Anyway, there are still some unclear places in this code:</p><h4>Runtime</h4><p>Runtime (line 17) represents a complete JavaScript execution environment. Each runtime that is created has its own isolated garbage-collected heap and, by default, its own just-in-time (JIT) compiler thread and garbage collector (GC) thread. (see <a href="https://github.com/Microsoft/ChakraCore/wiki/JavaScript-Runtime-(JSRT)-Overview#concepts">ChakraCore JSRT overview</a>)</p><h4>Execution Context</h4><p>Context (line 20–21) is an execution environment that allows separate, unrelated, JavaScript applications to run in a single instance of runtime. You must explicitly specify the context in which you want any JavaScript code to be run. (see <a href="https://github.com/v8/v8/wiki/Embedder&#39;s%20Guide#contexts">V8 Embedder’s Guide</a>)</p><h4>Extending global scope</h4><p>Now, when ChakraCore is set up, it’s time to build a bridge. In the previous chapter I briefly mentioned that we’re going to use hosted objects to expose C++ functions to JavaScript. So let’s write a function that will do it for us:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c9bd043fc34c1e307d760f5d32143f9/href">https://medium.com/media/3c9bd043fc34c1e307d760f5d32143f9/href</a></iframe><p>If you rewrite the code above to JavaScript, it’ll look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d80aca98d94a22a31c9724b43ff01717/href">https://medium.com/media/d80aca98d94a22a31c9724b43ff01717/href</a></iframe><p>As you can see from the snippet above, we use a special ChakraCore function JsGetGlobalObject to get context’s “global” object. Once it is there, we extend it by a custom hosted object called “bridge” to expose our C++ “render” function to JS. This approach is similar to the one we used in our web applications back in a day. I’m talking about namespaces, when you move all your application modules under window.app or a similar object in order to scope them by an organic global variable. In this code we do the same, but instead of modules we expose a custom C++ function. You are probably wondering why in the code above I use ChakraUtils. It’s a self-written wrapper over a standard ChakraCore API. I won’t go though the code, but you can find my implementation on the github.</p><p>However, the “render” function is still has to be defined. It should fit a JS function interface and perform an async dispatch to the main thread. The simplest implementation will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/819ca0aab35ae0cf2ba96420f6c4cd30/href">https://medium.com/media/819ca0aab35ae0cf2ba96420f6c4cd30/href</a></iframe><p>Try to not to be overwhelmed by the amount of function parameters, in this article we’re about to use only one — arguments. The code above will read the first two parameters passed to the function from JavaScript and invoke an Objective-C AppDelegate method called renderElementOfType . No callbacks, no return values. Let’s keep it simple for now.</p><p>One thing, that may make you feel confused is a dispatch_async call. We use this function in order to schedule a block (statement inside ^{}) to be executed in the main dispatch queue (see <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html">GCD documentation for details</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tAR8p-txDEKc7v4iYXqWAw.png" /><figcaption>Traveling to the Main Thread island</figcaption></figure><p>Now, once “render” function is invoked, it sends a block to the main thread. Inside the block we have a renderElementOfType call, which is responsible for a final element creation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74d1f022309262705bac3c562a27479d/href">https://medium.com/media/74d1f022309262705bac3c562a27479d/href</a></iframe><p>You may notice, that we hardcode window and don’t even use a name property. We’ll get back to this part in the third chapter. Other than that, this code should be pretty straight-forward: we create a CGWindow instance and call openWithSize method with a given params.</p><p>But when you call this function, you don’t see a window. Why? Because window is a local variable and it will be deallocated once you leave the function scope. So in order to see a window, we have to store a reference to this window somewhere outside of the function. Let’s create a UIManager class that will manage our UI references:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/391d74b96138ec7959157dc772a1e477/href">https://medium.com/media/391d74b96138ec7959157dc772a1e477/href</a></iframe><p>You’re probably wondering what does a sharedInstance mean? It’s one of the ways to <a href="http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/">create a singleton in Objective C</a>. It’s not necessary to make UIManager a singleton, but to me it feels like a right way to do it.</p><p>This class has the only one public API method: addValue. In the next chapter I’m going to add some more (like deleteValue), but let’s keep it as simple as possible for now.</p><p>Let’s update our renderElementOfType function to start using our storage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/496ca19816b7a812425539f3aee07c42/href">https://medium.com/media/496ca19816b7a812425539f3aee07c42/href</a></iframe><p>So once we get a UIManager instance, we generate a uuid for our window and put it in the storage by addValue. Inside the manager we generate a strong reference to the given object which prevents it from being deallocated (see <a href="https://en.wikipedia.org/wiki/Automatic_Reference_Counting">ARC</a>).</p><p>And you know what? That’s it! If you create a main.js file, add it to the bundle and type something like bridge.render(&#39;Window&#39;, 400, 400);, you’ll see a 400x400 window at the application startup!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*UBUX7yC9MJdbF_uqPL8T3A.png" /><figcaption>Empty 400x400 window, created from JavaScript</figcaption></figure><p>Buy hey, it has nothing about React yet! What do we need to provide for a React-like interface to our platform? How to return references from Objective-C to JS? All these questions will be answered in the Chapter 3!</p><p>In the meanwhile, you can play around with the <a href="https://github.com/Kureev/ExampleBridge/releases/tag/2.0">code from this article</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bfa050e708fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create your own native bridge]]></title>
            <link>https://medium.com/@kureevalexey/how-to-create-you-own-native-bridge-93a8d4a40bd2?source=rss-fef7a1c40734------2</link>
            <guid isPermaLink="false">https://medium.com/p/93a8d4a40bd2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Kureev Alexey]]></dc:creator>
            <pubDate>Thu, 29 Sep 2016 15:57:06 GMT</pubDate>
            <atom:updated>2017-09-22T20:39:10.469Z</atom:updated>
            <content:encoded><![CDATA[<h4>Chapter 1: Designing an architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-7Oy6J14IjibHZJV-SzPQ.png" /></figure><p>I’ve been using React Native for a while before I started to dive into the codebase to see how it works. Since then I’ve made quite a few contributions to the project and even managed to become a part of react-native core team.</p><p>Although, one part of the project was truly mystical to me — how it bridges two different languages: JavaScript and Objective C. This lack of understanding gave me the motivation to do some research on the bridge and document my findings in these articles.</p><p>No doubt, it is pointless to write code without having a clear mental picture of the finished product (or a milestone). Architectural design is an important step of software development. I will try to guide you through the thought process and explain in detail what decisions were made and why.</p><h4>First iteration</h4><p>When you start a new project, it makes sense to determine a list of technologies you’re going to use. In my case they were:</p><ul><li>Objective C to manipulate Cocoa UI</li><li>C++ to work with JSVM</li><li>JSVM (V8 or Chakra)</li><li>JavaScript</li><li>React + custom renderer</li></ul><p>I assumed my main executable will be written in C++. It will run a JS engine with a patched global context (all major JS engines provides you a possibility to extend their built-in functionality by custom objects/functions). Don’t be afraid of “patched global context”, it means nothing but adding a few new functions written in C++ to your JS environment. Later on, we will talk about it in details.</p><p>Once we call one of these custom JS functions, it goes through the JSVM (JavaScript Virtual Machine) and invokes a C++ implementation, which in turn, triggers the Objective C code to construct a UI element. It may sound a bit complex, but don’t give up yet!</p><p>All these things can be run in the same (main) thread, but it’ll cause certain performance issues. To avoid that, we dedicate a new thread to handle Objective C ↔ UI jobs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g3g15w4bZSLfgTCn4YTTFQ.png" /></figure><p>Even though this approach makes sense, it doesn’t work.</p><p>The problem is that Apple doesn’t allow you to render your UI in non-main thread. Moreover, if UI can be rendered only in the main thread, it means that Objective C should be run in the same thread as well. Unfortunately, this approach ties your application’s entry point to the platform (or, at least, makes it really complicated to run it on the other platforms). But we have what we have. Let’s change our initial approach to fit this requirement.</p><h4>Corrections</h4><p>If we have to run a UI in the main thread, let’s do it! Instead of running a C++ program, we run a Cocoa application. When its bootstrapped, we spawn a new background thread with a JSVM on board. JSVM runs the main JavaScript file (your bundle) in order to get instructions to execute. As in the previous approach, if you have any proxied C++ functions in your JS code, JSVM will take care of them. Once one of those functions is called, it sends a command to the main thread to draw UI.</p><p>In the main thread, Objective C process is given a command and renders an appropriate interface element for it. If there are no errors, Objective C triggers a callback that has been passed in from C++ in order to call a function representing a callback from JS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VYH6suIpqOTgzmBzCAnmXQ.png" /></figure><h4>Final architecture</h4><p>Now, let’s try to combine all these together:</p><ul><li>We start a native Cocoa Application because we need to run UI in the main thread.</li><li>Right at the moment when our blank application is bootstrapped, we create a new thread with JSVM. Once it’s done, we run JavaScript. As I said above, we patch our JSVM context and expose some additional APIs to manipulate the user interface layer.</li><li>Once we receive a command from JS to draw UI, we dispatch it to Objective C. In it’s turn, Objective C parses the command and renders appropriate UI elements.</li><li>After that, Objective C invokes a sequence of callbacks in order to pass a return value to JS.</li></ul><h3>Building a platform foundation</h3><p>Time for the hands on experience! First of all, let’s create a blank OS X Cocoa Application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*Td9oYDFwUhvEPTw2hcSjag.png" /><figcaption>Mac OS X Cocoa Application creation window</figcaption></figure><p>Once this step is done, you have your foundation. Now, if you open your AppDelegate.m, you will find two application lifetime hooks: <em>applicationDidFinishLaunching</em> and <em>applicationWillTerminate. </em>Our application should spawn a new thread at the moment of creation, so let’s change our <em>applicationDidFinishLaunching </em>to do the trick:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/448dd62f397dd28df7c44c94ab29f37b/href">https://medium.com/media/448dd62f397dd28df7c44c94ab29f37b/href</a></iframe><p>Now I have to answer two questions:</p><ul><li>What is a <em>_jsvmThread </em>variable?</li><li>What does @<em>selector(runJSVMThread)</em> mean<em>?</em></li></ul><p><em>_jsvmThread</em> is nothing else but an instance variable that stores a reference to our new <em>NSThread</em>. We need this reference in order to execute our C++ callbacks in the proper thread.</p><p>Selector, <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Selector.html">according to Apple’s docs</a>, is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled.</p><p>In other words, we initialize a new <em>NSThread</em> and run <em>runJSVMThread</em> method of object <em>self</em> (which points to the current instance of the class) in it.</p><p>Here’s the body of <em>runJSVMThread</em> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bdb00d54c89ed1a73f59c2e41d7edfc/href">https://medium.com/media/5bdb00d54c89ed1a73f59c2e41d7edfc/href</a></iframe><p>As you can see, I decided to use ChakraCore instead of V8. There are some reasons for that:</p><ul><li>It took me 1 hour to compile V8 and 2 hours to run HelloWorld example</li><li>ChakraCore took me 10 mins to compile and run HelloWorld example</li></ul><p><strong>Side note:</strong> since we’re building a prototype, I would prefer better developer experience that I have with ChakraCore.</p><p>Right after <em>ChakraProxy</em> initialization we have a run loop. We use it here to keep our thread alive (otherwise it’ll die after <em>run</em> function is finished). If you want to know more about run loops, <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html">Apple docs</a> is a good place to start.</p><p>Now we have an application that starts, spawns a new thread and waits for commands. Next step would be to define a <em>ChakraProxy</em> class.</p><p>ChakraProxy is just a container which encapsulates Objective C and C++ code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25a1eb8277e06f183e03c4995b26132a/href">https://medium.com/media/25a1eb8277e06f183e03c4995b26132a/href</a></iframe><p>Of course, this code doesn’t do anything handy yet, but gives you a room to maneuver. In the next chapter we’ll add some fancy things like ChakraCore, basic bridging model and way, way more.</p><p>In the meanwhile, you can play with the code in <a href="http://github.com/Kureev/ExampleBridge">my github repository</a>.</p><p>Want to know more about runtimes, contexts, scopes and their implementation? Or maybe extend idiomatic JavaScript by adding your own C++ functions?</p><p>All these and even more awaits you in the <a href="https://medium.com/@kureevalexey/how-to-create-your-own-native-bridge-bfa050e708fc#.8dy2ry7ed"><strong>Chapter 2: JSVM and the first adventure</strong></a><strong>.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93a8d4a40bd2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>