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