@lwrjs/router 0.10.0-alpha.6 → 0.10.0-alpha.7

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 (78) hide show
  1. package/build/es/modules/lwr/contextProvider/contextProvider.js +30 -0
  2. package/build/es/modules/lwr/contextUtils/contextInfo.js +93 -0
  3. package/build/es/modules/lwr/contextUtils/contextUtils.js +77 -0
  4. package/build/es/modules/lwr/contextUtils/navigationApiStore.js +46 -0
  5. package/build/es/modules/lwr/currentPageReference/currentPageReference.js +14 -0
  6. package/build/es/modules/lwr/currentView/currentView.js +62 -0
  7. package/build/es/modules/lwr/domRouter/domRouter.js +441 -0
  8. package/build/es/modules/lwr/domRouterUtils/domRouterUtils.js +3 -0
  9. package/build/es/modules/lwr/domRouterUtils/historyUtils.js +30 -0
  10. package/build/es/modules/lwr/domRouterUtils/types.js +2 -0
  11. package/build/es/modules/lwr/domRouterUtils/uriUtils.js +69 -0
  12. package/build/es/modules/lwr/historyRouter/historyRouter.js +88 -0
  13. package/build/es/modules/lwr/navigation/navigation.js +20 -0
  14. package/build/es/modules/lwr/navigation/navigationApi.js +27 -0
  15. package/build/es/modules/lwr/navigation/navigationMixin.js +76 -0
  16. package/build/es/modules/lwr/navigationContext/navigationContext.js +10 -0
  17. package/build/es/modules/lwr/navigationMixinHacks/navigationMixinHacks.js +5 -0
  18. package/build/es/modules/lwr/observable/observable.js +71 -0
  19. package/build/es/modules/lwr/outlet/outlet.js +69 -0
  20. package/build/es/modules/lwr/router/router.js +201 -0
  21. package/build/es/modules/lwr/routerBridge/routerBridge.js +85 -0
  22. package/build/es/modules/lwr/routerContainer/routerContainer.js +116 -0
  23. package/build/es/modules/lwr/routerContainer/utils.js +83 -0
  24. package/build/es/modules/lwr/routerErrors/routerErrors.js +154 -0
  25. package/build/es/modules/lwr/routerUtils/domUtils.js +3 -0
  26. package/build/es/modules/lwr/routerUtils/filterUtils.js +74 -0
  27. package/build/es/modules/lwr/routerUtils/parseUtils.js +182 -0
  28. package/build/es/modules/lwr/routerUtils/pathToRegexp.js +415 -0
  29. package/build/es/modules/lwr/routerUtils/routeDefUtils.js +204 -0
  30. package/build/es/modules/lwr/routerUtils/routeUtils.js +239 -0
  31. package/build/es/modules/lwr/routerUtils/routerUtils.js +19 -0
  32. package/build/es/modules/lwr/routerUtils/typeUtils.js +112 -0
  33. package/build/es/modules/lwr/routerUtils/types.js +2 -0
  34. package/build/es/modules/lwr/routerUtils/uriUtils.js +134 -0
  35. package/package.json +12 -9
  36. /package/build/bundle/prod/lwr/navigation/{modules → es/modules}/lwr/currentView/currentView.d.ts +0 -0
  37. /package/build/bundle/prod/lwr/router/{modules → es/modules}/lwr/currentView/currentView.d.ts +0 -0
  38. /package/build/bundle/prod/lwr/routerContainer/{modules → es/modules}/lwr/currentView/currentView.d.ts +0 -0
  39. /package/build/{modules → es/modules}/lwr/contextProvider/contextProvider.d.ts +0 -0
  40. /package/build/{modules → es/modules}/lwr/contextUtils/contextInfo.d.ts +0 -0
  41. /package/build/{modules → es/modules}/lwr/contextUtils/contextUtils.d.ts +0 -0
  42. /package/build/{modules → es/modules}/lwr/contextUtils/navigationApiStore.d.ts +0 -0
  43. /package/build/{modules → es/modules}/lwr/currentPageReference/currentPageReference.d.ts +0 -0
  44. /package/build/{modules → es/modules}/lwr/currentView/currentView.d.ts +0 -0
  45. /package/build/{modules → es/modules}/lwr/domRouter/domRouter.d.ts +0 -0
  46. /package/build/{modules → es/modules}/lwr/domRouterUtils/domRouterUtils.d.ts +0 -0
  47. /package/build/{modules → es/modules}/lwr/domRouterUtils/historyUtils.d.ts +0 -0
  48. /package/build/{modules → es/modules}/lwr/domRouterUtils/types.d.ts +0 -0
  49. /package/build/{modules → es/modules}/lwr/domRouterUtils/uriUtils.d.ts +0 -0
  50. /package/build/{modules → es/modules}/lwr/historyRouter/historyRouter.d.ts +0 -0
  51. /package/build/{modules → es/modules}/lwr/navigation/navigation.d.ts +0 -0
  52. /package/build/{modules → es/modules}/lwr/navigation/navigationApi.d.ts +0 -0
  53. /package/build/{modules → es/modules}/lwr/navigation/navigationMixin.d.ts +0 -0
  54. /package/build/{modules → es/modules}/lwr/navigationContext/navigationContext.d.ts +0 -0
  55. /package/build/{modules → es/modules}/lwr/navigationMixinHacks/navigationMixinHacks.d.ts +0 -0
  56. /package/build/{modules → es/modules}/lwr/observable/observable.d.ts +0 -0
  57. /package/build/{modules → es/modules}/lwr/outlet/outlet.d.ts +0 -0
  58. /package/build/{modules → es/modules}/lwr/router/router.d.ts +0 -0
  59. /package/build/{modules → es/modules}/lwr/routerBridge/routerBridge.d.ts +0 -0
  60. /package/build/{modules → es/modules}/lwr/routerContainer/routerContainer.d.ts +0 -0
  61. /package/build/{modules → es/modules}/lwr/routerContainer/utils.d.ts +0 -0
  62. /package/build/{modules → es/modules}/lwr/routerErrors/routerErrors.d.ts +0 -0
  63. /package/build/{modules → es/modules}/lwr/routerUtils/domUtils.d.ts +0 -0
  64. /package/build/{modules → es/modules}/lwr/routerUtils/filterUtils.d.ts +0 -0
  65. /package/build/{modules → es/modules}/lwr/routerUtils/parseUtils.d.ts +0 -0
  66. /package/build/{modules → es/modules}/lwr/routerUtils/pathToRegexp.d.ts +0 -0
  67. /package/build/{modules → es/modules}/lwr/routerUtils/routeDefUtils.d.ts +0 -0
  68. /package/build/{modules → es/modules}/lwr/routerUtils/routeUtils.d.ts +0 -0
  69. /package/build/{modules → es/modules}/lwr/routerUtils/routerUtils.d.ts +0 -0
  70. /package/build/{modules → es/modules}/lwr/routerUtils/typeUtils.d.ts +0 -0
  71. /package/build/{modules → es/modules}/lwr/routerUtils/types.d.ts +0 -0
  72. /package/build/{modules → es/modules}/lwr/routerUtils/uriUtils.d.ts +0 -0
  73. /package/build/{services → es/services}/index.d.ts +0 -0
  74. /package/build/{services → es/services}/index.js +0 -0
  75. /package/build/{services → es/services}/module-provider/index.d.ts +0 -0
  76. /package/build/{services → es/services}/module-provider/index.js +0 -0
  77. /package/build/{services → es/services}/module-provider/utils.d.ts +0 -0
  78. /package/build/{services → es/services}/module-provider/utils.js +0 -0
