<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Philippe Riand</title>
    <description>The latest articles on Forem by Philippe Riand (@philriand).</description>
    <link>https://forem.com/philriand</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F327488%2Ffcb4fb20-8fed-4966-805d-cda6a824f4fa.png</url>
      <title>Forem: Philippe Riand</title>
      <link>https://forem.com/philriand</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/philriand"/>
    <language>en</language>
    <item>
      <title>LWC: A sample fetch data accessor</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Sun, 15 Mar 2020 20:13:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/lwc-a-sample-fetch-data-accessor-jgi</link>
      <guid>https://forem.com/salesforceeng/lwc-a-sample-fetch-data-accessor-jgi</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-LybG8GQeWJc/Xm6KOPZ2VJI/AAAAAAAAA4I/__L1qJb3yH4YghMxA0v0rdhdBXiWDxwHQCLcBGAsYHQ/s1600/fetch-wire.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UMaiE3yA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-LybG8GQeWJc/Xm6KOPZ2VJI/AAAAAAAAA4I/__L1qJb3yH4YghMxA0v0rdhdBXiWDxwHQCLcBGAsYHQ/s320/fetch-wire.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What would an LWC application be if it does not fetch data from a server? The browser now made this easy, particularly with the fetch function, or its polyfill for older browsers. The general pattern with LWC/Web Components is to fetch data in &lt;code&gt;connectedCallback()&lt;/code&gt;, once the component is added to the DOM. If this can be achieved with simple code, let see how we can make it even easier with wire adapters. In particular, we like to do the following:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have a simple syntax for calling the services.&lt;/li&gt;
&lt;li&gt;Do not hard code the full URL in the components, so we can easily, and globally, switch the endpoint (dev, staging, prod...), or even mock the server for test purposes.
&lt;/li&gt;
&lt;li&gt;Use URL variable parts, or query parameters.
&lt;/li&gt;
&lt;li&gt;Be able to re-fetch the data when needed, in particular when the variable values change.
The code that illustrates this post is available as part of the LWC essentials: &lt;a href="https://github.com/LWC-Essentials/wire-adapters/tree/master/packages/fetch"&gt;https://github.com/LWC-Essentials/wire-adapters/tree/master/packages/fetch&lt;/a&gt;. Feel free to check it out and submit enhancements. or suggestions! You can also consume the NPM package @lwce/fetch.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using the fetch wire adapter
&lt;/h2&gt;

&lt;p&gt;The syntax is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;wire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;useFetch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;myproperty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let see an an example showing the wire adapter in action. This one calls a user service with query parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;UserList&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;LightningElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;track&lt;/span&gt; &lt;span class="nx"&gt;queryParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="na"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;wire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;useFetch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="na"&gt;queryParams&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;$queryParams&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;  
    &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When the component is initialized, the fetch request is executed and the users variables get the result.&lt;br&gt;&lt;br&gt;
Requests can also be executed explicitly. Obviously, this is useful for update requests, but also for on demand GET requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Users&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;LightningElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;track&lt;/span&gt; &lt;span class="nx"&gt;variables&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;xyz&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;wire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;useFetch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/{userId}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="na"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;$variables&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;  
    &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="nx"&gt;userById&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

    &lt;span class="nx"&gt;handleUserClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;_findUserIdx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="o"&gt;!==&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userById&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;  
                &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userById&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  
            &lt;span class="p"&gt;});&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result property exposes a &lt;code&gt;fetch()&lt;/code&gt; method that can be called programmatically, while the lazy parameter of the wire adapter prevent the automatic execution when the component is initialized.  &lt;/p&gt;

&lt;p&gt;There are several other options, but I don't want to go too much into the use of the adapter, as the &lt;a href="https://github.com/salesforce/lwc-essentials/blob/master/packages/fetch/README.md"&gt;README.md&lt;/a&gt; file already does that pretty extensively.  &lt;/p&gt;

&lt;p&gt;Let's rather dive into the implementation details.   &lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation details
&lt;/h2&gt;

&lt;p&gt;The fetch wire adapter implementation is fairly straightforward, but there are a few traps that we should be aware of.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Duplicating the result object
&lt;/h3&gt;

&lt;p&gt;The current result object is kept in a variable within the wire adapter closure, and new values are simply assigned to this object before it is sent to the component. This makes it easier from an implementation standpoint, for example setting the invariant values, like the &lt;code&gt;fetchClient&lt;/code&gt; instance, or the &lt;code&gt;fetch()&lt;/code&gt; method.&lt;br&gt;&lt;br&gt;
But has a drawback: the component will only detect a change, and thus redraw, when a new object is sent by the wire adapter. So the object kept by the adapter can't be sent as is to the component. Rather, the &lt;code&gt;&lt;br&gt;
update()&lt;/code&gt; function duplicates it and send the copy to the component.   &lt;/p&gt;

&lt;h3&gt;
  
  
  Concurrent requests
&lt;/h3&gt;

&lt;p&gt;The result from a fetch() call is by definition retrieved asynchronously, which means that we do not control when that result will be available. Moreover, if multiple requests are sent sequentially by the same wire adapter, there is no guarantee that the respective results will come in the same order than the requests were emitted. Imagine a UI featuring a button that triggers a change in the wire adapter. If a user frenziedly clicks that button, it is likely that new requests will be emitted by the wire adapter before the results of the previous ones come back, with no guarantee that the results will be retrieved in order. This is well known issue in JavaScript based UI, generally hard to detect and debug.&lt;/p&gt;

&lt;p&gt;Wire adapters, if not designed appropriately, are no exception. I created a code snippet to showcase the issue: &lt;a href="https://developer.salesforce.com/docs/component-library/tools/playground/8peP8wto/12/edit"&gt;https://developer.salesforce.com/docs/component-library/tools/playground/8peP8wto/12/edit.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, now that we are aware of this issue, here is a set of potential solutions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prevent a new request from being emitted while one is still pending &lt;/li&gt;
&lt;li&gt;Ignore the oldest ones when their result comes back and only process the latest one&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If 1. is easy to implement, it does not provide the best user experience because the UI is frozen until the current request's result comes back. On the other hand, it can also be implement at the application level, for example by disabling the button until the request comes back.&lt;/p&gt;

&lt;p&gt;The fetch wire adapter implements the second one by default. It actually associates a unique index to each request, keeps it in the closure of response handler (fetchIndex), and also globally keeps the index of the latest request (fetchCurrent). When a result comes back, it only processes it if it corresponds to the latest request. All other results are simply ignored.&lt;/p&gt;

&lt;h2&gt;
  
  
  As a conclusion...
&lt;/h2&gt;

