@ecopages/radiant 0.1.8 → 0.2.0
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.
- package/dist/context/context-provider.js +3 -279
- package/dist/context/context-provider.js.map +4 -6
- package/dist/context/create-context.js +3 -6
- package/dist/context/create-context.js.map +2 -2
- package/dist/context/decorators/consume-context.d.ts +3 -3
- package/dist/context/decorators/consume-context.js +3 -64
- package/dist/context/decorators/consume-context.js.map +4 -5
- package/dist/context/decorators/context-selector.d.ts +5 -8
- package/dist/context/decorators/context-selector.js +3 -71
- package/dist/context/decorators/context-selector.js.map +4 -5
- package/dist/context/decorators/legacy/consume-context.d.ts +3 -0
- package/dist/context/decorators/legacy/consume-context.js +4 -0
- package/dist/context/decorators/legacy/consume-context.js.map +10 -0
- package/dist/context/decorators/legacy/context-selector.d.ts +4 -0
- package/dist/context/decorators/legacy/context-selector.js +4 -0
- package/dist/context/decorators/legacy/context-selector.js.map +10 -0
- package/dist/context/decorators/legacy/provide-context.d.ts +4 -0
- package/dist/context/decorators/legacy/provide-context.js +4 -0
- package/dist/context/decorators/legacy/provide-context.js.map +10 -0
- package/dist/context/decorators/provide-context.d.ts +6 -7
- package/dist/context/decorators/provide-context.js +3 -291
- package/dist/context/decorators/provide-context.js.map +4 -7
- package/dist/context/decorators/standard/consume-context.d.ts +3 -0
- package/dist/context/decorators/standard/consume-context.js +4 -0
- package/dist/context/decorators/standard/consume-context.js.map +10 -0
- package/dist/context/decorators/standard/context-selector.d.ts +4 -0
- package/dist/context/decorators/standard/context-selector.js +4 -0
- package/dist/context/decorators/standard/context-selector.js.map +10 -0
- package/dist/context/decorators/standard/provide-context.d.ts +4 -0
- package/dist/context/decorators/standard/provide-context.js +4 -0
- package/dist/context/decorators/standard/provide-context.js.map +10 -0
- package/dist/context/events.d.ts +3 -2
- package/dist/context/events.js +3 -53
- package/dist/context/events.js.map +3 -3
- package/dist/core/radiant-element.d.ts +72 -20
- package/dist/core/radiant-element.js +3 -82
- package/dist/core/radiant-element.js.map +3 -3
- package/dist/decorators/bound.d.ts +2 -6
- package/dist/decorators/bound.js +2 -23
- package/dist/decorators/bound.js.map +3 -3
- package/dist/decorators/custom-element.d.ts +3 -1
- package/dist/decorators/custom-element.js +3 -14
- package/dist/decorators/custom-element.js.map +3 -3
- package/dist/decorators/debounce.d.ts +6 -1
- package/dist/decorators/debounce.js +2 -20
- package/dist/decorators/debounce.js.map +3 -3
- package/dist/decorators/event.d.ts +3 -2
- package/dist/decorators/event.js +3 -46
- package/dist/decorators/event.js.map +4 -5
- package/dist/decorators/legacy/bound.d.ts +9 -0
- package/dist/decorators/legacy/bound.js +4 -0
- package/dist/decorators/legacy/bound.js.map +10 -0
- package/dist/decorators/legacy/custom-element.d.ts +6 -0
- package/dist/decorators/legacy/custom-element.js +4 -0
- package/dist/decorators/legacy/custom-element.js.map +10 -0
- package/dist/decorators/legacy/debounce.d.ts +1 -0
- package/dist/decorators/legacy/debounce.js +4 -0
- package/dist/decorators/legacy/debounce.js.map +10 -0
- package/dist/decorators/legacy/event.d.ts +9 -0
- package/dist/decorators/legacy/event.js +3 -0
- package/dist/decorators/legacy/event.js.map +10 -0
- package/dist/decorators/legacy/on-event.d.ts +26 -0
- package/dist/decorators/legacy/on-event.js +4 -0
- package/dist/decorators/legacy/on-event.js.map +10 -0
- package/dist/decorators/legacy/on-updated.d.ts +6 -0
- package/dist/decorators/legacy/on-updated.js +4 -0
- package/dist/decorators/legacy/on-updated.js.map +10 -0
- package/dist/decorators/legacy/query.d.ts +23 -0
- package/dist/decorators/legacy/query.js +4 -0
- package/dist/decorators/legacy/query.js.map +10 -0
- package/dist/decorators/legacy/reactive-field.d.ts +10 -0
- package/dist/decorators/legacy/reactive-field.js +4 -0
- package/dist/decorators/legacy/reactive-field.js.map +10 -0
- package/dist/decorators/legacy/reactive-prop.d.ts +19 -0
- package/dist/decorators/legacy/reactive-prop.js +4 -0
- package/dist/decorators/legacy/reactive-prop.js.map +10 -0
- package/dist/decorators/on-event.d.ts +4 -7
- package/dist/decorators/on-event.js +3 -47
- package/dist/decorators/on-event.js.map +3 -3
- package/dist/decorators/on-updated.d.ts +3 -4
- package/dist/decorators/on-updated.js +3 -29
- package/dist/decorators/on-updated.js.map +3 -3
- package/dist/decorators/query.d.ts +9 -28
- package/dist/decorators/query.js +3 -36
- package/dist/decorators/query.js.map +3 -3
- package/dist/decorators/reactive-field.d.ts +3 -7
- package/dist/decorators/reactive-field.js +3 -26
- package/dist/decorators/reactive-field.js.map +3 -3
- package/dist/decorators/reactive-prop.d.ts +4 -15
- package/dist/decorators/reactive-prop.js +3 -214
- package/dist/decorators/reactive-prop.js.map +4 -5
- package/dist/decorators/standard/bound.d.ts +2 -0
- package/dist/decorators/standard/bound.js +4 -0
- package/dist/decorators/standard/bound.js.map +10 -0
- package/dist/decorators/standard/custom-element.d.ts +1 -0
- package/dist/decorators/standard/custom-element.js +4 -0
- package/dist/decorators/standard/custom-element.js.map +10 -0
- package/dist/decorators/standard/debounce.d.ts +2 -0
- package/dist/decorators/standard/debounce.js +4 -0
- package/dist/decorators/standard/debounce.js.map +10 -0
- package/dist/decorators/standard/event.d.ts +9 -0
- package/dist/decorators/standard/event.js +4 -0
- package/dist/decorators/standard/event.js.map +10 -0
- package/dist/decorators/standard/on-event.d.ts +27 -0
- package/dist/decorators/standard/on-event.js +4 -0
- package/dist/decorators/standard/on-event.js.map +10 -0
- package/dist/decorators/standard/on-updated.d.ts +2 -0
- package/dist/decorators/standard/on-updated.js +4 -0
- package/dist/decorators/standard/on-updated.js.map +10 -0
- package/dist/decorators/standard/query.d.ts +2 -0
- package/dist/decorators/standard/query.js +4 -0
- package/dist/decorators/standard/query.js.map +10 -0
- package/dist/decorators/standard/reactive-field.d.ts +2 -0
- package/dist/decorators/standard/reactive-field.js +4 -0
- package/dist/decorators/standard/reactive-field.js.map +10 -0
- package/dist/decorators/standard/reactive-prop.d.ts +2 -0
- package/dist/decorators/standard/reactive-prop.js +4 -0
- package/dist/decorators/standard/reactive-prop.js.map +10 -0
- package/dist/index.d.ts +14 -4
- package/dist/index.js +2 -716
- package/dist/index.js.map +3 -21
- package/dist/mixins/index.js +2 -23
- package/dist/mixins/index.js.map +3 -4
- package/dist/mixins/with-kita.js +3 -23
- package/dist/mixins/with-kita.js.map +3 -3
- package/dist/tools/event-emitter.d.ts +1 -1
- package/dist/tools/event-emitter.js +3 -22
- package/dist/tools/event-emitter.js.map +3 -3
- package/dist/tools/index.js +2 -28
- package/dist/tools/index.js.map +3 -5
- package/dist/tools/stringify-typed.js +3 -8
- package/dist/tools/stringify-typed.js.map +2 -2
- package/dist/types.d.ts +79 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +10 -0
- package/dist/utils/attribute-utils.d.ts +2 -0
- package/dist/utils/attribute-utils.js +3 -137
- package/dist/utils/attribute-utils.js.map +3 -3
- package/dist/utils/index.js +2 -137
- package/dist/utils/index.js.map +3 -4
- package/package.json +22 -34
- package/dist/context/index.d.ts +0 -7
- package/dist/context/index.js +0 -338
- package/dist/context/index.js.map +0 -16
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.js +0 -83
- package/dist/core/index.js.map +0 -10
- package/dist/decorators.d.ts +0 -7
- package/dist/decorators.js +0 -401
- package/dist/decorators.js.map +0 -18
|
@@ -1,280 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
switch (constant) {
|
|
4
|
-
case Array:
|
|
5
|
-
return "array";
|
|
6
|
-
case Boolean:
|
|
7
|
-
return "boolean";
|
|
8
|
-
case Number:
|
|
9
|
-
return "number";
|
|
10
|
-
case Object:
|
|
11
|
-
return "object";
|
|
12
|
-
case String:
|
|
13
|
-
return "string";
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function parseAttributeTypeDefault(defaultValue) {
|
|
17
|
-
switch (typeof defaultValue) {
|
|
18
|
-
case "boolean":
|
|
19
|
-
return "boolean";
|
|
20
|
-
case "number":
|
|
21
|
-
return "number";
|
|
22
|
-
case "string":
|
|
23
|
-
return "string";
|
|
24
|
-
}
|
|
25
|
-
if (Array.isArray(defaultValue))
|
|
26
|
-
return "array";
|
|
27
|
-
if (Object.prototype.toString.call(defaultValue) === "[object Object]")
|
|
28
|
-
return "object";
|
|
29
|
-
}
|
|
30
|
-
function defaultValueForType(type) {
|
|
31
|
-
switch (type) {
|
|
32
|
-
case Number:
|
|
33
|
-
return 0;
|
|
34
|
-
case String:
|
|
35
|
-
return "";
|
|
36
|
-
case Boolean:
|
|
37
|
-
return false;
|
|
38
|
-
default:
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function parseJSON(value) {
|
|
43
|
-
try {
|
|
44
|
-
return JSON.parse(value);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
throw new TypeError("Invalid JSON string");
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function writeJSON(value) {
|
|
50
|
-
return JSON.stringify(value);
|
|
51
|
-
}
|
|
52
|
-
function writeString(value) {
|
|
53
|
-
return `${value}`;
|
|
54
|
-
}
|
|
55
|
-
function readAttributeValue(value, type) {
|
|
56
|
-
const readerType = parseAttributeTypeConstant(type);
|
|
57
|
-
if (!readerType)
|
|
58
|
-
throw new TypeError(`[radiant-element] Unknown type "${type}"`);
|
|
59
|
-
return readers[readerType](value);
|
|
60
|
-
}
|
|
61
|
-
function writeAttributeValue(value, type) {
|
|
62
|
-
const writerType = parseAttributeTypeConstant(type);
|
|
63
|
-
if (!writerType)
|
|
64
|
-
throw new TypeError(`[radiant-element] Unknown type "${type}"`);
|
|
65
|
-
return (writers[writerType] || writers.default)(value);
|
|
66
|
-
}
|
|
67
|
-
function isBoolean(value) {
|
|
68
|
-
return typeof value === "boolean";
|
|
69
|
-
}
|
|
70
|
-
function isNumber(value) {
|
|
71
|
-
return typeof value === "number";
|
|
72
|
-
}
|
|
73
|
-
function isString(value) {
|
|
74
|
-
return typeof value === "string";
|
|
75
|
-
}
|
|
76
|
-
function isArray(value) {
|
|
77
|
-
return Array.isArray(value);
|
|
78
|
-
}
|
|
79
|
-
function isObject(value) {
|
|
80
|
-
return typeof value === "object" && !Array.isArray(value) && value !== null;
|
|
81
|
-
}
|
|
82
|
-
function isValueOfType(type, defaultValue) {
|
|
83
|
-
switch (type) {
|
|
84
|
-
case Boolean:
|
|
85
|
-
return isBoolean(defaultValue);
|
|
86
|
-
case Number:
|
|
87
|
-
return isNumber(defaultValue);
|
|
88
|
-
case String:
|
|
89
|
-
return isString(defaultValue);
|
|
90
|
-
case Array:
|
|
91
|
-
return isArray(defaultValue);
|
|
92
|
-
case Object:
|
|
93
|
-
return isObject(defaultValue);
|
|
94
|
-
default:
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
var readers = {
|
|
99
|
-
array(value) {
|
|
100
|
-
const array = parseJSON(value);
|
|
101
|
-
if (!Array.isArray(array)) {
|
|
102
|
-
throw new TypeError(`Expected an array but got a value of type "${typeof array}"`);
|
|
103
|
-
}
|
|
104
|
-
return array;
|
|
105
|
-
},
|
|
106
|
-
boolean(value) {
|
|
107
|
-
return !(value === "0" || String(value).toLowerCase() === "false");
|
|
108
|
-
},
|
|
109
|
-
number(value) {
|
|
110
|
-
const number = Number(value.replace(/_/g, ""));
|
|
111
|
-
return number;
|
|
112
|
-
},
|
|
113
|
-
object(value) {
|
|
114
|
-
const object = JSON.parse(value);
|
|
115
|
-
if (object === null || typeof object !== "object" || Array.isArray(object)) {
|
|
116
|
-
throw new TypeError(`expected value of type "object" but instead got value "${value}" of type "${parseAttributeTypeDefault(object)}"`);
|
|
117
|
-
}
|
|
118
|
-
return object;
|
|
119
|
-
},
|
|
120
|
-
string(value) {
|
|
121
|
-
return value;
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
var writers = {
|
|
125
|
-
default: writeString,
|
|
126
|
-
array: writeJSON,
|
|
127
|
-
object: writeJSON
|
|
128
|
-
};
|
|
1
|
+
import{q as G}from"./events.js";import{J as I}from"../utils/attribute-utils.js";class J{host;context;value;subscriptions=[];constructor(j,q){this.host=j,this.context=q.context;let z=q.initialValue;if(q.hydrate){const B=this.host.querySelector("script[data-hydration]");if(B?.textContent){const D=B.textContent,F=I(D,q.hydrate);if(q.hydrate===Object&&this.isObject(F)&&(this.isObject(z)||typeof z==="undefined"))z={...z??{},...F};else z=F}}this.value=z,this.registerEvents(),this.host.dispatchEvent(new G(this.context))}setContext=(j,q)=>{if(typeof this.value==="object"){const z={...this.value};if(this.value={...this.value,...j},q)q(this.value);this.notifySubscribers(this.value,z)}};getContext=()=>{return this.value};subscribe=({select:j,callback:q})=>{this.subscriptions.push({select:j,callback:q})};isObject(j){return typeof j==="object"&&!Array.isArray(j)&&j!==null}notifySubscribers=(j,q)=>{for(let z of this.subscriptions){if(!z.select)return this.sendSubscriptionUpdate(z,j);const B=z.select(j),D=z.select(q);if(B!==D)this.sendSubscriptionUpdate(z,j)}};sendSubscriptionUpdate=({select:j,callback:q},z)=>{if(!j)q(z);else q(j(z))};handleSubscriptionRequest=({select:j,callback:q,subscribe:z})=>{if(z)this.subscribe({select:j,callback:q});if(!this.value)return;if(j)q(j(this.value));else q(this.value)};onSubscriptionRequest=(j)=>{const{context:q,callback:z,subscribe:B,select:D,target:F}=j;if(q!==this.context)return;j.stopPropagation(),F.dispatchEvent(new G(this.context)),this.handleSubscriptionRequest({select:D,callback:z,subscribe:B})};onContextRequest=(j)=>{const{context:q,callback:z}=j;if(q!==this.context)return;j.stopPropagation(),z(this)};registerEvents=()=>{this.host.addEventListener("context-subscription-request",this.onSubscriptionRequest),this.host.addEventListener("context-request",this.onContextRequest)}}export{J as ContextProvider};
|
|
2
|
+
export{J as n};
|
|
129
3
|
|
|
130
|
-
|
|
131
|
-
var ContextEventsTypes;
|
|
132
|
-
((ContextEventsTypes2) => {
|
|
133
|
-
ContextEventsTypes2["SUBSCRIPTION_REQUEST"] = "context--subscription-request";
|
|
134
|
-
ContextEventsTypes2["CONTEXT_REQUEST"] = "context-request";
|
|
135
|
-
ContextEventsTypes2["ON_MOUNT"] = "context--on-mount";
|
|
136
|
-
})(ContextEventsTypes ||= {});
|
|
137
|
-
|
|
138
|
-
class ContextRequestEvent extends Event {
|
|
139
|
-
context;
|
|
140
|
-
callback;
|
|
141
|
-
subscribe;
|
|
142
|
-
constructor(context, callback, subscribe) {
|
|
143
|
-
super("context-request" /* CONTEXT_REQUEST */, { bubbles: true, composed: true });
|
|
144
|
-
this.context = context;
|
|
145
|
-
this.callback = callback;
|
|
146
|
-
this.subscribe = subscribe;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
class ContextOnMountEvent extends CustomEvent {
|
|
151
|
-
constructor(context) {
|
|
152
|
-
super("context--on-mount" /* ON_MOUNT */, {
|
|
153
|
-
detail: { context },
|
|
154
|
-
bubbles: true,
|
|
155
|
-
composed: true
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
class ContextSubscriptionRequestEvent extends Event {
|
|
161
|
-
context;
|
|
162
|
-
callback;
|
|
163
|
-
select;
|
|
164
|
-
subscribe;
|
|
165
|
-
constructor(context, callback, select, subscribe) {
|
|
166
|
-
super("context--subscription-request" /* SUBSCRIPTION_REQUEST */, {
|
|
167
|
-
bubbles: true,
|
|
168
|
-
composed: true
|
|
169
|
-
});
|
|
170
|
-
this.context = context;
|
|
171
|
-
this.callback = callback;
|
|
172
|
-
this.select = select;
|
|
173
|
-
this.subscribe = subscribe;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// src/context/context-provider.ts
|
|
178
|
-
class ContextProvider {
|
|
179
|
-
host;
|
|
180
|
-
context;
|
|
181
|
-
value;
|
|
182
|
-
subscriptions = [];
|
|
183
|
-
constructor(host, options) {
|
|
184
|
-
this.host = host;
|
|
185
|
-
this.context = options.context;
|
|
186
|
-
let contextValue = options.initialValue;
|
|
187
|
-
if (options.hydrate) {
|
|
188
|
-
const hydrationScriptElement = this.host.querySelector("script[data-hydration]");
|
|
189
|
-
if (hydrationScriptElement?.textContent) {
|
|
190
|
-
const hydrationValue = hydrationScriptElement.textContent;
|
|
191
|
-
const parsedHydrationValue = readAttributeValue(hydrationValue, options.hydrate);
|
|
192
|
-
if (options.hydrate === Object && this.isObject(parsedHydrationValue) && (this.isObject(contextValue) || typeof contextValue === "undefined")) {
|
|
193
|
-
contextValue = {
|
|
194
|
-
...contextValue ?? {},
|
|
195
|
-
...parsedHydrationValue
|
|
196
|
-
};
|
|
197
|
-
} else {
|
|
198
|
-
contextValue = parsedHydrationValue;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
this.value = contextValue;
|
|
203
|
-
this.registerEvents();
|
|
204
|
-
this.host.dispatchEvent(new ContextOnMountEvent(this.context));
|
|
205
|
-
}
|
|
206
|
-
setContext = (update, callback) => {
|
|
207
|
-
if (typeof this.value === "object") {
|
|
208
|
-
const oldContext = { ...this.value };
|
|
209
|
-
this.value = { ...this.value, ...update };
|
|
210
|
-
if (callback)
|
|
211
|
-
callback(this.value);
|
|
212
|
-
this.notifySubscribers(this.value, oldContext);
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
getContext = () => {
|
|
216
|
-
return this.value;
|
|
217
|
-
};
|
|
218
|
-
subscribe = ({ select, callback }) => {
|
|
219
|
-
this.subscriptions.push({ select, callback });
|
|
220
|
-
};
|
|
221
|
-
isObject(value) {
|
|
222
|
-
return typeof value === "object" && !Array.isArray(value) && value !== null;
|
|
223
|
-
}
|
|
224
|
-
notifySubscribers = (newContext, prevContext) => {
|
|
225
|
-
for (const sub of this.subscriptions) {
|
|
226
|
-
if (!sub.select)
|
|
227
|
-
return this.sendSubscriptionUpdate(sub, newContext);
|
|
228
|
-
const newSelected = sub.select(newContext);
|
|
229
|
-
const prevSelected = sub.select(prevContext);
|
|
230
|
-
if (newSelected !== prevSelected) {
|
|
231
|
-
this.sendSubscriptionUpdate(sub, newContext);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
sendSubscriptionUpdate = ({ select, callback }, context) => {
|
|
236
|
-
if (!select)
|
|
237
|
-
callback(context);
|
|
238
|
-
else
|
|
239
|
-
callback(select(context));
|
|
240
|
-
};
|
|
241
|
-
handleSubscriptionRequest = ({
|
|
242
|
-
select,
|
|
243
|
-
callback,
|
|
244
|
-
subscribe
|
|
245
|
-
}) => {
|
|
246
|
-
if (subscribe)
|
|
247
|
-
this.subscribe({ select, callback });
|
|
248
|
-
if (!this.value)
|
|
249
|
-
return;
|
|
250
|
-
if (select) {
|
|
251
|
-
callback(select(this.value));
|
|
252
|
-
} else {
|
|
253
|
-
callback(this.value);
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
onSubscriptionRequest = (event) => {
|
|
257
|
-
const { context, callback, subscribe, select, target } = event;
|
|
258
|
-
if (context !== this.context)
|
|
259
|
-
return;
|
|
260
|
-
event.stopPropagation();
|
|
261
|
-
target.dispatchEvent(new ContextOnMountEvent(this.context));
|
|
262
|
-
this.handleSubscriptionRequest({ select, callback, subscribe });
|
|
263
|
-
};
|
|
264
|
-
onContextRequest = (event) => {
|
|
265
|
-
const { context, callback } = event;
|
|
266
|
-
if (context !== this.context)
|
|
267
|
-
return;
|
|
268
|
-
event.stopPropagation();
|
|
269
|
-
callback(this);
|
|
270
|
-
};
|
|
271
|
-
registerEvents = () => {
|
|
272
|
-
this.host.addEventListener("context--subscription-request" /* SUBSCRIPTION_REQUEST */, this.onSubscriptionRequest);
|
|
273
|
-
this.host.addEventListener("context-request" /* CONTEXT_REQUEST */, this.onContextRequest);
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
export {
|
|
277
|
-
ContextProvider
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
//# debugId=19BF7CBFBD9F2B6364756E2164756E21
|
|
4
|
+
//# debugId=1E45EE002AF157AF64756E2164756E21
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/
|
|
3
|
+
"sources": ["../src/context/context-provider.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"
|
|
6
|
-
"import type { Context, ContextCallback, ContextType, UnknownContext } from './types';\n\n/**\n * List of events which can be emitted by a context provider or requester.\n */\nexport enum ContextEventsTypes {\n SUBSCRIPTION_REQUEST = 'context--subscription-request',\n CONTEXT_REQUEST = 'context-request',\n ON_MOUNT = 'context--on-mount',\n}\n\n/**\n * An event fired by a context requester to signal it desires a named context.\n *\n * A provider should inspect the `context` property of the event to determine if it has a value that can\n * satisfy the request, calling the `callback` with the requested value if so.\n *\n * If the requested context event contains a truthy `subscribe` value, then a provider can call the callback\n * multiple times if the value is changed, if this is the case the provider should pass an `unsubscribe`\n * function to the callback which requesters can invoke to indicate they no longer wish to receive these updates.\n */\nexport class ContextRequestEvent<T extends UnknownContext> extends Event {\n public constructor(\n public readonly context: T,\n public readonly callback: ContextCallback<ContextType<T>>,\n public readonly subscribe?: boolean,\n ) {\n super(ContextEventsTypes.CONTEXT_REQUEST, { bubbles: true, composed: true });\n }\n}\n\n/**\n * A type which represents a subscription to a context value.\n */\nexport type ContextSubscription<T extends UnknownContext> = {\n select?: (context: ContextType<T>) => unknown;\n callback: (value: unknown) => void;\n};\n\n/**\n * An event fired by a context provider to signal that a context value has been mounted and is available for consumption.\n */\nexport class ContextOnMountEvent extends CustomEvent<{ context: UnknownContext }> {\n public constructor(context: UnknownContext) {\n super(ContextEventsTypes.ON_MOUNT, {\n detail: { context },\n bubbles: true,\n composed: true,\n });\n }\n}\n\n/**\n * An event fired by a context requester to signal it desires a named context.\n *\n * A provider should inspect the `context` property of the event to determine if it has a value that can\n * satisfy the request, calling the `callback` with the requested value if so.\n *\n * If the requested context event contains a truthy `subscribe` value, then a provider can call the callback\n * multiple times if the value is changed, if this is the case the provider should pass an `unsubscribe`\n * function to the callback which requesters can invoke to indicate they no longer wish to receive these updates.\n *\n * It accepts a `selector` property which can be used to request a specific property of the context value.\n */\nexport class ContextSubscriptionRequestEvent<T extends UnknownContext> extends Event {\n public constructor(\n public readonly context: T,\n public readonly callback: (value: ContextType<T> | { [K in keyof ContextType<T>]: ContextType<T>[K] }) => void,\n public readonly select?: (context: ContextType<T>) => unknown,\n public readonly subscribe?: boolean,\n ) {\n super(ContextEventsTypes.SUBSCRIPTION_REQUEST, {\n bubbles: true,\n composed: true,\n });\n }\n}\n\ndeclare global {\n interface HTMLElementEventMap {\n /**\n * A 'context-request-subscription' event can be emitted by any element which desires\n * a context value to be injected by an external provider.\n */\n [ContextEventsTypes.SUBSCRIPTION_REQUEST]: ContextSubscriptionRequestEvent<UnknownContext>;\n /**\n * A context-request-provider event can be emitted by a context requester to signal\n * that it desires a context value to be provided by a context provider.\n */\n [ContextEventsTypes.CONTEXT_REQUEST]: ContextRequestEvent<Context<unknown, unknown>>;\n /**\n * A 'context-mount' event can be emitted by a context provider to signal\n * that a context value has been mounted and is available for consumption.\n */\n [ContextEventsTypes.ON_MOUNT]: ContextOnMountEvent;\n }\n}\n",
|
|
7
|
-
"import type { RadiantElement } from '@/core/radiant-element';\nimport { type AttributeTypeConstant, readAttributeValue } from '@/utils/attribute-utils';\nimport { query } from '..';\nimport {\n ContextEventsTypes,\n ContextOnMountEvent,\n type ContextRequestEvent,\n type ContextSubscription,\n type ContextSubscriptionRequestEvent,\n} from './events';\nimport type { Context, ContextType, UnknownContext } from './types';\n\ntype ContextProviderOptions<T extends UnknownContext> = {\n context: UnknownContext;\n initialValue?: T['__context__'];\n hydrate?: AttributeTypeConstant;\n};\n\n/**\n * Represents a context provider that allows setting and getting the context,\n * as well as subscribing to context updates.\n *\n * @template T - The type of the context.\n */\nexport interface IContextProvider<T extends Context<unknown, unknown>> {\n /**\n * Sets the context with the provided update and invokes the optional callback function.\n *\n * @param update - The partial update to be applied to the context.\n * @param callback - An optional callback function that receives the updated context.\n */\n setContext: (update: Partial<ContextType<T>>, callback?: (context: ContextType<T>) => void) => void;\n\n /**\n * Gets the current context.\n *\n * @returns The current context.\n */\n getContext: () => ContextType<T>;\n\n /**\n * Subscribes to context updates.\n *\n * @param subscription - The subscription object that defines the callback function to be invoked on context updates.\n */\n subscribe: (subscription: ContextSubscription<T>) => void;\n}\n\n/**\n * It creates a context provider that allows setting and getting the context,\n * It will also be in charge of notifying the subscribers when the context changes.\n *\n * @template T - The type of the context.\n * @implements IContextProvider\n *\n * @example\n * ```ts\n * export class MyElement extends RadiantElement {\n * provider = new ContextProvider<typeof myContext>(this, {\n * context: myContext,\n * initialValue: {\n * value: 'Hello World',\n * },\n * });\n * ```\n */\nexport class ContextProvider<T extends Context<unknown, unknown>> implements IContextProvider<T> {\n private host: RadiantElement;\n private context: UnknownContext;\n private value: ContextType<T> | undefined;\n\n subscriptions: ContextSubscription<T>[] = [];\n\n /**\n * Creates a new instance of the ContextProvider.\n *\n * @param host - The host element that will contain the context provider.\n * @param options - The options to configure the context provider.\n */\n constructor(host: RadiantElement, options: ContextProviderOptions<T>) {\n this.host = host;\n this.context = options.context;\n let contextValue: T['__context__'] | undefined = options.initialValue;\n\n if (options.hydrate) {\n const hydrationScriptElement = this.host.querySelector('script[data-hydration]');\n if (hydrationScriptElement?.textContent) {\n const hydrationValue = hydrationScriptElement.textContent;\n const parsedHydrationValue = readAttributeValue(hydrationValue, options.hydrate) as ContextType<T>;\n\n if (\n options.hydrate === Object &&\n this.isObject(parsedHydrationValue) &&\n (this.isObject(contextValue) || typeof contextValue === 'undefined')\n ) {\n contextValue = {\n ...(contextValue ?? {}),\n ...parsedHydrationValue,\n };\n } else {\n contextValue = parsedHydrationValue;\n }\n }\n }\n\n this.value = contextValue as ContextType<T>;\n\n this.registerEvents();\n this.host.dispatchEvent(new ContextOnMountEvent(this.context));\n }\n\n setContext = (update: Partial<ContextType<T>>, callback?: (context: ContextType<T>) => void) => {\n if (typeof this.value === 'object') {\n const oldContext = { ...this.value };\n this.value = { ...this.value, ...update };\n if (callback) callback(this.value);\n this.notifySubscribers(this.value, oldContext);\n }\n };\n\n getContext = () => {\n return this.value as ContextType<T>;\n };\n\n subscribe = ({ select, callback }: ContextSubscription<T>) => {\n this.subscriptions.push({ select, callback });\n };\n\n private isObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && !Array.isArray(value) && value !== null;\n }\n\n private notifySubscribers = (newContext: ContextType<T>, prevContext: ContextType<T>) => {\n for (const sub of this.subscriptions) {\n if (!sub.select) return this.sendSubscriptionUpdate(sub, newContext);\n const newSelected = sub.select(newContext);\n const prevSelected = sub.select(prevContext);\n if (newSelected !== prevSelected) {\n this.sendSubscriptionUpdate(sub, newContext);\n }\n }\n };\n\n private sendSubscriptionUpdate = ({ select, callback }: ContextSubscription<T>, context: ContextType<T>) => {\n if (!select) callback(context);\n else callback(select(context));\n };\n\n private handleSubscriptionRequest = ({\n select,\n callback,\n subscribe,\n }: {\n select?: ContextSubscription<T>['select'];\n callback: ContextSubscription<T>['callback'];\n subscribe?: boolean;\n }) => {\n if (subscribe) this.subscribe({ select, callback });\n\n if (!this.value) return;\n\n if (select) {\n callback(select(this.value));\n } else {\n callback(this.value as ContextType<T>);\n }\n };\n\n private onSubscriptionRequest = (event: ContextSubscriptionRequestEvent<UnknownContext>) => {\n const { context, callback, subscribe, select, target } = event;\n if (context !== this.context) return;\n\n event.stopPropagation();\n\n (target as HTMLElement).dispatchEvent(new ContextOnMountEvent(this.context));\n\n this.handleSubscriptionRequest({ select, callback, subscribe });\n };\n\n private onContextRequest = (event: ContextRequestEvent<UnknownContext>) => {\n const { context, callback } = event;\n if (context !== this.context) return;\n event.stopPropagation();\n callback(this);\n };\n\n private registerEvents = () => {\n this.host.addEventListener(ContextEventsTypes.SUBSCRIPTION_REQUEST, this.onSubscriptionRequest);\n this.host.addEventListener(ContextEventsTypes.CONTEXT_REQUEST, this.onContextRequest);\n };\n}\n"
|
|
5
|
+
"import type { RadiantElement } from '../core/radiant-element';\nimport { type AttributeTypeConstant, readAttributeValue } from '../utils/attribute-utils';\nimport {\n ContextEventsTypes,\n ContextOnMountEvent,\n type ContextRequestEvent,\n type ContextSubscription,\n type ContextSubscriptionRequestEvent,\n} from './events';\nimport type { Context, ContextType, UnknownContext } from './types';\n\ntype ContextProviderOptions<T extends UnknownContext> = {\n context: UnknownContext;\n initialValue?: T['__context__'];\n hydrate?: AttributeTypeConstant;\n};\n\n/**\n * Represents a context provider that allows setting and getting the context,\n * as well as subscribing to context updates.\n *\n * @template T - The type of the context.\n */\nexport interface IContextProvider<T extends Context<unknown, unknown>> {\n /**\n * Sets the context with the provided update and invokes the optional callback function.\n *\n * @param update - The partial update to be applied to the context.\n * @param callback - An optional callback function that receives the updated context.\n */\n setContext: (update: Partial<ContextType<T>>, callback?: (context: ContextType<T>) => void) => void;\n\n /**\n * Gets the current context.\n *\n * @returns The current context.\n */\n getContext: () => ContextType<T>;\n\n /**\n * Subscribes to context updates.\n *\n * @param subscription - The subscription object that defines the callback function to be invoked on context updates.\n */\n subscribe: (subscription: ContextSubscription<T>) => void;\n}\n\n/**\n * It creates a context provider that allows setting and getting the context,\n * It will also be in charge of notifying the subscribers when the context changes.\n *\n * @template T - The type of the context.\n * @implements IContextProvider\n *\n * @example\n * ```ts\n * export class MyElement extends RadiantElement {\n * provider = new ContextProvider<typeof myContext>(this, {\n * context: myContext,\n * initialValue: {\n * value: 'Hello World',\n * },\n * });\n * ```\n */\nexport class ContextProvider<T extends Context<unknown, unknown>> implements IContextProvider<T> {\n private host: RadiantElement;\n private context: UnknownContext;\n private value: ContextType<T> | undefined;\n\n subscriptions: ContextSubscription<T>[] = [];\n\n /**\n * Creates a new instance of the ContextProvider.\n *\n * @param host - The host element that will contain the context provider.\n * @param options - The options to configure the context provider.\n */\n constructor(host: RadiantElement, options: ContextProviderOptions<T>) {\n this.host = host;\n this.context = options.context;\n let contextValue: T['__context__'] | undefined = options.initialValue;\n\n if (options.hydrate) {\n const hydrationScriptElement = this.host.querySelector('script[data-hydration]');\n if (hydrationScriptElement?.textContent) {\n const hydrationValue = hydrationScriptElement.textContent;\n const parsedHydrationValue = readAttributeValue(hydrationValue, options.hydrate) as ContextType<T>;\n\n if (\n options.hydrate === Object &&\n this.isObject(parsedHydrationValue) &&\n (this.isObject(contextValue) || typeof contextValue === 'undefined')\n ) {\n contextValue = {\n ...(contextValue ?? {}),\n ...parsedHydrationValue,\n };\n } else {\n contextValue = parsedHydrationValue;\n }\n }\n }\n\n this.value = contextValue as ContextType<T>;\n\n this.registerEvents();\n this.host.dispatchEvent(new ContextOnMountEvent(this.context));\n }\n\n setContext = (update: Partial<ContextType<T>>, callback?: (context: ContextType<T>) => void) => {\n if (typeof this.value === 'object') {\n const oldContext = { ...this.value };\n this.value = { ...this.value, ...update };\n if (callback) callback(this.value);\n this.notifySubscribers(this.value, oldContext);\n }\n };\n\n getContext = () => {\n return this.value as ContextType<T>;\n };\n\n subscribe = ({ select, callback }: ContextSubscription<T>) => {\n this.subscriptions.push({ select, callback });\n };\n\n private isObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && !Array.isArray(value) && value !== null;\n }\n\n private notifySubscribers = (newContext: ContextType<T>, prevContext: ContextType<T>) => {\n for (const sub of this.subscriptions) {\n if (!sub.select) return this.sendSubscriptionUpdate(sub, newContext);\n const newSelected = sub.select(newContext);\n const prevSelected = sub.select(prevContext);\n if (newSelected !== prevSelected) {\n this.sendSubscriptionUpdate(sub, newContext);\n }\n }\n };\n\n private sendSubscriptionUpdate = ({ select, callback }: ContextSubscription<T>, context: ContextType<T>) => {\n if (!select) callback(context);\n else callback(select(context));\n };\n\n private handleSubscriptionRequest = ({\n select,\n callback,\n subscribe,\n }: {\n select?: ContextSubscription<T>['select'];\n callback: ContextSubscription<T>['callback'];\n subscribe?: boolean;\n }) => {\n if (subscribe) this.subscribe({ select, callback });\n\n if (!this.value) return;\n\n if (select) {\n callback(select(this.value));\n } else {\n callback(this.value as ContextType<T>);\n }\n };\n\n private onSubscriptionRequest = (event: ContextSubscriptionRequestEvent<UnknownContext>) => {\n const { context, callback, subscribe, select, target } = event;\n if (context !== this.context) return;\n\n event.stopPropagation();\n\n (target as HTMLElement).dispatchEvent(new ContextOnMountEvent(this.context));\n\n this.handleSubscriptionRequest({ select, callback, subscribe });\n };\n\n private onContextRequest = (event: ContextRequestEvent<UnknownContext>) => {\n const { context, callback } = event;\n if (context !== this.context) return;\n event.stopPropagation();\n callback(this);\n };\n\n private registerEvents = () => {\n this.host.addEventListener(ContextEventsTypes.SUBSCRIPTION_REQUEST, this.onSubscriptionRequest);\n this.host.addEventListener(ContextEventsTypes.CONTEXT_REQUEST, this.onContextRequest);\n };\n}\n"
|
|
8
6
|
],
|
|
9
|
-
"mappings": "
|
|
10
|
-
"debugId": "
|
|
7
|
+
"mappings": "8EAiEO,QAAM,CAAoF,CACvF,KACA,QACA,MAER,cAA0C,CAAC,EAQ3C,WAAW,CAAC,EAAsB,EAAoC,CACpE,KAAK,KAAO,EACZ,KAAK,QAAU,EAAQ,QACvB,IAAI,EAA6C,EAAQ,aAEzD,GAAI,EAAQ,QAAS,CACnB,MAAM,EAAyB,KAAK,KAAK,cAAc,wBAAwB,EAC/E,GAAI,GAAwB,YAAa,CACvC,MAAM,EAAiB,EAAuB,YACxC,EAAuB,EAAmB,EAAgB,EAAQ,OAAO,EAE/E,GACE,EAAQ,UAAY,QACpB,KAAK,SAAS,CAAoB,IACjC,KAAK,SAAS,CAAY,UAAY,IAAiB,aAExD,EAAe,IACT,GAAgB,CAAC,KAClB,CACL,MAEA,GAAe,GAKrB,KAAK,MAAQ,EAEb,KAAK,eAAe,EACpB,KAAK,KAAK,cAAc,IAAI,EAAoB,KAAK,OAAO,CAAC,EAG/D,WAAa,CAAC,EAAiC,IAAiD,CAC9F,UAAW,KAAK,QAAU,SAAU,CAClC,MAAM,EAAa,IAAK,KAAK,KAAM,EAEnC,GADA,KAAK,MAAQ,IAAK,KAAK,SAAU,CAAO,EACpC,EAAU,EAAS,KAAK,KAAK,EACjC,KAAK,kBAAkB,KAAK,MAAO,CAAU,IAIjD,WAAa,IAAM,CACjB,OAAO,KAAK,OAGd,UAAY,EAAG,SAAQ,cAAuC,CAC5D,KAAK,cAAc,KAAK,CAAE,SAAQ,UAAS,CAAC,GAGtC,QAAQ,CAAC,EAAkD,CACjE,cAAc,IAAU,WAAa,MAAM,QAAQ,CAAK,GAAK,IAAU,KAGjE,kBAAoB,CAAC,EAA4B,IAAgC,CACvF,QAAW,KAAO,KAAK,cAAe,CACpC,IAAK,EAAI,OAAQ,OAAO,KAAK,uBAAuB,EAAK,CAAU,EACnE,MAAM,EAAc,EAAI,OAAO,CAAU,EACnC,EAAe,EAAI,OAAO,CAAW,EAC3C,GAAI,IAAgB,EAClB,KAAK,uBAAuB,EAAK,CAAU,IAKzC,uBAAyB,EAAG,SAAQ,YAAoC,IAA4B,CAC1G,IAAK,EAAQ,EAAS,CAAO,MACxB,GAAS,EAAO,CAAO,CAAC,GAGvB,0BAA4B,EAClC,SACA,WACA,eAKI,CACJ,GAAI,EAAW,KAAK,UAAU,CAAE,SAAQ,UAAS,CAAC,EAElD,IAAK,KAAK,MAAO,OAEjB,GAAI,EACF,EAAS,EAAO,KAAK,KAAK,CAAC,MAE3B,GAAS,KAAK,KAAuB,GAIjC,sBAAwB,CAAC,IAA2D,CAC1F,MAAQ,UAAS,WAAU,YAAW,SAAQ,UAAW,EACzD,GAAI,IAAY,KAAK,QAAS,OAE9B,EAAM,gBAAgB,EAErB,EAAuB,cAAc,IAAI,EAAoB,KAAK,OAAO,CAAC,EAE3E,KAAK,0BAA0B,CAAE,SAAQ,WAAU,WAAU,CAAC,GAGxD,iBAAmB,CAAC,IAA+C,CACzE,MAAQ,UAAS,YAAa,EAC9B,GAAI,IAAY,KAAK,QAAS,OAC9B,EAAM,gBAAgB,EACtB,EAAS,IAAI,GAGP,eAAiB,IAAM,CAC7B,KAAK,KAAK,gDAA0D,KAAK,qBAAqB,EAC9F,KAAK,KAAK,mCAAqD,KAAK,gBAAgB,EAExF",
|
|
8
|
+
"debugId": "1E45EE002AF157AF64756E2164756E21",
|
|
11
9
|
"names": []
|
|
12
10
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import type { Context } from './types';\n\n/**\n * A function which creates a Context value object\n */\nexport const createContext = <ValueType>(key: unknown) => key as Context<typeof key, ValueType>;\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": "AAKO,IAAM,EAAgB,CAAY,IAAiB",
|
|
8
|
+
"debugId": "B8BB9E641176DF6864756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { StandardOrLegacyFieldDecoratorArgs } from '../../types';
|
|
2
|
+
import type { UnknownContext } from '../types';
|
|
3
3
|
/**
|
|
4
4
|
* A decorator to provide a context to the target element.
|
|
5
5
|
* @param contextToProvide
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
export declare function consumeContext(contextToProvide: UnknownContext): (
|
|
8
|
+
export declare function consumeContext(contextToProvide: UnknownContext): (protoOrTarget: StandardOrLegacyFieldDecoratorArgs["protoOrTarget"], nameOrContext: StandardOrLegacyFieldDecoratorArgs["nameOrContext"]) => any;
|
|
@@ -1,65 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
((ContextEventsTypes2) => {
|
|
4
|
-
ContextEventsTypes2["SUBSCRIPTION_REQUEST"] = "context--subscription-request";
|
|
5
|
-
ContextEventsTypes2["CONTEXT_REQUEST"] = "context-request";
|
|
6
|
-
ContextEventsTypes2["ON_MOUNT"] = "context--on-mount";
|
|
7
|
-
})(ContextEventsTypes ||= {});
|
|
1
|
+
import{g as k}from"./legacy/consume-context.js";import{o as q}from"./standard/consume-context.js";import"../events.js";function B(f){return function(j,b){if(typeof b==="object")return q(f)(j,b);return k(f)(j,b)}}export{B as consumeContext};
|
|
2
|
+
export{B as f};
|
|
8
3
|
|
|
9
|
-
|
|
10
|
-
context;
|
|
11
|
-
callback;
|
|
12
|
-
subscribe;
|
|
13
|
-
constructor(context, callback, subscribe) {
|
|
14
|
-
super("context-request" /* CONTEXT_REQUEST */, { bubbles: true, composed: true });
|
|
15
|
-
this.context = context;
|
|
16
|
-
this.callback = callback;
|
|
17
|
-
this.subscribe = subscribe;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
class ContextOnMountEvent extends CustomEvent {
|
|
22
|
-
constructor(context) {
|
|
23
|
-
super("context--on-mount" /* ON_MOUNT */, {
|
|
24
|
-
detail: { context },
|
|
25
|
-
bubbles: true,
|
|
26
|
-
composed: true
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class ContextSubscriptionRequestEvent extends Event {
|
|
32
|
-
context;
|
|
33
|
-
callback;
|
|
34
|
-
select;
|
|
35
|
-
subscribe;
|
|
36
|
-
constructor(context, callback, select, subscribe) {
|
|
37
|
-
super("context--subscription-request" /* SUBSCRIPTION_REQUEST */, {
|
|
38
|
-
bubbles: true,
|
|
39
|
-
composed: true
|
|
40
|
-
});
|
|
41
|
-
this.context = context;
|
|
42
|
-
this.callback = callback;
|
|
43
|
-
this.select = select;
|
|
44
|
-
this.subscribe = subscribe;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// src/context/decorators/consume-context.ts
|
|
49
|
-
function consumeContext(contextToProvide) {
|
|
50
|
-
return (proto, propertyKey) => {
|
|
51
|
-
const originalConnectedCallback = proto.connectedCallback;
|
|
52
|
-
proto.connectedCallback = function() {
|
|
53
|
-
originalConnectedCallback.call(this);
|
|
54
|
-
this.dispatchEvent(new ContextRequestEvent(contextToProvide, (context) => {
|
|
55
|
-
this[propertyKey] = context;
|
|
56
|
-
this.connectedContextCallback(contextToProvide);
|
|
57
|
-
}));
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
export {
|
|
62
|
-
consumeContext
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
//# debugId=D00E7FCC029AE39264756E2164756E21
|
|
4
|
+
//# debugId=D0E540181126F79564756E2164756E21
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/context/
|
|
3
|
+
"sources": ["../src/context/decorators/consume-context.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type {
|
|
6
|
-
"import { ContextRequestEvent } from '@/context/events';\nimport type { UnknownContext } from '@/context/types';\nimport type { RadiantElement } from '@/core/radiant-element';\n\n/**\n * A decorator to provide a context to the target element.\n * @param contextToProvide\n * @returns\n */\nexport function consumeContext(contextToProvide: UnknownContext) {\n return (proto: RadiantElement, propertyKey: string) => {\n const originalConnectedCallback = proto.connectedCallback;\n\n proto.connectedCallback = function (this: RadiantElement) {\n originalConnectedCallback.call(this);\n this.dispatchEvent(\n new ContextRequestEvent(contextToProvide, (context) => {\n (this as any)[propertyKey] = context;\n this.connectedContextCallback(contextToProvide);\n }),\n );\n };\n };\n}\n"
|
|
5
|
+
"import type {\n LegacyFieldDecoratorArgs,\n StandardFieldDecoratorArgs,\n StandardOrLegacyFieldDecoratorArgs,\n} from '../../types';\nimport type { UnknownContext } from '../types';\nimport { consumeContext as legacyConsumeContext } from './legacy/consume-context';\nimport { consumeContext as standardConsumeContext } from './standard/consume-context';\n\n/**\n * A decorator to provide a context to the target element.\n * @param contextToProvide\n * @returns\n */\nexport function consumeContext(contextToProvide: UnknownContext) {\n return function (\n protoOrTarget: StandardOrLegacyFieldDecoratorArgs['protoOrTarget'],\n nameOrContext: StandardOrLegacyFieldDecoratorArgs['nameOrContext'],\n ): any {\n if (typeof nameOrContext === 'object') {\n return standardConsumeContext(contextToProvide)(\n protoOrTarget as StandardFieldDecoratorArgs['protoOrTarget'],\n nameOrContext as StandardFieldDecoratorArgs['nameOrContext'],\n );\n }\n return legacyConsumeContext(contextToProvide)(\n protoOrTarget as LegacyFieldDecoratorArgs['protoOrTarget'],\n nameOrContext as LegacyFieldDecoratorArgs['nameOrContext'],\n );\n };\n}\n"
|
|
7
6
|
],
|
|
8
|
-
"mappings": "
|
|
9
|
-
"debugId": "
|
|
7
|
+
"mappings": "gIAcO,aAAS,ZAAc,CAAC,EAAkC,CAC/D,eAAgB,CACd,EACA,EACK,CACL,UAAW,IAAkB,SAC3B,OAAO,EAAuB,CAAgB,EAC5C,EACA,CACF,EAEF,OAAO,EAAqB,CAAgB,EAC1C,EACA,CACF",
|
|
8
|
+
"debugId": "D0E540181126F79564756E2164756E21",
|
|
10
9
|
"names": []
|
|
11
10
|
}
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
type SubscribeToContextOptions<T extends UnknownContext> = {
|
|
1
|
+
import type { StandardOrLegacyMethodDecoratorArgs } from '../../types';
|
|
2
|
+
import type { Context, UnknownContext } from '../types';
|
|
3
|
+
export type SubscribeToContextOptions<T extends UnknownContext> = {
|
|
4
4
|
context: T;
|
|
5
5
|
select?: (context: T['__context__']) => unknown;
|
|
6
6
|
subscribe?: boolean;
|
|
7
7
|
};
|
|
8
8
|
/**
|
|
9
9
|
* A decorator to subscribe to a context selector.
|
|
10
|
-
* @param
|
|
11
|
-
* @param selector The selector to subscribe to. If not provided, the whole context will be subscribed to.
|
|
12
|
-
* @param subscribe @default true Whether to subscribe or unsubscribe. Optional.
|
|
10
|
+
* @param option {@link SubscribeToContextOptions}
|
|
13
11
|
* @returns
|
|
14
12
|
*/
|
|
15
|
-
export declare function contextSelector<T extends Context<unknown, unknown>>(
|
|
16
|
-
export {};
|
|
13
|
+
export declare function contextSelector<T extends Context<unknown, unknown>>(options: SubscribeToContextOptions<T>): (protoOrTarget: StandardOrLegacyMethodDecoratorArgs["protoOrTarget"], nameOrContext: StandardOrLegacyMethodDecoratorArgs["nameOrContext"], descriptor?: StandardOrLegacyMethodDecoratorArgs["descriptor"]) => any;
|
|
@@ -1,72 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
((ContextEventsTypes2) => {
|
|
4
|
-
ContextEventsTypes2["SUBSCRIPTION_REQUEST"] = "context--subscription-request";
|
|
5
|
-
ContextEventsTypes2["CONTEXT_REQUEST"] = "context-request";
|
|
6
|
-
ContextEventsTypes2["ON_MOUNT"] = "context--on-mount";
|
|
7
|
-
})(ContextEventsTypes ||= {});
|
|
1
|
+
import{i as v}from"./standard/context-selector.js";import{j as q}from"./legacy/context-selector.js";import"../events.js";function D(h){return function(j,f,w){if(typeof f==="object")return v(h)(j,f);return q(h)(j,f,w)}}export{D as contextSelector};
|
|
2
|
+
export{D as h};
|
|
8
3
|
|
|
9
|
-
|
|
10
|
-
context;
|
|
11
|
-
callback;
|
|
12
|
-
subscribe;
|
|
13
|
-
constructor(context, callback, subscribe) {
|
|
14
|
-
super("context-request" /* CONTEXT_REQUEST */, { bubbles: true, composed: true });
|
|
15
|
-
this.context = context;
|
|
16
|
-
this.callback = callback;
|
|
17
|
-
this.subscribe = subscribe;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
class ContextOnMountEvent extends CustomEvent {
|
|
22
|
-
constructor(context) {
|
|
23
|
-
super("context--on-mount" /* ON_MOUNT */, {
|
|
24
|
-
detail: { context },
|
|
25
|
-
bubbles: true,
|
|
26
|
-
composed: true
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class ContextSubscriptionRequestEvent extends Event {
|
|
32
|
-
context;
|
|
33
|
-
callback;
|
|
34
|
-
select;
|
|
35
|
-
subscribe;
|
|
36
|
-
constructor(context, callback, select, subscribe) {
|
|
37
|
-
super("context--subscription-request" /* SUBSCRIPTION_REQUEST */, {
|
|
38
|
-
bubbles: true,
|
|
39
|
-
composed: true
|
|
40
|
-
});
|
|
41
|
-
this.context = context;
|
|
42
|
-
this.callback = callback;
|
|
43
|
-
this.select = select;
|
|
44
|
-
this.subscribe = subscribe;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// src/context/decorators/context-selector.ts
|
|
49
|
-
function contextSelector({
|
|
50
|
-
context,
|
|
51
|
-
select,
|
|
52
|
-
subscribe = true
|
|
53
|
-
}) {
|
|
54
|
-
return (proto, _, descriptor) => {
|
|
55
|
-
const originalMethod = descriptor.value;
|
|
56
|
-
const originalConnectedCallback = proto.connectedCallback;
|
|
57
|
-
proto.connectedCallback = function() {
|
|
58
|
-
originalConnectedCallback.call(this);
|
|
59
|
-
this.dispatchEvent(new ContextSubscriptionRequestEvent(context, originalMethod.bind(this), select, subscribe));
|
|
60
|
-
};
|
|
61
|
-
descriptor.value = function(...args) {
|
|
62
|
-
const result = originalMethod.apply(this, args);
|
|
63
|
-
return result;
|
|
64
|
-
};
|
|
65
|
-
return descriptor;
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
export {
|
|
69
|
-
contextSelector
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
//# debugId=BAE397795EE3704564756E2164756E21
|
|
4
|
+
//# debugId=0C0E19D0A850DDF264756E2164756E21
|