UNPKG

2.48 kBJavaScriptView Raw
1'use strict';
2
3const __chunk_1 = require('./stencilrouter-a3d77a87.js');
4
5const createProviderConsumer = (defaultState, consumerRender) => {
6 let listeners = new Map();
7 let currentState = defaultState;
8 const updateListener = (fields, instance) => {
9 if (Array.isArray(fields)) {
10 [...fields].forEach(fieldName => {
11 instance[fieldName] = currentState[fieldName];
12 });
13 }
14 else {
15 instance[fields] = Object.assign({}, currentState);
16 }
17 };
18 const subscribe = (instance, propList) => {
19 if (!listeners.has(instance)) {
20 listeners.set(instance, propList);
21 updateListener(propList, instance);
22 }
23 return () => {
24 if (listeners.has(instance)) {
25 listeners.delete(instance);
26 }
27 };
28 };
29 const Provider = ({ state }, children) => {
30 currentState = state;
31 listeners.forEach(updateListener);
32 return children;
33 };
34 const Consumer = (props, children) => {
35 // The casting on subscribe is to allow for crossover through the stencil compiler
36 // In the future we should allow for generics in components.
37 return consumerRender(subscribe, children[0]);
38 };
39 const injectProps = (Cstr, fieldList) => {
40 const CstrPrototype = Cstr.prototype;
41 const cstrConnectedCallback = CstrPrototype.connectedCallback;
42 const cstrDisconnectedCallback = CstrPrototype.disconnectedCallback;
43 CstrPrototype.connectedCallback = function () {
44 subscribe(this, fieldList);
45 if (cstrConnectedCallback) {
46 return cstrConnectedCallback.call(this);
47 }
48 };
49 CstrPrototype.disconnectedCallback = function () {
50 listeners.delete(this);
51 if (cstrDisconnectedCallback) {
52 cstrDisconnectedCallback.call(this);
53 }
54 };
55 };
56 return {
57 Provider,
58 Consumer,
59 injectProps
60 };
61};
62
63const ActiveRouter = createProviderConsumer({
64 historyType: 'browser',
65 location: {
66 pathname: '',
67 query: {},
68 key: ''
69 },
70 titleSuffix: '',
71 root: '/',
72 routeViewsUpdated: () => { }
73}, (subscribe, child) => (__chunk_1.h("context-consumer", { subscribe: subscribe, renderer: child })));
74
75exports.ActiveRouter = ActiveRouter;