bobe 0.0.56 → 0.0.57

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.
@@ -1,5 +1,5 @@
1
- import { Queue, isNum, matchIdStart2, matchId, escapeMap, jsVarRegexp, date32 } from 'bobe-shared';
2
- import { Signal, Computed, Keys, getPulling, setPulling, deepSignal, toRaw, ScheduleType, runWithPulling, Scope, Store, noopEffect, NoopEffect, Effect, effect as effect$1, shareSignal, flushMicroEffectManual } from 'aoye';
1
+ import { Queue, isNum, matchIdStart2, matchId, escapeMap, pickInPlace, jsVarRegexp, date32 } from 'bobe-shared';
2
+ import { Signal, Computed, Keys, isStore, getPulling, setPulling, deepSignal, toRaw, ScheduleType, runWithPulling, Scope, Store, noopEffect, NoopEffect, Effect, effect as effect$1, shareSignal, flushMicroEffectManual } from 'aoye';
3
3
  export { Store } from 'aoye';
4
4
 
5
5
  let TokenType = function (TokenType) {
@@ -32,11 +32,12 @@ let FakeType = function (FakeType) {
32
32
  FakeType[FakeType["Fragment"] = 32] = "Fragment";
33
33
  FakeType[FakeType["ForItem"] = 64] = "ForItem";
34
34
  FakeType[FakeType["Context"] = 128] = "Context";
35
+ FakeType[FakeType["DynamicText"] = 256] = "DynamicText";
35
36
  return FakeType;
36
37
  }({});
37
38
  const CondBit = FakeType.If | FakeType.Fail | FakeType.Else;
38
39
  const LogicalBit = FakeType.If | FakeType.Fail | FakeType.Else | FakeType.For | FakeType.ForItem;
39
- FakeType.If | FakeType.Fail | FakeType.Else | FakeType.For | FakeType.ForItem | FakeType.Component | FakeType.Fragment;
40
+ const CtxProviderBit = FakeType.If | FakeType.Fail | FakeType.Else | FakeType.For | FakeType.ForItem | FakeType.Component | FakeType.Fragment | FakeType.DynamicText;
40
41
  const ContextBit = FakeType.If | FakeType.Fail | FakeType.Else | FakeType.ForItem | FakeType.Context;
41
42
  const TokenizerSwitcherBit = FakeType.Component | FakeType.Fragment;
42
43
  let NodeSort = function (NodeSort) {
@@ -1444,6 +1445,17 @@ function macInc(arr) {
1444
1445
  }
1445
1446
  return candyLast;
1446
1447
  }
1448
+ class InlineFragment {
1449
+ [Keys.ProxyFreeObject] = true;
1450
+ constructor(snapshot, data, key, tokenizer) {
1451
+ this.snapshot = snapshot;
1452
+ this.data = data;
1453
+ this.key = key;
1454
+ this.tokenizer = tokenizer;
1455
+ }
1456
+ }
1457
+ const isUI = fn => typeof fn === 'function' && fn.__BOBE_IS_UI;
1458
+ const isRenderAble = val => isStore(val) || isUI(val) || val instanceof InlineFragment;
1447
1459
 
1448
1460
  const KEY_INDEX = '__BOBE_KEY_INDEX';
1449
1461
  let _ctxStack;
@@ -1500,7 +1512,7 @@ class Interpreter {
1500
1512
  stack.push({
1501
1513
  node: ctx.current,
1502
1514
  prev: ctx.prevSibling
1503
- }, !ctx.current.__logicType ? NodeSort.Real : (ctx.current.__logicType & LogicalBit ? NodeSort.Logic : 0) | (ctx.current.__logicType & TokenizerSwitcherBit ? NodeSort.TokenizerSwitcher : 0) | (ctx.current.__logicType & ContextBit ? NodeSort.Context : 0) | (ctx.current.__logicType === FakeType.Component ? NodeSort.Component : 0) | (ctx.current.__logicType !== FakeType.Context ? NodeSort.CtxProvider : 0));
1515
+ }, !ctx.current.__logicType ? NodeSort.Real : (ctx.current.__logicType & LogicalBit ? NodeSort.Logic : 0) | (ctx.current.__logicType & TokenizerSwitcherBit ? NodeSort.TokenizerSwitcher : 0) | (ctx.current.__logicType & ContextBit ? NodeSort.Context : 0) | (ctx.current.__logicType === FakeType.Component ? NodeSort.Component : 0) | (ctx.current.__logicType & CtxProviderBit ? NodeSort.CtxProvider : 0));
1504
1516
  if (ctx.current.__logicType) {
1505
1517
  if (isLogicNode) {
1506
1518
  setPulling(ctx.current.effect);
@@ -1634,21 +1646,14 @@ class Interpreter {
1634
1646
  } else if (hookType) {
1635
1647
  const data = this.getData();
1636
1648
  if (hookType === 'static') {
1637
- if (typeof value === 'function') {
1649
+ if (isRenderAble(value)) {
1638
1650
  _node = this.componentOrFragmentDeclaration(value, ctx);
1639
1651
  } else {
1640
- throw new SyntaxError(`declaration 不支持 ${value} 类型的静态插值`);
1641
- }
1642
- } else {
1643
- const valueIsMapKey = Reflect.has(data[Keys.Raw], value);
1644
- const val = data[Keys.Raw][value];
1645
- if (typeof val === 'function' || val instanceof InlineFragment) {
1646
- _node = this.componentOrFragmentDeclaration(val, ctx);
1647
- } else {
1648
- const str = valueIsMapKey ? value : this.getFn(data, value);
1649
1652
  _node = this.createNode('text');
1650
- this.onePropParsed(data, _node, 'text', str, valueIsMapKey, false);
1653
+ _node.text = String(value);
1651
1654
  }
1655
+ } else {
1656
+ return this.dynamicDeclaration(data, value, ctx);
1652
1657
  }
1653
1658
  } else {
1654
1659
  _node = this.createNode(value);
@@ -1666,6 +1671,96 @@ class Interpreter {
1666
1671
  }
1667
1672
  return _node;
1668
1673
  }
1674
+ dynamicDeclaration(pData, value, ctx) {
1675
+ const valueIsMapKey = Reflect.has(pData[Keys.Raw], value);
1676
+ let node = {
1677
+ __logicType: null,
1678
+ realParent: null,
1679
+ tokenizer: null,
1680
+ effect: null,
1681
+ textNode: null,
1682
+ owner: ctx.stack.peekByType(NodeSort.TokenizerSwitcher)?.node,
1683
+ snapshot: this.tokenizer.snapshot(['dentStack']),
1684
+ parentDataProvider: ctx.stack.peekByType(NodeSort.CtxProvider)?.node
1685
+ };
1686
+ let isUpdate = false;
1687
+ node.realAfter = this.insertAfterAnchor(`dynamic-after`);
1688
+ node.effect = this.effect(({
1689
+ old,
1690
+ val
1691
+ }) => {
1692
+ let oldLogicType = node.__logicType,
1693
+ oldTextNode = node.textNode;
1694
+ if (oldLogicType) {
1695
+ this.removeLogicNode(node);
1696
+ pickInPlace(node, ['realParent', 'realBefore', 'realAfter',, 'owner', 'snapshot', 'parentDataProvider']);
1697
+ }
1698
+ if (isRenderAble(val)) {
1699
+ if (oldTextNode) {
1700
+ this.remove(oldTextNode);
1701
+ }
1702
+ const info = this.createComponentData(val);
1703
+ info.__logicType;
1704
+ Object.assign(node, info);
1705
+ this.onePropParsed = info.onePropParsed;
1706
+ if (isUpdate) {
1707
+ this.tokenizer = node.owner.tokenizer;
1708
+ this.tokenizer.resume(node.snapshot);
1709
+ this.ctx.stack.push({
1710
+ node: node.parentDataProvider,
1711
+ prev: null
1712
+ }, NodeSort.CtxProvider);
1713
+ }
1714
+ this.tokenizer.nextToken();
1715
+ this.headerLineAndExtensions(node);
1716
+ if (isUpdate) {
1717
+ this.ctx.stack.pop();
1718
+ }
1719
+ this.onePropParsed = this.oneRealPropParsed;
1720
+ this.tokenizer = node.tokenizer;
1721
+ if (node.fragmentSnapshot) {
1722
+ this.tokenizer.resume(node.fragmentSnapshot);
1723
+ this.tokenizer.useDedentAsEof = true;
1724
+ this.tokenizer.initIndentWhenUseDedentAsEof();
1725
+ }
1726
+ if (isUpdate) {
1727
+ this.program(node.realParent, node.owner, node.realBefore, node);
1728
+ }
1729
+ } else {
1730
+ node.__logicType = FakeType.DynamicText;
1731
+ let textNode = oldTextNode;
1732
+ const isNewTextNode = !textNode;
1733
+ if (isNewTextNode) {
1734
+ textNode = node.textNode = this.createNode('text');
1735
+ }
1736
+ textNode.text = String(val);
1737
+ if (isNewTextNode) {
1738
+ if (isUpdate) {
1739
+ this.handleInsert(node.realParent, textNode, node.realBefore);
1740
+ } else {
1741
+ this.tokenizer.nextToken();
1742
+ this.headerLineAndExtensions(node);
1743
+ const _this$ctx2 = this.ctx,
1744
+ realParent = _this$ctx2.realParent,
1745
+ prevSibling = _this$ctx2.prevSibling;
1746
+ this.handleInsert(realParent, textNode, prevSibling);
1747
+ }
1748
+ }
1749
+ }
1750
+ isUpdate = true;
1751
+ return isDestroy => {
1752
+ if (isDestroy) {
1753
+ this.removeLogicNode(node);
1754
+ }
1755
+ };
1756
+ }, [() => {
1757
+ const val = valueIsMapKey ? pData[value] : this.getFn(pData, value)();
1758
+ return val;
1759
+ }], {
1760
+ type: 'render'
1761
+ });
1762
+ return node;
1763
+ }
1669
1764
  createContextNode() {
1670
1765
  const child = deepSignal({}, getPulling());
1671
1766
  const parentContext = this.ctx.stack.peekByType(NodeSort.Context)?.node?.context;
@@ -2084,20 +2179,22 @@ class Interpreter {
2084
2179
  }
2085
2180
  }
2086
2181
  oneRealPropParsed = this.onePropParsed.bind(this);
2087
- componentOrFragmentDeclaration(ComponentOrRender, ctx) {
2088
- let Component, tokenizer, child, fragmentSnapshot, resumeSnapshot;
2182
+ createComponentData(ComponentOrRender) {
2183
+ let tokenizer, child, fragmentSnapshot, resumeSnapshot, __logicType;
2089
2184
  const isCC = ComponentOrRender.prototype instanceof Store;
2090
2185
  if (isCC) {
2091
- Component = ComponentOrRender;
2186
+ const Component = ComponentOrRender;
2092
2187
  child = Component.new();
2093
2188
  tokenizer = child.ui(true);
2189
+ __logicType = FakeType.Component;
2094
2190
  } else if (ComponentOrRender instanceof InlineFragment) {
2095
2191
  const conf = ComponentOrRender;
2096
2192
  child = deepSignal({}, getPulling(), true);
2097
2193
  Object.setPrototypeOf(child, conf.data);
2098
2194
  tokenizer = conf.tokenizer;
2099
2195
  fragmentSnapshot = conf.snapshot;
2100
- resumeSnapshot = tokenizer.snapshot(['token', 'needIndent', 'isFirstToken', 'dentStack', 'isFirstToken', 'useDedentAsEof']);
2196
+ __logicType = FakeType.Fragment;
2197
+ resumeSnapshot = tokenizer.snapshot(['dentStack', 'token', 'needIndent', 'isFirstToken', 'isFirstToken', 'useDedentAsEof']);
2101
2198
  } else {
2102
2199
  const render = ComponentOrRender;
2103
2200
  const boundStore = render.boundStore;
@@ -2106,18 +2203,36 @@ class Interpreter {
2106
2203
  Object.setPrototypeOf(child, boundStore);
2107
2204
  }
2108
2205
  tokenizer = render(true);
2206
+ __logicType = FakeType.Fragment;
2109
2207
  }
2208
+ return {
2209
+ data: child,
2210
+ tokenizer,
2211
+ onePropParsed: createStoreOnePropParsed(child),
2212
+ fragmentSnapshot,
2213
+ resumeSnapshot,
2214
+ __logicType
2215
+ };
2216
+ }
2217
+ componentOrFragmentDeclaration(ComponentOrRender, ctx) {
2218
+ const _this$createComponent = this.createComponentData(ComponentOrRender),
2219
+ data = _this$createComponent.data,
2220
+ tokenizer = _this$createComponent.tokenizer,
2221
+ onePropParsed = _this$createComponent.onePropParsed,
2222
+ fragmentSnapshot = _this$createComponent.fragmentSnapshot,
2223
+ resumeSnapshot = _this$createComponent.resumeSnapshot,
2224
+ __logicType = _this$createComponent.__logicType;
2110
2225
  const node = {
2111
- __logicType: isCC ? FakeType.Component : FakeType.Fragment,
2226
+ __logicType,
2112
2227
  realParent: ctx.realParent,
2113
2228
  realBefore: null,
2114
2229
  realAfter: null,
2115
- data: child,
2230
+ data,
2116
2231
  tokenizer,
2117
2232
  fragmentSnapshot,
2118
2233
  resumeSnapshot
2119
2234
  };
2120
- this.onePropParsed = createStoreOnePropParsed(child);
2235
+ this.onePropParsed = onePropParsed;
2121
2236
  node.realAfter = this.insertAfterAnchor('component-after');
2122
2237
  return node;
2123
2238
  }
@@ -2300,7 +2415,7 @@ class Interpreter {
2300
2415
  if (key === 'ref') {
2301
2416
  const valueIsMapKey = Reflect.has(data[Keys.Raw], value);
2302
2417
  let refValue = _node;
2303
- if (_node.__logicType === FakeType.Component) {
2418
+ if (_node.__logicType & TokenizerSwitcherBit) {
2304
2419
  refValue = _node.data;
2305
2420
  } else {
2306
2421
  refValue[Keys.ProxyFreeObject] = true;
@@ -2326,8 +2441,8 @@ class Interpreter {
2326
2441
  } else {
2327
2442
  this.onePropParsed(data, _node, key, value, false, isFn, hookI);
2328
2443
  }
2329
- key = null;
2330
- eq = null;
2444
+ key = undefined;
2445
+ eq = undefined;
2331
2446
  }
2332
2447
  this.tokenizer.nextToken();
2333
2448
  }
@@ -2415,15 +2530,6 @@ function createStoreOnePropParsed(child) {
2415
2530
  };
2416
2531
  return onePropParsed;
2417
2532
  }
2418
- class InlineFragment {
2419
- [Keys.ProxyFreeObject] = true;
2420
- constructor(snapshot, data, key, tokenizer) {
2421
- this.snapshot = snapshot;
2422
- this.data = data;
2423
- this.key = key;
2424
- this.tokenizer = tokenizer;
2425
- }
2426
- }
2427
2533
 
2428
2534
  function bobe(fragments, ...values) {
2429
2535
  const ui = function ui(isSub) {
@@ -2436,6 +2542,7 @@ function bobe(fragments, ...values) {
2436
2542
  return tokenizer;
2437
2543
  };
2438
2544
  ui.boundStore = Store.Current;
2545
+ ui.__BOBE_IS_UI = true;
2439
2546
  return ui;
2440
2547
  }
2441
2548
  function customRender(option) {