&lt;p&gt;Basic wire adapters are easy to implement, while providing a unified way to expose data to component developers. Jump over the fence, and start to create yours! Or extend the one we are providing as part of the &lt;a href="https://github.com/LWC-Essentials/wire-adapters"&gt;LWC-Essentials/wire-adapters&lt;/a&gt; project. I'd love to grow this repo with your contributions.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>LWC: Accessing GraphQL data with the Apollo client</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Mon, 10 Feb 2020 17:29:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/lwc-accessing-graphql-data-with-the-apollo-client-57f3</link>
      <guid>https://forem.com/salesforceeng/lwc-accessing-graphql-data-with-the-apollo-client-57f3</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-Es_GizVjiHk/XkBpU4bM4FI/AAAAAAAAA3I/mnRtmywnF90P45aXrhbIyiFgxp88yMM0wCLcBGAsYHQ/s1600/apollo%2Bclient.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--skIO9SHn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-Es_GizVjiHk/XkBpU4bM4FI/AAAAAAAAA3I/mnRtmywnF90P45aXrhbIyiFgxp88yMM0wCLcBGAsYHQ/s320/apollo%252Bclient.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're a front-end developer calling &lt;a href="https://graphql.org/"&gt;GraphQL&lt;/a&gt; endpoints from your UI, you must have heard about the JavaScript &lt;a href="https://www.apollographql.com/docs/react/"&gt;Apollo Client&lt;/a&gt; library. This library is a state management library that let developers execute GraphQL queries against GraphQL servers as well as a local store. It handles caching, change notifications and features interesting capabilities like &lt;a href="https://www.apollographql.com/docs/react/performance/optimistic-ui/"&gt;optimistic UI&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;The flagship implementation from the Apollo team is dedicated to React but, fortunately, the implementation is split in two layers:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A pure JavaScript &lt;a href="https://www.apollographql.com/docs/react/api/apollo-client/"&gt;Apollo Client library&lt;/a&gt;
This library is not related to any client technology. It implements all the necessary plumbing to support GraphQL queries in JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.apollographql.com/docs/react/integrations/integrations/"&gt;View integrations&lt;/a&gt;
Provided on top of the aforementioned library, these integrations make it easier to consume the Apollo client with your technology of choice (React, Angular, Vue, Svelte, Ember, ...). If the React and Angular integrations are done by the Apollo team, the others are provided by third party contributors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Integrating LWC
&lt;/h2&gt;

&lt;p&gt;There is a &lt;a href="https://github.com/apollo-elements/apollo-elements"&gt;Web Component integration&lt;/a&gt; available, which can work with LWC as well. Actually, the LWC adapter code on top of this library would be minimal. On the other hand, it won't take advantage of the LWC specific features, in particular the wire adapters. So the idea is to create a new integration specifically for LWC, leveraging wire adapters similarly to hooks in the React integration.  &lt;/p&gt;

&lt;p&gt;For the impatient, the integration is available here: &lt;a href="https://www.npmjs.com/package/@lwce/apollo-client"&gt;apollo-client.&lt;/a&gt; It provides a sample application that shows CRUD operations on a server, in memory list of books.  &lt;/p&gt;

&lt;h2&gt;
  
  
  GraphQL Queries
&lt;/h2&gt;

&lt;p&gt;A GraphQL query in the React integration uses a hook named useQuery.  Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const { loading, error, data } = useQuery(MY_QUERY);  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A very similar syntax can be achieved with LWC wire adapters. We even keep the same name, useQuery, for the wire adapter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @wire(useQuery, { query: MY_QUERY }) results;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The results property features the following sub properties, similarly to the React hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  { loading, error, data }  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Any of these sub properties is available at any time, so the component template can display appropriate content, with pseudo code like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;template if:true={results.loading}&amp;gt;  
    &amp;lt;img src="loading.gif"/&amp;gt;Loading...   
  &amp;lt;/template&amp;gt;  
  &amp;lt;template if:true={results.error}&amp;gt;  
    Man, there is an error {results.error}   
  &amp;lt;/template&amp;gt;  
  &amp;lt;template if:true={results.data}&amp;gt;  
    Here is the data: {results.data}   
  &amp;lt;/template&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The results property also features even more sub properties. In particular, it offers a fetch() method that re-fetches the query.  &lt;/p&gt;

&lt;p&gt;Finally, a wire adapter has more options than just the query: it can use a specific Apollo client,  defer the query execution with lazy,  use variables and any other standard options used by the Apollo client &lt;a href="https://www.apollographql.com/docs/react/api/apollo-client/#ApolloClient.watchQuery"&gt;watchQuery()&lt;/a&gt;. See &lt;a href="https://github.com/LWC-Essentials/apollo-client/blob/master/packages/apollo-client/README.md"&gt;README.md&lt;/a&gt; for a more exhaustive list of options.   &lt;/p&gt;

