lwc 2.34.0 → 2.35.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/engine-dom/esm/es2017/engine-dom.js +939 -763
  2. package/dist/engine-dom/iife/es2017/engine-dom.js +939 -762
  3. package/dist/engine-dom/iife/es2017/engine-dom.min.js +1 -1
  4. package/dist/engine-dom/iife/es2017/engine-dom_debug.js +846 -684
  5. package/dist/engine-dom/iife/es5/engine-dom.js +1115 -922
  6. package/dist/engine-dom/iife/es5/engine-dom.min.js +1 -1
  7. package/dist/engine-dom/iife/es5/engine-dom_debug.js +801 -619
  8. package/dist/engine-dom/umd/es2017/engine-dom.js +939 -762
  9. package/dist/engine-dom/umd/es2017/engine-dom.min.js +1 -1
  10. package/dist/engine-dom/umd/es2017/engine-dom_debug.js +846 -684
  11. package/dist/engine-dom/umd/es5/engine-dom.js +1115 -922
  12. package/dist/engine-dom/umd/es5/engine-dom.min.js +1 -1
  13. package/dist/engine-dom/umd/es5/engine-dom_debug.js +801 -619
  14. package/dist/engine-server/commonjs/es2017/engine-server.js +1025 -1082
  15. package/dist/engine-server/commonjs/es2017/engine-server.min.js +1 -1
  16. package/dist/engine-server/esm/es2017/engine-server.js +1025 -1082
  17. package/dist/synthetic-shadow/esm/es2017/synthetic-shadow.js +21 -41
  18. package/dist/synthetic-shadow/iife/es2017/synthetic-shadow.js +21 -41
  19. package/dist/synthetic-shadow/iife/es2017/synthetic-shadow.min.js +2 -2
  20. package/dist/synthetic-shadow/iife/es2017/synthetic-shadow_debug.js +20 -39
  21. package/dist/synthetic-shadow/iife/es5/synthetic-shadow.js +24 -38
  22. package/dist/synthetic-shadow/iife/es5/synthetic-shadow.min.js +2 -2
  23. package/dist/synthetic-shadow/iife/es5/synthetic-shadow_debug.js +23 -36
  24. package/dist/synthetic-shadow/umd/es2017/synthetic-shadow.js +21 -41
  25. package/dist/synthetic-shadow/umd/es2017/synthetic-shadow.min.js +2 -2
  26. package/dist/synthetic-shadow/umd/es2017/synthetic-shadow_debug.js +20 -39
  27. package/dist/synthetic-shadow/umd/es5/synthetic-shadow.js +24 -38
  28. package/dist/synthetic-shadow/umd/es5/synthetic-shadow.min.js +2 -2
  29. package/dist/synthetic-shadow/umd/es5/synthetic-shadow_debug.js +23 -36
  30. package/dist/wire-service/esm/es2017/wire-service.js +2 -2
  31. package/dist/wire-service/iife/es2017/wire-service.js +2 -2
  32. package/dist/wire-service/iife/es2017/wire-service_debug.js +2 -2
  33. package/dist/wire-service/iife/es5/wire-service.js +2 -2
  34. package/dist/wire-service/iife/es5/wire-service_debug.js +2 -2
  35. package/dist/wire-service/umd/es2017/wire-service.js +2 -2
  36. package/dist/wire-service/umd/es2017/wire-service_debug.js +2 -2
  37. package/dist/wire-service/umd/es5/wire-service.js +2 -2
  38. package/dist/wire-service/umd/es5/wire-service_debug.js +2 -2
  39. package/package.json +7 -7
