@htmlplus/element 2.5.0 → 2.6.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/client/decorators/context.d.ts +3 -0
- package/client/decorators/context.js +114 -0
- package/client/decorators/index.d.ts +1 -2
- package/client/decorators/index.js +1 -2
- package/package.json +1 -1
- package/types/index.d.ts +0 -7
- package/client/decorators/consumer.d.ts +0 -6
- package/client/decorators/consumer.js +0 -21
- package/client/decorators/provider.d.ts +0 -6
- package/client/decorators/provider.js +0 -31
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { HTMLPlusElement } from '../../types/index.js';
|
|
2
|
+
export declare function Provider(namespace: string): (target: HTMLPlusElement, key: PropertyKey, descriptor: PropertyDescriptor) => void;
|
|
3
|
+
export declare function Consumer(namespace: string): (target: HTMLPlusElement, key: PropertyKey) => void;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as CONSTANTS from '../../constants/index.js';
|
|
2
|
+
import { appendToMethod, dispatch, off, on } from '../utils/index.js';
|
|
3
|
+
export function Provider(namespace) {
|
|
4
|
+
return function (target, key, descriptor) {
|
|
5
|
+
const symbol = Symbol();
|
|
6
|
+
const [MAIN, SUB] = namespace.split('.');
|
|
7
|
+
const prefix = `htmlplus:${MAIN}`;
|
|
8
|
+
const cleanups = (instance) => {
|
|
9
|
+
return (instance[symbol] || (instance[symbol] = new Map()));
|
|
10
|
+
};
|
|
11
|
+
const update = (instance) => {
|
|
12
|
+
const options = {};
|
|
13
|
+
options.detail = descriptor.get.call(instance);
|
|
14
|
+
dispatch(instance, `${prefix}:update`, options);
|
|
15
|
+
if (!SUB)
|
|
16
|
+
return;
|
|
17
|
+
options.bubbles = true;
|
|
18
|
+
dispatch(instance, `${prefix}:${instance[SUB]}:update`, options);
|
|
19
|
+
};
|
|
20
|
+
// TODO
|
|
21
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_CONNECTED, function () {
|
|
22
|
+
const cleanup = () => {
|
|
23
|
+
off(this, `${prefix}:presence`, onPresence);
|
|
24
|
+
cleanups(this).delete(prefix);
|
|
25
|
+
};
|
|
26
|
+
const onPresence = (event) => {
|
|
27
|
+
event.stopPropagation();
|
|
28
|
+
event.detail(this, descriptor.get.call(this));
|
|
29
|
+
};
|
|
30
|
+
on(this, `${prefix}:presence`, onPresence);
|
|
31
|
+
cleanups(this).set(prefix, cleanup);
|
|
32
|
+
});
|
|
33
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_UPDATE, function (states) {
|
|
34
|
+
var _a;
|
|
35
|
+
update(this);
|
|
36
|
+
if (cleanups(this).size && !states.has(SUB))
|
|
37
|
+
return;
|
|
38
|
+
(_a = cleanups(this).get(`${prefix}:${states.get(SUB)}`)) === null || _a === void 0 ? void 0 : _a();
|
|
39
|
+
const type = `${prefix}:${this[SUB]}`;
|
|
40
|
+
const cleanup = () => {
|
|
41
|
+
off(window, `${type}:presence`, onPresence);
|
|
42
|
+
cleanups(this).delete(type);
|
|
43
|
+
dispatch(window, `${type}:disconnect`);
|
|
44
|
+
};
|
|
45
|
+
const onPresence = () => {
|
|
46
|
+
update(this);
|
|
47
|
+
};
|
|
48
|
+
on(window, `${type}:presence`, onPresence);
|
|
49
|
+
cleanups(this).set(type, cleanup);
|
|
50
|
+
});
|
|
51
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_DISCONNECTED, function () {
|
|
52
|
+
cleanups(this).forEach((cleanup) => cleanup());
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function Consumer(namespace) {
|
|
57
|
+
return function (target, key) {
|
|
58
|
+
const symbol = Symbol();
|
|
59
|
+
const [MAIN, SUB] = namespace.split('.');
|
|
60
|
+
const prefix = `htmlplus:${MAIN}`;
|
|
61
|
+
const cleanups = (instance) => {
|
|
62
|
+
return (instance[symbol] || (instance[symbol] = new Map()));
|
|
63
|
+
};
|
|
64
|
+
const update = (instance, state) => {
|
|
65
|
+
instance[key] = state;
|
|
66
|
+
};
|
|
67
|
+
// TODO
|
|
68
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_CONNECTED, function () {
|
|
69
|
+
const options = {
|
|
70
|
+
bubbles: true
|
|
71
|
+
};
|
|
72
|
+
options.detail = (parent, state) => {
|
|
73
|
+
update(this, state);
|
|
74
|
+
const cleanup = () => {
|
|
75
|
+
off(parent, `${prefix}:update`, onUpdate);
|
|
76
|
+
cleanups(this).delete(prefix);
|
|
77
|
+
update(this, undefined);
|
|
78
|
+
};
|
|
79
|
+
const onUpdate = (event) => {
|
|
80
|
+
update(this, event.detail);
|
|
81
|
+
};
|
|
82
|
+
on(parent, `${prefix}:update`, onUpdate);
|
|
83
|
+
cleanups(this).set(prefix, cleanup);
|
|
84
|
+
};
|
|
85
|
+
dispatch(this, `${prefix}:presence`, options);
|
|
86
|
+
});
|
|
87
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_UPDATE, function (states) {
|
|
88
|
+
var _a;
|
|
89
|
+
if (cleanups(this).size && !states.has(SUB))
|
|
90
|
+
return;
|
|
91
|
+
(_a = cleanups(this).get(`${prefix}:${states.get(SUB)}`)) === null || _a === void 0 ? void 0 : _a();
|
|
92
|
+
const type = `${prefix}:${this[SUB]}`;
|
|
93
|
+
const cleanup = () => {
|
|
94
|
+
off(window, `${type}:disconnect`, onDisconnect);
|
|
95
|
+
off(window, `${type}:update`, onUpdate);
|
|
96
|
+
cleanups(this).delete(type);
|
|
97
|
+
update(this, undefined);
|
|
98
|
+
};
|
|
99
|
+
const onDisconnect = () => {
|
|
100
|
+
update(this, undefined);
|
|
101
|
+
};
|
|
102
|
+
const onUpdate = (event) => {
|
|
103
|
+
update(this, event.detail);
|
|
104
|
+
};
|
|
105
|
+
on(window, `${type}:disconnect`, onDisconnect);
|
|
106
|
+
on(window, `${type}:update`, onUpdate);
|
|
107
|
+
cleanups(this).set(type, cleanup);
|
|
108
|
+
dispatch(window, `${type}:presence`);
|
|
109
|
+
});
|
|
110
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_DISCONNECTED, function () {
|
|
111
|
+
cleanups(this).forEach((cleanup) => cleanup());
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from './bind.js';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './context.js';
|
|
3
3
|
export * from './direction.js';
|
|
4
4
|
export * from './element.js';
|
|
5
5
|
export * from './event.js';
|
|
@@ -8,7 +8,6 @@ export * from './isRTL.js';
|
|
|
8
8
|
export * from './listen.js';
|
|
9
9
|
export * from './method.js';
|
|
10
10
|
export * from './property.js';
|
|
11
|
-
export * from './provider.js';
|
|
12
11
|
export * from './query.js';
|
|
13
12
|
export * from './queryAll.js';
|
|
14
13
|
export * from './slots.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from './bind.js';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './context.js';
|
|
3
3
|
export * from './direction.js';
|
|
4
4
|
export * from './element.js';
|
|
5
5
|
export * from './event.js';
|
|
@@ -8,7 +8,6 @@ export * from './isRTL.js';
|
|
|
8
8
|
export * from './listen.js';
|
|
9
9
|
export * from './method.js';
|
|
10
10
|
export * from './property.js';
|
|
11
|
-
export * from './provider.js';
|
|
12
11
|
export * from './query.js';
|
|
13
12
|
export * from './queryAll.js';
|
|
14
13
|
export * from './slots.js';
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,9 +1,2 @@
|
|
|
1
1
|
export interface HTMLPlusElement {
|
|
2
|
-
adoptedCallback?(): void;
|
|
3
|
-
connectedCallback?(): void;
|
|
4
|
-
constructedCallback?(): void;
|
|
5
|
-
disconnectedCallback?(): void;
|
|
6
|
-
loadedCallback?(): void;
|
|
7
|
-
updateCallback?(states: Map<string, any>): void;
|
|
8
|
-
updatedCallback?(states: Map<string, any>): void;
|
|
9
2
|
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import * as CONSTANTS from '../../constants/index.js';
|
|
2
|
-
import { appendToMethod, dispatch, host } from '../utils/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* TODO
|
|
5
|
-
* @param namespace
|
|
6
|
-
*/
|
|
7
|
-
export function Consumer(namespace) {
|
|
8
|
-
return function (target, key) {
|
|
9
|
-
appendToMethod(target, CONSTANTS.LIFECYCLE_CONSTRUCTED, function () {
|
|
10
|
-
const options = {
|
|
11
|
-
bubbles: true
|
|
12
|
-
};
|
|
13
|
-
options.detail = (state) => {
|
|
14
|
-
this[key] = state;
|
|
15
|
-
const successful = !!host(this).parentElement;
|
|
16
|
-
return successful;
|
|
17
|
-
};
|
|
18
|
-
dispatch(this, `internal:context:${namespace}`, options);
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as CONSTANTS from '../../constants/index.js';
|
|
2
|
-
import { appendToMethod, on } from '../utils/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* TODO
|
|
5
|
-
* @param namespace
|
|
6
|
-
*/
|
|
7
|
-
export function Provider(namespace) {
|
|
8
|
-
return function (target, key, descriptor) {
|
|
9
|
-
const symbol = Symbol();
|
|
10
|
-
const update = (instance) => (updater) => {
|
|
11
|
-
const state = descriptor.get.call(instance);
|
|
12
|
-
const successful = updater(state);
|
|
13
|
-
if (successful)
|
|
14
|
-
return;
|
|
15
|
-
instance[symbol].delete(updater);
|
|
16
|
-
};
|
|
17
|
-
appendToMethod(target, CONSTANTS.LIFECYCLE_CONSTRUCTED, function () {
|
|
18
|
-
this[symbol] || (this[symbol] = new Set());
|
|
19
|
-
const handler = (event) => {
|
|
20
|
-
event.stopPropagation();
|
|
21
|
-
const updater = event.detail;
|
|
22
|
-
this[symbol].add(updater);
|
|
23
|
-
update(this)(updater);
|
|
24
|
-
};
|
|
25
|
-
on(this, `internal:context:${namespace}`, handler);
|
|
26
|
-
});
|
|
27
|
-
appendToMethod(target, CONSTANTS.LIFECYCLE_UPDATED, function () {
|
|
28
|
-
this[symbol].forEach(update(this));
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
}
|