&lt;p&gt;Here is a example of a query using variables for pagination, assuming that the query supports offset and limit parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const BOOK_LIST = gql`  
      query($offset: Int!, $limit: Int!) {  
          books(offset: $offset, limit: $limit) {  
            id,  
            title  
            author  
          }  
      }`;  

  class Booklist extends LightningElement {  

    variables = {  
        offset: 0,  
        limit: 10  
    }  

    @wire(useQuery, {  
        query: BOOK_LIST,  
        variables: '$variables'  
    }) books;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A more complete example is available in the project &lt;a href="https://github.com/salesforce/lwc-apollo-client/tree/master/packages/sample-app"&gt;sample application&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;Because wire adapters options can only observe changes to root properties, we created the variables property to hold the query variables. Then, when we want to update one of its value (ex: offset), then we have to assign a brand new object to that property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    handleFirst() {  
        this.variables = {  
            ...this.variables,  
            offset: 0  
        }  
    }  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, if it is possible to use the query inline, it is better to declare it as a constant (BOOK_LIST in the example). This is because the current wire adapter implementation will repeat that value multiple times in the generated code.  &lt;/p&gt;

&lt;h2&gt;
  
  
  GraphQL Mutations
&lt;/h2&gt;

&lt;p&gt;Mutations are also provided using wire adapters, although they should be executed on demand as opposed to when the component is connected. The wire adapter provides a mutate method that can be called anytime to execute the mutation.  &lt;/p&gt;

&lt;p&gt;Bellow is a simple sample that creates a book:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // Define the mutation query   
  const BOOK_CREATE = gql`  
    mutation createBook($book: BookInput!) {  
        createBook(book: $book) {  
            id  
            title  
            author  
            genre  
            publisher  
        }  
    }  
  `;  

  // Mutation adapter use in the component class   
  @wire(useMutation, {mutation: BOOK_CREATE}) bookCreate;  
  onCreateBook() {   
    const newBookValues = {  
      book: {  
        title: "A book",  
        author: "John Doe",  
        genre: "Novel",  
        publisher: "MyBook"  
      }  
    };  
    this.bookCreate.mutate({  
      newBookValues  
    });  
  }   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The sample app coming with the project shows all the CRUD operations on books: Create, Read, Update and Delete.  &lt;/p&gt;

&lt;h2&gt;
  
  
  And What's Next?
&lt;/h2&gt;

&lt;p&gt;Well this project is a first shot. A lot more has to be done, but this is a good start, isn't it? It needs your contributions to be on par with the React implementation. Please feel free to submit your ideas, code or examples.&lt;/p&gt;

</description>
      <category>lwc</category>
      <category>webcomponents</category>
    </item>
    <item>
      <title>LWC Reactive State Manager</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Wed, 05 Feb 2020 18:07:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/lwc-reactive-state-manager-28bl</link>
      <guid>https://forem.com/salesforceeng/lwc-reactive-state-manager-28bl</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-K-jo5kZC1JE/XjhTrDmmcnI/AAAAAAAAA2s/yxOLSAMm9dYvfUzejanHjwyEiOQsy1hLACLcBGAsYHQ/s1600/reactive-state.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F1.bp.blogspot.com%2F-K-jo5kZC1JE%2FXjhTrDmmcnI%2FAAAAAAAAA2s%2FyxOLSAMm9dYvfUzejanHjwyEiOQsy1hLACLcBGAsYHQ%2Fs320%2Freactive-state.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you started to create LWC applications, you might have found that managing the state of a component is trivial, thanks to the reactive properties and the @track decorator. You do not have to deal with a specific state property, or explicitly ask the component to render when its state changed.  &lt;/p&gt;

&lt;p&gt;Now, when your application gets more complex, you often want this state to be shared with other components. This is when state managers, like &lt;a href="https://redux.js.org/" rel="noopener noreferrer"&gt;Redux&lt;/a&gt; or &lt;a href="https://mobx.js.org/README.html" rel="noopener noreferrer"&gt;MobX&lt;/a&gt;, come into play. As these libraries are technology agnostic, you can obviously use them with LWC components. To make it easier, you can even create a few helpers, typically wire adapters.  &lt;/p&gt;

&lt;p&gt;But can we create a store manager that is closer to LWC, leveraging the core LWC capabilities to provide global state management?  &lt;/p&gt;

&lt;h2&gt;
  
  
  Towards a Reactive LWC Store
&lt;/h2&gt;

&lt;p&gt;First, the code described bellow is available as a library published here: &lt;a href="https://www.npmjs.com/package/@lwce/store" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/@lwce/store&lt;/a&gt;. The source code is on Github: &lt;a href="https://github.com/LWC-Essentials/wire-adapters/tree/master/packages/store" rel="noopener noreferrer"&gt;https://github.com/LWC-Essentials/wire-adapters/tree/master/packages/store&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;The idea is simple: we can have a global object, the "store", that holds [key:value] entries. Then, we'll use a wire adapter that lets a component access any entry in the store using a key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @wire(useStore, {key: 'user'}) user;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. The user property is "reactive" so any change to its content will be tracked by the component. Multiple components can share the same entry coming from the store, by using the same key.  Even pure JavaScript code can access an entry programmatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const u = getEntry('user')  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make all the entries reactive, we need the global store object, holding all these entries, to be itself reactive. The LWC component reactivity is provided by a specific membrane, called the reactive membrane, which is private to the runtime. The solution is then to create an invisible component that holds the whole store object as a reactive property. This component is silently created when we initialize the store using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  initStore(content?: object)  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The  content parameter is optional, just in case we want to initialize the store with initial values. It can be useful, for example, when the store is pre-populated from server side rendering results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Entries to the Store
&lt;/h2&gt;

&lt;p&gt;Accessing a store value using the @wire or the API is simple, but how do we add an entry to that store? Well, the answer is straightforward: lazily, when we need it. In other words, an entry is created in the store the first time it is requested by key. And, right after being created, it gets initialized. For this, the developer can register initializers that will be invoked during this step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  registerInitializer(  
    (key) =&amp;gt; {  
        if(key==='user') {  
            return {name: 'Doe', first: 'John'};  
        }  
        return undefined; // not for this entry  
    }  
  );  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the initialization data is not immediately available, for example because the data comes from a network request, an initializer can return a Promise. The store will consume the value when the promise will be resolved. By the meantime, the consumer can check some boolean values to understand the status of the entry, see bellow.   &lt;/p&gt;

&lt;h2&gt;
  
  
  Rich Entry Content
&lt;/h2&gt;

&lt;p&gt;Entries in the store are actually not just exposing the raw data, but an object with the following properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  {  
    data: any  
    error: string  
    loading: boolean  
    initialized: boolean  
  }  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in our example above, the user content have to be accessed through the data property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   const u = getEntry('user').data  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly in a component template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;p&amp;gt;{user.data.firstName} {user.data.lastName}&amp;lt;/p&amp;gt;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because an entry contains {error, loading, initialized}, a consumer can also check if:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An error happened during initialization. error will then contain the error message.&lt;/li&gt;
&lt;li&gt;The data is still being loaded&lt;/li&gt;
&lt;li&gt;The data has already be initialized once
The difference between loading and initialized is subtle: as a store entry can be reloaded, loading can happen while data has already been loaded once. In that case, the developer can choose to provide a slightly different UI (ex: the previous data is still displayed with a loading icon while the entry is being reloaded).
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Putting the Store in Action
&lt;/h2&gt;

&lt;p&gt;The sample-app accompanying the library on Github shows a commerce cart built using this library: &lt;a href="https://github.com/LWC-Essentials/wire-adapters/tree/master/packages/sample-app" rel="noopener noreferrer"&gt;https://github.com/LWC-Essentials/wire-adapters/tree/master/packages/sample-app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the components (cart, checkout, basket icon) are sharing the same 'cart' store entry. They all update when cart changes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-FKK9NugDObc/XjYZL_SGjzI/AAAAAAAAA2U/faZ9e_GNYvQyi9WfKC41e6mNM35OCwqugCLcBGAsYHQ/s1600/store-example.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F1.bp.blogspot.com%2F-FKK9NugDObc%2FXjYZL_SGjzI%2FAAAAAAAAA2U%2FfaZ9e_GNYvQyi9WfKC41e6mNM35OCwqugCLcBGAsYHQ%2Fs640%2Fstore-example.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As all the store entries are reactive, simply changing some values within the cart makes all the components re-render.&lt;/p&gt;

</description>
      <category>lwc</category>
      <category>webcomponents</category>
    </item>
    <item>
      <title>LWC Wire Adapters #2 - Producing Data Streams</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Fri, 31 Jan 2020 22:02:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/lwc-wire-adapters-2-producing-data-streams-1b5i</link>
      <guid>https://forem.com/salesforceeng/lwc-wire-adapters-2-producing-data-streams-1b5i</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-dHNa-ZGKysc/XiDJg9wEeYI/AAAAAAAAA0w/V6I4JLlHizMzn_YTH5RTKzSVeetU9DEtACLcBGAsYHQ/s1600/wireadapters-providers.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r5aAHb_U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-dHNa-ZGKysc/XiDJg9wEeYI/AAAAAAAAA0w/V6I4JLlHizMzn_YTH5RTKzSVeetU9DEtACLcBGAsYHQ/s320/wireadapters-providers.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We saw in the previous post that wire adapters are a very elegant and efficient way to consume data streams. Let's see now how we can produce these data streams. If it seems magic, it is actually a pretty easy API.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;*Disclaimers*&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;#1&lt;/strong&gt; - This post talks about the original wire adapter API, as described &lt;a href="https://github.com/salesforce/lwc-rfcs/blob/master/text/0103-wire-adapters.md"&gt;here&lt;/a&gt;.  There is an upcoming new implementation that keeps the same consumer interface, but where the provider API is a bit different, see &lt;a href="https://github.com/salesforce/lwc-rfcs/blob/master/text/0000-wire-reform.md"&gt;wire-reform&lt;/a&gt;. This blog post talks about the former, and will be updated when the later will be released.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;#2&lt;/strong&gt; -  Custom wire adapters are currently only available to the Open Source version of &lt;a href="https://lwc.dev/"&gt;LWC&lt;/a&gt;, not to LWC running in Salesforce Core.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Our First Wire Adapter
&lt;/h2&gt;

&lt;p&gt;To introduce the wire adapter API, let's implement a stopWatch component that behind the scene uses a wire adapter streaming the time elapsed. This wire adapter offers some callbacks to start, stop and reset the timer:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-mcF-pFMg_zU/XjSdd_qkWFI/AAAAAAAAA1k/vs2ieLgls3ECR7djrWhaBu_yBGFPUD22QCLcBGAsYHQ/s1600/stopwatch.gif"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t3LbiayC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://1.bp.blogspot.com/-mcF-pFMg_zU/XjSdd_qkWFI/AAAAAAAAA1k/vs2ieLgls3ECR7djrWhaBu_yBGFPUD22QCLcBGAsYHQ/s400/stopwatch.gif" alt=""&gt;&lt;/a&gt;&lt;br&gt;
For the impatient, the source code is provided through the LWC playground:&lt;br&gt;
   &lt;a href="https://developer.salesforce.com/docs/component-library/tools/playground/8peP8wto/8/edit"&gt;https://developer.salesforce.com/docs/component-library/tools/playground/8peP8wto/8/edit&lt;/a&gt; &lt;/p&gt;
&lt;h2&gt;
  
  
  Wire Adapters Registry
&lt;/h2&gt;

&lt;p&gt;LWC maintains a global registry of wire adapters, so each wire adapter has to be registered before it can be consumed by a component. It is uniquely identified by a key, which can be a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"&gt;Symbol&lt;/a&gt; or a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions"&gt;Function&lt;/a&gt;. As a good practice, this key is exported from the wire adapter JavaScript module. The registration is done through the register method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  register(key, myAdapterFunction);  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The current implementation also requires a global, LWC provided, "wire service" to be registered before any wire adapter can be used. The main file of an application is generally a good place for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  import { register } from "lwc";  
  // The wire service has to be registered once  
  import { registerWireService } from 'wire-service';  
  registerWireService(register);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that the example above does not have to do this, as the Playground does it automatically for any code snippet.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Wire Adapter Implementation
&lt;/h2&gt;

&lt;p&gt;A wire adapter is defined by a function that is called once when the wire adapter is initialized. It has a unique parameter, eventTarget, which represents the consumer of this wire adapter.  &lt;/p&gt;

&lt;p&gt;To get started, here is how our simple wire adapter skeleton stopWatch looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // Define the Symbol identifying the wire adapter  
  export const stopWatch = Symbol('stop-watch');  

  // Register the wire adapter implementation  
  register(stopWatch, eventTarget =&amp;gt; {  
      // wire adapter implementation

      // ...  
  });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;stopWatch
This is the wire adapter key, exported so components can reference it.&lt;/li&gt;
&lt;li&gt;eventTarget
The context passed by the engine, see bellow.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Initialization
&lt;/h3&gt;

&lt;p&gt;The wire adapter API is event based: it receives notifications when it has to take an action. In fact it can register handlers for the following 3 different events: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;connect
This event is emitted when the component owning the wire adapter is connected to the DOM. At that time, it is safe for the wire adapter to send data to the component.&lt;/li&gt;
&lt;li&gt;disconnect
This event is emitted when the component owning the wire adapter is disconnected from the DOM. When the component is disconnected, the wire adapter can cleanup its internal data. For example, if it was observing a data stream, it can disconnect from it. Once a component is disconnected, the wire adapter should no longer send it data, until it connects again.&lt;/li&gt;
&lt;li&gt;config
This is emitted once initially, and thereafter when the wire adapter options have different values ("reactive options"). The options value are passed as an argument to the handler.
To handle these events, the wire adapter has to register handlers to the eventTarget:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  register(stopWatch, eventTarget =&amp;gt; {  

    function handleConfig(options) {

      // Do something with the config  
    }  

    function handleConnect() {        
      // The component is connected, send it some values!   
    }  

    function handleDisconnect() {

      // The component is disconnected, cleanup the adapter  
    }  

    // Connect the wire adapter  
    eventTarget.addEventListener('config', handleConfig);  
    eventTarget.addEventListener('connect', handleConnect);  
    eventTarget.addEventListener('disconnect', handleDisconnect);  
  })
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Sending data to the component
&lt;/h3&gt;

&lt;p&gt;Once the wire adapter receives the connect event, it can send data to the component. For this, it dispatches a ValueChangedEvent to the component with the new value as an argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const result = ...  
  eventTarget.dispatchEvent(new ValueChangedEvent(result));  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once connected, the wire adapter can send new data to the component at anytime. For example, if the component is listening to a websocket, it can update the component when its gets new data from that websocket.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Assembling the pieces
&lt;/h3&gt;

&lt;p&gt;Well, it is assembled in the playground sample code:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;stopwatch.js contains the wire adapter code&lt;/li&gt;
&lt;li&gt;app.js/.html defines the component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: there are 2 differences between this code running within the playground and a potential standalone application:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The wire service does not have to be registered, the playground does it&lt;/li&gt;
&lt;li&gt;The wire adapter in the playground imports wire-service, while it should be @lwc/wire-service in an app using LWC Open Source.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  A few implementation details
&lt;/h3&gt;

&lt;p&gt;The wire adapter uses a configuration option to set the refresh interval. In the example, the value is passed through a reactive property. This makes the handleConfig() handler called with a new value when this property changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @track interval = 50;  
  @wire(timeWatch,{interval:'$interval'}) stopWatch;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The data sent by the wire adapter to the component contains both the timer values (hour, mins, ...) as well as some callback functions to interact with the stopwatch adapter. Here is a mock of that object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; {  
    // Timer values  
    hour: '00',  
    mins: '00',  
    secs: '00',  
    mils: '000',  
    // Callback functions  
    start: function() {...},  
    stop: function() {...},  
    reset: function() {...},    
}  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To make the wire adapter robust, it maintains a connected flag and only sends data to the component when it this flag is true.&lt;/p&gt;

&lt;p&gt;This wire adapter implementation creates and sends a copy of the date to the component.  Sending the same object instance multiple times to the component does not make the component to re-render, while a copy does.  &lt;/p&gt;

&lt;p&gt;Finally, wire adapters are simple to implement, aren't they?&lt;/p&gt;

</description>
      <category>lwc</category>
      <category>webcomponents</category>
    </item>
    <item>
      <title>LWC Wire Adapters #1 - Consuming Data Streams</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Fri, 17 Jan 2020 17:07:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/lwc-wire-adapters-1-consuming-data-streams-3bae</link>
      <guid>https://forem.com/salesforceeng/lwc-wire-adapters-1-consuming-data-streams-3bae</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-Miy00L-dZhw/XiDJa3MdA6I/AAAAAAAAA0s/ejAUqCxDJok1lsV5pO8vCaibvHRHvILgQCLcBGAsYHQ/s1600/wireadapters-consumers.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yxwWYD0i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-Miy00L-dZhw/XiDJa3MdA6I/AAAAAAAAA0s/ejAUqCxDJok1lsV5pO8vCaibvHRHvILgQCLcBGAsYHQ/s320/wireadapters-consumers.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The previous post talked about LWC reactivity using membranes, which re-render the components when the tracked data is changed, like bellow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyComponent extends LightningElement {  
  @track value;  
}   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But how does the data get to the component property, value in the example above? It can come from an external data source like a REST service, and it might even update over time. Think about a stream of data feeding the component with new values when they become available.  &lt;/p&gt;

