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/index.d.ts CHANGED
@@ -29,6 +29,7 @@ declare class Interpreter {
29
29
  * <declaration> ::= <tagName=token> <headerLine> <extensionLines>
30
30
  * */
31
31
  declaration(ctx: ProgramCtx): any;
32
+ createContextNode(): ContextNode;
32
33
  forDeclaration(): ForNode | ForItemNode;
33
34
  insertForItem(forNode: ForNode, i: number, parentData: any, newChildren: ForItemNode[], before: any, snapshotForUpdate: any): void;
34
35
  removeForItem(children: ForItemNode[], i: number): void;
@@ -149,7 +150,8 @@ declare enum FakeType {
149
150
  For = 8,
150
151
  Component = 16,
151
152
  Fragment = 32,
152
- ForItem = 64
153
+ ForItem = 64,
154
+ Context = 128
153
155
  }
154
156
  type NodeSortBit = number;
155
157
  type BaseType = string | number | boolean | undefined | null;
@@ -250,6 +252,7 @@ type ForItemNode = LogicNode & {
250
252
  forNode: ForNode;
251
253
  effect: Scope;
252
254
  key?: any;
255
+ context: any;
253
256
  };
254
257
  type IfNode = LogicNode & {
255
258
  condition: SignalNode;
@@ -258,6 +261,11 @@ type IfNode = LogicNode & {
258
261
  effect: Effect;
259
262
  preCond: IfNode | null;
260
263
  owner: ComponentNode$1 | FragmentNode$1;
264
+ context: any;
265
+ };
266
+ /** data 是 map<storeKey, store> */
267
+ type ContextNode = Omit<LogicNode, 'data'> & {
268
+ context: any;
261
269
  };
262
270
  type FragmentNode$1 = LogicNode & {
263
271
  tokenizer: Tokenizer;
@@ -521,5 +529,11 @@ type ParseHooks = Partial<{
521
529
  declare function bobe(fragments: TemplateStringsArray, ...values: any[]): BobeUI;
522
530
  declare function customRender(option: CustomRenderConf): <T>(Ctor: typeof Store, root: any) => (ComponentNode$1 | Store)[];
523
531
 
524
- export { Compiler, NodeType, ParseErrorCode, ParseSyntaxError, Tokenizer, bobe, customRender };
525
- export type { ASTNodeType, BaseNode, CommentNode, ComponentNode, ConditionalNode, DynamicValue, ElementNode, FragmentNode, InterpolationNode, LoopNode, ParseError, Program, Property, PropertyKeyNode, PropertyValue, SourceLocation, StaticValue, TemplateNode, TextNode };
532
+ type IContext = {
533
+ <T = any>(name: string): T;
534
+ <T = any>(): T;
535
+ };
536
+ declare const context: IContext;
537
+
538
+ export { Compiler, NodeType, ParseErrorCode, ParseSyntaxError, Tokenizer, bobe, context, customRender };
539
+ export type { ASTNodeType, BaseNode, CommentNode, ComponentNode, ConditionalNode, DynamicValue, ElementNode, FragmentNode, IContext, InterpolationNode, LoopNode, ParseError, Program, Property, PropertyKeyNode, PropertyValue, SourceLocation, StaticValue, TemplateNode, TextNode };
package/dist/index.umd.js CHANGED
@@ -33,11 +33,13 @@
33
33
  FakeType[FakeType["Component"] = 16] = "Component";
34
34
  FakeType[FakeType["Fragment"] = 32] = "Fragment";
35
35
  FakeType[FakeType["ForItem"] = 64] = "ForItem";
36
+ FakeType[FakeType["Context"] = 128] = "Context";
36
37
  return FakeType;
37
38
  }({});
38
39
  const CondBit = FakeType.If | FakeType.Fail | FakeType.Else;
39
40
  const LogicalBit = FakeType.If | FakeType.Fail | FakeType.Else | FakeType.For | FakeType.ForItem;
40
41
  FakeType.If | FakeType.Fail | FakeType.Else | FakeType.For | FakeType.ForItem | FakeType.Component | FakeType.Fragment;
42
+ const ContextBit = FakeType.If | FakeType.Fail | FakeType.Else | FakeType.ForItem | FakeType.Context;
41
43
  const TokenizerSwitcherBit = FakeType.Component | FakeType.Fragment;
42
44
  let NodeSort = function (NodeSort) {
43
45
  NodeSort[NodeSort["Logic"] = 1] = "Logic";
@@ -45,6 +47,7 @@
45
47
  NodeSort[NodeSort["Component"] = 4] = "Component";
46
48
  NodeSort[NodeSort["CtxProvider"] = 8] = "CtxProvider";
47
49
  NodeSort[NodeSort["TokenizerSwitcher"] = 16] = "TokenizerSwitcher";
50
+ NodeSort[NodeSort["Context"] = 32] = "Context";
48
51
  return NodeSort;
49
52
  }({});
50
53
  (function (TerpEvt) {
@@ -1371,6 +1374,9 @@
1371
1374
  }
1372
1375
 
1373
1376
  const KEY_INDEX = '__BOBE_KEY_INDEX';
1377
+ let _ctxStack;
1378
+ const getCtxStack = () => _ctxStack;
1379
+ const setCtxStack = stack => _ctxStack = stack;
1374
1380
 
1375
1381
  const _excluded = ["dentStack", "isFirstToken"];
1376
1382
  class Interpreter {
@@ -1385,6 +1391,7 @@
1385
1391
  this.rootComponent = componentNode;
1386
1392
  this.tokenizer.nextToken();
1387
1393
  const stack = new MultiTypeStack();
1394
+ setCtxStack(stack);
1388
1395
  stack.push({
1389
1396
  node: root,
1390
1397
  prev: null
@@ -1421,7 +1428,7 @@
1421
1428
  stack.push({
1422
1429
  node: ctx.current,
1423
1430
  prev: ctx.prevSibling
1424
- }, !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);
1431
+ }, !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));
1425
1432
  if (ctx.current.__logicType) {
1426
1433
  if (isLogicNode) {
1427
1434
  aoye.setPulling(ctx.current.effect);
@@ -1545,6 +1552,8 @@
1545
1552
  let _node;
1546
1553
  if (value === 'if' || value === 'else' || value === 'fail') {
1547
1554
  return this.condDeclaration(ctx);
1555
+ } else if (value === 'context') {
1556
+ _node = this.createContextNode();
1548
1557
  } else if (value === 'for') {
1549
1558
  return this.forDeclaration();
1550
1559
  } else if (hookType) {
@@ -1572,12 +1581,29 @@
1572
1581
  this.tokenizer.nextToken();
1573
1582
  this.headerLine(_node);
1574
1583
  this.extensionLines(_node);
1584
+ this.onePropParsed = this.oneRealPropParsed;
1575
1585
  if (_node.__logicType & TokenizerSwitcherBit) {
1576
- this.onePropParsed = this.oneRealPropParsed;
1577
1586
  this.tokenizer = _node.tokenizer;
1578
1587
  }
1579
1588
  return _node;
1580
1589
  }
1590
+ createContextNode() {
1591
+ const child = aoye.deepSignal({}, aoye.getPulling());
1592
+ const parentContext = this.ctx.stack.peekByType(NodeSort.Context)?.node?.context;
1593
+ if (parentContext) {
1594
+ Object.setPrototypeOf(child, parentContext);
1595
+ }
1596
+ this.onePropParsed = createStoreOnePropParsed(child);
1597
+ const node = {
1598
+ __logicType: FakeType.Context,
1599
+ context: child,
1600
+ realParent: null,
1601
+ realBefore: null,
1602
+ realAfter: null
1603
+ };
1604
+ node.realAfter = this.insertAfterAnchor('context-after');
1605
+ return node;
1606
+ }
1581
1607
  forDeclaration() {
1582
1608
  const arrExp = this.tokenizer.jsExp().value;
1583
1609
  this.tokenizer.nextToken();
@@ -1857,6 +1883,7 @@
1857
1883
  scope.get();
1858
1884
  }, null);
1859
1885
  data = this.getItemData(forNode, i, parentData);
1886
+ const context = this.ctx.stack.peekByType(NodeSort.Context)?.node?.data;
1860
1887
  forItemNode = {
1861
1888
  id: this.forItemId++,
1862
1889
  __logicType: FakeType.ForItem,
@@ -1866,7 +1893,8 @@
1866
1893
  forNode,
1867
1894
  key: forNode.getKey?.(data),
1868
1895
  effect: null,
1869
- data
1896
+ data,
1897
+ context
1870
1898
  };
1871
1899
  forItemNode.effect = scope;
1872
1900
  return forItemNode;
@@ -1950,26 +1978,7 @@
1950
1978
  data: child,
1951
1979
  tokenizer: render ? render(true) : child.ui(true)
1952
1980
  };
1953
- this.onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
1954
- if (isFn) {
1955
- child[aoye.Keys.Raw][key] = value;
1956
- } else if (valueIsMapKey) {
1957
- aoye.shareSignal(data, value, child, key);
1958
- } else {
1959
- const meta = child[aoye.Keys.Meta];
1960
- const cells = meta.cells;
1961
- if (typeof value === 'function') {
1962
- const computed = new aoye.Computed(() => value(data));
1963
- cells.set(key, computed);
1964
- child[aoye.Keys.Raw][key] = undefined;
1965
- } else {
1966
- cells.set(key, {
1967
- get: () => value
1968
- });
1969
- child[aoye.Keys.Raw][key] = value;
1970
- }
1971
- }
1972
- };
1981
+ this.onePropParsed = createStoreOnePropParsed(child);
1973
1982
  node.realAfter = this.insertAfterAnchor('component-after');
1974
1983
  return node;
1975
1984
  }
@@ -1992,6 +2001,7 @@
1992
2001
  const noCond = value === true;
1993
2002
  const valueIsMapKey = !noCond && Reflect.has(data[aoye.Keys.Raw], value);
1994
2003
  const owner = ctx.stack.peekByType(NodeSort.TokenizerSwitcher)?.node;
2004
+ const context = ctx.stack.peekByType(NodeSort.Context)?.node?.data;
1995
2005
  const ifNode = {
1996
2006
  __logicType: isElse ? FakeType.Else : isIf ? FakeType.If : FakeType.Fail,
1997
2007
  snapshot: this.tokenizer.snapshot(),
@@ -2003,7 +2013,8 @@
2003
2013
  isFirstRender: true,
2004
2014
  effect: null,
2005
2015
  owner,
2006
- data
2016
+ data,
2017
+ context
2007
2018
  };
2008
2019
  let signal;
2009
2020
  switch (keyWord.value) {
@@ -2221,6 +2232,29 @@
2221
2232
  node.props[key] = value;
2222
2233
  }
2223
2234
  }
2235
+ function createStoreOnePropParsed(child) {
2236
+ const onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
2237
+ if (isFn) {
2238
+ child[aoye.Keys.Raw][key] = value;
2239
+ } else if (valueIsMapKey) {
2240
+ aoye.shareSignal(data, value, child, key);
2241
+ } else {
2242
+ const meta = child[aoye.Keys.Meta];
2243
+ const cells = meta.cells;
2244
+ if (typeof value === 'function') {
2245
+ const computed = new aoye.Computed(() => value(data));
2246
+ cells.set(key, computed);
2247
+ child[aoye.Keys.Raw][key] = undefined;
2248
+ } else {
2249
+ cells.set(key, {
2250
+ get: () => value
2251
+ });
2252
+ child[aoye.Keys.Raw][key] = value;
2253
+ }
2254
+ }
2255
+ };
2256
+ return onePropParsed;
2257
+ }
2224
2258
 
2225
2259
  function bobe(fragments, ...values) {
2226
2260
  const ui = function ui(isSub) {
@@ -2252,11 +2286,27 @@
2252
2286
  };
2253
2287
  }
2254
2288
 
2289
+ const context = name => {
2290
+ const stack = getCtxStack();
2291
+ if (!stack) {
2292
+ throw new Error('context() api 只能在组件中使用');
2293
+ }
2294
+ let context = stack.peekByType(NodeSort.Context)?.node?.context;
2295
+ if (name) {
2296
+ context = context?.[name];
2297
+ }
2298
+ if (!context) {
2299
+ console.warn(`context(${name ?? ''}) 为空`);
2300
+ }
2301
+ return context;
2302
+ };
2303
+
2255
2304
  exports.Compiler = Compiler;
2256
2305
  exports.NodeType = NodeType;
2257
2306
  exports.ParseSyntaxError = ParseSyntaxError;
2258
2307
  exports.Tokenizer = Tokenizer;
2259
2308
  exports.bobe = bobe;
2309
+ exports.context = context;
2260
2310
  exports.customRender = customRender;
2261
2311
  Object.keys(aoye).forEach(function (k) {
2262
2312
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {