|
| 1 | +wv2winrt Cacheable Properties |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | + |
| 6 | +WebView2 supports WinRT projection into JavaScript similar to how the previous edgehtml |
| 7 | +WebView support WinRT projection. However unlike the previous WebView, for WebView2 the |
| 8 | +WinRT objects live in a different process from the JavaScript that invokes the WinRT. |
| 9 | +Because of this cross-process access, performance is something we're working on improving. |
| 10 | +To that end, this feature allows you to mark individual runtimeclass properties as |
| 11 | +cacheable so that the JavaScript running in the renderer process can cache the result of |
| 12 | +the property value the first time and avoid subsequent cross-process calls each time the |
| 13 | +property is accessed. Working with partner apps with existing JavaScript that uses WinRT |
| 14 | +this was identified as something in particular that could help improve runtime |
| 15 | +performance. |
| 16 | + |
| 17 | +# Examples |
| 18 | + |
| 19 | +```c# (but really MIDL3) |
| 20 | + [default_interface] |
| 21 | + runtimeclass Toaster |
| 22 | + { |
| 23 | + // This property changes value throughout the lifetime of the object so is not |
| 24 | + // marked readonly. |
| 25 | + Boolean Available { get; }; |
| 26 | + |
| 27 | + // This property has one value for the lifetime of the object so we mark it |
| 28 | + // readonly to improve wv2winrt performance. |
| 29 | + [corewebview2readonly] |
| 30 | + String Model { get; }; |
| 31 | + |
| 32 | + // ... |
| 33 | + } |
| 34 | +``` |
| 35 | + |
| 36 | +# API Details |
| 37 | + |
| 38 | +```c# (but really MIDL3) |
| 39 | +namespace Microsoft.Web.WebView2.Core |
| 40 | +{ |
| 41 | + /// You can use the CoreWebView2ReadOnly attribute on a runtimeclass property |
| 42 | + /// definition in MIDL3 if the property value doesn't change for the lifetime |
| 43 | + /// of its object. When an object is projected into JavaScript via |
| 44 | + /// `CoreWebView2.AddHostObjectToScript`, WebView2 will cache property values |
| 45 | + /// marked with this attribute. This can potentially improve performance by |
| 46 | + /// reducing the number of cross-process calls to obtain the latest value of |
| 47 | + /// the property. |
| 48 | + [attributeusage(target_property)] |
| 49 | + [attributename("corewebview2readonly")] |
| 50 | + attribute CoreWebView2ReadOnlyAttribute |
| 51 | + { |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +# Appendix |
| 57 | + |
| 58 | +Names considered for the attribute: |
| 59 | + * **Cacheable**: Caching is what WebView2 will do with the property rather than describing |
| 60 | + an aspect of the runtimeclass property. |
| 61 | + * **ReadOnly**: Similar to C#'s readonly keyword which indicates a value won't change (once |
| 62 | + initialized). A more familiar term to end developers than 'immutable'. It does |
| 63 | + convey that the caller can't set it, but does it also convey that the implementer also |
| 64 | + cannot change the value? |
| 65 | + * **Immutable**: Perhaps more explicit than readonly that the implementer also cannot |
| 66 | + change the value, but perhaps less familiar of a term. |
| 67 | + |
| 68 | +For the sample code, the only code you have to write is applying the attribute to the |
| 69 | +property. The only effect this has is to potentially improve performance so there's no other |
| 70 | +code to demonstrate anything. Accordingly, not sure what else to do in the sample code other |
| 71 | +than the MIDL3. |
0 commit comments