&lt;p&gt;Part of the standard web component &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements"&gt;lifecycle&lt;/a&gt;, the connectedCallback() method is called when the component is added to a document connected element. In short, when the custom element is added to the DOM of your page. At that time, it has access to the DOM element with its attributes, so it can call a service using these values. This is the recommended way for developers to connect the component with data sources.  &lt;/p&gt;

&lt;p&gt;LWC goes further than that, as it makes it simpler, thanks to the "wire adapters". Technically, a wire adapter is a data provider that streams data to a component. The notion of stream is important: it is not only a one time data retrieval, like a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;Promise&lt;/a&gt;, but the data can be continuously updated and the component notified. For the JS experts, it is much like an &lt;a href="https://rxjs-dev.firebaseapp.com/"&gt;RxJS&lt;/a&gt; &lt;a href="https://www.stackchief.com/tutorials/JavaScript%20Observables%20in%205%20Minutes"&gt;Observable&lt;/a&gt;.   &lt;/p&gt;

&lt;h2&gt;
  
  
  Wire Adapters from a Consumer Standpoint
&lt;/h2&gt;

&lt;p&gt;Wire adapters are objects instantiated using the @wire decorator. This decorator uses two parameters:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The wire adapter key, that uniquely identifies the adapter to use &lt;/li&gt;
&lt;li&gt;A set of options, specific to the wire adapter implementation
Let's use a fictitious wire adapter to access a list of books. With the use of a parameter, this wire adapter can even filter the books based on the author. Assigning a list of books to a component property is done with the following syntax:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  class Books extends LightningElement {  
    @wire(BookProvider, {author: 'Mark Twain'}) bookList;   
  }    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;BookProvider is the wire adapter key. It is generally a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"&gt;Symbol&lt;/a&gt;, exported by the module defining the wire adapter. &lt;/li&gt;
