bobe 0.0.43 → 0.0.44

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
1
  import { Queue, isNum, matchIdStart2, matchId, escapeMap, jsVarRegexp, date32 } from 'bobe-shared';
2
- import { getPulling, setPulling, Keys, Computed, Effect, toRaw, runWithPulling, Scope, deepSignal, Store, shareSignal, effect } from 'aoye';
2
+ import { getPulling, setPulling, Keys, deepSignal, Computed, Effect, toRaw, runWithPulling, Scope, Store, effect, shareSignal } from 'aoye';
3
3
  export * from 'aoye';
4
4
 
5
5
  let TokenType = function (TokenType) {
@@ -31,11 +31,13 @@ let FakeType = function (FakeType) {
31
31
  FakeType[FakeType["Component"] = 16] = "Component";
32
32
  FakeType[FakeType["Fragment"] = 32] = "Fragment";
33
33
  FakeType[FakeType["ForItem"] = 64] = "ForItem";
34
+ FakeType[FakeType["Context"] = 128] = "Context";
34
35
  return FakeType;
35
36
  }({});
36
37
  const CondBit = FakeType.If | FakeType.Fail | FakeType.Else;
37
38
  const LogicalBit = FakeType.If | FakeType.Fail | FakeType.Else | FakeType.For | FakeType.ForItem;
38
39
  FakeType.If | FakeType.Fail | FakeType.Else | FakeType.For | FakeType.ForItem | FakeType.Component | FakeType.Fragment;
40
+ const ContextBit = FakeType.If | FakeType.Fail | FakeType.Else | FakeType.ForItem | FakeType.Context;
39
41
  const TokenizerSwitcherBit = FakeType.Component | FakeType.Fragment;
40
42
  let NodeSort = function (NodeSort) {
41
43
  NodeSort[NodeSort["Logic"] = 1] = "Logic";
@@ -43,6 +45,7 @@ let NodeSort = function (NodeSort) {
43
45
  NodeSort[NodeSort["Component"] = 4] = "Component";
44
46
  NodeSort[NodeSort["CtxProvider"] = 8] = "CtxProvider";
45
47
  NodeSort[NodeSort["TokenizerSwitcher"] = 16] = "TokenizerSwitcher";
48
+ NodeSort[NodeSort["Context"] = 32] = "Context";
46
49
  return NodeSort;
47
50
  }({});
48
51
  (function (TerpEvt) {
@@ -1410,6 +1413,9 @@ function macInc(arr) {
1410
1413
  }
1411
1414
 
1412
1415
  const KEY_INDEX = '__BOBE_KEY_INDEX';
1416
+ let _ctxStack;
1417
+ const getCtxStack = () => _ctxStack;
1418
+ const setCtxStack = stack => _ctxStack = stack;
1413
1419
 
1414
1420
  const _excluded = ["dentStack", "isFirstToken"];
1415
1421
  class Interpreter {
@@ -1424,6 +1430,7 @@ class Interpreter {
1424
1430
  this.rootComponent = componentNode;
1425
1431
  this.tokenizer.nextToken();
1426
1432
  const stack = new MultiTypeStack();
1433
+ setCtxStack(stack);
1427
1434
  stack.push({
1428
1435
  node: root,
1429
1436
  prev: null
@@ -1460,7 +1467,7 @@ class Interpreter {
1460
1467
  stack.push({
1461
1468
  node: ctx.current,
1462
1469
  prev: ctx.prevSibling
1463
- }, !ctx.current.__logicType ? NodeSort.Real : (ctx.current.__logicType & LogicalBit ? NodeSort.Logic : 0) | (ctx.current.__logicType & TokenizerSwitcherBit ? NodeSort.TokenizerSwitcher : 0) | (ctx.current.__logicType === FakeType.Component ? NodeSort.Component : 0) | NodeSort.CtxProvider);
1470
+ }, !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));
1464
1471
  if (ctx.current.__logicType) {
1465
1472
  if (isLogicNode) {
1466
1473
  setPulling(ctx.current.effect);
@@ -1584,6 +1591,8 @@ class Interpreter {
1584
1591
  let _node;
1585
1592
  if (value === 'if' || value === 'else' || value === 'fail') {
1586
1593
  return this.condDeclaration(ctx);
1594
+ } else if (value === 'context') {
1595
+ _node = this.createContextNode();
1587
1596
  } else if (value === 'for') {
1588
1597
  return this.forDeclaration();
1589
1598
  } else if (hookType) {
@@ -1611,12 +1620,29 @@ class Interpreter {
1611
1620
  this.tokenizer.nextToken();
1612
1621
  this.headerLine(_node);
1613
1622
  this.extensionLines(_node);
1623
+ this.onePropParsed = this.oneRealPropParsed;
1614
1624
  if (_node.__logicType & TokenizerSwitcherBit) {
1615
- this.onePropParsed = this.oneRealPropParsed;
1616
1625
  this.tokenizer = _node.tokenizer;
1617
1626
  }
1618
1627
  return _node;
1619
1628
  }
1629
+ createContextNode() {
1630
+ const child = deepSignal({}, getPulling());
1631
+ const parentContext = this.ctx.stack.peekByType(NodeSort.Context)?.node?.context;
1632
+ if (parentContext) {
1633
+ Object.setPrototypeOf(child, parentContext);
1634
+ }
1635
+ this.onePropParsed = createStoreOnePropParsed(child);
1636
+ const node = {
1637
+ __logicType: FakeType.Context,
1638
+ context: child,
1639
+ realParent: null,
1640
+ realBefore: null,
1641
+ realAfter: null
1642
+ };
1643
+ node.realAfter = this.insertAfterAnchor('context-after');
1644
+ return node;
1645
+ }
1620
1646
  forDeclaration() {
1621
1647
  const arrExp = this.tokenizer.jsExp().value;
1622
1648
  this.tokenizer.nextToken();
@@ -1896,6 +1922,7 @@ class Interpreter {
1896
1922
  scope.get();
1897
1923
  }, null);
1898
1924
  data = this.getItemData(forNode, i, parentData);
1925
+ const context = this.ctx.stack.peekByType(NodeSort.Context)?.node?.data;
1899
1926
  forItemNode = {
1900
1927
  id: this.forItemId++,
1901
1928
  __logicType: FakeType.ForItem,
@@ -1905,7 +1932,8 @@ class Interpreter {
1905
1932
  forNode,
1906
1933
  key: forNode.getKey?.(data),
1907
1934
  effect: null,
1908
- data
1935
+ data,
1936
+ context
1909
1937
  };
1910
1938
  forItemNode.effect = scope;
1911
1939
  return forItemNode;
@@ -1989,26 +2017,7 @@ class Interpreter {
1989
2017
  data: child,
1990
2018
  tokenizer: render ? render(true) : child.ui(true)
1991
2019
  };
1992
- this.onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
1993
- if (isFn) {
1994
- child[Keys.Raw][key] = value;
1995
- } else if (valueIsMapKey) {
1996
- shareSignal(data, value, child, key);
1997
- } else {
1998
- const meta = child[Keys.Meta];
1999
- const cells = meta.cells;
2000
- if (typeof value === 'function') {
2001
- const computed = new Computed(() => value(data));
2002
- cells.set(key, computed);
2003
- child[Keys.Raw][key] = undefined;
2004
- } else {
2005
- cells.set(key, {
2006
- get: () => value
2007
- });
2008
- child[Keys.Raw][key] = value;
2009
- }
2010
- }
2011
- };
2020
+ this.onePropParsed = createStoreOnePropParsed(child);
2012
2021
  node.realAfter = this.insertAfterAnchor('component-after');
2013
2022
  return node;
2014
2023
  }
@@ -2031,6 +2040,7 @@ class Interpreter {
2031
2040
  const noCond = value === true;
2032
2041
  const valueIsMapKey = !noCond && Reflect.has(data[Keys.Raw], value);
2033
2042
  const owner = ctx.stack.peekByType(NodeSort.TokenizerSwitcher)?.node;
2043
+ const context = ctx.stack.peekByType(NodeSort.Context)?.node?.data;
2034
2044
  const ifNode = {
2035
2045
  __logicType: isElse ? FakeType.Else : isIf ? FakeType.If : FakeType.Fail,
2036
2046
  snapshot: this.tokenizer.snapshot(),
@@ -2042,7 +2052,8 @@ class Interpreter {
2042
2052
  isFirstRender: true,
2043
2053
  effect: null,
2044
2054
  owner,
2045
- data
2055
+ data,
2056
+ context
2046
2057
  };
2047
2058
  let signal;
2048
2059
  switch (keyWord.value) {
@@ -2260,6 +2271,29 @@ class Interpreter {
2260
2271
  node.props[key] = value;
2261
2272
  }
2262
2273
  }
2274
+ function createStoreOnePropParsed(child) {
2275
+ const onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
2276
+ if (isFn) {
2277
+ child[Keys.Raw][key] = value;
2278
+ } else if (valueIsMapKey) {
2279
+ shareSignal(data, value, child, key);
2280
+ } else {
2281
+ const meta = child[Keys.Meta];
2282
+ const cells = meta.cells;
2283
+ if (typeof value === 'function') {
2284
+ const computed = new Computed(() => value(data));
2285
+ cells.set(key, computed);
2286
+ child[Keys.Raw][key] = undefined;
2287
+ } else {
2288
+ cells.set(key, {
2289
+ get: () => value
2290
+ });
2291
+ child[Keys.Raw][key] = value;
2292
+ }
2293
+ }
2294
+ };
2295
+ return onePropParsed;
2296
+ }
2263
2297
 
2264
2298
  function bobe(fragments, ...values) {
2265
2299
  const ui = function ui(isSub) {
@@ -2291,5 +2325,20 @@ function customRender(option) {
2291
2325
  };
2292
2326
  }
2293
2327
 
2294
- export { Compiler, NodeType, ParseSyntaxError, Tokenizer, bobe, customRender };
2328
+ const context = name => {
2329
+ const stack = getCtxStack();
2330
+ if (!stack) {
2331
+ throw new Error('context() api 只能在组件中使用');
2332
+ }
2333
+ let context = stack.peekByType(NodeSort.Context)?.node?.context;
2334
+ if (name) {
2335
+ context = context?.[name];
2336
+ }
2337
+ if (!context) {
2338
+ console.warn(`context(${name ?? ''}) 为空`);
2339
+ }
2340
+ return context;
2341
+ };
2342
+
2343
+ export { Compiler, NodeType, ParseSyntaxError, Tokenizer, bobe, context, customRender };
2295
2344
  //# sourceMappingURL=bobe.compiler.esm.js.map