@@ -45,7 +45,7 @@ const { assign, create, defineProperties, defineProperty, freeze, getOwnProperty
45
45
  const { isArray: isArray$1 } = Array;
46
46
  const { concat: ArrayConcat$1, copyWithin: ArrayCopyWithin, fill: ArrayFill, filter: ArrayFilter, find: ArrayFind, indexOf: ArrayIndexOf, join: ArrayJoin, map: ArrayMap, pop: ArrayPop, push: ArrayPush$1, reduce: ArrayReduce, reverse: ArrayReverse, shift: ArrayShift, slice: ArraySlice, some: ArraySome, sort: ArraySort, splice: ArraySplice, unshift: ArrayUnshift, forEach, } = Array.prototype;
47
47
  const { fromCharCode: StringFromCharCode } = String;
48
- const { charCodeAt: StringCharCodeAt, replace: StringReplace, slice: StringSlice, toLowerCase: StringToLowerCase, } = String.prototype;
48
+ const { charCodeAt: StringCharCodeAt, replace: StringReplace, split: StringSplit, slice: StringSlice, toLowerCase: StringToLowerCase, } = String.prototype;
49
49
  function isUndefined$1(obj) {
50
50
  return obj === undefined;
51
51
  }
@@ -192,41 +192,11 @@ function isAriaAttribute(attrName) {
192
192
  * SPDX-License-Identifier: MIT
193
193
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
194
194
  */
195
- // Inspired from: https://mathiasbynens.be/notes/globalthis
196
- const _globalThis = /*@__PURE__*/ (function () {
197
- // On recent browsers, `globalThis` is already defined. In this case return it directly.
198
- if (typeof globalThis === 'object') {
199
- return globalThis;
200
- }
201
- let _globalThis;
202
- try {
203
- // eslint-disable-next-line no-extend-native
204
- Object.defineProperty(Object.prototype, '__magic__', {
205
- get: function () {
206
- return this;
207
- },
208
- configurable: true,
209
- });
210
- // __magic__ is undefined in Safari 10 and IE10 and older.
211
- // @ts-ignore
212
- // eslint-disable-next-line no-undef
213
- _globalThis = __magic__;
214
- // @ts-ignore
215
- delete Object.prototype.__magic__;
216
- }
217
- catch (ex) {
218
- // In IE8, Object.defineProperty only works on DOM objects.
219
- }
220
- finally {
221
- // If the magic above fails for some reason we assume that we are in a legacy browser.
222
- // Assume `window` exists in this case.
223
- if (typeof _globalThis === 'undefined') {
224
- // @ts-ignore
225
- _globalThis = window;
226
- }
227
- }
228
- return _globalThis;
229
- })();
195
+ // See browser support for globalThis:
196
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#browser_compatibility
197
+ /* istanbul ignore next */
198
+ // @ts-ignore
199
+ const _globalThis = typeof globalThis === 'object' ? globalThis : window;
230
200
  const KEY__SHADOW_RESOLVER = '$shadowResolver$';
231
201
  const KEY__SHADOW_STATIC = '$shadowStaticNode$';
232
202
  const KEY__SYNTHETIC_MODE = '$$lwc-synthetic-mode';
@@ -470,9 +440,9 @@ function htmlEscape(str, attrMode = false) {
470
440
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
471
441
  */
472
442
  // Increment whenever the LWC template compiler changes
473
- const LWC_VERSION = "2.34.0";
443
+ const LWC_VERSION = "2.35.1";
474
444
  const LWC_VERSION_COMMENT_REGEX = /\/\*LWC compiler v([\d.]+)\*\/\s*}/;
475
- /** version: 2.34.0 */
445
+ /** version: 2.35.1 */
476
446
 
477
447
  /*
478
448
  * Copyright (c) 2020, salesforce.com, inc.
@@ -557,7 +527,8 @@ function setFeatureFlag(name, value) {
557
527
  console.warn(`Failed to set the value "${value}" for the runtime feature flag "${name}" because it is undefined. Available flags: ${availableFlags}.`);
558
528
  return;
559
529
  }
560
- if (process.env.NODE_ENV !== 'production') {
530
+ // This may seem redundant, but `process.env.NODE_ENV === 'test-karma-lwc'` is replaced by Karma tests
531
+ if (process.env.NODE_ENV === 'test-karma-lwc' || process.env.NODE_ENV !== 'production') {
561
532
  // Allow the same flag to be set more than once outside of production to enable testing
562
533
  lwcRuntimeFlags[name] = value;
563
534
  }
@@ -577,11 +548,12 @@ function setFeatureFlag(name, value) {
577
548
  * purposes. It is a no-op when invoked in production mode.
578
549
  */
579
550
  function setFeatureFlagForTest(name, value) {
580
- if (process.env.NODE_ENV !== 'production') {
551
+ // This may seem redundant, but `process.env.NODE_ENV === 'test-karma-lwc'` is replaced by Karma tests
552
+ if (process.env.NODE_ENV === 'test-karma-lwc' || process.env.NODE_ENV !== 'production') {
581
553
  setFeatureFlag(name, value);
582
554
  }
583
555
  }
584
- /** version: 2.34.0 */
556
+ /** version: 2.35.1 */
585
557
 
586
558
  /**
587
559
  * Copyright (C) 2018 salesforce.com, inc.
@@ -645,7 +617,7 @@ function applyAriaReflection(prototype = Element.prototype) {
645
617
  }
646
618
  }
647
619
  }
648
- /** version: 2.34.0 */
620
+ /** version: 2.35.1 */
649
621
 
650
622
  /* proxy-compat-disable */
651
623
 
@@ -656,8 +628,7 @@ function applyAriaReflection(prototype = Element.prototype) {
656
628
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
657
629
  */
658
630
  // Only used in LWC's Karma tests
659
- // @ts-ignore
660
- if (process.env.NODE_ENV !== 'production' && typeof __karma__ !== 'undefined') {
631
+ if (process.env.NODE_ENV === 'test-karma-lwc') {
661
632
  window.addEventListener('test-dummy-flag', () => {
662
633
  let hasFlag = false;
663
634
  if (lwcRuntimeFlags.DUMMY_TEST_FLAG) {
@@ -671,87 +642,6 @@ if (process.env.NODE_ENV !== 'production' && typeof __karma__ !== 'undefined') {
671
642
  }));
672
643
  });
673
644
  }
674
- const SPACE_CHAR = 32;
675
- const EmptyObject = seal(create(null));
676
- const EmptyArray = seal([]);
677
- function guid() {
678
- function s4() {
679
- return Math.floor((1 + Math.random()) * 0x10000)
680
- .toString(16)
681
- .substring(1);
682
- }
683
- return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
684
- }
685
- function flattenStylesheets(stylesheets) {
686
- const list = [];
687
- for (const stylesheet of stylesheets) {
688
- if (!Array.isArray(stylesheet)) {
689
- list.push(stylesheet);
690
- }
691
- else {
692
- list.push(...flattenStylesheets(stylesheet));
693
- }
694
- }
695
- return list;
696
- }
697
- // Set a ref (lwc:ref) on a VM, from a template API
698
- function setRefVNode(vm, ref, vnode) {
699
- if (process.env.NODE_ENV !== 'production' && isUndefined$1(vm.refVNodes)) {
700
- throw new Error('refVNodes must be defined when setting a ref');
701
- }
702
- // If this method is called, then vm.refVNodes is set as the template has refs.
703
- // If not, then something went wrong and we threw an error above.
704
- const refVNodes = vm.refVNodes;
705
- // In cases of conflict (two elements with the same ref), prefer, the last one,
706
- // in depth-first traversal order.
707
- if (!(ref in refVNodes) || refVNodes[ref].key < vnode.key) {
708
- refVNodes[ref] = vnode;
709
- }
710
- }
711
-
712
- /*
713
- * Copyright (c) 2019, salesforce.com, inc.
714
- * All rights reserved.
715
- * SPDX-License-Identifier: MIT
716
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
717
- */
718
- const TargetToReactiveRecordMap = new WeakMap();
719
- function valueMutated(target, key) {
720
- const reactiveRecord = TargetToReactiveRecordMap.get(target);
721
- if (!isUndefined$1(reactiveRecord)) {
722
- const reactiveObservers = reactiveRecord[key];
723
- if (!isUndefined$1(reactiveObservers)) {
724
- for (let i = 0, len = reactiveObservers.length; i < len; i += 1) {
725
- const ro = reactiveObservers[i];
726
- ro.notify();
727
- }
728
- }
729
- }
730
- }
731
- function valueObserved(target, key) {
732
- // We should determine if an active Observing Record is present to track mutations.
733
- {
734
- return;
735
- }
736
- }
737
-
738
- /*
739
- * Copyright (c) 2018, salesforce.com, inc.
740
- * All rights reserved.
741
- * SPDX-License-Identifier: MIT
742
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
743
- */
744
- const DUMMY_REACTIVE_OBSERVER = {
745
- observe(job) {
746
- job();
747
- },
748
- reset() { },
749
- link() { },
750
- };
751
- function createReactiveObserver(callback) {
752
- // On the server side, we don't need mutation tracking. Skipping it improves performance.
753
- return DUMMY_REACTIVE_OBSERVER;
754
- }
755
645
 
756
646
  /*
757
647
  * Copyright (c) 2018, salesforce.com, inc.
@@ -806,11 +696,25 @@ function addErrorComponentStack(vm, error) {
806
696
  * SPDX-License-Identifier: MIT
807
697
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
808
698
  */
809
- function log(method, message, vm) {
699
+ const alreadyLoggedMessages = new Set();
700
+ // Only used in LWC's Karma tests
701
+ if (process.env.NODE_ENV === 'test-karma-lwc') {
702
+ // @ts-ignore
703
+ window.__lwcResetAlreadyLoggedMessages = () => {
704
+ alreadyLoggedMessages.clear();
705
+ };
706
+ }
707
+ function log(method, message, vm, once) {
810
708
  let msg = `[LWC ${method}]: ${message}`;
811
709
  if (!isUndefined$1(vm)) {
812
710
  msg = `${msg}\n${getComponentStack(vm)}`;
813
711
  }
712
+ if (once) {
713
+ if (alreadyLoggedMessages.has(msg)) {
714
+ return;
715
+ }
716
+ alreadyLoggedMessages.add(msg);
717
+ }
814
718
  // In Jest tests, reduce the warning and error verbosity by not printing the callstack
815
719
  if (process.env.NODE_ENV === 'test') {
816
720
  /* eslint-disable-next-line no-console */
@@ -826,7 +730,96 @@ function log(method, message, vm) {
826
730
  }
827
731
  }
828
732
  function logError(message, vm) {
829
- log('error', message, vm);
733
+ log('error', message, vm, false);
734
+ }
735
+
736
+ /*
737
+ * Copyright (c) 2019, salesforce.com, inc.
738
+ * All rights reserved.
739
+ * SPDX-License-Identifier: MIT
740
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
741
+ */
742
+ const TargetToReactiveRecordMap = new WeakMap();
743
+ function valueMutated(target, key) {
744
+ const reactiveRecord = TargetToReactiveRecordMap.get(target);
745
+ if (!isUndefined$1(reactiveRecord)) {
746
+ const reactiveObservers = reactiveRecord[key];
747
+ if (!isUndefined$1(reactiveObservers)) {
748
+ for (let i = 0, len = reactiveObservers.length; i < len; i += 1) {
749
+ const ro = reactiveObservers[i];
750
+ ro.notify();
751
+ }
752
+ }
753
+ }
754
+ }
755
+ function valueObserved(target, key) {
756
+ // We should determine if an active Observing Record is present to track mutations.
757
+ {
758
+ return;
759
+ }
760
+ }
761
+
762
+ /*
763
+ * Copyright (c) 2018, salesforce.com, inc.
764
+ * All rights reserved.
765
+ * SPDX-License-Identifier: MIT
766
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
767
+ */
768
+ const DUMMY_REACTIVE_OBSERVER = {
769
+ observe(job) {
770
+ job();
771
+ },
772
+ reset() { },
773
+ link() { },
774
+ };
775
+ function createReactiveObserver(callback) {
776
+ // On the server side, we don't need mutation tracking. Skipping it improves performance.
777
+ return DUMMY_REACTIVE_OBSERVER;
778
+ }
779
+ const SPACE_CHAR = 32;
780
+ const EmptyObject = seal(create(null));
781
+ const EmptyArray = seal([]);
782
+ function guid() {
783
+ function s4() {
784
+ return Math.floor((1 + Math.random()) * 0x10000)
785
+ .toString(16)
786
+ .substring(1);
787
+ }
788
+ return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
789
+ }
790
+ function flattenStylesheets(stylesheets) {
791
+ const list = [];
792
+ for (const stylesheet of stylesheets) {
793
+ if (!Array.isArray(stylesheet)) {
794
+ list.push(stylesheet);
795
+ }
796
+ else {
797
+ list.push(...flattenStylesheets(stylesheet));
798
+ }
799
+ }
800
+ return list;
801
+ }
802
+ // Set a ref (lwc:ref) on a VM, from a template API
803
+ function setRefVNode(vm, ref, vnode) {
804
+ if (process.env.NODE_ENV !== 'production' && isUndefined$1(vm.refVNodes)) {
805
+ throw new Error('refVNodes must be defined when setting a ref');
806
+ }
807
+ // If this method is called, then vm.refVNodes is set as the template has refs.
808
+ // If not, then something went wrong and we threw an error above.
809
+ const refVNodes = vm.refVNodes;
810
+ // In cases of conflict (two elements with the same ref), prefer, the last one,
811
+ // in depth-first traversal order.
812
+ if (!(ref in refVNodes) || refVNodes[ref].key < vnode.key) {
813
+ refVNodes[ref] = vnode;
814
+ }
815
+ }
816
+ // Throw an error if we're running in prod mode. Ensures code is truly removed from prod mode.
817
+ function assertNotProd() {
818
+ /* istanbul ignore if */
819
+ if (process.env.NODE_ENV === 'production') {
820
+ // this method should never leak to prod
821
+ throw new ReferenceError();
822
+ }
830
823
  }
831
824
 
832
825
  /*
@@ -881,7 +874,7 @@ function offsetPropertyErrorMessage(name) {
881
874
  //
882
875
  // If you update this list, check for test files that recapitulate the same list. Searching the codebase
883
876
  // for e.g. "dropzone" should suffice.
884
- const globalHTMLProperties = assign(create(null), {
877
+ const globalHTMLProperties = {
885
878
  accessKey: {
886
879
  attribute: 'accesskey',
887
880
  },
@@ -966,7 +959,7 @@ const globalHTMLProperties = assign(create(null), {
966
959
  role: {
967
960
  attribute: 'role',
968
961
  },
969
- });
962
+ };
970
963
  let controlledElement = null;
971
964
  let controlledAttributeName;
972
965
  function isAttributeLocked(elm, attrName) {
@@ -1033,27 +1026,18 @@ function generateAccessorDescriptor(options) {
1033
1026
  }
1034
1027
  let isDomMutationAllowed = false;
1035
1028
  function unlockDomMutation() {
1036
- if (process.env.NODE_ENV === 'production') {
1037
- // this method should never leak to prod
1038
- throw new ReferenceError();
1039
- }
1029
+ assertNotProd(); // this method should never leak to prod
1040
1030
  isDomMutationAllowed = true;
1041
1031
  }
1042
1032
  function lockDomMutation() {
1043
- if (process.env.NODE_ENV === 'production') {
1044
- // this method should never leak to prod
1045
- throw new ReferenceError();
1046
- }
1033
+ assertNotProd(); // this method should never leak to prod
1047
1034
  isDomMutationAllowed = false;
1048
1035
  }
1049
1036
  function logMissingPortalError(name, type) {
1050
1037
  return logError(`The \`${name}\` ${type} is available only on elements that use the \`lwc:dom="manual"\` directive.`);
1051
1038
  }
1052
1039
  function patchElementWithRestrictions(elm, options) {
1053
- if (process.env.NODE_ENV === 'production') {
1054
- // this method should never leak to prod
1055
- throw new ReferenceError();
1056
- }
1040
+ assertNotProd(); // this method should never leak to prod
1057
1041
  const originalOuterHTMLDescriptor = getPropertyDescriptor(elm, 'outerHTML');
1058
1042
  const descriptors = {
1059
1043
  outerHTML: generateAccessorDescriptor({
@@ -1134,10 +1118,7 @@ function patchElementWithRestrictions(elm, options) {
1134
1118
  defineProperties(elm, descriptors);
1135
1119
  }
1136
1120
  function getShadowRootRestrictionsDescriptors(sr) {
1137
- if (process.env.NODE_ENV === 'production') {
1138
- // this method should never leak to prod
1139
- throw new ReferenceError();
1140
- }
1121
+ assertNotProd(); // this method should never leak to prod
1141
1122
  // Disallowing properties in dev mode only to avoid people doing the wrong
1142
1123
  // thing when using the real shadow root, because if that's the case,
1143
1124
  // the component will not work when running with synthetic shadow.
@@ -1178,10 +1159,7 @@ function getShadowRootRestrictionsDescriptors(sr) {
1178
1159
  // Custom Elements Restrictions:
1179
1160
  // -----------------------------
1180
1161
  function getCustomElementRestrictionsDescriptors(elm) {
1181
- if (process.env.NODE_ENV === 'production') {
1182
- // this method should never leak to prod
1183
- throw new ReferenceError();
1184
- }
1162
+ assertNotProd(); // this method should never leak to prod
1185
1163
  const originalAddEventListener = elm.addEventListener;
1186
1164
  const originalInnerHTMLDescriptor = getPropertyDescriptor(elm, 'innerHTML');
1187
1165
  const originalOuterHTMLDescriptor = getPropertyDescriptor(elm, 'outerHTML');
@@ -1226,10 +1204,7 @@ function getCustomElementRestrictionsDescriptors(elm) {
1226
1204
  };
1227
1205
  }
1228
1206
  function getComponentRestrictionsDescriptors() {
1229
- if (process.env.NODE_ENV === 'production') {
1230
- // this method should never leak to prod
1231
- throw new ReferenceError();
1232
- }
1207
+ assertNotProd(); // this method should never leak to prod
1233
1208
  return {
1234
1209
  tagName: generateAccessorDescriptor({
1235
1210
  get() {
@@ -1243,10 +1218,7 @@ function getComponentRestrictionsDescriptors() {
1243
1218
  };
1244
1219
  }
1245
1220
  function getLightningElementPrototypeRestrictionsDescriptors(proto) {
1246
- if (process.env.NODE_ENV === 'production') {
1247
- // this method should never leak to prod
1248
- throw new ReferenceError();
1249
- }
1221
+ assertNotProd(); // this method should never leak to prod
1250
1222
  const originalDispatchEvent = proto.dispatchEvent;
1251
1223
  const descriptors = {
1252
1224
  dispatchEvent: generateDataDescriptor({
@@ -1951,7 +1923,6 @@ function createBridgeToElementDescriptor(propName, descriptor) {
1951
1923
  }
1952
1924
  };
1953
1925
  }
1954
- const EMPTY_REFS = freeze(create(null));
1955
1926
  const refsCache = new WeakMap();
1956
1927
  /**
1957
1928
  * This class is the base class for any LWC element.
@@ -2238,7 +2209,6 @@ LightningElement.prototype = {
2238
2209
  }
2239
2210
  const {
2240
2211
  refVNodes,
2241
- hasRefVNodes,
2242
2212
  cmpTemplate
2243
2213
  } = vm;
2244
2214
  // If the `cmpTemplate` is null, that means that the template has not been rendered yet. Most likely this occurs
@@ -2252,15 +2222,9 @@ LightningElement.prototype = {
2252
2222
  // were introduced, we return undefined if the template has no refs defined
2253
2223
  // anywhere. This fixes components that may want to add an expando called `refs`
2254
2224
  // and are checking if it exists with `if (this.refs)` before adding it.
2255
- // Note it is not sufficient to just check if `refVNodes` is null or empty,
2256
- // because a template may have `lwc:ref` defined within a falsy `if:true` block.
2257
- if (!hasRefVNodes) {
2258
- return;
2259
- }
2260
- // For templates that are using `lwc:ref`, if there are no refs currently available
2261
- // (e.g. refs inside of a falsy `if:true` block), we return an empty object.
2225
+ // Note we use a null refVNodes to indicate that the template has no refs defined.
2262
2226
  if (isNull(refVNodes)) {
2263
- return EMPTY_REFS;
2227
+ return;
2264
2228
  }
2265
2229
  // The refNodes can be cached based on the refVNodes, since the refVNodes
2266
2230
  // are recreated from scratch every time the template is rendered.
@@ -2419,16 +2383,291 @@ function createObservedFieldPropertyDescriptor(key) {
2419
2383
  * SPDX-License-Identifier: MIT
2420
2384
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2421
2385
  */
2422
- function api$1() {
2423
- if (process.env.NODE_ENV !== 'production') {
2424
- assert.fail(`@api decorator can only be used as a decorator function.`);
2425
- }
2426
- throw new Error();
2386
+ const DeprecatedWiredElementHost = '$$DeprecatedWiredElementHostKey$$';
2387
+ const DeprecatedWiredParamsMeta = '$$DeprecatedWiredParamsMetaKey$$';
2388
+ const WIRE_DEBUG_ENTRY = '@wire';
2389
+ const WireMetaMap = new Map();
2390
+ class WireContextRegistrationEvent extends CustomEvent {
2391
+ constructor(adapterToken, {
2392
+ setNewContext,
2393
+ setDisconnectedCallback
2394
+ }) {
2395
+ super(adapterToken, {
2396
+ bubbles: true,
2397
+ composed: true
2398
+ });
2399
+ defineProperties(this, {
2400
+ setNewContext: {
2401
+ value: setNewContext
2402
+ },
2403
+ setDisconnectedCallback: {
2404
+ value: setDisconnectedCallback
2405
+ }
2406
+ });
2407
+ }
2427
2408
  }
2428
- function createPublicPropertyDescriptor(key) {
2429
- return {
2430
- get() {
2431
- const vm = getAssociatedVM(this);
2409
+ function createFieldDataCallback(vm, name) {
2410
+ return value => {
2411
+ updateComponentValue(vm, name, value);
2412
+ };
2413
+ }
2414
+ function createMethodDataCallback(vm, method) {
2415
+ return value => {
2416
+ // dispatching new value into the wired method
2417
+ runWithBoundaryProtection(vm, vm.owner, noop, () => {
2418
+ // job
2419
+ method.call(vm.component, value);
2420
+ }, noop);
2421
+ };
2422
+ }
2423
+ function createConfigWatcher(component, configCallback, callbackWhenConfigIsReady) {
2424
+ // creating the reactive observer for reactive params when needed
2425
+ const ro = createReactiveObserver();
2426
+ const computeConfigAndUpdate = () => {
2427
+ let config;
2428
+ ro.observe(() => config = configCallback(component));
2429
+ // eslint-disable-next-line @lwc/lwc-internal/no-invalid-todo
2430
+ // TODO: dev-mode validation of config based on the adapter.configSchema
2431
+ // @ts-ignore it is assigned in the observe() callback
2432
+ callbackWhenConfigIsReady(config);
2433
+ };
2434
+ return {
2435
+ computeConfigAndUpdate,
2436
+ ro
2437
+ };
2438
+ }
2439
+ function createContextWatcher(vm, wireDef, callbackWhenContextIsReady) {
2440
+ const {
2441
+ adapter
2442
+ } = wireDef;
2443
+ const adapterContextToken = getAdapterToken(adapter);
2444
+ if (isUndefined$1(adapterContextToken)) {
2445
+ return; // no provider found, nothing to be done
2446
+ }
2447
+
2448
+ const {
2449
+ elm,
2450
+ context: {
2451
+ wiredConnecting,
2452
+ wiredDisconnecting
2453
+ },
2454
+ renderer: {
2455
+ dispatchEvent
2456
+ }
2457
+ } = vm;
2458
+ // waiting for the component to be connected to formally request the context via the token
2459
+ ArrayPush$1.call(wiredConnecting, () => {
2460
+ // This event is responsible for connecting the host element with another
2461
+ // element in the composed path that is providing contextual data. The provider
2462
+ // must be listening for a special dom event with the name corresponding to the value of
2463
+ // `adapterContextToken`, which will remain secret and internal to this file only to
2464
+ // guarantee that the linkage can be forged.
2465
+ const contextRegistrationEvent = new WireContextRegistrationEvent(adapterContextToken, {
2466
+ setNewContext(newContext) {
2467
+ // eslint-disable-next-line @lwc/lwc-internal/no-invalid-todo
2468
+ // TODO: dev-mode validation of config based on the adapter.contextSchema
2469
+ callbackWhenContextIsReady(newContext);
2470
+ },
2471
+ setDisconnectedCallback(disconnectCallback) {
2472
+ // adds this callback into the disconnect bucket so it gets disconnected from parent
2473
+ // the the element hosting the wire is disconnected
2474
+ ArrayPush$1.call(wiredDisconnecting, disconnectCallback);
2475
+ }
2476
+ });
2477
+ dispatchEvent(elm, contextRegistrationEvent);
2478
+ });
2479
+ }
2480
+ function createConnector(vm, name, wireDef) {
2481
+ const {
2482
+ method,
2483
+ adapter,
2484
+ configCallback,
2485
+ dynamic
2486
+ } = wireDef;
2487
+ let debugInfo;
2488
+ if (process.env.NODE_ENV !== 'production') {
2489
+ const wiredPropOrMethod = isUndefined$1(method) ? name : method.name;
2490
+ debugInfo = create(null);
2491
+ debugInfo.wasDataProvisionedForConfig = false;
2492
+ vm.debugInfo[WIRE_DEBUG_ENTRY][wiredPropOrMethod] = debugInfo;
2493
+ }
2494
+ const fieldOrMethodCallback = isUndefined$1(method) ? createFieldDataCallback(vm, name) : createMethodDataCallback(vm, method);
2495
+ const dataCallback = value => {
2496
+ if (process.env.NODE_ENV !== 'production') {
2497
+ debugInfo.data = value;
2498
+ // Note: most of the time, the data provided is for the current config, but there may be
2499
+ // some conditions in which it does not, ex:
2500
+ // race conditions in a poor network while the adapter does not cancel a previous request.
2501
+ debugInfo.wasDataProvisionedForConfig = true;
2502
+ }
2503
+ fieldOrMethodCallback(value);
2504
+ };
2505
+ let context;
2506
+ let connector;
2507
+ // Workaround to pass the component element associated to this wire adapter instance.
2508
+ defineProperty(dataCallback, DeprecatedWiredElementHost, {
2509
+ value: vm.elm
2510
+ });
2511
+ defineProperty(dataCallback, DeprecatedWiredParamsMeta, {
2512
+ value: dynamic
2513
+ });
2514
+ runWithBoundaryProtection(vm, vm, noop, () => {
2515
+ // job
2516
+ connector = new adapter(dataCallback);
2517
+ }, noop);
2518
+ const updateConnectorConfig = config => {
2519
+ // every time the config is recomputed due to tracking,
2520
+ // this callback will be invoked with the new computed config
2521
+ runWithBoundaryProtection(vm, vm, noop, () => {
2522
+ // job
2523
+ if (process.env.NODE_ENV !== 'production') {
2524
+ debugInfo.config = config;
2525
+ debugInfo.context = context;
2526
+ debugInfo.wasDataProvisionedForConfig = false;
2527
+ }
2528
+ connector.update(config, context);
2529
+ }, noop);
2530
+ };
2531
+ // Computes the current wire config and calls the update method on the wire adapter.
2532
+ // If it has params, we will need to observe changes in the next tick.
2533
+ const {
2534
+ computeConfigAndUpdate,
2535
+ ro
2536
+ } = createConfigWatcher(vm.component, configCallback, updateConnectorConfig);
2537
+ // if the adapter needs contextualization, we need to watch for new context and push it alongside the config
2538
+ if (!isUndefined$1(adapter.contextSchema)) {
2539
+ createContextWatcher(vm, wireDef, newContext => {
2540
+ // every time the context is pushed into this component,
2541
+ // this callback will be invoked with the new computed context
2542
+ if (context !== newContext) {
2543
+ context = newContext;
2544
+ // Note: when new context arrives, the config will be recomputed and pushed along side the new
2545
+ // context, this is to preserve the identity characteristics, config should not have identity
2546
+ // (ever), while context can have identity
2547
+ if (vm.state === 1 /* VMState.connected */) {
2548
+ computeConfigAndUpdate();
2549
+ }
2550
+ }
2551
+ });
2552
+ }
2553
+ return {
2554
+ // @ts-ignore the boundary protection executes sync, connector is always defined
2555
+ connector,
2556
+ computeConfigAndUpdate,
2557
+ resetConfigWatcher: () => ro.reset()
2558
+ };
2559
+ }
2560
+ const AdapterToTokenMap = new Map();
2561
+ function getAdapterToken(adapter) {
2562
+ return AdapterToTokenMap.get(adapter);
2563
+ }
2564
+ function setAdapterToken(adapter, token) {
2565
+ AdapterToTokenMap.set(adapter, token);
2566
+ }
2567
+ function storeWiredMethodMeta(descriptor, adapter, configCallback, dynamic) {
2568
+ // support for callable adapters
2569
+ if (adapter.adapter) {
2570
+ adapter = adapter.adapter;
2571
+ }
2572
+ const method = descriptor.value;
2573
+ const def = {
2574
+ adapter,
2575
+ method,
2576
+ configCallback,
2577
+ dynamic
2578
+ };
2579
+ WireMetaMap.set(descriptor, def);
2580
+ }
2581
+ function storeWiredFieldMeta(descriptor, adapter, configCallback, dynamic) {
2582
+ // support for callable adapters
2583
+ if (adapter.adapter) {
2584
+ adapter = adapter.adapter;
2585
+ }
2586
+ const def = {
2587
+ adapter,
2588
+ configCallback,
2589
+ dynamic
2590
+ };
2591
+ WireMetaMap.set(descriptor, def);
2592
+ }
2593
+ function installWireAdapters(vm) {
2594
+ const {
2595
+ context,
2596
+ def: {
2597
+ wire
2598
+ }
2599
+ } = vm;
2600
+ if (process.env.NODE_ENV !== 'production') {
2601
+ vm.debugInfo[WIRE_DEBUG_ENTRY] = create(null);
2602
+ }
2603
+ const wiredConnecting = context.wiredConnecting = [];
2604
+ const wiredDisconnecting = context.wiredDisconnecting = [];
2605
+ for (const fieldNameOrMethod in wire) {
2606
+ const descriptor = wire[fieldNameOrMethod];
2607
+ const wireDef = WireMetaMap.get(descriptor);
2608
+ if (process.env.NODE_ENV !== 'production') {
2609
+ assert.invariant(wireDef, `Internal Error: invalid wire definition found.`);
2610
+ }
2611
+ if (!isUndefined$1(wireDef)) {
2612
+ const {
2613
+ connector,
2614
+ computeConfigAndUpdate,
2615
+ resetConfigWatcher
2616
+ } = createConnector(vm, fieldNameOrMethod, wireDef);
2617
+ const hasDynamicParams = wireDef.dynamic.length > 0;
2618
+ ArrayPush$1.call(wiredConnecting, () => {
2619
+ connector.connect();
2620
+ if (!lwcRuntimeFlags.ENABLE_WIRE_SYNC_EMIT) {
2621
+ if (hasDynamicParams) {
2622
+ Promise.resolve().then(computeConfigAndUpdate);
2623
+ return;
2624
+ }
2625
+ }
2626
+ computeConfigAndUpdate();
2627
+ });
2628
+ ArrayPush$1.call(wiredDisconnecting, () => {
2629
+ connector.disconnect();
2630
+ resetConfigWatcher();
2631
+ });
2632
+ }
2633
+ }
2634
+ }
2635
+ function connectWireAdapters(vm) {
2636
+ const {
2637
+ wiredConnecting
2638
+ } = vm.context;
2639
+ for (let i = 0, len = wiredConnecting.length; i < len; i += 1) {
2640
+ wiredConnecting[i]();
2641
+ }
2642
+ }
2643
+ function disconnectWireAdapters(vm) {
2644
+ const {
2645
+ wiredDisconnecting
2646
+ } = vm.context;
2647
+ runWithBoundaryProtection(vm, vm, noop, () => {
2648
+ // job
2649
+ for (let i = 0, len = wiredDisconnecting.length; i < len; i += 1) {
2650
+ wiredDisconnecting[i]();
2651
+ }
2652
+ }, noop);
2653
+ }
2654
+
2655
+ /*
2656
+ * Copyright (c) 2018, salesforce.com, inc.
2657
+ * All rights reserved.
2658
+ * SPDX-License-Identifier: MIT
2659
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2660
+ */
2661
+ function api$1() {
2662
+ if (process.env.NODE_ENV !== 'production') {
2663
+ assert.fail(`@api decorator can only be used as a decorator function.`);
2664
+ }
2665
+ throw new Error();
2666
+ }
2667
+ function createPublicPropertyDescriptor(key) {
2668
+ return {
2669
+ get() {
2670
+ const vm = getAssociatedVM(this);
2432
2671
  if (isBeingConstructed(vm)) {
2433
2672
  if (process.env.NODE_ENV !== 'production') {
2434
2673
  logError(`Can’t read the value of property \`${toString$1(key)}\` from the constructor because the owner component hasn’t set the value yet. Instead, use the constructor to set a default value for the property.`, vm);
@@ -2784,8 +3023,8 @@ function getDecoratorsMeta(Ctor) {
2784
3023
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2785
3024
  */
2786
3025
  let warned = false;
2787
- // @ts-ignore
2788
- if (process.env.NODE_ENV !== 'production' && typeof __karma__ !== 'undefined') {
3026
+ // Only used in LWC's Karma tests
3027
+ if (process.env.NODE_ENV === 'test-karma-lwc') {
2789
3028
  // @ts-ignore
2790
3029
  window.__lwcResetWarnedOnVersionMismatch = () => {
2791
3030
  warned = false;
@@ -3007,10 +3246,7 @@ const activeTemplates = new WeakMap();
3007
3246
  const activeComponents = new WeakMap();
3008
3247
  const activeStyles = new WeakMap();
3009
3248
  function getTemplateOrSwappedTemplate(tpl) {
3010
- if (process.env.NODE_ENV === 'production') {
3011
- // this method should never leak to prod
3012
- throw new ReferenceError();
3013
- }
3249
+ assertNotProd(); // this method should never leak to prod
3014
3250
  const visited = new Set();
3015
3251
  while (swappedTemplateMap.has(tpl) && !visited.has(tpl)) {
3016
3252
  visited.add(tpl);
@@ -3019,10 +3255,7 @@ function getTemplateOrSwappedTemplate(tpl) {
3019
3255
  return tpl;
3020
3256
  }
3021
3257
  function getComponentOrSwappedComponent(Ctor) {
3022
- if (process.env.NODE_ENV === 'production') {
3023
- // this method should never leak to prod
3024
- throw new ReferenceError();
3025
- }
3258
+ assertNotProd(); // this method should never leak to prod
3026
3259
  const visited = new Set();
3027
3260
  while (swappedComponentMap.has(Ctor) && !visited.has(Ctor)) {
3028
3261
  visited.add(Ctor);
@@ -3031,10 +3264,7 @@ function getComponentOrSwappedComponent(Ctor) {
3031
3264
  return Ctor;
3032
3265
  }
3033
3266
  function getStyleOrSwappedStyle(style) {
3034
- if (process.env.NODE_ENV === 'production') {
3035
- // this method should never leak to prod
3036
- throw new ReferenceError();
3037
- }
3267
+ assertNotProd(); // this method should never leak to prod
3038
3268
  const visited = new Set();
3039
3269
  while (swappedStyleMap.has(style) && !visited.has(style)) {
3040
3270
  visited.add(style);
@@ -3043,10 +3273,7 @@ function getStyleOrSwappedStyle(style) {
3043
3273
  return style;
3044
3274
  }
3045
3275
  function setActiveVM(vm) {
3046
- if (process.env.NODE_ENV === 'production') {
3047
- // this method should never leak to prod
3048
- throw new ReferenceError();
3049
- }
3276
+ assertNotProd(); // this method should never leak to prod
3050
3277
  // tracking active component
3051
3278
  const Ctor = vm.def.ctor;
3052
3279
  let componentVMs = activeComponents.get(Ctor);
@@ -3089,10 +3316,7 @@ function setActiveVM(vm) {
3089
3316
  }
3090
3317
  }
3091
3318
  function removeActiveVM(vm) {
3092
- if (process.env.NODE_ENV === 'production') {
3093
- // this method should never leak to prod
3094
- throw new ReferenceError();
3095
- }
3319
+ assertNotProd(); // this method should never leak to prod
3096
3320
  // tracking inactive component
3097
3321
  const Ctor = vm.def.ctor;
3098
3322
  let list = activeComponents.get(Ctor);
@@ -5143,10 +5367,7 @@ function setVMBeingRendered(vm) {
5143
5367
  vmBeingRendered = vm;
5144
5368
  }
5145
5369
  function validateSlots(vm, html) {
5146
- if (process.env.NODE_ENV === 'production') {
5147
- // this method should never leak to prod
5148
- throw new ReferenceError();
5149
- }
5370
+ assertNotProd(); // this method should never leak to prod
5150
5371
  const { cmpSlots } = vm;
5151
5372
  const { slots = EmptyArray } = html;
5152
5373
  for (const slotName in cmpSlots.slotAssignments) {
@@ -5275,9 +5496,7 @@ function evaluateTemplate(vm, html) {
5275
5496
  setActiveVM(vm);
5276
5497
  }
5277
5498
  // reset the refs; they will be set during the tmpl() instantiation
5278
- const hasRefVNodes = Boolean(html.hasRefs);
5279
- vm.hasRefVNodes = hasRefVNodes;
5280
- vm.refVNodes = hasRefVNodes ? create(null) : null;
5499
+ vm.refVNodes = html.hasRefs ? create(null) : null;
5281
5500
  // right before producing the vnodes, we clear up all internal references
5282
5501
  // to custom elements from the template.
5283
5502
  vm.velements = [];
@@ -5292,220 +5511,32 @@ function evaluateTemplate(vm, html) {
5292
5511
  }, () => {
5293
5512
  // post
5294
5513
  isUpdatingTemplate = isUpdatingTemplateInception;
5295
- vmBeingRendered = vmOfTemplateBeingUpdatedInception;
5296
- logOperationEnd(1 /* OperationId.Render */, vm);
5297
- });
5298
- if (process.env.NODE_ENV !== 'production') {
5299
- assert.invariant(isArray$1(vnodes), `Compiler should produce html functions that always return an array.`);
5300
- }
5301
- return vnodes;
5302
- }
5303
- function computeHasScopedStylesInStylesheets(stylesheets) {
5304
- if (hasStyles(stylesheets)) {
5305
- for (let i = 0; i < stylesheets.length; i++) {
5306
- if (isTrue(stylesheets[i][KEY__SCOPED_CSS])) {
5307
- return true;
5308
- }
5309
- }
5310
- }
5311
- return false;
5312
- }
5313
- function computeHasScopedStyles(template, vm) {
5314
- const { stylesheets } = template;
5315
- const vmStylesheets = !isUndefined$1(vm) ? vm.stylesheets : null;
5316
- return (computeHasScopedStylesInStylesheets(stylesheets) ||
5317
- computeHasScopedStylesInStylesheets(vmStylesheets));
5318
- }
5319
- function hasStyles(stylesheets) {
5320
- return !isUndefined$1(stylesheets) && !isNull(stylesheets) && stylesheets.length > 0;
5321
- }
5322
-
5323
- /*
5324
- * Copyright (c) 2018, salesforce.com, inc.
5325
- * All rights reserved.
5326
- * SPDX-License-Identifier: MIT
5327
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5328
- */
5329
- let isInvokingRender = false;
5330
- let vmBeingConstructed = null;
5331
- function isBeingConstructed(vm) {
5332
- return vmBeingConstructed === vm;
5333
- }
5334
- function invokeComponentCallback(vm, fn, args) {
5335
- const { component, callHook, owner } = vm;
5336
- runWithBoundaryProtection(vm, owner, noop, () => {
5337
- callHook(component, fn, args);
5338
- }, noop);
5339
- }
5340
- function invokeComponentConstructor(vm, Ctor) {
5341
- const vmBeingConstructedInception = vmBeingConstructed;
5342
- let error;
5343
- logOperationStart(0 /* OperationId.Constructor */, vm);
5344
- vmBeingConstructed = vm;
5345
- /**
5346
- * Constructors don't need to be wrapped with a boundary because for root elements
5347
- * it should throw, while elements from template are already wrapped by a boundary
5348
- * associated to the diffing algo.
5349
- */
5350
- try {
5351
- // job
5352
- const result = new Ctor();
5353
- // Check indirectly if the constructor result is an instance of LightningElement. Using
5354
- // the "instanceof" operator would not work here since Locker Service provides its own
5355
- // implementation of LightningElement, so we indirectly check if the base constructor is
5356
- // invoked by accessing the component on the vm.
5357
- if (vmBeingConstructed.component !== result) {
5358
- throw new TypeError('Invalid component constructor, the class should extend LightningElement.');
5359
- }
5360
- }
5361
- catch (e) {
5362
- error = Object(e);
5363
- }
5364
- finally {
5365
- logOperationEnd(0 /* OperationId.Constructor */, vm);
5366
- vmBeingConstructed = vmBeingConstructedInception;
5367
- if (!isUndefined$1(error)) {
5368
- addErrorComponentStack(vm, error);
5369
- // re-throwing the original error annotated after restoring the context
5370
- throw error; // eslint-disable-line no-unsafe-finally
5371
- }
5372
- }
5373
- }
5374
- function invokeComponentRenderMethod(vm) {
5375
- const { def: { render }, callHook, component, owner, } = vm;
5376
- const isRenderBeingInvokedInception = isInvokingRender;
5377
- const vmBeingRenderedInception = getVMBeingRendered();
5378
- let html;
5379
- let renderInvocationSuccessful = false;
5380
- runWithBoundaryProtection(vm, owner, () => {
5381
- // pre
5382
- isInvokingRender = true;
5383
- setVMBeingRendered(vm);
5384
- }, () => {
5385
- // job
5386
- vm.tro.observe(() => {
5387
- html = callHook(component, render);
5388
- renderInvocationSuccessful = true;
5389
- });
5390
- }, () => {
5391
- // post
5392
- isInvokingRender = isRenderBeingInvokedInception;
5393
- setVMBeingRendered(vmBeingRenderedInception);
5394
- });
5395
- // If render() invocation failed, process errorCallback in boundary and return an empty template
5396
- return renderInvocationSuccessful ? evaluateTemplate(vm, html) : [];
5397
- }
5398
- function invokeEventListener(vm, fn, thisValue, event) {
5399
- const { callHook, owner } = vm;
5400
- runWithBoundaryProtection(vm, owner, noop, () => {
5401
- // job
5402
- if (process.env.NODE_ENV !== 'production') {
5403
- assert.isTrue(isFunction$1(fn), `Invalid event handler for event '${event.type}' on ${vm}.`);
5404
- }
5405
- callHook(thisValue, fn, [event]);
5406
- }, noop);
5407
- }
5408
-
5409
- /*
5410
- * Copyright (c) 2018, salesforce.com, inc.
5411
- * All rights reserved.
5412
- * SPDX-License-Identifier: MIT
5413
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5414
- */
5415
- const signedTemplateMap = new Map();
5416
- /**
5417
- * INTERNAL: This function can only be invoked by compiled code. The compiler
5418
- * will prevent this function from being imported by userland code.
5419
- */
5420
- function registerComponent(
5421
- // We typically expect a LightningElementConstructor, but technically you can call this with anything
5422
- Ctor, { tmpl }) {
5423
- if (isFunction$1(Ctor)) {
5424
- if (process.env.NODE_ENV !== 'production') {
5425
- checkVersionMismatch(Ctor, 'component');
5426
- }
5427
- signedTemplateMap.set(Ctor, tmpl);
5428
- }
5429
- // chaining this method as a way to wrap existing assignment of component constructor easily,
5430
- // without too much transformation
5431
- return Ctor;
5432
- }
5433
- function getComponentRegisteredTemplate(Ctor) {
5434
- return signedTemplateMap.get(Ctor);
5435
- }
5436
- function getTemplateReactiveObserver(vm) {
5437
- return createReactiveObserver();
5438
- }
5439
- function renderComponent$1(vm) {
5440
- if (process.env.NODE_ENV !== 'production') {
5441
- assert.invariant(vm.isDirty, `${vm} is not dirty.`);
5442
- }
5443
- vm.tro.reset();
5444
- const vnodes = invokeComponentRenderMethod(vm);
5445
- vm.isDirty = false;
5446
- vm.isScheduled = false;
5447
- return vnodes;
5448
- }
5449
- function markComponentAsDirty(vm) {
5450
- if (process.env.NODE_ENV !== 'production') {
5451
- const vmBeingRendered = getVMBeingRendered();
5452
- assert.isFalse(vm.isDirty, `markComponentAsDirty() for ${vm} should not be called when the component is already dirty.`);
5453
- assert.isFalse(isInvokingRender, `markComponentAsDirty() for ${vm} cannot be called during rendering of ${vmBeingRendered}.`);
5454
- assert.isFalse(isUpdatingTemplate, `markComponentAsDirty() for ${vm} cannot be called while updating template of ${vmBeingRendered}.`);
5455
- }
5456
- vm.isDirty = true;
5457
- }
5458
- const cmpEventListenerMap = new WeakMap();
5459
- function getWrappedComponentsListener(vm, listener) {
5460
- if (!isFunction$1(listener)) {
5461
- throw new TypeError(); // avoiding problems with non-valid listeners
5462
- }
5463
- let wrappedListener = cmpEventListenerMap.get(listener);
5464
- if (isUndefined$1(wrappedListener)) {
5465
- wrappedListener = function (event) {
5466
- invokeEventListener(vm, listener, undefined, event);
5467
- };
5468
- cmpEventListenerMap.set(listener, wrappedListener);
5469
- }
5470
- return wrappedListener;
5471
- }
5472
-
5473
- /*
5474
- * Copyright (c) 2018, salesforce.com, inc.
5475
- * All rights reserved.
5476
- * SPDX-License-Identifier: MIT
5477
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5478
- */
5479
- const Services = create(null);
5480
- const hooks = ['rendered', 'connected', 'disconnected'];
5481
- /**
5482
- * EXPERIMENTAL: This function allows for the registration of "services"
5483
- * in LWC by exposing hooks into the component life-cycle. This API is
5484
- * subject to change or being removed.
5485
- */
5486
- function register(service) {
5514
+ vmBeingRendered = vmOfTemplateBeingUpdatedInception;
5515
+ logOperationEnd(1 /* OperationId.Render */, vm);
5516
+ });
5487
5517
  if (process.env.NODE_ENV !== 'production') {
5488
- assert.isTrue(isObject(service), `Invalid service declaration, ${service}: service must be an object`);
5518
+ assert.invariant(isArray$1(vnodes), `Compiler should produce html functions that always return an array.`);
5489
5519
  }
5490
- for (let i = 0; i < hooks.length; ++i) {
5491
- const hookName = hooks[i];
5492
- if (hookName in service) {
5493
- let l = Services[hookName];
5494
- if (isUndefined$1(l)) {
5495
- Services[hookName] = l = [];
5520
+ return vnodes;
5521
+ }
5522
+ function computeHasScopedStylesInStylesheets(stylesheets) {
5523
+ if (hasStyles(stylesheets)) {
5524
+ for (let i = 0; i < stylesheets.length; i++) {
5525
+ if (isTrue(stylesheets[i][KEY__SCOPED_CSS])) {
5526
+ return true;
5496
5527
  }
5497
- ArrayPush$1.call(l, service[hookName]);
5498
5528
  }
5499
5529
  }
5530
+ return false;
5500
5531
  }
5501
- function invokeServiceHook(vm, cbs) {
5502
- if (process.env.NODE_ENV !== 'production') {
5503
- assert.isTrue(isArray$1(cbs) && cbs.length > 0, `Optimize invokeServiceHook() to be invoked only when needed`);
5504
- }
5505
- const { component, def, context } = vm;
5506
- for (let i = 0, len = cbs.length; i < len; ++i) {
5507
- cbs[i].call(undefined, component, {}, def, context);
5508
- }
5532
+ function computeHasScopedStyles(template, vm) {
5533
+ const { stylesheets } = template;
5534
+ const vmStylesheets = !isUndefined$1(vm) ? vm.stylesheets : null;
5535
+ return (computeHasScopedStylesInStylesheets(stylesheets) ||
5536
+ computeHasScopedStylesInStylesheets(vmStylesheets));
5537
+ }
5538
+ function hasStyles(stylesheets) {
5539
+ return !isUndefined$1(stylesheets) && !isNull(stylesheets) && stylesheets.length > 0;
5509
5540
  }
5510
5541
 
5511
5542
  /*
@@ -5514,742 +5545,654 @@ function invokeServiceHook(vm, cbs) {
5514
5545
  * SPDX-License-Identifier: MIT
5515
5546
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5516
5547
  */
5517
- let idx = 0;
5518
- /** The internal slot used to associate different objects the engine manipulates with the VM */
5519
- const ViewModelReflection = new WeakMap();
5520
- function callHook(cmp, fn, args = []) {
5521
- return fn.apply(cmp, args);
5522
- }
5523
- function setHook(cmp, prop, newValue) {
5524
- cmp[prop] = newValue;
5525
- }
5526
- function getHook(cmp, prop) {
5527
- return cmp[prop];
5528
- }
5529
- function rerenderVM(vm) {
5530
- rehydrate(vm);
5531
- }
5532
- function connectRootElement(elm) {
5533
- const vm = getAssociatedVM(elm);
5534
- logGlobalOperationStart(7 /* OperationId.GlobalHydrate */, vm);
5535
- // Usually means moving the element from one place to another, which is observable via
5536
- // life-cycle hooks.
5537
- if (vm.state === 1 /* VMState.connected */) {
5538
- disconnectRootElement(elm);
5539
- }
5540
- runConnectedCallback(vm);
5541
- rehydrate(vm);
5542
- logGlobalOperationEnd(7 /* OperationId.GlobalHydrate */, vm);
5543
- }
5544
- function disconnectRootElement(elm) {
5545
- const vm = getAssociatedVM(elm);
5546
- resetComponentStateWhenRemoved(vm);
5547
- }
5548
- function appendVM(vm) {
5549
- rehydrate(vm);
5550
- }
5551
- // just in case the component comes back, with this we guarantee re-rendering it
5552
- // while preventing any attempt to rehydration until after reinsertion.
5553
- function resetComponentStateWhenRemoved(vm) {
5554
- const {
5555
- state
5556
- } = vm;
5557
- if (state !== 2 /* VMState.disconnected */) {
5558
- const {
5559
- tro
5560
- } = vm;
5561
- // Making sure that any observing record will not trigger the rehydrated on this vm
5562
- tro.reset();
5563
- runDisconnectedCallback(vm);
5564
- // Spec: https://dom.spec.whatwg.org/#concept-node-remove (step 14-15)
5565
- runChildNodesDisconnectedCallback(vm);
5566
- runLightChildNodesDisconnectedCallback(vm);
5567
- }
5568
- if (process.env.NODE_ENV !== 'production') {
5569
- removeActiveVM(vm);
5570
- }
5571
- }
5572
- // this method is triggered by the diffing algo only when a vnode from the
5573
- // old vnode.children is removed from the DOM.
5574
- function removeVM(vm) {
5575
- if (process.env.NODE_ENV !== 'production') {
5576
- assert.isTrue(vm.state === 1 /* VMState.connected */ || vm.state === 2 /* VMState.disconnected */, `${vm} must have been connected.`);
5577
- }
5578
- resetComponentStateWhenRemoved(vm);
5548
+ let isInvokingRender = false;
5549
+ let vmBeingConstructed = null;
5550
+ function isBeingConstructed(vm) {
5551
+ return vmBeingConstructed === vm;
5579
5552
  }
5580
- function getNearestShadowAncestor(vm) {
5581
- let ancestor = vm.owner;
5582
- while (!isNull(ancestor) && ancestor.renderMode === 0 /* RenderMode.Light */) {
5583
- ancestor = ancestor.owner;
5584
- }
5585
- return ancestor;
5553
+ function invokeComponentCallback(vm, fn, args) {
5554
+ const { component, callHook, owner } = vm;
5555
+ runWithBoundaryProtection(vm, owner, noop, () => {
5556
+ callHook(component, fn, args);
5557
+ }, noop);
5586
5558
  }
5587
- function createVM(elm, ctor, renderer, options) {
5588
- const {
5589
- mode,
5590
- owner,
5591
- tagName,
5592
- hydrated
5593
- } = options;
5594
- const def = getComponentInternalDef(ctor);
5595
- const vm = {
5596
- elm,
5597
- def,
5598
- idx: idx++,
5599
- state: 0 /* VMState.created */,
5600
- isScheduled: false,
5601
- isDirty: true,
5602
- tagName,
5603
- mode,
5604
- owner,
5605
- refVNodes: null,
5606
- hasRefVNodes: false,
5607
- children: EmptyArray,
5608
- aChildren: EmptyArray,
5609
- velements: EmptyArray,
5610
- cmpProps: create(null),
5611
- cmpFields: create(null),
5612
- cmpSlots: {
5613
- slotAssignments: create(null)
5614
- },
5615
- cmpTemplate: null,
5616
- hydrated: Boolean(hydrated),
5617
- renderMode: def.renderMode,
5618
- context: {
5619
- stylesheetToken: undefined,
5620
- hasTokenInClass: undefined,
5621
- hasTokenInAttribute: undefined,
5622
- hasScopedStyles: undefined,
5623
- styleVNodes: null,
5624
- tplCache: EmptyObject,
5625
- wiredConnecting: EmptyArray,
5626
- wiredDisconnecting: EmptyArray
5627
- },
5628
- // Properties set right after VM creation.
5629
- tro: null,
5630
- shadowMode: null,
5631
- stylesheets: null,
5632
- // Properties set by the LightningElement constructor.
5633
- component: null,
5634
- shadowRoot: null,
5635
- renderRoot: null,
5636
- callHook,
5637
- setHook,
5638
- getHook,
5639
- renderer
5640
- };
5641
- if (process.env.NODE_ENV !== 'production') {
5642
- vm.debugInfo = create(null);
5643
- }
5644
- vm.stylesheets = computeStylesheets(vm, def.ctor);
5645
- vm.shadowMode = computeShadowMode(vm, renderer);
5646
- vm.tro = getTemplateReactiveObserver();
5647
- if (process.env.NODE_ENV !== 'production') {
5648
- vm.toString = () => {
5649
- return `[object:vm ${def.name} (${vm.idx})]`;
5650
- };
5651
- if (lwcRuntimeFlags.ENABLE_FORCE_NATIVE_SHADOW_MODE_FOR_TEST) {
5652
- vm.shadowMode = 0 /* ShadowMode.Native */;
5559
+ function invokeComponentConstructor(vm, Ctor) {
5560
+ const vmBeingConstructedInception = vmBeingConstructed;
5561
+ let error;
5562
+ logOperationStart(0 /* OperationId.Constructor */, vm);
5563
+ vmBeingConstructed = vm;
5564
+ /**
5565
+ * Constructors don't need to be wrapped with a boundary because for root elements
5566
+ * it should throw, while elements from template are already wrapped by a boundary
5567
+ * associated to the diffing algo.
5568
+ */
5569
+ try {
5570
+ // job
5571
+ const result = new Ctor();
5572
+ // Check indirectly if the constructor result is an instance of LightningElement. Using
5573
+ // the "instanceof" operator would not work here since Locker Service provides its own
5574
+ // implementation of LightningElement, so we indirectly check if the base constructor is
5575
+ // invoked by accessing the component on the vm.
5576
+ if (vmBeingConstructed.component !== result) {
5577
+ throw new TypeError('Invalid component constructor, the class should extend LightningElement.');
5578
+ }
5653
5579
  }
5654
- }
5655
- // Create component instance associated to the vm and the element.
5656
- invokeComponentConstructor(vm, def.ctor);
5657
- // Initializing the wire decorator per instance only when really needed
5658
- if (hasWireAdapters(vm)) {
5659
- installWireAdapters(vm);
5660
- }
5661
- return vm;
5662
- }
5663
- function validateComponentStylesheets(vm, stylesheets) {
5664
- let valid = true;
5665
- const validate = arrayOrStylesheet => {
5666
- if (isArray$1(arrayOrStylesheet)) {
5667
- for (let i = 0; i < arrayOrStylesheet.length; i++) {
5668
- validate(arrayOrStylesheet[i]);
5669
- }
5670
- } else if (!isFunction$1(arrayOrStylesheet)) {
5671
- // function assumed to be a stylesheet factory
5672
- valid = false;
5580
+ catch (e) {
5581
+ error = Object(e);
5673
5582
  }
5674
- };
5675
- if (!isArray$1(stylesheets)) {
5676
- valid = false;
5677
- } else {
5678
- validate(stylesheets);
5679
- }
5680
- return valid;
5583
+ finally {
5584
+ logOperationEnd(0 /* OperationId.Constructor */, vm);
5585
+ vmBeingConstructed = vmBeingConstructedInception;
5586
+ if (!isUndefined$1(error)) {
5587
+ addErrorComponentStack(vm, error);
5588
+ // re-throwing the original error annotated after restoring the context
5589
+ throw error; // eslint-disable-line no-unsafe-finally
5590
+ }
5591
+ }
5592
+ }
5593
+ function invokeComponentRenderMethod(vm) {
5594
+ const { def: { render }, callHook, component, owner, } = vm;
5595
+ const isRenderBeingInvokedInception = isInvokingRender;
5596
+ const vmBeingRenderedInception = getVMBeingRendered();
5597
+ let html;
5598
+ let renderInvocationSuccessful = false;
5599
+ runWithBoundaryProtection(vm, owner, () => {
5600
+ // pre
5601
+ isInvokingRender = true;
5602
+ setVMBeingRendered(vm);
5603
+ }, () => {
5604
+ // job
5605
+ vm.tro.observe(() => {
5606
+ html = callHook(component, render);
5607
+ renderInvocationSuccessful = true;
5608
+ });
5609
+ }, () => {
5610
+ // post
5611
+ isInvokingRender = isRenderBeingInvokedInception;
5612
+ setVMBeingRendered(vmBeingRenderedInception);
5613
+ });
5614
+ // If render() invocation failed, process errorCallback in boundary and return an empty template
5615
+ return renderInvocationSuccessful ? evaluateTemplate(vm, html) : [];
5681
5616
  }
5682
- // Validate and flatten any stylesheets defined as `static stylesheets`
5683
- function computeStylesheets(vm, ctor) {
5684
- if (lwcRuntimeFlags.ENABLE_PROGRAMMATIC_STYLESHEETS) {
5685
- const {
5686
- stylesheets
5687
- } = ctor;
5688
- if (!isUndefined$1(stylesheets)) {
5689
- const valid = validateComponentStylesheets(vm, stylesheets);
5690
- if (valid) {
5691
- return flattenStylesheets(stylesheets);
5692
- } else if (process.env.NODE_ENV !== 'production') {
5693
- logError(`static stylesheets must be an array of CSS stylesheets. Found invalid stylesheets on <${vm.tagName}>`, vm);
5694
- }
5695
- }
5696
- }
5697
- return null;
5617
+ function invokeEventListener(vm, fn, thisValue, event) {
5618
+ const { callHook, owner } = vm;
5619
+ runWithBoundaryProtection(vm, owner, noop, () => {
5620
+ // job
5621
+ if (process.env.NODE_ENV !== 'production') {
5622
+ assert.isTrue(isFunction$1(fn), `Invalid event handler for event '${event.type}' on ${vm}.`);
5623
+ }
5624
+ callHook(thisValue, fn, [event]);
5625
+ }, noop);
5698
5626
  }
5699
- function computeShadowMode(vm, renderer) {
5700
- const {
5701
- def
5702
- } = vm;
5703
- const {
5704
- isSyntheticShadowDefined,
5705
- isNativeShadowDefined
5706
- } = renderer;
5707
- let shadowMode;
5708
- if (isSyntheticShadowDefined) {
5709
- if (def.renderMode === 0 /* RenderMode.Light */) {
5710
- // ShadowMode.Native implies "not synthetic shadow" which is consistent with how
5711
- // everything defaults to native when the synthetic shadow polyfill is unavailable.
5712
- shadowMode = 0 /* ShadowMode.Native */;
5713
- } else if (isNativeShadowDefined) {
5714
- // Not combined with above condition because @lwc/features only supports identifiers in
5715
- // the if-condition.
5716
- if (lwcRuntimeFlags.ENABLE_MIXED_SHADOW_MODE) {
5717
- if (def.shadowSupportMode === "any" /* ShadowSupportMode.Any */) {
5718
- shadowMode = 0 /* ShadowMode.Native */;
5719
- } else {
5720
- const shadowAncestor = getNearestShadowAncestor(vm);
5721
- if (!isNull(shadowAncestor) && shadowAncestor.shadowMode === 0 /* ShadowMode.Native */) {
5722
- // Transitive support for native Shadow DOM. A component in native mode
5723
- // transitively opts all of its descendants into native.
5724
- shadowMode = 0 /* ShadowMode.Native */;
5725
- } else {
5726
- // Synthetic if neither this component nor any of its ancestors are configured
5727
- // to be native.
5728
- shadowMode = 1 /* ShadowMode.Synthetic */;
5729
- }
5627
+
5628
+ /*
5629
+ * Copyright (c) 2018, salesforce.com, inc.
5630
+ * All rights reserved.
5631
+ * SPDX-License-Identifier: MIT
5632
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5633
+ */
5634
+ const signedTemplateMap = new Map();
5635
+ /**
5636
+ * INTERNAL: This function can only be invoked by compiled code. The compiler
5637
+ * will prevent this function from being imported by userland code.
5638
+ */
5639
+ function registerComponent(
5640
+ // We typically expect a LightningElementConstructor, but technically you can call this with anything
5641
+ Ctor, { tmpl }) {
5642
+ if (isFunction$1(Ctor)) {
5643
+ if (process.env.NODE_ENV !== 'production') {
5644
+ checkVersionMismatch(Ctor, 'component');
5730
5645
  }
5731
- } else {
5732
- shadowMode = 1 /* ShadowMode.Synthetic */;
5733
- }
5734
- } else {
5735
- // Synthetic if there is no native Shadow DOM support.
5736
- shadowMode = 1 /* ShadowMode.Synthetic */;
5646
+ signedTemplateMap.set(Ctor, tmpl);
5737
5647
  }
5738
- } else {
5739
- // Native if the synthetic shadow polyfill is unavailable.
5740
- shadowMode = 0 /* ShadowMode.Native */;
5741
- }
5742
-
5743
- return shadowMode;
5744
- }
5745
- function assertIsVM(obj) {
5746
- if (isNull(obj) || !isObject(obj) || !('renderRoot' in obj)) {
5747
- throw new TypeError(`${obj} is not a VM.`);
5748
- }
5648
+ // chaining this method as a way to wrap existing assignment of component constructor easily,
5649
+ // without too much transformation
5650
+ return Ctor;
5749
5651
  }
5750
- function associateVM(obj, vm) {
5751
- ViewModelReflection.set(obj, vm);
5652
+ function getComponentRegisteredTemplate(Ctor) {
5653
+ return signedTemplateMap.get(Ctor);
5752
5654
  }
5753
- function getAssociatedVM(obj) {
5754
- const vm = ViewModelReflection.get(obj);
5755
- if (process.env.NODE_ENV !== 'production') {
5756
- assertIsVM(vm);
5757
- }
5758
- return vm;
5655
+ function getTemplateReactiveObserver(vm) {
5656
+ return createReactiveObserver();
5759
5657
  }
5760
- function getAssociatedVMIfPresent(obj) {
5761
- const maybeVm = ViewModelReflection.get(obj);
5762
- if (process.env.NODE_ENV !== 'production') {
5763
- if (!isUndefined$1(maybeVm)) {
5764
- assertIsVM(maybeVm);
5658
+ function renderComponent$1(vm) {
5659
+ if (process.env.NODE_ENV !== 'production') {
5660
+ assert.invariant(vm.isDirty, `${vm} is not dirty.`);
5765
5661
  }
5766
- }
5767
- return maybeVm;
5662
+ vm.tro.reset();
5663
+ const vnodes = invokeComponentRenderMethod(vm);
5664
+ vm.isDirty = false;
5665
+ vm.isScheduled = false;
5666
+ return vnodes;
5768
5667
  }
5769
- function rehydrate(vm) {
5770
- if (isTrue(vm.isDirty)) {
5771
- const children = renderComponent$1(vm);
5772
- patchShadowRoot(vm, children);
5773
- }
5668
+ function markComponentAsDirty(vm) {
5669
+ if (process.env.NODE_ENV !== 'production') {
5670
+ const vmBeingRendered = getVMBeingRendered();
5671
+ assert.isFalse(vm.isDirty, `markComponentAsDirty() for ${vm} should not be called when the component is already dirty.`);
5672
+ assert.isFalse(isInvokingRender, `markComponentAsDirty() for ${vm} cannot be called during rendering of ${vmBeingRendered}.`);
5673
+ assert.isFalse(isUpdatingTemplate, `markComponentAsDirty() for ${vm} cannot be called while updating template of ${vmBeingRendered}.`);
5674
+ }
5675
+ vm.isDirty = true;
5774
5676
  }
5775
- function patchShadowRoot(vm, newCh) {
5776
- const {
5777
- renderRoot,
5778
- children: oldCh,
5779
- renderer
5780
- } = vm;
5781
- // caching the new children collection
5782
- vm.children = newCh;
5783
- if (newCh.length > 0 || oldCh.length > 0) {
5784
- // patch function mutates vnodes by adding the element reference,
5785
- // however, if patching fails it contains partial changes.
5786
- if (oldCh !== newCh) {
5787
- runWithBoundaryProtection(vm, vm, () => {
5788
- // pre
5789
- logOperationStart(2 /* OperationId.Patch */, vm);
5790
- }, () => {
5791
- // job
5792
- patchChildren(oldCh, newCh, renderRoot, renderer);
5793
- }, () => {
5794
- // post
5795
- logOperationEnd(2 /* OperationId.Patch */, vm);
5796
- });
5677
+ const cmpEventListenerMap = new WeakMap();
5678
+ function getWrappedComponentsListener(vm, listener) {
5679
+ if (!isFunction$1(listener)) {
5680
+ throw new TypeError('Expected an EventListener but received ' + typeof listener); // avoiding problems with non-valid listeners
5797
5681
  }
5798
- }
5799
- if (vm.state === 1 /* VMState.connected */) ;
5682
+ let wrappedListener = cmpEventListenerMap.get(listener);
5683
+ if (isUndefined$1(wrappedListener)) {
5684
+ wrappedListener = function (event) {
5685
+ invokeEventListener(vm, listener, undefined, event);
5686
+ };
5687
+ cmpEventListenerMap.set(listener, wrappedListener);
5688
+ }
5689
+ return wrappedListener;
5800
5690
  }
5801
5691
 
5802
- function runConnectedCallback(vm) {
5803
- const {
5804
- state
5805
- } = vm;
5806
- if (state === 1 /* VMState.connected */) {
5807
- return; // nothing to do since it was already connected
5808
- }
5692
+ /*
5693
+ * Copyright (c) 2018, salesforce.com, inc.
5694
+ * All rights reserved.
5695
+ * SPDX-License-Identifier: MIT
5696
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5697
+ */
5698
+ const Services = create(null);
5699
+ const hooks = ['rendered', 'connected', 'disconnected'];
5700
+ /**
5701
+ * EXPERIMENTAL: This function allows for the registration of "services"
5702
+ * in LWC by exposing hooks into the component life-cycle. This API is
5703
+ * subject to change or being removed.
5704
+ */
5705
+ function register(service) {
5706
+ if (process.env.NODE_ENV !== 'production') {
5707
+ assert.isTrue(isObject(service), `Invalid service declaration, ${service}: service must be an object`);
5708
+ }
5709
+ for (let i = 0; i < hooks.length; ++i) {
5710
+ const hookName = hooks[i];
5711
+ if (hookName in service) {
5712
+ let l = Services[hookName];
5713
+ if (isUndefined$1(l)) {
5714
+ Services[hookName] = l = [];
5715
+ }
5716
+ ArrayPush$1.call(l, service[hookName]);
5717
+ }
5718
+ }
5719
+ }
5720
+ function invokeServiceHook(vm, cbs) {
5721
+ if (process.env.NODE_ENV !== 'production') {
5722
+ assert.isTrue(isArray$1(cbs) && cbs.length > 0, `Optimize invokeServiceHook() to be invoked only when needed`);
5723
+ }
5724
+ const { component, def, context } = vm;
5725
+ for (let i = 0, len = cbs.length; i < len; ++i) {
5726
+ cbs[i].call(undefined, component, {}, def, context);
5727
+ }
5728
+ }
5809
5729
 
5810
- vm.state = 1 /* VMState.connected */;
5811
- // reporting connection
5812
- const {
5813
- connected
5814
- } = Services;
5815
- if (connected) {
5816
- invokeServiceHook(vm, connected);
5817
- }
5818
- if (hasWireAdapters(vm)) {
5819
- connectWireAdapters(vm);
5820
- }
5821
- const {
5822
- connectedCallback
5823
- } = vm.def;
5824
- if (!isUndefined$1(connectedCallback)) {
5825
- logOperationStart(3 /* OperationId.ConnectedCallback */, vm);
5826
- invokeComponentCallback(vm, connectedCallback);
5827
- logOperationEnd(3 /* OperationId.ConnectedCallback */, vm);
5730
+ /*
5731
+ * Copyright (c) 2018, salesforce.com, inc.
5732
+ * All rights reserved.
5733
+ * SPDX-License-Identifier: MIT
5734
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5735
+ */
5736
+ let idx = 0;
5737
+ /** The internal slot used to associate different objects the engine manipulates with the VM */
5738
+ const ViewModelReflection = new WeakMap();
5739
+ function callHook(cmp, fn, args = []) {
5740
+ return fn.apply(cmp, args);
5741
+ }
5742
+ function setHook(cmp, prop, newValue) {
5743
+ cmp[prop] = newValue;
5744
+ }
5745
+ function getHook(cmp, prop) {
5746
+ return cmp[prop];
5747
+ }
5748
+ function rerenderVM(vm) {
5749
+ rehydrate(vm);
5750
+ }
5751
+ function connectRootElement(elm) {
5752
+ const vm = getAssociatedVM(elm);
5753
+ logGlobalOperationStart(7 /* OperationId.GlobalHydrate */, vm);
5754
+ // Usually means moving the element from one place to another, which is observable via
5755
+ // life-cycle hooks.
5756
+ if (vm.state === 1 /* VMState.connected */) {
5757
+ disconnectRootElement(elm);
5828
5758
  }
5759
+ runConnectedCallback(vm);
5760
+ rehydrate(vm);
5761
+ logGlobalOperationEnd(7 /* OperationId.GlobalHydrate */, vm);
5829
5762
  }
5830
- function hasWireAdapters(vm) {
5831
- return getOwnPropertyNames$1(vm.def.wire).length > 0;
5763
+ function disconnectRootElement(elm) {
5764
+ const vm = getAssociatedVM(elm);
5765
+ resetComponentStateWhenRemoved(vm);
5832
5766
  }
5833
- function runDisconnectedCallback(vm) {
5834
- if (process.env.NODE_ENV !== 'production') {
5835
- assert.isTrue(vm.state !== 2 /* VMState.disconnected */, `${vm} must be inserted.`);
5836
- }
5837
- if (isFalse(vm.isDirty)) {
5838
- // this guarantees that if the component is reused/reinserted,
5839
- // it will be re-rendered because we are disconnecting the reactivity
5840
- // linking, so mutations are not automatically reflected on the state
5841
- // of disconnected components.
5842
- vm.isDirty = true;
5843
- }
5844
- vm.state = 2 /* VMState.disconnected */;
5845
- // reporting disconnection
5846
- const {
5847
- disconnected
5848
- } = Services;
5849
- if (disconnected) {
5850
- invokeServiceHook(vm, disconnected);
5851
- }
5852
- if (hasWireAdapters(vm)) {
5853
- disconnectWireAdapters(vm);
5854
- }
5855
- const {
5856
- disconnectedCallback
5857
- } = vm.def;
5858
- if (!isUndefined$1(disconnectedCallback)) {
5859
- logOperationStart(5 /* OperationId.DisconnectedCallback */, vm);
5860
- invokeComponentCallback(vm, disconnectedCallback);
5861
- logOperationEnd(5 /* OperationId.DisconnectedCallback */, vm);
5862
- }
5767
+ function appendVM(vm) {
5768
+ rehydrate(vm);
5863
5769
  }
5864
- function runChildNodesDisconnectedCallback(vm) {
5770
+ // just in case the component comes back, with this we guarantee re-rendering it
5771
+ // while preventing any attempt to rehydration until after reinsertion.
5772
+ function resetComponentStateWhenRemoved(vm) {
5865
5773
  const {
5866
- velements: vCustomElementCollection
5774
+ state
5867
5775
  } = vm;
5868
- // Reporting disconnection for every child in inverse order since they are
5869
- // inserted in reserved order.
5870
- for (let i = vCustomElementCollection.length - 1; i >= 0; i -= 1) {
5776
+ if (state !== 2 /* VMState.disconnected */) {
5871
5777
  const {
5872
- elm
5873
- } = vCustomElementCollection[i];
5874
- // There are two cases where the element could be undefined:
5875
- // * when there is an error during the construction phase, and an error
5876
- // boundary picks it, there is a possibility that the VCustomElement
5877
- // is not properly initialized, and therefore is should be ignored.
5878
- // * when slotted custom element is not used by the element where it is
5879
- // slotted into it, as a result, the custom element was never
5880
- // initialized.
5881
- if (!isUndefined$1(elm)) {
5882
- const childVM = getAssociatedVMIfPresent(elm);
5883
- // The VM associated with the element might be associated undefined
5884
- // in the case where the VM failed in the middle of its creation,
5885
- // eg: constructor throwing before invoking super().
5886
- if (!isUndefined$1(childVM)) {
5887
- resetComponentStateWhenRemoved(childVM);
5888
- }
5889
- }
5778
+ tro
5779
+ } = vm;
5780
+ // Making sure that any observing record will not trigger the rehydrated on this vm
5781
+ tro.reset();
5782
+ runDisconnectedCallback(vm);
5783
+ // Spec: https://dom.spec.whatwg.org/#concept-node-remove (step 14-15)
5784
+ runChildNodesDisconnectedCallback(vm);
5785
+ runLightChildNodesDisconnectedCallback(vm);
5786
+ }
5787
+ if (process.env.NODE_ENV !== 'production') {
5788
+ removeActiveVM(vm);
5890
5789
  }
5891
5790
  }
5892
- function runLightChildNodesDisconnectedCallback(vm) {
5893
- const {
5894
- aChildren: adoptedChildren
5895
- } = vm;
5896
- recursivelyDisconnectChildren(adoptedChildren);
5791
+ // this method is triggered by the diffing algo only when a vnode from the
5792
+ // old vnode.children is removed from the DOM.
5793
+ function removeVM(vm) {
5794
+ if (process.env.NODE_ENV !== 'production') {
5795
+ assert.isTrue(vm.state === 1 /* VMState.connected */ || vm.state === 2 /* VMState.disconnected */, `${vm} must have been connected.`);
5796
+ }
5797
+ resetComponentStateWhenRemoved(vm);
5897
5798
  }
5898
- /**
5899
- * The recursion doesn't need to be a complete traversal of the vnode graph,
5900
- * instead it can be partial, when a custom element vnode is found, we don't
5901
- * need to continue into its children because by attempting to disconnect the
5902
- * custom element itself will trigger the removal of anything slotted or anything
5903
- * defined on its shadow.
5904
- */
5905
- function recursivelyDisconnectChildren(vnodes) {
5906
- for (let i = 0, len = vnodes.length; i < len; i += 1) {
5907
- const vnode = vnodes[i];
5908
- if (!isNull(vnode) && !isUndefined$1(vnode.elm)) {
5909
- switch (vnode.type) {
5910
- case 2 /* VNodeType.Element */:
5911
- recursivelyDisconnectChildren(vnode.children);
5912
- break;
5913
- case 3 /* VNodeType.CustomElement */:
5914
- {
5915
- const vm = getAssociatedVM(vnode.elm);
5916
- resetComponentStateWhenRemoved(vm);
5917
- break;
5918
- }
5919
- }
5920
- }
5799
+ function getNearestShadowAncestor(vm) {
5800
+ let ancestor = vm.owner;
5801
+ while (!isNull(ancestor) && ancestor.renderMode === 0 /* RenderMode.Light */) {
5802
+ ancestor = ancestor.owner;
5921
5803
  }
5804
+ return ancestor;
5922
5805
  }
5923
- // This is a super optimized mechanism to remove the content of the root node (shadow root
5924
- // for shadow DOM components and the root element itself for light DOM) without having to go
5925
- // into snabbdom. Especially useful when the reset is a consequence of an error, in which case the
5926
- // children VNodes might not be representing the current state of the DOM.
5927
- function resetComponentRoot(vm) {
5806
+ function createVM(elm, ctor, renderer, options) {
5928
5807
  const {
5929
- children,
5930
- renderRoot,
5931
- renderer: {
5932
- remove
5933
- }
5934
- } = vm;
5935
- for (let i = 0, len = children.length; i < len; i++) {
5936
- const child = children[i];
5937
- if (!isNull(child) && !isUndefined$1(child.elm)) {
5938
- remove(child.elm, renderRoot);
5808
+ mode,
5809
+ owner,
5810
+ tagName,
5811
+ hydrated
5812
+ } = options;
5813
+ const def = getComponentInternalDef(ctor);
5814
+ const vm = {
5815
+ elm,
5816
+ def,
5817
+ idx: idx++,
5818
+ state: 0 /* VMState.created */,
5819
+ isScheduled: false,
5820
+ isDirty: true,
5821
+ tagName,
5822
+ mode,
5823
+ owner,
5824
+ refVNodes: null,
5825
+ children: EmptyArray,
5826
+ aChildren: EmptyArray,
5827
+ velements: EmptyArray,
5828
+ cmpProps: create(null),
5829
+ cmpFields: create(null),
5830
+ cmpSlots: {
5831
+ slotAssignments: create(null)
5832
+ },
5833
+ cmpTemplate: null,
5834
+ hydrated: Boolean(hydrated),
5835
+ renderMode: def.renderMode,
5836
+ context: {
5837
+ stylesheetToken: undefined,
5838
+ hasTokenInClass: undefined,
5839
+ hasTokenInAttribute: undefined,
5840
+ hasScopedStyles: undefined,
5841
+ styleVNodes: null,
5842
+ tplCache: EmptyObject,
5843
+ wiredConnecting: EmptyArray,
5844
+ wiredDisconnecting: EmptyArray
5845
+ },
5846
+ // Properties set right after VM creation.
5847
+ tro: null,
5848
+ shadowMode: null,
5849
+ stylesheets: null,
5850
+ // Properties set by the LightningElement constructor.
5851
+ component: null,
5852
+ shadowRoot: null,
5853
+ renderRoot: null,
5854
+ callHook,
5855
+ setHook,
5856
+ getHook,
5857
+ renderer
5858
+ };
5859
+ if (process.env.NODE_ENV !== 'production') {
5860
+ vm.debugInfo = create(null);
5861
+ }
5862
+ vm.stylesheets = computeStylesheets(vm, def.ctor);
5863
+ vm.shadowMode = computeShadowMode(vm, renderer);
5864
+ vm.tro = getTemplateReactiveObserver();
5865
+ if (process.env.NODE_ENV !== 'production') {
5866
+ vm.toString = () => {
5867
+ return `[object:vm ${def.name} (${vm.idx})]`;
5868
+ };
5869
+ if (lwcRuntimeFlags.ENABLE_FORCE_NATIVE_SHADOW_MODE_FOR_TEST) {
5870
+ vm.shadowMode = 0 /* ShadowMode.Native */;
5939
5871
  }
5940
5872
  }
5941
- vm.children = EmptyArray;
5942
- runChildNodesDisconnectedCallback(vm);
5943
- vm.velements = EmptyArray;
5873
+ // Create component instance associated to the vm and the element.
5874
+ invokeComponentConstructor(vm, def.ctor);
5875
+ // Initializing the wire decorator per instance only when really needed
5876
+ if (hasWireAdapters(vm)) {
5877
+ installWireAdapters(vm);
5878
+ }
5879
+ return vm;
5944
5880
  }
5945
- function getErrorBoundaryVM(vm) {
5946
- let currentVm = vm;
5947
- while (!isNull(currentVm)) {
5948
- if (!isUndefined$1(currentVm.def.errorCallback)) {
5949
- return currentVm;
5881
+ function validateComponentStylesheets(vm, stylesheets) {
5882
+ let valid = true;
5883
+ const validate = arrayOrStylesheet => {
5884
+ if (isArray$1(arrayOrStylesheet)) {
5885
+ for (let i = 0; i < arrayOrStylesheet.length; i++) {
5886
+ validate(arrayOrStylesheet[i]);
5887
+ }
5888
+ } else if (!isFunction$1(arrayOrStylesheet)) {
5889
+ // function assumed to be a stylesheet factory
5890
+ valid = false;
5950
5891
  }
5951
- currentVm = currentVm.owner;
5892
+ };
5893
+ if (!isArray$1(stylesheets)) {
5894
+ valid = false;
5895
+ } else {
5896
+ validate(stylesheets);
5952
5897
  }
5898
+ return valid;
5953
5899
  }
5954
- function runWithBoundaryProtection(vm, owner, pre, job, post) {
5955
- let error;
5956
- pre();
5957
- try {
5958
- job();
5959
- } catch (e) {
5960
- error = Object(e);
5961
- } finally {
5962
- post();
5963
- if (!isUndefined$1(error)) {
5964
- addErrorComponentStack(vm, error);
5965
- const errorBoundaryVm = isNull(owner) ? undefined : getErrorBoundaryVM(owner);
5966
- if (isUndefined$1(errorBoundaryVm)) {
5967
- throw error; // eslint-disable-line no-unsafe-finally
5900
+ // Validate and flatten any stylesheets defined as `static stylesheets`
5901
+ function computeStylesheets(vm, ctor) {
5902
+ if (lwcRuntimeFlags.ENABLE_PROGRAMMATIC_STYLESHEETS) {
5903
+ const {
5904
+ stylesheets
5905
+ } = ctor;
5906
+ if (!isUndefined$1(stylesheets)) {
5907
+ const valid = validateComponentStylesheets(vm, stylesheets);
5908
+ if (valid) {
5909
+ return flattenStylesheets(stylesheets);
5910
+ } else if (process.env.NODE_ENV !== 'production') {
5911
+ logError(`static stylesheets must be an array of CSS stylesheets. Found invalid stylesheets on <${vm.tagName}>`, vm);
5968
5912
  }
5969
-
5970
- resetComponentRoot(vm); // remove offenders
5971
- logOperationStart(6 /* OperationId.ErrorCallback */, vm);
5972
- // error boundaries must have an ErrorCallback
5973
- const errorCallback = errorBoundaryVm.def.errorCallback;
5974
- invokeComponentCallback(errorBoundaryVm, errorCallback, [error, error.wcStack]);
5975
- logOperationEnd(6 /* OperationId.ErrorCallback */, vm);
5976
5913
  }
5977
5914
  }
5915
+ return null;
5978
5916
  }
5979
-
5980
- /*
5981
- * Copyright (c) 2018, salesforce.com, inc.
5982
- * All rights reserved.
5983
- * SPDX-License-Identifier: MIT
5984
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5985
- */
5986
- const DeprecatedWiredElementHost = '$$DeprecatedWiredElementHostKey$$';
5987
- const DeprecatedWiredParamsMeta = '$$DeprecatedWiredParamsMetaKey$$';
5988
- const WIRE_DEBUG_ENTRY = '@wire';
5989
- const WireMetaMap = new Map();
5990
- class WireContextRegistrationEvent extends CustomEvent {
5991
- constructor(adapterToken, {
5992
- setNewContext,
5993
- setDisconnectedCallback
5994
- }) {
5995
- super(adapterToken, {
5996
- bubbles: true,
5997
- composed: true
5998
- });
5999
- defineProperties(this, {
6000
- setNewContext: {
6001
- value: setNewContext
6002
- },
6003
- setDisconnectedCallback: {
6004
- value: setDisconnectedCallback
5917
+ function computeShadowMode(vm, renderer) {
5918
+ const {
5919
+ def
5920
+ } = vm;
5921
+ const {
5922
+ isSyntheticShadowDefined,
5923
+ isNativeShadowDefined
5924
+ } = renderer;
5925
+ let shadowMode;
5926
+ if (isSyntheticShadowDefined) {
5927
+ if (def.renderMode === 0 /* RenderMode.Light */) {
5928
+ // ShadowMode.Native implies "not synthetic shadow" which is consistent with how
5929
+ // everything defaults to native when the synthetic shadow polyfill is unavailable.
5930
+ shadowMode = 0 /* ShadowMode.Native */;
5931
+ } else if (isNativeShadowDefined) {
5932
+ // Not combined with above condition because @lwc/features only supports identifiers in
5933
+ // the if-condition.
5934
+ if (lwcRuntimeFlags.ENABLE_MIXED_SHADOW_MODE) {
5935
+ if (def.shadowSupportMode === "any" /* ShadowSupportMode.Any */) {
5936
+ shadowMode = 0 /* ShadowMode.Native */;
5937
+ } else {
5938
+ const shadowAncestor = getNearestShadowAncestor(vm);
5939
+ if (!isNull(shadowAncestor) && shadowAncestor.shadowMode === 0 /* ShadowMode.Native */) {
5940
+ // Transitive support for native Shadow DOM. A component in native mode
5941
+ // transitively opts all of its descendants into native.
5942
+ shadowMode = 0 /* ShadowMode.Native */;
5943
+ } else {
5944
+ // Synthetic if neither this component nor any of its ancestors are configured
5945
+ // to be native.
5946
+ shadowMode = 1 /* ShadowMode.Synthetic */;
5947
+ }
5948
+ }
5949
+ } else {
5950
+ shadowMode = 1 /* ShadowMode.Synthetic */;
6005
5951
  }
6006
- });
5952
+ } else {
5953
+ // Synthetic if there is no native Shadow DOM support.
5954
+ shadowMode = 1 /* ShadowMode.Synthetic */;
5955
+ }
5956
+ } else {
5957
+ // Native if the synthetic shadow polyfill is unavailable.
5958
+ shadowMode = 0 /* ShadowMode.Native */;
6007
5959
  }
5960
+
5961
+ return shadowMode;
6008
5962
  }
6009
- function createFieldDataCallback(vm, name) {
6010
- return value => {
6011
- updateComponentValue(vm, name, value);
6012
- };
6013
- }
6014
- function createMethodDataCallback(vm, method) {
6015
- return value => {
6016
- // dispatching new value into the wired method
6017
- runWithBoundaryProtection(vm, vm.owner, noop, () => {
6018
- // job
6019
- method.call(vm.component, value);
6020
- }, noop);
6021
- };
5963
+ function assertIsVM(obj) {
5964
+ if (isNull(obj) || !isObject(obj) || !('renderRoot' in obj)) {
5965
+ throw new TypeError(`${obj} is not a VM.`);
5966
+ }
6022
5967
  }
6023
- function createConfigWatcher(component, configCallback, callbackWhenConfigIsReady) {
6024
- // creating the reactive observer for reactive params when needed
6025
- const ro = createReactiveObserver();
6026
- const computeConfigAndUpdate = () => {
6027
- let config;
6028
- ro.observe(() => config = configCallback(component));
6029
- // eslint-disable-next-line @lwc/lwc-internal/no-invalid-todo
6030
- // TODO: dev-mode validation of config based on the adapter.configSchema
6031
- // @ts-ignore it is assigned in the observe() callback
6032
- callbackWhenConfigIsReady(config);
6033
- };
6034
- return {
6035
- computeConfigAndUpdate,
6036
- ro
6037
- };
5968
+ function associateVM(obj, vm) {
5969
+ ViewModelReflection.set(obj, vm);
6038
5970
  }
6039
- function createContextWatcher(vm, wireDef, callbackWhenContextIsReady) {
6040
- const {
6041
- adapter
6042
- } = wireDef;
6043
- const adapterContextToken = getAdapterToken(adapter);
6044
- if (isUndefined$1(adapterContextToken)) {
6045
- return; // no provider found, nothing to be done
5971
+ function getAssociatedVM(obj) {
5972
+ const vm = ViewModelReflection.get(obj);
5973
+ if (process.env.NODE_ENV !== 'production') {
5974
+ assertIsVM(vm);
6046
5975
  }
6047
-
6048
- const {
6049
- elm,
6050
- context: {
6051
- wiredConnecting,
6052
- wiredDisconnecting
6053
- },
6054
- renderer: {
6055
- dispatchEvent
6056
- }
6057
- } = vm;
6058
- // waiting for the component to be connected to formally request the context via the token
6059
- ArrayPush$1.call(wiredConnecting, () => {
6060
- // This event is responsible for connecting the host element with another
6061
- // element in the composed path that is providing contextual data. The provider
6062
- // must be listening for a special dom event with the name corresponding to the value of
6063
- // `adapterContextToken`, which will remain secret and internal to this file only to
6064
- // guarantee that the linkage can be forged.
6065
- const contextRegistrationEvent = new WireContextRegistrationEvent(adapterContextToken, {
6066
- setNewContext(newContext) {
6067
- // eslint-disable-next-line @lwc/lwc-internal/no-invalid-todo
6068
- // TODO: dev-mode validation of config based on the adapter.contextSchema
6069
- callbackWhenContextIsReady(newContext);
6070
- },
6071
- setDisconnectedCallback(disconnectCallback) {
6072
- // adds this callback into the disconnect bucket so it gets disconnected from parent
6073
- // the the element hosting the wire is disconnected
6074
- ArrayPush$1.call(wiredDisconnecting, disconnectCallback);
6075
- }
6076
- });
6077
- dispatchEvent(elm, contextRegistrationEvent);
6078
- });
5976
+ return vm;
6079
5977
  }
6080
- function createConnector(vm, name, wireDef) {
6081
- const {
6082
- method,
6083
- adapter,
6084
- configCallback,
6085
- dynamic
6086
- } = wireDef;
6087
- let debugInfo;
5978
+ function getAssociatedVMIfPresent(obj) {
5979
+ const maybeVm = ViewModelReflection.get(obj);
6088
5980
  if (process.env.NODE_ENV !== 'production') {
6089
- const wiredPropOrMethod = isUndefined$1(method) ? name : method.name;
6090
- debugInfo = create(null);
6091
- debugInfo.wasDataProvisionedForConfig = false;
6092
- vm.debugInfo[WIRE_DEBUG_ENTRY][wiredPropOrMethod] = debugInfo;
6093
- }
6094
- const fieldOrMethodCallback = isUndefined$1(method) ? createFieldDataCallback(vm, name) : createMethodDataCallback(vm, method);
6095
- const dataCallback = value => {
6096
- if (process.env.NODE_ENV !== 'production') {
6097
- debugInfo.data = value;
6098
- // Note: most of the time, the data provided is for the current config, but there may be
6099
- // some conditions in which it does not, ex:
6100
- // race conditions in a poor network while the adapter does not cancel a previous request.
6101
- debugInfo.wasDataProvisionedForConfig = true;
5981
+ if (!isUndefined$1(maybeVm)) {
5982
+ assertIsVM(maybeVm);
6102
5983
  }
6103
- fieldOrMethodCallback(value);
6104
- };
6105
- let context;
6106
- let connector;
6107
- // Workaround to pass the component element associated to this wire adapter instance.
6108
- defineProperty(dataCallback, DeprecatedWiredElementHost, {
6109
- value: vm.elm
6110
- });
6111
- defineProperty(dataCallback, DeprecatedWiredParamsMeta, {
6112
- value: dynamic
6113
- });
6114
- runWithBoundaryProtection(vm, vm, noop, () => {
6115
- // job
6116
- connector = new adapter(dataCallback);
6117
- }, noop);
6118
- const updateConnectorConfig = config => {
6119
- // every time the config is recomputed due to tracking,
6120
- // this callback will be invoked with the new computed config
6121
- runWithBoundaryProtection(vm, vm, noop, () => {
6122
- // job
6123
- if (process.env.NODE_ENV !== 'production') {
6124
- debugInfo.config = config;
6125
- debugInfo.context = context;
6126
- debugInfo.wasDataProvisionedForConfig = false;
6127
- }
6128
- connector.update(config, context);
6129
- }, noop);
6130
- };
6131
- // Computes the current wire config and calls the update method on the wire adapter.
6132
- // If it has params, we will need to observe changes in the next tick.
5984
+ }
5985
+ return maybeVm;
5986
+ }
5987
+ function rehydrate(vm) {
5988
+ if (isTrue(vm.isDirty)) {
5989
+ const children = renderComponent$1(vm);
5990
+ patchShadowRoot(vm, children);
5991
+ }
5992
+ }
5993
+ function patchShadowRoot(vm, newCh) {
6133
5994
  const {
6134
- computeConfigAndUpdate,
6135
- ro
6136
- } = createConfigWatcher(vm.component, configCallback, updateConnectorConfig);
6137
- // if the adapter needs contextualization, we need to watch for new context and push it alongside the config
6138
- if (!isUndefined$1(adapter.contextSchema)) {
6139
- createContextWatcher(vm, wireDef, newContext => {
6140
- // every time the context is pushed into this component,
6141
- // this callback will be invoked with the new computed context
6142
- if (context !== newContext) {
6143
- context = newContext;
6144
- // Note: when new context arrives, the config will be recomputed and pushed along side the new
6145
- // context, this is to preserve the identity characteristics, config should not have identity
6146
- // (ever), while context can have identity
6147
- if (vm.state === 1 /* VMState.connected */) {
6148
- computeConfigAndUpdate();
6149
- }
6150
- }
6151
- });
5995
+ renderRoot,
5996
+ children: oldCh,
5997
+ renderer
5998
+ } = vm;
5999
+ // caching the new children collection
6000
+ vm.children = newCh;
6001
+ if (newCh.length > 0 || oldCh.length > 0) {
6002
+ // patch function mutates vnodes by adding the element reference,
6003
+ // however, if patching fails it contains partial changes.
6004
+ if (oldCh !== newCh) {
6005
+ runWithBoundaryProtection(vm, vm, () => {
6006
+ // pre
6007
+ logOperationStart(2 /* OperationId.Patch */, vm);
6008
+ }, () => {
6009
+ // job
6010
+ patchChildren(oldCh, newCh, renderRoot, renderer);
6011
+ }, () => {
6012
+ // post
6013
+ logOperationEnd(2 /* OperationId.Patch */, vm);
6014
+ });
6015
+ }
6152
6016
  }
6153
- return {
6154
- // @ts-ignore the boundary protection executes sync, connector is always defined
6155
- connector,
6156
- computeConfigAndUpdate,
6157
- resetConfigWatcher: () => ro.reset()
6158
- };
6017
+ if (vm.state === 1 /* VMState.connected */) ;
6159
6018
  }
6160
- const AdapterToTokenMap = new Map();
6161
- function getAdapterToken(adapter) {
6162
- return AdapterToTokenMap.get(adapter);
6019
+
6020
+ function runConnectedCallback(vm) {
6021
+ const {
6022
+ state
6023
+ } = vm;
6024
+ if (state === 1 /* VMState.connected */) {
6025
+ return; // nothing to do since it was already connected
6026
+ }
6027
+
6028
+ vm.state = 1 /* VMState.connected */;
6029
+ // reporting connection
6030
+ const {
6031
+ connected
6032
+ } = Services;
6033
+ if (connected) {
6034
+ invokeServiceHook(vm, connected);
6035
+ }
6036
+ if (hasWireAdapters(vm)) {
6037
+ connectWireAdapters(vm);
6038
+ }
6039
+ const {
6040
+ connectedCallback
6041
+ } = vm.def;
6042
+ if (!isUndefined$1(connectedCallback)) {
6043
+ logOperationStart(3 /* OperationId.ConnectedCallback */, vm);
6044
+ invokeComponentCallback(vm, connectedCallback);
6045
+ logOperationEnd(3 /* OperationId.ConnectedCallback */, vm);
6046
+ }
6163
6047
  }
6164
- function setAdapterToken(adapter, token) {
6165
- AdapterToTokenMap.set(adapter, token);
6048
+ function hasWireAdapters(vm) {
6049
+ return getOwnPropertyNames$1(vm.def.wire).length > 0;
6166
6050
  }
6167
- function storeWiredMethodMeta(descriptor, adapter, configCallback, dynamic) {
6168
- // support for callable adapters
6169
- if (adapter.adapter) {
6170
- adapter = adapter.adapter;
6051
+ function runDisconnectedCallback(vm) {
6052
+ if (process.env.NODE_ENV !== 'production') {
6053
+ assert.isTrue(vm.state !== 2 /* VMState.disconnected */, `${vm} must be inserted.`);
6171
6054
  }
6172
- const method = descriptor.value;
6173
- const def = {
6174
- adapter,
6175
- method,
6176
- configCallback,
6177
- dynamic
6178
- };
6179
- WireMetaMap.set(descriptor, def);
6180
- }
6181
- function storeWiredFieldMeta(descriptor, adapter, configCallback, dynamic) {
6182
- // support for callable adapters
6183
- if (adapter.adapter) {
6184
- adapter = adapter.adapter;
6055
+ if (isFalse(vm.isDirty)) {
6056
+ // this guarantees that if the component is reused/reinserted,
6057
+ // it will be re-rendered because we are disconnecting the reactivity
6058
+ // linking, so mutations are not automatically reflected on the state
6059
+ // of disconnected components.
6060
+ vm.isDirty = true;
6061
+ }
6062
+ vm.state = 2 /* VMState.disconnected */;
6063
+ // reporting disconnection
6064
+ const {
6065
+ disconnected
6066
+ } = Services;
6067
+ if (disconnected) {
6068
+ invokeServiceHook(vm, disconnected);
6069
+ }
6070
+ if (hasWireAdapters(vm)) {
6071
+ disconnectWireAdapters(vm);
6072
+ }
6073
+ const {
6074
+ disconnectedCallback
6075
+ } = vm.def;
6076
+ if (!isUndefined$1(disconnectedCallback)) {
6077
+ logOperationStart(5 /* OperationId.DisconnectedCallback */, vm);
6078
+ invokeComponentCallback(vm, disconnectedCallback);
6079
+ logOperationEnd(5 /* OperationId.DisconnectedCallback */, vm);
6185
6080
  }
6186
- const def = {
6187
- adapter,
6188
- configCallback,
6189
- dynamic
6190
- };
6191
- WireMetaMap.set(descriptor, def);
6192
6081
  }
6193
- function installWireAdapters(vm) {
6082
+ function runChildNodesDisconnectedCallback(vm) {
6194
6083
  const {
6195
- context,
6196
- def: {
6197
- wire
6198
- }
6084
+ velements: vCustomElementCollection
6199
6085
  } = vm;
6200
- if (process.env.NODE_ENV !== 'production') {
6201
- vm.debugInfo[WIRE_DEBUG_ENTRY] = create(null);
6202
- }
6203
- const wiredConnecting = context.wiredConnecting = [];
6204
- const wiredDisconnecting = context.wiredDisconnecting = [];
6205
- for (const fieldNameOrMethod in wire) {
6206
- const descriptor = wire[fieldNameOrMethod];
6207
- const wireDef = WireMetaMap.get(descriptor);
6208
- if (process.env.NODE_ENV !== 'production') {
6209
- assert.invariant(wireDef, `Internal Error: invalid wire definition found.`);
6086
+ // Reporting disconnection for every child in inverse order since they are
6087
+ // inserted in reserved order.
6088
+ for (let i = vCustomElementCollection.length - 1; i >= 0; i -= 1) {
6089
+ const {
6090
+ elm
6091
+ } = vCustomElementCollection[i];
6092
+ // There are two cases where the element could be undefined:
6093
+ // * when there is an error during the construction phase, and an error
6094
+ // boundary picks it, there is a possibility that the VCustomElement
6095
+ // is not properly initialized, and therefore is should be ignored.
6096
+ // * when slotted custom element is not used by the element where it is
6097
+ // slotted into it, as a result, the custom element was never
6098
+ // initialized.
6099
+ if (!isUndefined$1(elm)) {
6100
+ const childVM = getAssociatedVMIfPresent(elm);
6101
+ // The VM associated with the element might be associated undefined
6102
+ // in the case where the VM failed in the middle of its creation,
6103
+ // eg: constructor throwing before invoking super().
6104
+ if (!isUndefined$1(childVM)) {
6105
+ resetComponentStateWhenRemoved(childVM);
6106
+ }
6210
6107
  }
6211
- if (!isUndefined$1(wireDef)) {
6212
- const {
6213
- connector,
6214
- computeConfigAndUpdate,
6215
- resetConfigWatcher
6216
- } = createConnector(vm, fieldNameOrMethod, wireDef);
6217
- const hasDynamicParams = wireDef.dynamic.length > 0;
6218
- ArrayPush$1.call(wiredConnecting, () => {
6219
- connector.connect();
6220
- if (!lwcRuntimeFlags.ENABLE_WIRE_SYNC_EMIT) {
6221
- if (hasDynamicParams) {
6222
- Promise.resolve().then(computeConfigAndUpdate);
6223
- return;
6108
+ }
6109
+ }
6110
+ function runLightChildNodesDisconnectedCallback(vm) {
6111
+ const {
6112
+ aChildren: adoptedChildren
6113
+ } = vm;
6114
+ recursivelyDisconnectChildren(adoptedChildren);
6115
+ }
6116
+ /**
6117
+ * The recursion doesn't need to be a complete traversal of the vnode graph,
6118
+ * instead it can be partial, when a custom element vnode is found, we don't
6119
+ * need to continue into its children because by attempting to disconnect the
6120
+ * custom element itself will trigger the removal of anything slotted or anything
6121
+ * defined on its shadow.
6122
+ */
6123
+ function recursivelyDisconnectChildren(vnodes) {
6124
+ for (let i = 0, len = vnodes.length; i < len; i += 1) {
6125
+ const vnode = vnodes[i];
6126
+ if (!isNull(vnode) && !isUndefined$1(vnode.elm)) {
6127
+ switch (vnode.type) {
6128
+ case 2 /* VNodeType.Element */:
6129
+ recursivelyDisconnectChildren(vnode.children);
6130
+ break;
6131
+ case 3 /* VNodeType.CustomElement */:
6132
+ {
6133
+ const vm = getAssociatedVM(vnode.elm);
6134
+ resetComponentStateWhenRemoved(vm);
6135
+ break;
6224
6136
  }
6225
- }
6226
- computeConfigAndUpdate();
6227
- });
6228
- ArrayPush$1.call(wiredDisconnecting, () => {
6229
- connector.disconnect();
6230
- resetConfigWatcher();
6231
- });
6137
+ }
6232
6138
  }
6233
6139
  }
6234
6140
  }
6235
- function connectWireAdapters(vm) {
6141
+ // This is a super optimized mechanism to remove the content of the root node (shadow root
6142
+ // for shadow DOM components and the root element itself for light DOM) without having to go
6143
+ // into snabbdom. Especially useful when the reset is a consequence of an error, in which case the
6144
+ // children VNodes might not be representing the current state of the DOM.
6145
+ function resetComponentRoot(vm) {
6236
6146
  const {
6237
- wiredConnecting
6238
- } = vm.context;
6239
- for (let i = 0, len = wiredConnecting.length; i < len; i += 1) {
6240
- wiredConnecting[i]();
6147
+ children,
6148
+ renderRoot,
6149
+ renderer: {
6150
+ remove
6151
+ }
6152
+ } = vm;
6153
+ for (let i = 0, len = children.length; i < len; i++) {
6154
+ const child = children[i];
6155
+ if (!isNull(child) && !isUndefined$1(child.elm)) {
6156
+ remove(child.elm, renderRoot);
6157
+ }
6241
6158
  }
6159
+ vm.children = EmptyArray;
6160
+ runChildNodesDisconnectedCallback(vm);
6161
+ vm.velements = EmptyArray;
6242
6162
  }
6243
- function disconnectWireAdapters(vm) {
6244
- const {
6245
- wiredDisconnecting
6246
- } = vm.context;
6247
- runWithBoundaryProtection(vm, vm, noop, () => {
6248
- // job
6249
- for (let i = 0, len = wiredDisconnecting.length; i < len; i += 1) {
6250
- wiredDisconnecting[i]();
6163
+ function getErrorBoundaryVM(vm) {
6164
+ let currentVm = vm;
6165
+ while (!isNull(currentVm)) {
6166
+ if (!isUndefined$1(currentVm.def.errorCallback)) {
6167
+ return currentVm;
6251
6168
  }
6252
- }, noop);
6169
+ currentVm = currentVm.owner;
6170
+ }
6171
+ }
6172
+ function runWithBoundaryProtection(vm, owner, pre, job, post) {
6173
+ let error;
6174
+ pre();
6175
+ try {
6176
+ job();
6177
+ } catch (e) {
6178
+ error = Object(e);
6179
+ } finally {
6180
+ post();
6181
+ if (!isUndefined$1(error)) {
6182
+ addErrorComponentStack(vm, error);
6183
+ const errorBoundaryVm = isNull(owner) ? undefined : getErrorBoundaryVM(owner);
6184
+ if (isUndefined$1(errorBoundaryVm)) {
6185
+ throw error; // eslint-disable-line no-unsafe-finally
6186
+ }
6187
+
6188
+ resetComponentRoot(vm); // remove offenders
6189
+ logOperationStart(6 /* OperationId.ErrorCallback */, vm);
6190
+ // error boundaries must have an ErrorCallback
6191
+ const errorCallback = errorBoundaryVm.def.errorCallback;
6192
+ invokeComponentCallback(errorBoundaryVm, errorCallback, [error, error.wcStack]);
6193
+ logOperationEnd(6 /* OperationId.ErrorCallback */, vm);
6194
+ }
6195
+ }
6253
6196
  }
6254
6197
 
6255
6198
  /*
@@ -6496,7 +6439,7 @@ function freezeTemplate(tmpl) {
6496
6439
  }
6497
6440
  }
6498
6441
  }
6499
- /* version: 2.34.0 */
6442
+ /* version: 2.35.1 */
6500
6443
 
6501
6444
  /*
6502
6445
  * Copyright (c) 2020, salesforce.com, inc.
@@ -6967,6 +6910,6 @@ function renderComponent(tagName, Ctor, props = {}) {
6967
6910
  */
6968
6911
  freeze(LightningElement);
6969
6912
  seal(LightningElement.prototype);
6970
- /* version: 2.34.0 */
6913
+ /* version: 2.35.1 */
6971
6914
 
6972
6915
  export { LightningElement, api$1 as api, createContextProvider, freezeTemplate, getComponentDef, isComponentConstructor, parseFragment, parseFragment as parseSVGFragment, readonly, register, registerComponent, registerDecorators, registerTemplate, renderComponent, renderer, sanitizeAttribute, setFeatureFlag, setFeatureFlagForTest, setHooks, track, unwrap, wire };