&lt;li&gt;{author: 'Mark Twain'} is the set of options passed to the wire adapter.  This wire adapter only handles one, which is the author filter.
When the component is added to the DOM, the wire adapter is connected and executed. Obviously, it will retrieve a list of books and assign it to bookList. The result can be assigned synchronously or asynchronously, depending on the wire adapter implementation. With the later, the component should not assume that the data is available right away, but it can come later.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What the bookList variable contains is up to the wire adapter: it can be  raw data, like an array of books, of it can be a richer structure giving more information {data,error,loading, ...}. The later is a good practice. It allows, for example, the component to display an icon while the data is loading asynchronously. Moreover, the property content is not limited to values, but can also contain any kind of JS objects, including callback functions (ex: reload(), fetchMore(), ...). Again, it is up to the wire adapter to define the shape of the data being assigned to the property.   &lt;/p&gt;

&lt;p&gt;The wired properties are automatically reactive, similarly to properties decorated with @track.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic Wire Adapters
&lt;/h2&gt;

&lt;p&gt;The wire adapter options can be dynamic. Let's, for example, filter the book list using an author passed as an attribute to the component. This way, we can use the same component multiple times while displaying different lists:  &lt;/p&gt;



&lt;p&gt;Our Books component becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  class Books extends LightningElement {  
    @api author;   
    @wire(BookProvider, {author: '$author'}) bookList;   
  }    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The @api decorator in LWC means that the 'author' property is &lt;a href="https://developer.salesforce.com/docs/component-library/documentation/lwc/js_props_public"&gt;public&lt;/a&gt;: it can be set through an attribute in the markup, or programmatically from outside of the component. The $author value is an LWC convention: it means that the component's author property value should be used instead of a static string value. Note that this only applies to root values. Options like {user: {name:'$name'}} will not be recognized as a reference to a property value, because name is not a root property.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Wire Adapters
&lt;/h2&gt;

&lt;p&gt;If the value provided by a wire adapter can be assigned to a component property, it can also be streamed to a component method. In this case, the value is passed to the method as an argument.&lt;br&gt;&lt;br&gt;
Let's assume, as an example, that we want to split a book properties into different component tracked properties, we can do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  class Books extends LightningElement {  
    @track title;  
    @track author;   
    @wire(BookByISBN, {ISBN: 'xxxxx'}) book(value) {  
      this.title = value.title;      this.author = value.author;   
    };   
  }    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Of course, the code of such a method can do a lot more, like calculation, sending updates, ...  &lt;/p&gt;

&lt;h2&gt;
  
  
  Imperative Access
&lt;/h2&gt;

&lt;p&gt;To be complete, the wire adapter key can be either a Symbol or a Function. When it is a function (a "callable"), it means that it can be called by code, simply by calling that function with parameters. If the parameters could match the wire adapter options, this is not a requirement. Also, the returned value is adapter specific: it can be anything, and does not have to be a Promise. This is reserved to very specific use cases.  &lt;/p&gt;

&lt;p&gt;The journey continues. Now that we know how to consume a wire adapter, let's figure out how to build one. That will be the topic of the next post.&lt;/p&gt;

</description>
      <category>lwc</category>
      <category>webcomponents</category>
    </item>
    <item>
      <title>LWC Reactivity Using Membranes</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Mon, 13 Jan 2020 13:37:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/lwc-reactivity-using-membranes-4ab3</link>
      <guid>https://forem.com/salesforceeng/lwc-reactivity-using-membranes-4ab3</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-fd9Wdi1wXjs/XhtUaN6oDeI/AAAAAAAAA0E/duNyhimWQgArig1rml1B2l6jlw3whmZ5ACLcBGAsYHQ/s1600/membrane_main.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZTcVWFmM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-fd9Wdi1wXjs/XhtUaN6oDeI/AAAAAAAAA0E/duNyhimWQgArig1rml1B2l6jlw3whmZ5ACLcBGAsYHQ/s320/membrane_main.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LWC allows you to create reactive components that automatically update their UI when they get new data, or when existing data is modified. At the heart of this reactive capability is the implementation of a pattern called "membrane". Let's dive into that pattern and how it powers LWC.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a membrane
&lt;/h2&gt;

