bobe 0.0.43 → 0.0.45

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