@wcstack/state 1.11.1 → 1.12.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.
- package/README.ja.md +152 -4
- package/README.md +147 -0
- package/dist/index.esm.js +240 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -59,7 +59,7 @@ function setConfig(partialConfig) {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
var version$1 = "1.
|
|
62
|
+
var version$1 = "1.12.1";
|
|
63
63
|
var pkg = {
|
|
64
64
|
version: version$1};
|
|
65
65
|
|
|
@@ -137,6 +137,8 @@ const WEBCOMPONENT_STATE_READY_CALLBACK_NAME = "$stateReadyCallback";
|
|
|
137
137
|
const STATE_BINDABLES_NAME = "$bindables";
|
|
138
138
|
const STATE_COMMAND_TOKENS_NAME = "$commandTokens";
|
|
139
139
|
const STATE_COMMAND_NAMESPACE_NAME = "$command";
|
|
140
|
+
const STATE_EVENT_TOKENS_NAME = "$eventTokens";
|
|
141
|
+
const STATE_ON_NAME = "$on";
|
|
140
142
|
const DCC_DEFINITION_ATTRIBUTE = "data-wc-definition";
|
|
141
143
|
|
|
142
144
|
const _cache$4 = new Map();
|
|
@@ -1485,6 +1487,15 @@ function parseBindTextsForElement(bindText) {
|
|
|
1485
1487
|
else {
|
|
1486
1488
|
const stateResult = parseStatePart(statePart);
|
|
1487
1489
|
const propResult = parsePropPart(propPart);
|
|
1490
|
+
// eventToken.<prop>: <name> は要素 dispatch を state へ流す pub/sub 配線。
|
|
1491
|
+
// 値適用ではないため bindingType 'event' として listener attach 経路に乗せる。
|
|
1492
|
+
if (propResult.propSegments[0] === 'eventToken') {
|
|
1493
|
+
return {
|
|
1494
|
+
...propResult,
|
|
1495
|
+
...stateResult,
|
|
1496
|
+
bindingType: 'event',
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1488
1499
|
if (propResult.propSegments[0].startsWith('on')) {
|
|
1489
1500
|
return {
|
|
1490
1501
|
...propResult,
|
|
@@ -1748,9 +1759,14 @@ function createStateAddress(pathInfo, listIndex) {
|
|
|
1748
1759
|
}
|
|
1749
1760
|
}
|
|
1750
1761
|
|
|
1762
|
+
// command-token / event-token が共有する pub/sub プリミティブ。
|
|
1751
1763
|
// _subscribers は Set のため挿入順を保持する。
|
|
1752
1764
|
// emit() は subscribe() された順に呼び出され、戻り値配列も同じ順序で返る。
|
|
1753
|
-
|
|
1765
|
+
//
|
|
1766
|
+
// 「誰が subscribe し誰が emit するか」だけが command / event の違い:
|
|
1767
|
+
// - command-token: element が subscribe / state が emit
|
|
1768
|
+
// - event-token: state(`$on`) が subscribe / element(listener) が emit
|
|
1769
|
+
class Token {
|
|
1754
1770
|
_name;
|
|
1755
1771
|
_subscribers = new Set();
|
|
1756
1772
|
constructor(name) {
|
|
@@ -1779,6 +1795,11 @@ class CommandToken {
|
|
|
1779
1795
|
return results;
|
|
1780
1796
|
}
|
|
1781
1797
|
}
|
|
1798
|
+
|
|
1799
|
+
// CommandToken は共有 pub/sub プリミティブ Token の薄い特化。
|
|
1800
|
+
// instanceof による型判別を成立させるため独立クラスとして維持する。
|
|
1801
|
+
class CommandToken extends Token {
|
|
1802
|
+
}
|
|
1782
1803
|
function isCommandToken(value) {
|
|
1783
1804
|
return value instanceof CommandToken;
|
|
1784
1805
|
}
|
|
@@ -1877,6 +1898,129 @@ function attachEventHandler(binding) {
|
|
|
1877
1898
|
return true;
|
|
1878
1899
|
}
|
|
1879
1900
|
|
|
1901
|
+
// EventToken は共有 pub/sub プリミティブ Token の薄い特化(element→state 方向)。
|
|
1902
|
+
// instanceof による型判別を成立させるため独立クラスとして維持する。
|
|
1903
|
+
class EventToken extends Token {
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
const registryByStateElement$1 = new WeakMap();
|
|
1907
|
+
function getOrCreateEventToken(stateElement, name) {
|
|
1908
|
+
let registry = registryByStateElement$1.get(stateElement);
|
|
1909
|
+
if (typeof registry === "undefined") {
|
|
1910
|
+
registry = new Map();
|
|
1911
|
+
registryByStateElement$1.set(stateElement, registry);
|
|
1912
|
+
}
|
|
1913
|
+
let token = registry.get(name);
|
|
1914
|
+
if (typeof token === "undefined") {
|
|
1915
|
+
token = new EventToken(name);
|
|
1916
|
+
registry.set(name, token);
|
|
1917
|
+
}
|
|
1918
|
+
return token;
|
|
1919
|
+
}
|
|
1920
|
+
function clearEventTokenRegistry(stateElement) {
|
|
1921
|
+
registryByStateElement$1.delete(stateElement);
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
/**
|
|
1925
|
+
* eventToken.<propertyName>: <eventTokenName> バインディングの attach ハンドラ。
|
|
1926
|
+
*
|
|
1927
|
+
* command-token の双対(element→state)。要素が dispatch する CustomEvent を受けて
|
|
1928
|
+
* event-token を emit し、state 側の `$on` ハンドラ群へ pub/sub で配送する。
|
|
1929
|
+
*
|
|
1930
|
+
* 設計(MVP スコープ: wc-bindable カスタム要素のみ):
|
|
1931
|
+
* - キーは生イベント名ではなく **wcBindable property 名**。実 DOM イベント名は
|
|
1932
|
+
* wcBindable.properties[].event から解決する(command-token が wcBindable.commands で
|
|
1933
|
+
* 検証するのと対称。コロンを含む namespaced event 名と binding 構文の `:` 衝突も回避)。
|
|
1934
|
+
* - <prop> が wcBindable.properties に宣言されていることは attach 時に検証する
|
|
1935
|
+
* (要素クラス参照のみで DOM 接続に非依存。fail-fast / typo 耐性)。
|
|
1936
|
+
* - <eventTokenName> が $eventTokens に宣言されていることは **発火時** に検証する
|
|
1937
|
+
* (state 解決が必要なため。詳細は下記の fire-time 解決の注記を参照)。
|
|
1938
|
+
* - subscriber 引数規約は `(state, event, ...listIndexes)`。
|
|
1939
|
+
* - modifier `#prevent` / `#stop` は既存イベント binding と同等にサポート。
|
|
1940
|
+
*
|
|
1941
|
+
* token はイベント発火ごとに registry から解決する(getOrCreateEventToken)。これにより
|
|
1942
|
+
* state の再 set で registry が作り直されても最新の subscriber 群へ配送できる。
|
|
1943
|
+
*
|
|
1944
|
+
* state element の解決と `$eventTokens` 検証は **発火時** に行う(attach 時ではない)。
|
|
1945
|
+
* 構造ブロック(for/if)や SSR hydration では、binding 初期化時にノードが detached な
|
|
1946
|
+
* DocumentFragment / wrapper 上にあり、その時点では element.getRootNode() から state を
|
|
1947
|
+
* 解決できないため。onclick / two-way ハンドラと同じく fire-time 解決に揃えている。
|
|
1948
|
+
*/
|
|
1949
|
+
const listenerByBinding = new WeakMap();
|
|
1950
|
+
function getWcBindable$1(element) {
|
|
1951
|
+
const customTagName = getCustomElement(element);
|
|
1952
|
+
if (customTagName === null) {
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
// attach 側で未定義要素は whenDefined 後に再試行するため、ここに来る時点で customClass は定義済み。
|
|
1956
|
+
const customClass = customElements.get(customTagName);
|
|
1957
|
+
const bindable = customClass?.wcBindable;
|
|
1958
|
+
if (bindable?.protocol === "wc-bindable" && bindable?.version === 1) {
|
|
1959
|
+
return bindable;
|
|
1960
|
+
}
|
|
1961
|
+
return null;
|
|
1962
|
+
}
|
|
1963
|
+
function attachEventTokenHandler(binding) {
|
|
1964
|
+
if (binding.propSegments[0] !== "eventToken") {
|
|
1965
|
+
return false;
|
|
1966
|
+
}
|
|
1967
|
+
const element = binding.node;
|
|
1968
|
+
// カスタム要素が未定義なら定義後に再試行(wcBindable が必要なため)。
|
|
1969
|
+
const customTagName = getCustomElement(element);
|
|
1970
|
+
if (customTagName !== null && customElements.get(customTagName) === undefined) {
|
|
1971
|
+
customElements.whenDefined(customTagName).then(() => {
|
|
1972
|
+
attachEventTokenHandler(binding);
|
|
1973
|
+
});
|
|
1974
|
+
return true;
|
|
1975
|
+
}
|
|
1976
|
+
// 再評価で二重 attach しない。
|
|
1977
|
+
if (listenerByBinding.has(binding)) {
|
|
1978
|
+
return true;
|
|
1979
|
+
}
|
|
1980
|
+
const propertyName = binding.propSegments[1];
|
|
1981
|
+
if (typeof propertyName !== "string" || propertyName.length === 0) {
|
|
1982
|
+
raiseError(`eventToken binding requires a property name (e.g., "eventToken.error").`);
|
|
1983
|
+
}
|
|
1984
|
+
const bindable = getWcBindable$1(element);
|
|
1985
|
+
if (bindable === null) {
|
|
1986
|
+
raiseError(`eventToken binding requires a wc-bindable custom element. <${element.tagName.toLowerCase()}> is not wc-bindable.`);
|
|
1987
|
+
}
|
|
1988
|
+
const propDesc = bindable.properties.find((p) => p.name === propertyName);
|
|
1989
|
+
if (typeof propDesc === "undefined") {
|
|
1990
|
+
raiseError(`Property "${propertyName}" is not declared in wcBindable.properties of <${element.tagName.toLowerCase()}>.`);
|
|
1991
|
+
}
|
|
1992
|
+
const eventName = propDesc.event;
|
|
1993
|
+
const tokenName = binding.statePathName;
|
|
1994
|
+
const stateName = binding.stateName;
|
|
1995
|
+
const modifiers = binding.propModifiers;
|
|
1996
|
+
const handler = (event) => {
|
|
1997
|
+
if (modifiers.includes("prevent"))
|
|
1998
|
+
event.preventDefault();
|
|
1999
|
+
if (modifiers.includes("stop"))
|
|
2000
|
+
event.stopPropagation();
|
|
2001
|
+
// state は発火時の live root から解決する(attach 時は detached の可能性があるため)。
|
|
2002
|
+
const rootNode = element.getRootNode();
|
|
2003
|
+
const stateElement = getStateElementByName(rootNode, stateName);
|
|
2004
|
+
if (stateElement === null) {
|
|
2005
|
+
raiseError(`State element with name "${stateName}" not found for eventToken handler.`);
|
|
2006
|
+
}
|
|
2007
|
+
if (!stateElement.eventTokenNames.has(tokenName)) {
|
|
2008
|
+
raiseError(`eventToken "${tokenName}" is not declared in $eventTokens of state "${stateName}".`);
|
|
2009
|
+
}
|
|
2010
|
+
const loopContext = getLoopContextByNode(element);
|
|
2011
|
+
stateElement.createStateAsync("writable", async (state) => {
|
|
2012
|
+
state[setLoopContextSymbol](loopContext, () => {
|
|
2013
|
+
const indexes = loopContext?.listIndex.indexes ?? [];
|
|
2014
|
+
const token = getOrCreateEventToken(stateElement, tokenName);
|
|
2015
|
+
return token.emit(state, event, ...indexes);
|
|
2016
|
+
});
|
|
2017
|
+
});
|
|
2018
|
+
};
|
|
2019
|
+
element.addEventListener(eventName, handler);
|
|
2020
|
+
listenerByBinding.set(binding, { eventName, handler });
|
|
2021
|
+
return true;
|
|
2022
|
+
}
|
|
2023
|
+
|
|
1880
2024
|
const CHECK_TYPES = new Set(['radio', 'checkbox']);
|
|
1881
2025
|
const DEFAULT_VALUE_PROP_NAMES = new Set(['value', 'valueAsNumber', 'valueAsDate']);
|
|
1882
2026
|
function isPossibleTwoWay(node, propName) {
|
|
@@ -3325,6 +3469,21 @@ const SSR_ATTR_PROPS = {
|
|
|
3325
3469
|
},
|
|
3326
3470
|
};
|
|
3327
3471
|
function applyChangeToProperty(binding, _context, newValue) {
|
|
3472
|
+
// undefined は「状態が値を持たない=無意見」であり、書き込み自体をスキップして
|
|
3473
|
+
// 要素側の既定値を生かす。書き込んでしまうと setter の文字列化で
|
|
3474
|
+
// "undefined" 属性や removeAttribute が走り要素が壊れる (spread で未初期化
|
|
3475
|
+
// slot を配線したときに顕在化)。明示的なクリアは null で表現する。
|
|
3476
|
+
// mirror 属性 (applyMirrorAttribute) の「undefined → 属性削除」と同じ語彙。
|
|
3477
|
+
if (typeof newValue === "undefined") {
|
|
3478
|
+
if (config.debug) {
|
|
3479
|
+
console.debug(`Skipped property write: state value is undefined.`, {
|
|
3480
|
+
element: binding.node,
|
|
3481
|
+
propSegments: binding.propSegments,
|
|
3482
|
+
statePathName: binding.statePathName,
|
|
3483
|
+
});
|
|
3484
|
+
}
|
|
3485
|
+
return;
|
|
3486
|
+
}
|
|
3328
3487
|
const element = binding.node;
|
|
3329
3488
|
const propSegments = binding.propSegments;
|
|
3330
3489
|
if (propSegments.length === 1) {
|
|
@@ -3871,6 +4030,10 @@ function _initializeBindings(allBindings) {
|
|
|
3871
4030
|
if (attachEventHandler(binding)) {
|
|
3872
4031
|
continue;
|
|
3873
4032
|
}
|
|
4033
|
+
// event token (element → state)
|
|
4034
|
+
if (attachEventTokenHandler(binding)) {
|
|
4035
|
+
continue;
|
|
4036
|
+
}
|
|
3874
4037
|
// two-way binding
|
|
3875
4038
|
attachTwowayEventHandler(binding);
|
|
3876
4039
|
// radio binding
|
|
@@ -4398,6 +4561,8 @@ function collectBindingsFromLiveNodes(nodes) {
|
|
|
4398
4561
|
replaceToReplaceNode(binding);
|
|
4399
4562
|
if (attachEventHandler(binding))
|
|
4400
4563
|
continue;
|
|
4564
|
+
if (attachEventTokenHandler(binding))
|
|
4565
|
+
continue;
|
|
4401
4566
|
attachTwowayEventHandler(binding);
|
|
4402
4567
|
attachRadioEventHandler(binding);
|
|
4403
4568
|
attachCheckboxEventHandler(binding);
|
|
@@ -4637,6 +4802,9 @@ async function hydrateBindings(root) {
|
|
|
4637
4802
|
if (attachEventHandler(binding)) {
|
|
4638
4803
|
continue;
|
|
4639
4804
|
}
|
|
4805
|
+
if (attachEventTokenHandler(binding)) {
|
|
4806
|
+
continue;
|
|
4807
|
+
}
|
|
4640
4808
|
attachTwowayEventHandler(binding);
|
|
4641
4809
|
attachRadioEventHandler(binding);
|
|
4642
4810
|
attachCheckboxEventHandler(binding);
|
|
@@ -5368,6 +5536,67 @@ function clearCommandNamespace(stateElement) {
|
|
|
5368
5536
|
namespaceProxyByStateElement.delete(stateElement);
|
|
5369
5537
|
}
|
|
5370
5538
|
|
|
5539
|
+
/**
|
|
5540
|
+
* `$eventTokens: ["a", "b", ...]` 配列宣言を解析し、宣言された名前群を Set で返す。
|
|
5541
|
+
*
|
|
5542
|
+
* event-token は command-token の双対(element→state 方向)。要素が dispatch する
|
|
5543
|
+
* イベントを `eventToken.<prop>: <name>` で token に流し、state 側は `$on` マップで受ける。
|
|
5544
|
+
* ここで宣言された名前のみが `eventToken.X` / `$on` の有効なチャネル名になる(typo 耐性)。
|
|
5545
|
+
*
|
|
5546
|
+
* 対応している宣言形式は **オブジェクトリテラル** のみ。
|
|
5547
|
+
*/
|
|
5548
|
+
function processEventTokensDeclaration(state) {
|
|
5549
|
+
const names = new Set();
|
|
5550
|
+
const declared = state[STATE_EVENT_TOKENS_NAME];
|
|
5551
|
+
if (typeof declared === "undefined") {
|
|
5552
|
+
return names;
|
|
5553
|
+
}
|
|
5554
|
+
if (!Array.isArray(declared)) {
|
|
5555
|
+
raiseError(`${STATE_EVENT_TOKENS_NAME} must be an array of strings.`);
|
|
5556
|
+
}
|
|
5557
|
+
for (const name of declared) {
|
|
5558
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
5559
|
+
raiseError(`${STATE_EVENT_TOKENS_NAME} entries must be non-empty strings.`);
|
|
5560
|
+
}
|
|
5561
|
+
if (names.has(name)) {
|
|
5562
|
+
raiseError(`${STATE_EVENT_TOKENS_NAME} entry "${name}" is duplicated.`);
|
|
5563
|
+
}
|
|
5564
|
+
names.add(name);
|
|
5565
|
+
}
|
|
5566
|
+
return names;
|
|
5567
|
+
}
|
|
5568
|
+
|
|
5569
|
+
/**
|
|
5570
|
+
* `$on: { <name>: (state, event, ...listIndexes) => {...} }` マップを解析し、
|
|
5571
|
+
* 各ハンドラを対応する event-token に subscribe する(state 側の受信配線)。
|
|
5572
|
+
*
|
|
5573
|
+
* - `$on` のキーは `$eventTokens` で宣言済みでなければならない(typo 耐性)。
|
|
5574
|
+
* - 各値は関数でなければならない。
|
|
5575
|
+
* - 引数規約は `(state, event, ...listIndexes)`。`this` 束縛は行わず引数で state を渡すため
|
|
5576
|
+
* アロー関数で書ける(command-token の emit 規約と対称)。
|
|
5577
|
+
*
|
|
5578
|
+
* `$eventTokens` で宣言されたが `$on` に対応が無い token は subscriber ゼロ(emit は no-op)。
|
|
5579
|
+
*/
|
|
5580
|
+
function processOnDeclaration(stateElement, state, eventTokenNames) {
|
|
5581
|
+
const declared = state[STATE_ON_NAME];
|
|
5582
|
+
if (typeof declared === "undefined") {
|
|
5583
|
+
return;
|
|
5584
|
+
}
|
|
5585
|
+
if (typeof declared !== "object" || declared === null) {
|
|
5586
|
+
raiseError(`${STATE_ON_NAME} must be an object mapping event-token names to handler functions.`);
|
|
5587
|
+
}
|
|
5588
|
+
for (const [name, handler] of Object.entries(declared)) {
|
|
5589
|
+
if (!eventTokenNames.has(name)) {
|
|
5590
|
+
raiseError(`${STATE_ON_NAME} entry "${name}" is not declared in $eventTokens.`);
|
|
5591
|
+
}
|
|
5592
|
+
if (typeof handler !== "function") {
|
|
5593
|
+
raiseError(`${STATE_ON_NAME} entry "${name}" must be a function.`);
|
|
5594
|
+
}
|
|
5595
|
+
const token = getOrCreateEventToken(stateElement, name);
|
|
5596
|
+
token.subscribe(handler);
|
|
5597
|
+
}
|
|
5598
|
+
}
|
|
5599
|
+
|
|
5371
5600
|
function getterFn(name) {
|
|
5372
5601
|
return function () {
|
|
5373
5602
|
const stateEl = this.stateElement;
|
|
@@ -7199,6 +7428,7 @@ class State extends HTMLElement {
|
|
|
7199
7428
|
_boundComponentStateProp = null;
|
|
7200
7429
|
_bindableEventMap = {};
|
|
7201
7430
|
_commandTokenNames = new Set();
|
|
7431
|
+
_eventTokenNames = new Set();
|
|
7202
7432
|
constructor() {
|
|
7203
7433
|
super();
|
|
7204
7434
|
this._initializePromise = new Promise((resolve) => {
|
|
@@ -7222,7 +7452,11 @@ class State extends HTMLElement {
|
|
|
7222
7452
|
}
|
|
7223
7453
|
set _state(value) {
|
|
7224
7454
|
this._commandTokenNames = processCommandTokensDeclaration(value);
|
|
7455
|
+
this._eventTokenNames = processEventTokensDeclaration(value);
|
|
7225
7456
|
this.__state = value;
|
|
7457
|
+
// 再 set 時に二重 subscribe しないよう registry をクリアしてから $on を配線し直す。
|
|
7458
|
+
clearEventTokenRegistry(this);
|
|
7459
|
+
processOnDeclaration(this, value, this._eventTokenNames);
|
|
7226
7460
|
this._listPaths.clear();
|
|
7227
7461
|
this._elementPaths.clear();
|
|
7228
7462
|
this._getterPaths.clear();
|
|
@@ -7436,6 +7670,7 @@ class State extends HTMLElement {
|
|
|
7436
7670
|
setStateElementByName(this.rootNode, this._name, null);
|
|
7437
7671
|
clearCommandTokenRegistry(this);
|
|
7438
7672
|
clearCommandNamespace(this);
|
|
7673
|
+
clearEventTokenRegistry(this);
|
|
7439
7674
|
this._rootNode = null;
|
|
7440
7675
|
}
|
|
7441
7676
|
}
|
|
@@ -7484,6 +7719,9 @@ class State extends HTMLElement {
|
|
|
7484
7719
|
get commandTokenNames() {
|
|
7485
7720
|
return this._commandTokenNames;
|
|
7486
7721
|
}
|
|
7722
|
+
get eventTokenNames() {
|
|
7723
|
+
return this._eventTokenNames;
|
|
7724
|
+
}
|
|
7487
7725
|
setBindableEventMap(map) {
|
|
7488
7726
|
this._bindableEventMap = map;
|
|
7489
7727
|
}
|