&lt;p&gt;Basically a membrane is a thin layer between the consumer of an object and that object. In other words, a membrane exposes consumer facing proxies for any existing object. From a consumer standpoint, accessing the object through the proxy is indistinguisable from intereacting directly with the object. But the membrane can execute business logic ("hooks") whenever the consumer interacts with the object. It can track property access (read/write), method calls, properties enumeration and so on... It can also hide some content to the consumer, or even distort existing content (e.g. change the values being returned). It is a very powerful pattern used for many different purposes, from content isolation to change tracking.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-wn6fcaepTHA/Xhs_Zn5sP_I/AAAAAAAAAzc/OMFmW6xSZlA7_Q8FwMbAm-gZaYRDAid9gCLcBGAsYHQ/s1600/membrane_1.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yCIhoXPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-wn6fcaepTHA/Xhs_Zn5sP_I/AAAAAAAAAzc/OMFmW6xSZlA7_Q8FwMbAm-gZaYRDAid9gCLcBGAsYHQ/s400/membrane_1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another important point with a membrane is that any object accessed through a proxy is automatically proxied by the membrane as well. Let's note a proxied object X as P(X).&lt;br&gt;&lt;br&gt;
If A has a property b of type B, then A.b returns a object of type B. But P(A).b returns P(B). The membrane hooked the access to the property b and distorted the result to return a proxy object for b. This is obviously transparent to the consumer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-68qzTWGRFII/XhtCl2mwEOI/AAAAAAAAAz4/CuqQT14JQK0GgBgFatypqMavI1TRHQT4gCLcBGAsYHQ/s1600/membrane_2.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bdmTr1y7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-68qzTWGRFII/XhtCl2mwEOI/AAAAAAAAAz4/CuqQT14JQK0GgBgFatypqMavI1TRHQT4gCLcBGAsYHQ/s400/membrane_2.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
Because of this, if one starts from a proxied root object and only access the graph from this root object, then it guarantees that all the objects accessed are proxies, and are thus managed by the membrane.  &lt;/p&gt;

&lt;p&gt;Finally, membranes have another important property: there is a one to one relationship between an object and its proxy for a given membrane. It means that the same P(A) object is returned for a given object A, regardless how it is retrieved. Suppose, for example, that a component has list of child components and a reference to its parent. The equality bellow is true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         comp.children[0].parent === comp   
That equality is preserved with proxies as well:  
         P(comp).children[0].parent === P(comp)  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;More detailed information on membranes can be found here: &lt;a href="https://tvcutsem.github.io/membranes"&gt;membrane&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Salesforce Membrane Implementation
&lt;/h2&gt;

&lt;p&gt;As said earlier, LWC is using membranes to implement reactivity. But, instead of coding membranes within LWC, Salesforce created a separate general purpose, open source library called &lt;a href="https://github.com/salesforce/observable-membrane"&gt;observable-membrane&lt;/a&gt;. Like other JavaScript implementations, it is based on top of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy"&gt;Proxy API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With this library, a developer can create a membrane and proxy objects with the simple code bellow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const membrane = new ObservableMembrane();  
  ...  
  const o = membrane.getProxy(o); 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The membrane object holds a set of functions called when a caller interacts with a proxied object. Thus, the membrane can track when a value is read/updated and eventually distort that value. In short, the ObservableMembrane knows about any property access/change in the graph starting from any proxied object, obtained by calling getProxy().&lt;/p&gt;

&lt;h2&gt;
  
  
  LWC and Membrane
&lt;/h2&gt;

