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