bobe 0.0.42 → 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
- import { Queue, isNum, matchIdStart2, matchId, escapeMap, jsVarRegexp } from 'bobe-shared';
2
- import { getPulling, setPulling, Keys, Computed, Effect, toRaw, runWithPulling, Scope, deepSignal, Store, shareSignal, effect } from 'aoye';
1
+ import { Queue, isNum, matchIdStart2, matchId, escapeMap, jsVarRegexp, date32 } from 'bobe-shared';
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;
@@ -1949,16 +1977,20 @@ class Interpreter {
1949
1977
  }
1950
1978
  onePropParsed(data, node, key, value, valueIsMapKey, isFn, hookI) {
1951
1979
  if (isFn) {
1952
- this.setProp(node, key, value, hookI);
1980
+ new Scope(() => {
1981
+ return this.setProp(node, key, value, hookI);
1982
+ }).get();
1953
1983
  } else if (typeof value === 'function') {
1954
1984
  new Effect(() => {
1955
- const res = value();
1956
- this.setProp(node, key, res, hookI);
1985
+ const res = value(data);
1986
+ const dispose = this.setProp(node, key, res, hookI);
1987
+ return dispose;
1957
1988
  });
1958
1989
  } else if (valueIsMapKey) {
1959
1990
  new Effect(() => {
1960
1991
  const res = data[value];
1961
- this.setProp(node, key, res, hookI);
1992
+ const dispose = this.setProp(node, key, res, hookI);
1993
+ return dispose;
1962
1994
  });
1963
1995
  } else {
1964
1996
  this.setProp(node, key, value, hookI);
@@ -1983,34 +2015,19 @@ class Interpreter {
1983
2015
  realBefore: null,
1984
2016
  realAfter: null,
1985
2017
  data: child,
1986
- tokenizer: render ? render(true) : child['ui'](true)
1987
- };
1988
- this.onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
1989
- if (isFn) {
1990
- child[Keys.Raw][key] = value;
1991
- } else if (valueIsMapKey) {
1992
- shareSignal(data, value, child, key);
1993
- } else {
1994
- const meta = child[Keys.Meta];
1995
- const cells = meta.cells;
1996
- if (typeof value === 'function') {
1997
- const computed = new Computed(value);
1998
- cells.set(key, computed);
1999
- child[Keys.Raw][key] = undefined;
2000
- } else {
2001
- cells.set(key, {
2002
- get: () => value
2003
- });
2004
- child[Keys.Raw][key] = value;
2005
- }
2006
- }
2018
+ tokenizer: render ? render(true) : child.ui(true)
2007
2019
  };
2020
+ this.onePropParsed = createStoreOnePropParsed(child);
2008
2021
  node.realAfter = this.insertAfterAnchor('component-after');
2009
2022
  return node;
2010
2023
  }
2011
2024
  getFn(data, expression) {
2012
2025
  return new Function('data', `let v;with(data){v=${expression}};return v;`).bind(undefined, data);
2013
2026
  }
2027
+ getAssignFn(data, expression) {
2028
+ const valueId = `value_bobe_${date32()}`;
2029
+ return new Function('data', valueId, `with(data){${expression}=${valueId}};`).bind(undefined, data);
2030
+ }
2014
2031
  condDeclaration(ctx) {
2015
2032
  const prevSibling = ctx.prevSibling;
2016
2033
  const keyWord = this.tokenizer.token;
@@ -2023,6 +2040,7 @@ class Interpreter {
2023
2040
  const noCond = value === true;
2024
2041
  const valueIsMapKey = !noCond && Reflect.has(data[Keys.Raw], value);
2025
2042
  const owner = ctx.stack.peekByType(NodeSort.TokenizerSwitcher)?.node;
2043
+ const context = ctx.stack.peekByType(NodeSort.Context)?.node?.data;
2026
2044
  const ifNode = {
2027
2045
  __logicType: isElse ? FakeType.Else : isIf ? FakeType.If : FakeType.Fail,
2028
2046
  snapshot: this.tokenizer.snapshot(),
@@ -2034,7 +2052,8 @@ class Interpreter {
2034
2052
  isFirstRender: true,
2035
2053
  effect: null,
2036
2054
  owner,
2037
- data
2055
+ data,
2056
+ context
2038
2057
  };
2039
2058
  let signal;
2040
2059
  switch (keyWord.value) {
@@ -2164,7 +2183,27 @@ class Interpreter {
2164
2183
  hookI = _this$tokenizer$_hook4[2];
2165
2184
  const rawVal = data[Keys.Raw][value];
2166
2185
  const isFn = typeof rawVal === 'function';
2167
- if (hookType === 'dynamic') {
2186
+ if (key === 'ref') {
2187
+ const valueIsMapKey = Reflect.has(data[Keys.Raw], value);
2188
+ let refValue = _node;
2189
+ if (_node.__logicType === FakeType.Component) {
2190
+ refValue = _node.data;
2191
+ } else {
2192
+ refValue[Keys.ProxyFreeObject] = true;
2193
+ }
2194
+ if (valueIsMapKey) {
2195
+ data[value] = refValue;
2196
+ new Scope(() => () => {
2197
+ data[value] = null;
2198
+ }).get();
2199
+ } else {
2200
+ const fn = this.getAssignFn(data, value);
2201
+ fn(refValue);
2202
+ new Scope(() => () => {
2203
+ fn(null);
2204
+ }).get();
2205
+ }
2206
+ } else if (hookType === 'dynamic') {
2168
2207
  const valueIsMapKey = Reflect.has(data[Keys.Raw], value);
2169
2208
  const fn = isFn ? rawVal : valueIsMapKey ? value : this.getFn(data, value);
2170
2209
  this.onePropParsed(data, _node, key, fn, valueIsMapKey, isFn, hookI);
@@ -2232,6 +2271,29 @@ class Interpreter {
2232
2271
  node.props[key] = value;
2233
2272
  }
2234
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
+ }
2235
2297
 
2236
2298
  function bobe(fragments, ...values) {
2237
2299
  const ui = function ui(isSub) {
@@ -2249,7 +2311,7 @@ function bobe(fragments, ...values) {
2249
2311
  function customRender(option) {
2250
2312
  return function render(Ctor, root) {
2251
2313
  const store = Ctor.new();
2252
- const tokenizer = store['ui'](false);
2314
+ const tokenizer = store.ui(false);
2253
2315
  const terp = new Interpreter(tokenizer);
2254
2316
  terp.config(option);
2255
2317
  const componentNode = {
@@ -2263,5 +2325,20 @@ function customRender(option) {
2263
2325
  };
2264
2326
  }
2265
2327
 
2266
- 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 };
2267
2344
  //# sourceMappingURL=bobe.compiler.esm.js.map