&lt;p&gt;This is where it becomes interesting: if a membrane can track any change in an object graph, it means that it can re-render a component when it detects a change in the data graph it consumes. This is exactly what the @track property decorator is for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  class Banner extends LightningElement {
      @track user  

  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This decorator ensures that any value assigned to the decorated property is actually proxied using the reactiveMembrane. Typically, assigning a value to a tracked property like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  this.user={ name: 'Paul', ...}

is internally equivalent to:

  this.user=reactiveMembrane.getProxy({name: 'Paul', ...}).
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;reactiveMembrane is a singleton, defined in the LWC engine library. It tracks the value changes in any object it created proxies for, find the components impacted by the changes and re-render them when necessary.  For example, if a component has a onclick event handler like bellow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  handleClick() {

    this.user.name = 'Bob';

  } 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The user name change is detected by the reactiveMembrane and the component is re-rendered without any further work from the developer.  &lt;/p&gt;

&lt;p&gt;Note that the re-rendering does not happen synchronously when the change is detected. The component is internally tagged as 'dirty' and the rendering will happen later. Thus, sequentially changing multiple tracked values for a component only triggers a single re-rendering.&lt;/p&gt;

&lt;p&gt;Multiple components can track changes of the same object. Fortunately, every component will be updated when a change happens to this object, regardless where and how it was updated.  &lt;/p&gt;

&lt;p&gt;Finally, all the fields in a component are reactive. Adding &lt;a href="https://lwc.dev/guide/reference#%40track"&gt;@track&lt;/a&gt; to a component property is only needed for values you'd like to track deeper, like objects or array, not for basic strings, numbers or booleans.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The use of membranes within LWC makes it very easy to track changes and react when they happen. The developers don't have to care about notifying the system when some data is changed, it is "magic". From an implementation standpoint, the use of the Proxy API also makes it very efficient at runtime.  &lt;/p&gt;

&lt;p&gt;But wait: it reveals all its power when coupled with other LWC technologies. Let's explore that in the next post!&lt;/p&gt;

</description>
      <category>lwc</category>
      <category>webcomponents</category>
    </item>
    <item>
      <title>Introducing LWC - Lightning Web Components</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Wed, 08 Jan 2020 17:57:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/introducing-lwc-lightning-web-components-2h8o</link>
      <guid>https://forem.com/salesforceeng/introducing-lwc-lightning-web-components-2h8o</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-SgEgl_mvceU/Xh84R-T7frI/AAAAAAAAA0g/LeguRKrk27oSGbBVRrULWoTW4MOxn9hZwCLcBGAsYHQ/s1600/lightning-web-components.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AabkIKrI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-SgEgl_mvceU/Xh84R-T7frI/AAAAAAAAA0g/LeguRKrk27oSGbBVRrULWoTW4MOxn9hZwCLcBGAsYHQ/s320/lightning-web-components.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the previous &lt;a href="https://dev.to/philriand/the-rise-of-web-components-1iie-temp-slug-5711248"&gt;post&lt;/a&gt;, I talked about Web Components in general. But Salesforce released at &lt;a href="https://www.salesforce.com/company/news-press/press-releases/2019/05/192915-e/"&gt;TrailheadDX 2019&lt;/a&gt; a new open source technology called Lightning Web Components, aka &lt;a href="https://lwc.dev/"&gt;LWC&lt;/a&gt;. Basically, LWC is a set of tools, including a compiler, a runtime engine, some build utilities that you can use to create both reusable components and applications.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Why a tool for creating Web Components?
&lt;/h2&gt;

&lt;p&gt;Actually, Web Components are defined through a set of new browser specifications, so they don't require any framework. There are many examples showing how to create such reusable components just using the bare APIs!  &lt;/p&gt;

&lt;p&gt;If this is correct, you'll quickly find out that these APIs are very low level. As a result, creating an even moderately complex component requires a bunch of boilerplate code: you need to maintain the synchronization between HTML attributes and JavaScript properties, you need to keep the DOM up-to-date on data changes, ... and many other boring stuff!  &lt;/p&gt;

&lt;p&gt;That's the reason why several technologies appeared in the past years: they provide you an easier experience with Web Components. They are kind of lightweight frameworks on top of the Web Components APIs. They are often called "invisible" frameworks as, once the components are created, the technology used to create the components is not apparent. As a consumer of the components, you don't really care what has been used behind the scene to create them.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What does LWC bring on the table?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Enterprise ready&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;LWC is an enterprise ready technology, designed to support Salesforce business in the long term. When I think about it, I can find a lot of similarities between &lt;a href="https://www.salesforce.com/"&gt;Salesforce&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Lotus_Software"&gt;Lotus/IBM&lt;/a&gt;: my latest &lt;a href="https://en.wikipedia.org/wiki/IBM_Notes"&gt;Notes&lt;/a&gt; client can still execute applications from the '90, and so Salesforce can execute apps built with older releases (or seamlessly convert them). Such a backward compatibility is critical for businesses! It is even more emphasized with cloud architectures, as the customers are upgraded automatically without the choice to stay on an older release. From an LWC standpoint, it means that every feature added to the tool will have to be supported "forever" and thus is carefully evaluated with a risk assessment. Any new feature has to go through an RFC, see: &lt;a href="https://github.com/salesforce/lwc-rfcs"&gt;lwc-rfcs&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ES6+/Typescript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are in 2020 now, I can't see myself back using ES5 and JQuery. I believe you're on the same boat! LWC eases the use of ES6+, making your code future proof even when staging features like &lt;a href="https://tc39.es/proposal-decorators/"&gt;decorators&lt;/a&gt; are involved. It is using &lt;a href="https://babeljs.io/"&gt;Babel&lt;/a&gt; behind the scene, for which &lt;a href="https://engineering.salesforce.com/"&gt;Salesforce Engineering&lt;/a&gt; is a proud sponsor.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Browser support *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If the latest "evergreen" browsers natively support the Web Components specification, older browsers should  use polyfills and/or the compiler should generate specific code for these browsers. LWC does a bit of both, including some tailored polyfills to achieve performance goals. &lt;a href="https://www.npmjs.com/package/@lwc/synthetic-shadow"&gt;@lwc/synthetic-shadow&lt;/a&gt; is a good example of such a polyfill.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Templating&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;LWC provides a template engine that renders markup from an HTML template. Somehow, it is similar to &lt;a href="https://lit-html.polymer-project.org/guide"&gt;lit-html&lt;/a&gt;, except that it is compiled to Javascript at build time, for a faster execution. In that respect, it is similar to the &lt;a href="https://babeljs.io/docs/en/babel-plugin-transform-react-jsx"&gt;JSX compiler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reactivity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the template syntax supports one-way data binding, the component reactivity comes from &lt;a href="https://github.com/salesforce/observable-membrane"&gt;observable-membrane&lt;/a&gt;, which is another Salesforce open source project. In a nutshell, it allows objects to be observed for changes. Typically, Components are redrawn when at least one of their properties is changed (can be the root property, or another object referenced by that property).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Binding&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Associated with the above membrane is the notion of wire adapters. Basically, a wire adapter streams data to a component and makes it re-render when needed. It can be used similarly to &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;React hooks&lt;/a&gt;. More information here: &lt;a href="https://github.com/salesforce/lwc-rfcs/blob/master/text/0103-wire-adapters.md"&gt;wire adapters,&lt;/a&gt; superseded by the &lt;a href="https://github.com/salesforce/lwc-rfcs/blob/master/text/0000-wire-reform.md"&gt;wire-reform.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;Performance is a key characteristic of LWC. It is constantly, and deeply, tuned to provide the best client side performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Static analysis&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
For advanced users, the LWC compiler can be used to extract meta-data from the components. For example, a plugin could extract the data sources being accessed and generate some initialization code.  &lt;/p&gt;

&lt;p&gt;And several others, like matching attribute and properties, ...: a set of goodies that makes your life easier as a developer! &lt;/p&gt;

&lt;h2&gt;
  
  
  The flavors of LWC
&lt;/h2&gt;

&lt;p&gt;Well, if there is one LWC technology, there are two deployments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Salesforce platform
The Salesforce platform handles many things for free, like the LWC compilation, so you don't have to manage rollup/webpack builds, nor you have to maintain package.json. On the other hand, it runs within the constraints of the Salesforce platform, including &lt;a href="https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_code.htm"&gt;Locker&lt;/a&gt; and other differences.&lt;/li&gt;
&lt;li&gt;Open source, aka LWC OSS (&lt;a href="https://github.com/salesforce/lwc"&gt;Github&lt;/a&gt;)
LWC can be used similarly to any other Javascript technology. To get started, you can use the nice &lt;a href="https://github.com/muenzpraeger/create-lwc-app"&gt;lwc-create-app&lt;/a&gt; tools from &lt;a href="https://twitter.com/muenzpraeger?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"&gt;Rene Winkelmeyer (&lt;/a&gt;&lt;a href="https://twitter.com/muenzpraeger"&gt;@muenzpraeger&lt;/a&gt;&lt;a href="https://twitter.com/muenzpraeger?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"&gt;)&lt;/a&gt;, that some of you know from our prior IBM life.
This is LWC unleashed!
The differences are detailed &lt;a href="https://developer.salesforce.com/blogs/2019/06/differences-between-building-lightning-web-components-on-lightning-platform-and-open-source.html"&gt;here&lt;/a&gt;, thanks again to Rene.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  And what next?
&lt;/h2&gt;

&lt;p&gt;Now that I introduced the LWC technology (I hope it got you hooked!), it is time to get our hands dirty. I'm not going to provide any tutorial or getting started document because they already exist. I'll rather focus on some specific aspect of Web Components and LWC.&lt;/p&gt;

&lt;p&gt;By the meantime, you can get started here: &lt;a href="https://developer.salesforce.com/docs/component-library/documentation/lwc"&gt;Introducing Lightning Web Components&lt;/a&gt; or here: &lt;a href="https://lwc.dev/"&gt;lwc.dev.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>lwc</category>
      <category>webcomponents</category>
    </item>
    <item>
      <title>The rise of Web Components</title>
      <dc:creator>Philippe Riand</dc:creator>
      <pubDate>Sat, 04 Jan 2020 20:41:00 +0000</pubDate>
      <link>https://forem.com/salesforceeng/the-rise-of-web-components-41hh</link>
      <guid>https://forem.com/salesforceeng/the-rise-of-web-components-41hh</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-Fdo0A7-LED8/Xg-xQcM976I/AAAAAAAAAyE/HWA9dNT2bV0Sv4LVLaCkWB9m5yAKZu1XQCLcBGAsYHQ/s1600/webcomponents.jpg"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cxZJ74th--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-Fdo0A7-LED8/Xg-xQcM976I/AAAAAAAAAyE/HWA9dNT2bV0Sv4LVLaCkWB9m5yAKZu1XQCLcBGAsYHQ/s1600/webcomponents.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is now been a while since people started to talk about &lt;a href="https://en.wikipedia.org/wiki/Web_Components"&gt;Web Components&lt;/a&gt;. But it finally became a reality. As of the beginning of 2020, all the major browsers implement most of the specification. Moreover, the need to support some of the ancient browsers, like the older IEs, is &lt;a href="https://gs.statcounter.com/browser-market-share"&gt;drastically decreasing&lt;/a&gt;. Fortunately, there are  &lt;a href="https://www.webcomponents.org/polyfills"&gt;polyfills&lt;/a&gt; to still support IE11 and other not too old generation. So what are web components and should you use them?  &lt;/p&gt;

&lt;h2&gt;
  
  
  Web Components
&lt;/h2&gt;

&lt;p&gt;Web Components is actually a generic term that encompasses several different specifications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom Elements
Define new HTML element types (e.g. new HTML tags). They are easily identifiable from the stock HTML elements as their name must include an hyphen '-'.
Support: &lt;a href="https://caniuse.com/#feat=custom-elementsv1"&gt;https://caniuse.com/#feat=custom-elementsv1&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Shadow DOM
Encapsulates the markup and the styles of a component. Basically, the shadow DOM isolates the implementation of your component from the rest of the page.
Support: &lt;a href="https://caniuse.com/#feat=shadowdomv1"&gt;https://caniuse.com/#feat=shadowdomv1&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ES6 modules
As Web Components heavily rely on JavaScript, ES6 modules define a elegant way to load JavaScript code within the browser.
Support: &lt;a href="https://caniuse.com/#feat=es6-module"&gt;https://caniuse.com/#feat=es6-module&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;HTML template
Used to define fragments of markup that are not rendered by the page, but can be reused later in particular from JavaScript.
Support: &lt;a href="https://caniuse.com/#feat=template"&gt;https://caniuse.com/#feat=template&lt;/a&gt;
Here is a great introduction to these specifications: &lt;a href="https://www.webcomponents.org/introduction"&gt;https://www.webcomponents.org/introduction&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not that the idea of natively extending the HTML markup and isolating components is not new. There is prior art, like &lt;a href="https://en.wikipedia.org/wiki/HTML_Components"&gt;Microsoft HTC&lt;/a&gt; or &lt;a href="https://developer.mozilla.org/en-US/docs/Archive/Mozilla/XUL"&gt;Mozilla XUL&lt;/a&gt;. Web Components, instead of being vendor specific, are part of the W3C specification and work similarly on every browser following that spec.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web components vs Frameworks
&lt;/h2&gt;

&lt;p&gt;Let's put it this way: they do not compete, they rather complement each other. The Web Components specification provides native behaviors implementation, thus helping the frameworks to be simpler and lightweight. On the other hand, the frameworks make it easier to write Web Components. If you were brave enough to write a web component manually, then you know what I'm talking about. What would take a few lines of React code could lead to hundreds using the bare Web Components APIs.  &lt;/p&gt;

&lt;p&gt;The Web Components specification is an evolving one, that still has gaps to fill. But it reached a state where it provides real value in term of performance and browser compatibility.    &lt;/p&gt;

&lt;h2&gt;
  
  
  How to write Web Components
&lt;/h2&gt;

&lt;p&gt;Ok, let's forget about solely using the raw browser APIs, but let's get help from Frameworks, as they handle a lots of things for us (data binding, event handlers, ...).  &lt;/p&gt;

&lt;p&gt;Even though existing frameworks came before the Web Components specification, they all have ways to create and consume custom elements. On the other hand, they generally do not (yet?) properly handle advanced capabilities like Shadow DOM:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt;
React Components are actually fairly different from Web Components. That being said, a React application can both consume custom elements and expose its components as custom elements, see: &lt;a href="https://reactjs.org/docs/web-components.html"&gt;https://reactjs.org/docs/web-components.html&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://angular.io/"&gt;Angular&lt;/a&gt;
Angular, if not built on top of the Web Components specification, is actually closer than React. The Angular team came with &lt;a href="https://angular.io/guide/elements"&gt;Angular Elements&lt;/a&gt;, which let's you package your components as custom elements.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://vuejs.org/"&gt;Vue.JS&lt;/a&gt;
Similarly, Vue has its own library to expose custom element: &lt;a href="https://github.com/vuejs/vue-web-component-wrapper"&gt;@vue/web-component-wrapper.&lt;/a&gt;
If using these well known technologies feels appealing, they come with some caveats/limitations when it is about Web Components: the component models have different life cycles, they carry more code than necessary, the features are not matching 1-1...
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fortunately a new generation of frameworks, based on top of the Web Components specification, appeared. Will they replace the legacy ones is a good question. It won't certainly happen soon, although they make it easier and more efficient when leveraging Web Components.&lt;br&gt;&lt;br&gt;
Part of these frameworks are:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://lwc.dev/"&gt;Lighning Web Components (LWC)&lt;/a&gt;
This is one of the latest, but promising framework brought by &lt;a href="https://www.salesforce.com/"&gt;Salesforce&lt;/a&gt;. Ok, my judgment is certainly biased because I'm a Salesforce employee, and because we are using this technology on a daily basis. But trust me, it deserves a look :-) Most of my upcoming posts going further will be around on that technology!&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://stenciljs.com/"&gt;Stencil.JS&lt;/a&gt;
This technology is developed by the &lt;a href="https://ionicframework.com/"&gt;Ionic&lt;/a&gt; team, basically to fulfill their own needs. In short, the first Ionic release was Angular based and they got many requests for both a React and a Vue version. So they decided to standardize on a common Web Components implementation and then wrap them within each of these framework. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.polymer-project.org/"&gt;Polymer&lt;/a&gt;
That's one of the original frameworks brought by Google. It evolved a lot in the past 5 years, following the browser implementations, finally making it an efficient framework for writing Web Components. Finally, instead of the initial large monolithic framework, it now provides several projects like &lt;a href="https://lit-element.polymer-project.org/"&gt;LitElement&lt;/a&gt; or &lt;a href="https://lit-html.polymer-project.org/"&gt;lit-html.&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://svelte.dev/"&gt;Svelte&lt;/a&gt;
Svelte is an efficient &lt;a href="https://svelte.dev/blog/svelte-3-rethinking-reactivity"&gt;reactive&lt;/a&gt; library, oriented towards performance. It brings some innovation, particularly with the drop of the virtual DOM. Its community development model is very interesting, with an impressive list of involved people.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And many more!&lt;br&gt;
&lt;a href="http://./"&gt;Hybrids&lt;/a&gt;, &lt;a href="https://slimjs.com/#/getting-started"&gt;Slim.JS&lt;/a&gt;, &lt;a href="https://bosonic.github.io/"&gt;Bosonic,&lt;/a&gt; &lt;a href="https://riot.js.org/"&gt;Riot.JS&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/JQWidgets#Smart_HTML_Elements"&gt;Smart HTML Elements&lt;/a&gt;... And yes, I'm missing some, in particular the ones that do not yet exist. &lt;br&gt;
Choosing one framework or the other can be matter of preferences, skills or actual framework capabilities. But we should not forget a non negligible aspect, which is the sustainability of the technology:  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Will the technology remain compatible over time?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How will it adapt to the new standards?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Who is behind it? Is it another open source project that will loose attention&amp;amp; love in a foreseeable future?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is the community? &lt;br&gt;
These are fair questions that we need to answer before we choose a technology for business purposes. Of course, your mileage may vary.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>lwc</category>
      <category>webcomponents</category>
    </item>
  </channel>
</rss>
