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