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.
package/dist/bobe.cjs.js CHANGED
@@ -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) {
@@ -1370,6 +1373,9 @@ function macInc(arr) {
1370
1373
  }
1371
1374
 
1372
1375
  const KEY_INDEX = '__BOBE_KEY_INDEX';
1376
+ let _ctxStack;
1377
+ const getCtxStack = () => _ctxStack;
1378
+ const setCtxStack = stack => _ctxStack = stack;
1373
1379
 
1374
1380
  const _excluded = ["dentStack", "isFirstToken"];
1375
1381
  class Interpreter {
@@ -1384,6 +1390,7 @@ class Interpreter {
1384
1390
  this.rootComponent = componentNode;
1385
1391
  this.tokenizer.nextToken();
1386
1392
  const stack = new MultiTypeStack();
1393
+ setCtxStack(stack);
1387
1394
  stack.push({
1388
1395
  node: root,
1389
1396
  prev: null
@@ -1420,7 +1427,7 @@ class Interpreter {
1420
1427
  stack.push({
1421
1428
  node: ctx.current,
1422
1429
  prev: ctx.prevSibling
1423
- }, !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);
1430
+ }, !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));
1424
1431
  if (ctx.current.__logicType) {
1425
1432
  if (isLogicNode) {
1426
1433
  aoye.setPulling(ctx.current.effect);
@@ -1544,6 +1551,8 @@ class Interpreter {
1544
1551
  let _node;
1545
1552
  if (value === 'if' || value === 'else' || value === 'fail') {
1546
1553
  return this.condDeclaration(ctx);
1554
+ } else if (value === 'context') {
1555
+ _node = this.createContextNode();
1547
1556
  } else if (value === 'for') {
1548
1557
  return this.forDeclaration();
1549
1558
  } else if (hookType) {
@@ -1571,12 +1580,29 @@ class Interpreter {
1571
1580
  this.tokenizer.nextToken();
1572
1581
  this.headerLine(_node);
1573
1582
  this.extensionLines(_node);
1583
+ this.onePropParsed = this.oneRealPropParsed;
1574
1584
  if (_node.__logicType & TokenizerSwitcherBit) {
1575
- this.onePropParsed = this.oneRealPropParsed;
1576
1585
  this.tokenizer = _node.tokenizer;
1577
1586
  }
1578
1587
  return _node;
1579
1588
  }
1589
+ createContextNode() {
1590
+ const child = aoye.deepSignal({}, aoye.getPulling());
1591
+ const parentContext = this.ctx.stack.peekByType(NodeSort.Context)?.node?.context;
1592
+ if (parentContext) {
1593
+ Object.setPrototypeOf(child, parentContext);
1594
+ }
1595
+ this.onePropParsed = createStoreOnePropParsed(child);
1596
+ const node = {
1597
+ __logicType: FakeType.Context,
1598
+ context: child,
1599
+ realParent: null,
1600
+ realBefore: null,
1601
+ realAfter: null
1602
+ };
1603
+ node.realAfter = this.insertAfterAnchor('context-after');
1604
+ return node;
1605
+ }
1580
1606
  forDeclaration() {
1581
1607
  const arrExp = this.tokenizer.jsExp().value;
1582
1608
  this.tokenizer.nextToken();
@@ -1856,6 +1882,7 @@ class Interpreter {
1856
1882
  scope.get();
1857
1883
  }, null);
1858
1884
  data = this.getItemData(forNode, i, parentData);
1885
+ const context = this.ctx.stack.peekByType(NodeSort.Context)?.node?.data;
1859
1886
  forItemNode = {
1860
1887
  id: this.forItemId++,
1861
1888
  __logicType: FakeType.ForItem,
@@ -1865,7 +1892,8 @@ class Interpreter {
1865
1892
  forNode,
1866
1893
  key: forNode.getKey?.(data),
1867
1894
  effect: null,
1868
- data
1895
+ data,
1896
+ context
1869
1897
  };
1870
1898
  forItemNode.effect = scope;
1871
1899
  return forItemNode;
@@ -1949,26 +1977,7 @@ class Interpreter {
1949
1977
  data: child,
1950
1978
  tokenizer: render ? render(true) : child.ui(true)
1951
1979
  };
1952
- this.onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
1953
- if (isFn) {
1954
- child[aoye.Keys.Raw][key] = value;
1955
- } else if (valueIsMapKey) {
1956
- aoye.shareSignal(data, value, child, key);
1957
- } else {
1958
- const meta = child[aoye.Keys.Meta];
1959
- const cells = meta.cells;
1960
- if (typeof value === 'function') {
1961
- const computed = new aoye.Computed(() => value(data));
1962
- cells.set(key, computed);
1963
- child[aoye.Keys.Raw][key] = undefined;
1964
- } else {
1965
- cells.set(key, {
1966
- get: () => value
1967
- });
1968
- child[aoye.Keys.Raw][key] = value;
1969
- }
1970
- }
1971
- };
1980
+ this.onePropParsed = createStoreOnePropParsed(child);
1972
1981
  node.realAfter = this.insertAfterAnchor('component-after');
1973
1982
  return node;
1974
1983
  }
@@ -1991,6 +2000,7 @@ class Interpreter {
1991
2000
  const noCond = value === true;
1992
2001
  const valueIsMapKey = !noCond && Reflect.has(data[aoye.Keys.Raw], value);
1993
2002
  const owner = ctx.stack.peekByType(NodeSort.TokenizerSwitcher)?.node;
2003
+ const context = ctx.stack.peekByType(NodeSort.Context)?.node?.data;
1994
2004
  const ifNode = {
1995
2005
  __logicType: isElse ? FakeType.Else : isIf ? FakeType.If : FakeType.Fail,
1996
2006
  snapshot: this.tokenizer.snapshot(),
@@ -2002,7 +2012,8 @@ class Interpreter {
2002
2012
  isFirstRender: true,
2003
2013
  effect: null,
2004
2014
  owner,
2005
- data
2015
+ data,
2016
+ context
2006
2017
  };
2007
2018
  let signal;
2008
2019
  switch (keyWord.value) {
@@ -2220,6 +2231,29 @@ class Interpreter {
2220
2231
  node.props[key] = value;
2221
2232
  }
2222
2233
  }
2234
+ function createStoreOnePropParsed(child) {
2235
+ const onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
2236
+ if (isFn) {
2237
+ child[aoye.Keys.Raw][key] = value;
2238
+ } else if (valueIsMapKey) {
2239
+ aoye.shareSignal(data, value, child, key);
2240
+ } else {
2241
+ const meta = child[aoye.Keys.Meta];
2242
+ const cells = meta.cells;
2243
+ if (typeof value === 'function') {
2244
+ const computed = new aoye.Computed(() => value(data));
2245
+ cells.set(key, computed);
2246
+ child[aoye.Keys.Raw][key] = undefined;
2247
+ } else {
2248
+ cells.set(key, {
2249
+ get: () => value
2250
+ });
2251
+ child[aoye.Keys.Raw][key] = value;
2252
+ }
2253
+ }
2254
+ };
2255
+ return onePropParsed;
2256
+ }
2223
2257
 
2224
2258
  function bobe(fragments, ...values) {
2225
2259
  const ui = function ui(isSub) {
@@ -2251,11 +2285,27 @@ function customRender(option) {
2251
2285
  };
2252
2286
  }
2253
2287
 
2288
+ const context = name => {
2289
+ const stack = getCtxStack();
2290
+ if (!stack) {
2291
+ throw new Error('context() api 只能在组件中使用');
2292
+ }
2293
+ let context = stack.peekByType(NodeSort.Context)?.node?.context;
2294
+ if (name) {
2295
+ context = context?.[name];
2296
+ }
2297
+ if (!context) {
2298
+ console.warn(`context(${name ?? ''}) 为空`);
2299
+ }
2300
+ return context;
2301
+ };
2302
+
2254
2303
  exports.Compiler = Compiler;
2255
2304
  exports.NodeType = NodeType;
2256
2305
  exports.ParseSyntaxError = ParseSyntaxError;
2257
2306
  exports.Tokenizer = Tokenizer;
2258
2307
  exports.bobe = bobe;
2308
+ exports.context = context;
2259
2309
  exports.customRender = customRender;
2260
2310
  Object.keys(aoye).forEach(function (k) {
2261
2311
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {