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.
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;
@@ -1909,16 +1937,20 @@ class Interpreter {
1909
1937
  }
1910
1938
  onePropParsed(data, node, key, value, valueIsMapKey, isFn, hookI) {
1911
1939
  if (isFn) {
1912
- this.setProp(node, key, value, hookI);
1940
+ new aoye.Scope(() => {
1941
+ return this.setProp(node, key, value, hookI);
1942
+ }).get();
1913
1943
  } else if (typeof value === 'function') {
1914
1944
  new aoye.Effect(() => {
1915
- const res = value();
1916
- this.setProp(node, key, res, hookI);
1945
+ const res = value(data);
1946
+ const dispose = this.setProp(node, key, res, hookI);
1947
+ return dispose;
1917
1948
  });
1918
1949
  } else if (valueIsMapKey) {
1919
1950
  new aoye.Effect(() => {
1920
1951
  const res = data[value];
1921
- this.setProp(node, key, res, hookI);
1952
+ const dispose = this.setProp(node, key, res, hookI);
1953
+ return dispose;
1922
1954
  });
1923
1955
  } else {
1924
1956
  this.setProp(node, key, value, hookI);
@@ -1943,34 +1975,19 @@ class Interpreter {
1943
1975
  realBefore: null,
1944
1976
  realAfter: null,
1945
1977
  data: child,
1946
- tokenizer: render ? render(true) : child['ui'](true)
1947
- };
1948
- this.onePropParsed = (data, _, key, value, valueIsMapKey, isFn, hookI) => {
1949
- if (isFn) {
1950
- child[aoye.Keys.Raw][key] = value;
1951
- } else if (valueIsMapKey) {
1952
- aoye.shareSignal(data, value, child, key);
1953
- } else {
1954
- const meta = child[aoye.Keys.Meta];
1955
- const cells = meta.cells;
1956
- if (typeof value === 'function') {
1957
- const computed = new aoye.Computed(value);
1958
- cells.set(key, computed);
1959
- child[aoye.Keys.Raw][key] = undefined;
1960
- } else {
1961
- cells.set(key, {
1962
- get: () => value
1963
- });
1964
- child[aoye.Keys.Raw][key] = value;
1965
- }
1966
- }
1978
+ tokenizer: render ? render(true) : child.ui(true)
1967
1979
  };
1980
+ this.onePropParsed = createStoreOnePropParsed(child);
1968
1981
  node.realAfter = this.insertAfterAnchor('component-after');
1969
1982
  return node;
1970
1983
  }
1971
1984
  getFn(data, expression) {
1972
1985
  return new Function('data', `let v;with(data){v=${expression}};return v;`).bind(undefined, data);
1973
1986
  }
1987
+ getAssignFn(data, expression) {
1988
+ const valueId = `value_bobe_${bobeShared.date32()}`;
1989
+ return new Function('data', valueId, `with(data){${expression}=${valueId}};`).bind(undefined, data);
1990
+ }
1974
1991
  condDeclaration(ctx) {
1975
1992
  const prevSibling = ctx.prevSibling;
1976
1993
  const keyWord = this.tokenizer.token;
@@ -1983,6 +2000,7 @@ class Interpreter {
1983
2000
  const noCond = value === true;
1984
2001
  const valueIsMapKey = !noCond && Reflect.has(data[aoye.Keys.Raw], value);
1985
2002
  const owner = ctx.stack.peekByType(NodeSort.TokenizerSwitcher)?.node;
2003
+ const context = ctx.stack.peekByType(NodeSort.Context)?.node?.data;
1986
2004
  const ifNode = {
1987
2005
  __logicType: isElse ? FakeType.Else : isIf ? FakeType.If : FakeType.Fail,
1988
2006
  snapshot: this.tokenizer.snapshot(),
@@ -1994,7 +2012,8 @@ class Interpreter {
1994
2012
  isFirstRender: true,
1995
2013
  effect: null,
1996
2014
  owner,
1997
- data
2015
+ data,
2016
+ context
1998
2017
  };
1999
2018
  let signal;
2000
2019
  switch (keyWord.value) {
@@ -2124,7 +2143,27 @@ class Interpreter {
2124
2143
  hookI = _this$tokenizer$_hook4[2];
2125
2144
  const rawVal = data[aoye.Keys.Raw][value];
2126
2145
  const isFn = typeof rawVal === 'function';
2127
- if (hookType === 'dynamic') {
2146
+ if (key === 'ref') {
2147
+ const valueIsMapKey = Reflect.has(data[aoye.Keys.Raw], value);
2148
+ let refValue = _node;
2149
+ if (_node.__logicType === FakeType.Component) {
2150
+ refValue = _node.data;
2151
+ } else {
2152
+ refValue[aoye.Keys.ProxyFreeObject] = true;
2153
+ }
2154
+ if (valueIsMapKey) {
2155
+ data[value] = refValue;
2156
+ new aoye.Scope(() => () => {
2157
+ data[value] = null;
2158
+ }).get();
2159
+ } else {
2160
+ const fn = this.getAssignFn(data, value);
2161
+ fn(refValue);
2162
+ new aoye.Scope(() => () => {
2163
+ fn(null);
2164
+ }).get();
2165
+ }
2166
+ } else if (hookType === 'dynamic') {
2128
2167
  const valueIsMapKey = Reflect.has(data[aoye.Keys.Raw], value);
2129
2168
  const fn = isFn ? rawVal : valueIsMapKey ? value : this.getFn(data, value);
2130
2169
  this.onePropParsed(data, _node, key, fn, valueIsMapKey, isFn, hookI);
@@ -2192,6 +2231,29 @@ class Interpreter {
2192
2231
  node.props[key] = value;
2193
2232
  }
2194
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
+ }
2195
2257
 
2196
2258
  function bobe(fragments, ...values) {
2197
2259
  const ui = function ui(isSub) {
@@ -2209,7 +2271,7 @@ function bobe(fragments, ...values) {
2209
2271
  function customRender(option) {
2210
2272
  return function render(Ctor, root) {
2211
2273
  const store = Ctor.new();
2212
- const tokenizer = store['ui'](false);
2274
+ const tokenizer = store.ui(false);
2213
2275
  const terp = new Interpreter(tokenizer);
2214
2276
  terp.config(option);
2215
2277
  const componentNode = {
@@ -2223,11 +2285,27 @@ function customRender(option) {
2223
2285
  };
2224
2286
  }
2225
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
+
2226
2303
  exports.Compiler = Compiler;
2227
2304
  exports.NodeType = NodeType;
2228
2305
  exports.ParseSyntaxError = ParseSyntaxError;
2229
2306
  exports.Tokenizer = Tokenizer;
2230
2307
  exports.bobe = bobe;
2308
+ exports.context = context;
2231
2309
  exports.customRender = customRender;
2232
2310
  Object.keys(aoye).forEach(function (k) {
2233
2311
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {