bobe 0.0.54 → 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 } 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) {
@@ -251,6 +252,7 @@ class Tokenizer {
251
252
  }
252
253
  isEof() {
253
254
  if (!this.token) return false;
255
+ if (this.i >= this.code.length && !this.waitingTokens.len) return true;
254
256
  return this.token.type & TokenType.Identifier && this.token.value === Tokenizer.EofId;
255
257
  }
256
258
  setToken(type, value, dt = 1) {
@@ -1443,6 +1445,17 @@ function macInc(arr) {
1443
1445
  }
1444
1446
  return candyLast;
1445
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;
1446
1459
 
1447
1460
  const KEY_INDEX = '__BOBE_KEY_INDEX';
1448
1461
  let _ctxStack;
@@ -1499,7 +1512,7 @@ class Interpreter {
1499
1512
  stack.push({
1500
1513
  node: ctx.current,
1501
1514
  prev: ctx.prevSibling
1502
- }, !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));
1503
1516
  if (ctx.current.__logicType) {
1504
1517
  if (isLogicNode) {
1505
1518
  setPulling(ctx.current.effect);
@@ -1633,21 +1646,14 @@ class Interpreter {
1633
1646
  } else if (hookType) {
1634
1647
  const data = this.getData();
1635
1648
  if (hookType === 'static') {
1636
- if (typeof value === 'function') {
1649
+ if (isRenderAble(value)) {
1637
1650
  _node = this.componentOrFragmentDeclaration(value, ctx);
1638
1651
  } else {
1639
- throw new SyntaxError(`declaration 不支持 ${value} 类型的静态插值`);
1640
- }
1641
- } else {
1642
- const valueIsMapKey = Reflect.has(data[Keys.Raw], value);
1643
- const val = data[Keys.Raw][value];
1644
- if (typeof val === 'function' || val instanceof InlineFragment) {
1645
- _node = this.componentOrFragmentDeclaration(val, ctx);
1646
- } else {
1647
- const str = valueIsMapKey ? value : this.getFn(data, value);
1648
1652
  _node = this.createNode('text');
1649
- this.onePropParsed(data, _node, 'text', str, valueIsMapKey, false);
1653
+ _node.text = String(value);
1650
1654
  }
1655
+ } else {
1656
+ return this.dynamicDeclaration(data, value, ctx);
1651
1657
  }
1652
1658
  } else {
1653
1659
  _node = this.createNode(value);
@@ -1665,6 +1671,96 @@ class Interpreter {
1665
1671
  }
1666
1672
  return _node;
1667
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
+ }
1668
1764
  createContextNode() {
1669
1765
  const child = deepSignal({}, getPulling());
1670
1766
  const parentContext = this.ctx.stack.peekByType(NodeSort.Context)?.node?.context;
@@ -2083,20 +2179,22 @@ class Interpreter {
2083
2179
  }
2084
2180
  }
2085
2181
  oneRealPropParsed = this.onePropParsed.bind(this);
2086
- componentOrFragmentDeclaration(ComponentOrRender, ctx) {
2087
- let Component, tokenizer, child, fragmentSnapshot, resumeSnapshot;
2182
+ createComponentData(ComponentOrRender) {
2183
+ let tokenizer, child, fragmentSnapshot, resumeSnapshot, __logicType;
2088
2184
  const isCC = ComponentOrRender.prototype instanceof Store;
2089
2185
  if (isCC) {
2090
- Component = ComponentOrRender;
2186
+ const Component = ComponentOrRender;
2091
2187
  child = Component.new();
2092
2188
  tokenizer = child.ui(true);
2189
+ __logicType = FakeType.Component;
2093
2190
  } else if (ComponentOrRender instanceof InlineFragment) {
2094
2191
  const conf = ComponentOrRender;
2095
2192
  child = deepSignal({}, getPulling(), true);
2096
2193
  Object.setPrototypeOf(child, conf.data);
2097
2194
  tokenizer = conf.tokenizer;
2098
2195
  fragmentSnapshot = conf.snapshot;
2099
- resumeSnapshot = tokenizer.snapshot(['token', 'needIndent', 'isFirstToken', 'dentStack', 'isFirstToken', 'useDedentAsEof']);
2196
+ __logicType = FakeType.Fragment;
2197
+ resumeSnapshot = tokenizer.snapshot(['dentStack', 'token', 'needIndent', 'isFirstToken', 'isFirstToken', 'useDedentAsEof']);
2100
2198
  } else {
2101
2199
  const render = ComponentOrRender;
2102
2200
  const boundStore = render.boundStore;
@@ -2105,18 +2203,36 @@ class Interpreter {
2105
2203
  Object.setPrototypeOf(child, boundStore);
2106
2204
  }
2107
2205
  tokenizer = render(true);
2206
+ __logicType = FakeType.Fragment;
2108
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;
2109
2225
  const node = {
2110
- __logicType: isCC ? FakeType.Component : FakeType.Fragment,
2226
+ __logicType,
2111
2227
  realParent: ctx.realParent,
2112
2228
  realBefore: null,
2113
2229
  realAfter: null,
2114
- data: child,
2230
+ data,
2115
2231
  tokenizer,
2116
2232
  fragmentSnapshot,
2117
2233
  resumeSnapshot
2118
2234
  };
2119
- this.onePropParsed = createStoreOnePropParsed(child);
2235
+ this.onePropParsed = onePropParsed;
2120
2236
  node.realAfter = this.insertAfterAnchor('component-after');
2121
2237
  return node;
2122
2238
  }
@@ -2299,7 +2415,7 @@ class Interpreter {
2299
2415
  if (key === 'ref') {
2300
2416
  const valueIsMapKey = Reflect.has(data[Keys.Raw], value);
2301
2417
  let refValue = _node;
2302
- if (_node.__logicType === FakeType.Component) {
2418
+ if (_node.__logicType & TokenizerSwitcherBit) {
2303
2419
  refValue = _node.data;
2304
2420
  } else {
2305
2421
  refValue[Keys.ProxyFreeObject] = true;
@@ -2325,8 +2441,8 @@ class Interpreter {
2325
2441
  } else {
2326
2442
  this.onePropParsed(data, _node, key, value, false, isFn, hookI);
2327
2443
  }
2328
- key = null;
2329
- eq = null;
2444
+ key = undefined;
2445
+ eq = undefined;
2330
2446
  }
2331
2447
  this.tokenizer.nextToken();
2332
2448
  }
@@ -2414,15 +2530,6 @@ function createStoreOnePropParsed(child) {
2414
2530
  };
2415
2531
  return onePropParsed;
2416
2532
  }
2417
- class InlineFragment {
2418
- [Keys.ProxyFreeObject] = true;
2419
- constructor(snapshot, data, key, tokenizer) {
2420
- this.snapshot = snapshot;
2421
- this.data = data;
2422
- this.key = key;
2423
- this.tokenizer = tokenizer;
2424
- }
2425
- }
2426
2533
 
2427
2534
  function bobe(fragments, ...values) {
2428
2535
  const ui = function ui(isSub) {
@@ -2435,6 +2542,7 @@ function bobe(fragments, ...values) {
2435
2542
  return tokenizer;
2436
2543
  };
2437
2544
  ui.boundStore = Store.Current;
2545
+ ui.__BOBE_IS_UI = true;
2438
2546
  return ui;
2439
2547
  }
2440
2548
  function customRender(option) {
@@ -2450,6 +2558,7 @@ function customRender(option) {
2450
2558
  tokenizer
2451
2559
  };
2452
2560
  terp.program(root, componentNode);
2561
+ flushMicroEffectManual();
2453
2562
  return [componentNode, store];
2454
2563
  };
2455
2564
  }
@@ -2493,5 +2602,5 @@ const effect = (callback, depOrOpt, opt) => {
2493
2602
  return effect$1(callback, newDeps, option);
2494
2603
  };
2495
2604
 
2496
- export { Compiler, NodeType, ParseSyntaxError, Tokenizer, bobe, context, customRender, effect };
2605
+ export { Compiler, FakeType, NodeType, ParseSyntaxError, Tokenizer, bobe, context, customRender, effect };
2497
2606
  //# sourceMappingURL=bobe.compiler.esm.js.map