@@ -0,0 +1,30 @@
1
+ import { createContextProvider } from 'lwc';
2
+ import { hasDocument } from 'lwr/routerUtils';
3
+ import { CurrentPageReference, CurrentView, NavigationContext } from 'lwr/navigation';
4
+ // Creating a context with LWC requires eventing APIs, which are not available from "@lwc/engine-server"
5
+ export const currentPageReferenceContextualizer = hasDocument
6
+ ? createContextProvider(CurrentPageReference)
7
+ : undefined;
8
+ export const currentViewContextualizer = hasDocument ? createContextProvider(CurrentView) : undefined;
9
+ export const navigationContextContextualizer = hasDocument
10
+ ? createContextProvider(NavigationContext)
11
+ : undefined;
12
+ /**
13
+ *
14
+ * @param {TContext} contextValue - Context API object
15
+ * @param {EventTarget} providerNode - Context DOM element
16
+ * @param {Contextualizer} contextualizer - Function for providing this context to subtree nodes wired to a specific adapter
17
+ * @param {ContextualWireAdapter<TContext, TEmit, TConfig>} contextualAdapter - Contextual wire adapter capable of subscribing to context changes
18
+ */
19
+ export function provideContext(contextValue, providerNode, contextualizer, contextualAdapter) {
20
+ if (contextualizer && contextualAdapter) {
21
+ // Set up provider to give context to wire adpaters so that a component connected
22
+ // under the providerNode subtree and wired to those adapters will receive this id
23
+ contextualAdapter.setContext(providerNode, contextValue);
24
+ contextualizer(providerNode, {
25
+ consumerConnectedCallback: contextualAdapter.subscribeContext.bind(contextualAdapter, providerNode),
26
+ consumerDisconnectedCallback: contextualAdapter.unsubscribeContext.bind(contextualAdapter, providerNode),
27
+ });
28
+ }
29
+ }
30
+ //# sourceMappingURL=contextProvider.js.map
@@ -0,0 +1,93 @@
1
+ function validateProvider(obj) {
2
+ if (obj === undefined || obj === null) {
3
+ throw new TypeError('Provider must be defined.');
4
+ }
5
+ }
6
+ /**
7
+ * Manages context for various providers
8
+ */
9
+ export class ContextInfo {
10
+ constructor(defaultValue) {
11
+ this.infoMap = new WeakMap();
12
+ this.defaultValue = defaultValue;
13
+ }
14
+ /**
15
+ * Gets the stored info for a context provider
16
+ * @param targetProvider the context provider
17
+ */
18
+ getInfo(targetProvider) {
19
+ let info = this.infoMap.get(targetProvider);
20
+ if (info === undefined) {
21
+ info = { consumers: new Set() };
22
+ this.infoMap.set(targetProvider, info);
23
+ }
24
+ return info;
25
+ }
26
+ /**
27
+ * Set the context value directly associated with the target as a context provider.
28
+ *
29
+ * @param {Object} targetProvider
30
+ * @param {FeatureContext} contextValue
31
+ */
32
+ setContext(targetProvider, contextValue) {
33
+ validateProvider(targetProvider);
34
+ const value = contextValue;
35
+ const info = this.getInfo(targetProvider);
36
+ info.contextValue = contextValue;
37
+ info.consumers.forEach((consumer) => consumer.provide(value));
38
+ if (info.consumers.size === 0 && value?.onComplete) {
39
+ // Ensure that wire callbacks are executed even if the wire has 0 subscribers
40
+ value.onComplete();
41
+ }
42
+ }
43
+ /**
44
+ * Get the context value directly associated with the target as a context provider.
45
+ *
46
+ * @param {Object} targetProvider
47
+ */
48
+ getContext(targetProvider) {
49
+ validateProvider(targetProvider);
50
+ const { contextValue } = this.getInfo(targetProvider);
51
+ return contextValue !== undefined ? contextValue : this.defaultValue;
52
+ }
53
+ /**
54
+ * Clear the context value and registered subscribers directly associated with the
55
+ * target as a context provider.
56
+ *
57
+ * @param {Object} targetProvider
58
+ */
59
+ clearContext(targetProvider) {
60
+ validateProvider(targetProvider);
61
+ this.infoMap.delete(targetProvider);
62
+ }
63
+ /**
64
+ * Subscribe a consumer to the context value directly associated with the target as
65
+ * a context provider. Calls to #set(targetProvider, contextValue) with the same
66
+ * target will invoke the consumer.provide(contextValue) function.
67
+ *
68
+ * NOTE: Mutations to the contextValue directly do not result in calls to
69
+ * consumer.provide(contextValue).
70
+ *
71
+ * @param {Object} targetProvider
72
+ * @param {ContextConsumer} consumer object with a provide(context) function property.
73
+ */
74
+ subscribeContext(targetProvider, consumer) {
75
+ validateProvider(targetProvider);
76
+ const { consumers, contextValue } = this.getInfo(targetProvider);
77
+ if (!consumers.has(consumer)) {
78
+ consumers.add(consumer);
79
+ consumer.provide(contextValue);
80
+ }
81
+ }
82
+ /**
83
+ * Unsubscribe a previously subscribed consumer from listening to changes on the
84
+ * target
85
+ * @param {Object} targetProvider
86
+ * @param {ContextConsumer} consumer
87
+ */
88
+ unsubscribeContext(targetProvider, consumer) {
89
+ validateProvider(targetProvider);
90
+ this.getInfo(targetProvider).consumers.delete(consumer);
91
+ }
92
+ }
93
+ //# sourceMappingURL=contextInfo.js.map
@@ -0,0 +1,77 @@
1
+ import { ContextInfo } from './contextInfo';
2
+ export { ContextInfo };
3
+ export { getNavigationHelm, registerNavigationHelm } from './navigationApiStore';
4
+ export function generateContextualWireAdapter(contextInstance) {
5
+ var _a;
6
+ const Adapter = (_a = class Adapter {
7
+ constructor(callback) {
8
+ this._callback = callback;
9
+ }
10
+ connect() {
11
+ // There is no 'connect' because context is provided via the update API.
12
+ }
13
+ disconnect() {
14
+ // no-op
15
+ }
16
+ // The default update method services wires that emit context as TEmit when their value changes.
17
+ // Wires that need more complex logic or use another emit type should override this method.
18
+ update(config, context) {
19
+ if (context) {
20
+ this._callback(context);
21
+ }
22
+ }
23
+ /**
24
+ * Set the context value directly associated with the target as a context provider.
25
+ *
26
+ * @param {EventTarget} targetProvider
27
+ * @param {ContextId} contextValue
28
+ */
29
+ static setContext(targetProvider, contextValue) {
30
+ contextInstance.setContext(targetProvider, contextValue);
31
+ }
32
+ /**
33
+ * Get the context value directly associated with the target as a context provider.
34
+ *
35
+ * @param {EventTarget} targetProvider
36
+ */
37
+ static getContext(targetProvider) {
38
+ return contextInstance.getContext(targetProvider);
39
+ }
40
+ /**
41
+ * Clear the context value and registered subscribers directly associated with the
42
+ * target as a context provider.
43
+ *
44
+ * @param {EventTarget} targetProvider
45
+ */
46
+ static clearContext(targetProvider) {
47
+ contextInstance.clearContext(targetProvider);
48
+ }
49
+ /**
50
+ * Subscribe a consumer to the context value directly associated with the target as
51
+ * a context provider. Calls to #set(targetProvider, contextValue) with the same
52
+ * target will invoke the consumer.provide(contextValue) function.
53
+ *
54
+ * NOTE: Mutations to the contextValue directly do not result in calls to
55
+ * consumer.provide(contextValue).
56
+ *
57
+ * @param {EventTarget} targetProvider
58
+ * @param {ContextConsumer} consumer object with a provide(context) function property.
59
+ */
60
+ static subscribeContext(targetProvider, consumer) {
61
+ contextInstance.subscribeContext(targetProvider, consumer);
62
+ }
63
+ /**
64
+ * Unsubscribe a previously subscribed consumer from listening to changes on the
65
+ * target
66
+ * @param {EventTarget} targetProvider
67
+ * @param {ContextConsumer} consumer
68
+ */
69
+ static unsubscribeContext(targetProvider, consumer) {
70
+ contextInstance.unsubscribeContext(targetProvider, consumer);
71
+ }
72
+ },
73
+ _a.contextSchema = { value: 'required' },
74
+ _a);
75
+ return Adapter;
76
+ }
77
+ //# sourceMappingURL=contextUtils.js.map
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Copyright (c) 2019, salesforce.com, inc.
3
+ * All rights reserved.
4
+ * SPDX-License-Identifier: MIT
5
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
+ */
7
+ import { generateMessage, messages } from 'lwr/routerErrors';
8
+ // Keep a cache of context metadata, with their IDs as keys
9
+ const CACHE = new WeakMap();
10
+ /**
11
+ * Return a navigation context by ID.
12
+ * Exported API.
13
+ *
14
+ * @param {*} id - The ID of a navigation context.
15
+ *
16
+ * @return {object} - { navigate(), generateUrl(), subscribe() }
17
+ */
18
+ export function getNavigationHelm(id) {
19
+ const metadata = CACHE.get(id);
20
+ if (!metadata || !metadata.value) {
21
+ throw new Error(generateMessage(messages.MISSING_CONTEXT));
22
+ }
23
+ return metadata.value;
24
+ }
25
+ /**
26
+ * Create and return the metadata for this context provider.
27
+ * Cache the metadata by ID.
28
+ *
29
+ * @param {object} contextValue - Context API object
30
+ * @param {RouteDefinition[]} data - Route data for this context
31
+ *
32
+ * @return {object} - The metadata object
33
+ */
34
+ export function registerNavigationHelm(contextId, contextValue) {
35
+ const metadata = {
36
+ id: contextId,
37
+ value: contextValue,
38
+ update: (newValue) => {
39
+ metadata.value = newValue;
40
+ },
41
+ };
42
+ // Cache and return.
43
+ CACHE.set(metadata.id, metadata);
44
+ return metadata;
45
+ }
46
+ //# sourceMappingURL=navigationApiStore.js.map
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Copyright (c) 2019, salesforce.com, inc.
3
+ * All rights reserved.
4
+ * SPDX-License-Identifier: MIT
5
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
+ */
7
+ import { generateContextualWireAdapter, ContextInfo } from 'lwr/contextUtils';
8
+ const CURRENT_PAGE_REFERENCE_CONTEXT = new ContextInfo(undefined);
9
+ /**
10
+ * Services @wire(CurrentPageReference) requests.
11
+ * Hooks up to an Observable from the current navigation context.
12
+ */
13
+ export const CurrentPageReference = generateContextualWireAdapter(CURRENT_PAGE_REFERENCE_CONTEXT);
14
+ //# sourceMappingURL=currentPageReference.js.map
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Copyright (c) 2019, salesforce.com, inc.
3
+ * All rights reserved.
4
+ * SPDX-License-Identifier: MIT
5
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
+ */
7
+ import { generateContextualWireAdapter, ContextInfo } from 'lwr/contextUtils';
8
+ import { generateMessageObject, messages } from 'lwr/routerErrors';
9
+ const CURRENT_VIEW_CONTEXT = new ContextInfo(undefined);
10
+ /**
11
+ * Services @wire(CurrentView) requests.
12
+ * Hooks up to an Observable from the current navigation context.
13
+ */
14
+ export const CurrentView = class CurrentView extends generateContextualWireAdapter(CURRENT_VIEW_CONTEXT) {
15
+ async update(config, context) {
16
+ if (context) {
17
+ const viewName = config && config.viewName ? config.viewName : 'default';
18
+ const viewEntry = context.viewset[viewName]; // either ViewInfo or an importer function
19
+ const viewInfo = viewEntry;
20
+ const viewImporter = (viewInfo && viewInfo.module) || viewEntry;
21
+ let importError;
22
+ if (viewImporter) {
23
+ try {
24
+ const viewModule = await viewImporter();
25
+ const newViewCtor = viewModule && viewModule.default;
26
+ if (newViewCtor && newViewCtor.constructor !== undefined) {
27
+ this._callback(newViewCtor);
28
+ }
29
+ else {
30
+ // delegate to catch for consistent error handling
31
+ throw new Error('error occurred with view import');
32
+ }
33
+ }
34
+ catch (e) {
35
+ const error = e;
36
+ if (viewInfo.specifier) {
37
+ importError = generateMessageObject(messages.VIEW_IMPORT_FAILED_WITH_SPECIFIER, [
38
+ viewInfo.specifier,
39
+ viewName,
40
+ error.message,
41
+ error.stack || '',
42
+ ]);
43
+ }
44
+ else {
45
+ importError = generateMessageObject(messages.VIEW_IMPORT_FAILED, [
46
+ viewName,
47
+ error.message,
48
+ error.stack || '',
49
+ ]);
50
+ }
51
+ }
52
+ }
53
+ else {
54
+ importError = generateMessageObject(messages.VIEW_MISSING, [viewName]);
55
+ }
56
+ if (context.onComplete) {
57
+ context.onComplete(importError);
58
+ }
59
+ }
60
+ }
61
+ };
62
+ //# sourceMappingURL=currentView.js.map