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.
@@ -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;
@@ -1950,16 +1978,20 @@ class Interpreter {
1950
1978
  }
1951
1979
  onePropParsed(data, node, key, value, valueIsMapKey, isFn, hookI) {
1952
1980
  if (isFn) {
1953
- this.setProp(node, key, value, hookI);
1981
+ new aoye.Scope(() => {
1982
+ return this.setProp(node, key, value, hookI);
1983
+ }).get();
1954
1984
  } else if (typeof value === 'function') {
1955
1985
  new aoye.Effect(() => {
1956
- const res = value();
1957
- this.setProp(node, key, res, hookI);
1986
+ const res = value(data);
1987
+ const dispose = this.setProp(node, key, res, hookI);
1988
+ return dispose;
1958
1989
  });
1959
1990
  } else if (valueIsMapKey) {
1960
1991
  new aoye.Effect(() => {
1961
1992
  const res = data[value];
1962
- this.setProp(node, key, res, hookI);
1993
+ const dispose = this.setProp(node, key, res, hookI);
1994
+ return dispose;
1963
1995
  });
1964
1996
  } else {
1965
1997
  this.setProp(node, key, value, hookI);
@@ -1984,34 +2016,19 @@ class Interpreter {
1984
2016
  realBefore: null,
1985
2017
  realAfter: null,
1986
2018
  data: child,
1987
- tokenizer: render ? render(true) : child['ui'](true)
1988
- };
1989
- this.onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
1990
- if (isFn) {
1991
- child[aoye.Keys.Raw][key] = value;
1992
- } else if (valueIsMapKey) {
1993
- aoye.shareSignal(data, value, child, key);
1994
- } else {
1995
- const meta = child[aoye.Keys.Meta];
1996
- const cells = meta.cells;
1997
- if (typeof value === 'function') {
1998
- const computed = new aoye.Computed(value);
1999
- cells.set(key, computed);
2000
- child[aoye.Keys.Raw][key] = undefined;
2001
- } else {
2002
- cells.set(key, {
2003
- get: () => value
2004
- });
2005
- child[aoye.Keys.Raw][key] = value;
2006
- }
2007
- }
2019
+ tokenizer: render ? render(true) : child.ui(true)
2008
2020
  };
2021
+ this.onePropParsed = createStoreOnePropParsed(child);
2009
2022
  node.realAfter = this.insertAfterAnchor('component-after');
2010
2023
  return node;
2011
2024
  }
2012
2025
  getFn(data, expression) {
2013
2026
  return new Function('data', `let v;with(data){v=${expression}};return v;`).bind(undefined, data);
2014
2027
  }
2028
+ getAssignFn(data, expression) {
2029
+ const valueId = `value_bobe_${bobeShared.date32()}`;
2030
+ return new Function('data', valueId, `with(data){${expression}=${valueId}};`).bind(undefined, data);
2031
+ }
2015
2032
  condDeclaration(ctx) {
2016
2033
  const prevSibling = ctx.prevSibling;
2017
2034
  const keyWord = this.tokenizer.token;
@@ -2024,6 +2041,7 @@ class Interpreter {
2024
2041
  const noCond = value === true;
2025
2042
  const valueIsMapKey = !noCond && Reflect.has(data[aoye.Keys.Raw], value);
2026
2043
  const owner = ctx.stack.peekByType(NodeSort.TokenizerSwitcher)?.node;
2044
+ const context = ctx.stack.peekByType(NodeSort.Context)?.node?.data;
2027
2045
  const ifNode = {
2028
2046
  __logicType: isElse ? FakeType.Else : isIf ? FakeType.If : FakeType.Fail,
2029
2047
  snapshot: this.tokenizer.snapshot(),
@@ -2035,7 +2053,8 @@ class Interpreter {
2035
2053
  isFirstRender: true,
2036
2054
  effect: null,
2037
2055
  owner,
2038
- data
2056
+ data,
2057
+ context
2039
2058
  };
2040
2059
  let signal;
2041
2060
  switch (keyWord.value) {
@@ -2165,7 +2184,27 @@ class Interpreter {
2165
2184
  hookI = _this$tokenizer$_hook4[2];
2166
2185
  const rawVal = data[aoye.Keys.Raw][value];
2167
2186
  const isFn = typeof rawVal === 'function';
2168
- if (hookType === 'dynamic') {
2187
+ if (key === 'ref') {
2188
+ const valueIsMapKey = Reflect.has(data[aoye.Keys.Raw], value);
2189
+ let refValue = _node;
2190
+ if (_node.__logicType === FakeType.Component) {
2191
+ refValue = _node.data;
2192
+ } else {
2193
+ refValue[aoye.Keys.ProxyFreeObject] = true;
2194
+ }
2195
+ if (valueIsMapKey) {
2196
+ data[value] = refValue;
2197
+ new aoye.Scope(() => () => {
2198
+ data[value] = null;
2199
+ }).get();
2200
+ } else {
2201
+ const fn = this.getAssignFn(data, value);
2202
+ fn(refValue);
2203
+ new aoye.Scope(() => () => {
2204
+ fn(null);
2205
+ }).get();
2206
+ }
2207
+ } else if (hookType === 'dynamic') {
2169
2208
  const valueIsMapKey = Reflect.has(data[aoye.Keys.Raw], value);
2170
2209
  const fn = isFn ? rawVal : valueIsMapKey ? value : this.getFn(data, value);
2171
2210
  this.onePropParsed(data, _node, key, fn, valueIsMapKey, isFn, hookI);
@@ -2233,6 +2272,29 @@ class Interpreter {
2233
2272
  node.props[key] = value;
2234
2273
  }
2235
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
+ }
2236
2298
 
2237
2299
  function bobe(fragments, ...values) {
2238
2300
  const ui = function ui(isSub) {
@@ -2250,7 +2312,7 @@ function bobe(fragments, ...values) {
2250
2312
  function customRender(option) {
2251
2313
  return function render(Ctor, root) {
2252
2314
  const store = Ctor.new();
2253
- const tokenizer = store['ui'](false);
2315
+ const tokenizer = store.ui(false);
2254
2316
  const terp = new Interpreter(tokenizer);
2255
2317
  terp.config(option);
2256
2318
  const componentNode = {
@@ -2264,11 +2326,27 @@ function customRender(option) {
2264
2326
  };
2265
2327
  }
2266
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
+
2267
2344
  exports.Compiler = Compiler;
2268
2345
  exports.NodeType = NodeType;
2269
2346
  exports.ParseSyntaxError = ParseSyntaxError;
2270
2347
  exports.Tokenizer = Tokenizer;
2271
2348
  exports.bobe = bobe;
2349
+ exports.context = context;
2272
2350
  exports.customRender = customRender;
2273
2351
  Object.keys(aoye).forEach(function (k) {
2274
2352
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {