nesquick 2.0.2 → 2.2.0

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.
@@ -21,30 +21,75 @@ function getAttributeNs(attributes, k) {
21
21
  }
22
22
  return null;
23
23
  }
24
- function getterFromFunctions(props) {
25
- const res = Object.create(null);
24
+ function getterFromFunctionsSingleChildren(props) {
25
+ const res = {};
26
26
  for (const k in props) {
27
- Object.defineProperty(res, k, {
28
- get() {
29
- return props[k]();
27
+ if (k === "children") {
28
+ const v = props.children;
29
+ if (typeof v === "function" && !(v instanceof NesquickComponent)) {
30
+ Object.defineProperty(res, "children", {
31
+ get() {
32
+ return v();
33
+ }
34
+ });
35
+ }
36
+ else {
37
+ res.children = v;
38
+ }
39
+ }
40
+ else {
41
+ const v = props[k];
42
+ Object.defineProperty(res, k, {
43
+ get() {
44
+ return v();
45
+ }
46
+ });
47
+ }
48
+ }
49
+ return res;
50
+ }
51
+ function getterFromFunctionsArrayChildren(props) {
52
+ const res = {};
53
+ for (const k in props) {
54
+ if (k === "children") {
55
+ res.children = props.children;
56
+ for (let i = 0; i < props.children.length; i++) {
57
+ const v = props.children[i];
58
+ if (typeof v === "function" && !(v instanceof NesquickComponent)) {
59
+ Object.defineProperty(res.children, i, {
60
+ get() {
61
+ return v();
62
+ }
63
+ });
64
+ }
30
65
  }
31
- });
66
+ }
67
+ else {
68
+ const v = props[k];
69
+ Object.defineProperty(res, k, {
70
+ get() {
71
+ return v();
72
+ }
73
+ });
74
+ }
32
75
  }
33
76
  return res;
34
77
  }
35
78
  class NesquickComponent {
36
- constructor(_render, props) {
79
+ constructor(_render, props, jsxs = false) {
37
80
  this._render = _render;
38
81
  this.props = props;
82
+ this.jsxs = jsxs;
39
83
  this._subscriptions = new State_1.Subscriptions();
40
84
  this._styleSubscriptions = null;
41
85
  this._xmlns = null;
86
+ this._onUpdate = null;
42
87
  this._children = [];
43
88
  }
44
89
  render(document) {
45
90
  State_1.subscriptions.set(this._subscriptions);
46
91
  if (typeof this._render === "function") {
47
- this.props = getterFromFunctions(this.props);
92
+ this.props = this.jsxs ? getterFromFunctionsArrayChildren(this.props) : getterFromFunctionsSingleChildren(this.props);
48
93
  const element = this._render(this.props);
49
94
  if (this._xmlns) {
50
95
  element.setXmlns(this._xmlns);
@@ -88,8 +133,15 @@ class NesquickComponent {
88
133
  }
89
134
  }
90
135
  this._renderChildren(document, element, this.props.children);
91
- if (this.props.ref != null) {
92
- this.props.ref(element);
136
+ if (typeof this.props["nq:ref"] === "function") {
137
+ this.props["nq:ref"](element);
138
+ }
139
+ if (typeof this.props["nq:update"] === "function") {
140
+ this._onUpdate = {
141
+ waiting: true,
142
+ element: element,
143
+ cb: this.props["nq:update"]
144
+ };
93
145
  }
94
146
  this.props = {};
95
147
  State_1.subscriptions.reset();
@@ -98,9 +150,18 @@ class NesquickComponent {
98
150
  setXmlns(xmlns) {
99
151
  this._xmlns = xmlns;
100
152
  }
153
+ _onUpdated() {
154
+ if (this._onUpdate?.waiting) {
155
+ this._onUpdate.waiting = false;
156
+ (0, State_1.afterRender)(() => {
157
+ this._onUpdate.waiting = true;
158
+ this._onUpdate.cb(this._onUpdate.element);
159
+ });
160
+ }
161
+ }
101
162
  _renderPropsNs(attributes, element, props) {
102
163
  for (const k in props) {
103
- if (k !== "children" && k !== "xmlns" && k !== "ref") {
164
+ if (k !== "children" && k !== "xmlns" && k !== "nq:ref" && k !== "nq:update") {
104
165
  if (k === "style") {
105
166
  this._renderStyle(element, props[k]);
106
167
  }
@@ -113,11 +174,13 @@ class NesquickComponent {
113
174
  if (attribute) {
114
175
  (0, State_1.useRender)(props[k], v => {
115
176
  element.setAttributeNS(attribute.namespace, attribute.name, String(v));
177
+ this._onUpdated();
116
178
  });
117
179
  }
118
180
  else {
119
181
  (0, State_1.useRender)(props[k], v => {
120
182
  element.setAttribute(k, String(v));
183
+ this._onUpdated();
121
184
  });
122
185
  }
123
186
  }
@@ -137,7 +200,7 @@ class NesquickComponent {
137
200
  ;
138
201
  _renderProps(element, props) {
139
202
  for (const k in props) {
140
- if (k !== "children" && k !== "xmlns" && k !== "ref") {
203
+ if (k !== "children" && k !== "xmlns" && k !== "nq:ref" && k !== "nq:update") {
141
204
  if (k === "style") {
142
205
  this._renderStyle(element, props[k]);
143
206
  }
@@ -148,6 +211,7 @@ class NesquickComponent {
148
211
  else {
149
212
  (0, State_1.useRender)(props[k], v => {
150
213
  element.setAttribute(k, String(v));
214
+ this._onUpdated();
151
215
  });
152
216
  }
153
217
  }
@@ -162,6 +226,7 @@ class NesquickComponent {
162
226
  if (typeof styles[k] === "function") {
163
227
  (0, State_1.useRender)(styles[k], v => {
164
228
  element.style[k] = String(v);
229
+ this._onUpdated();
165
230
  });
166
231
  }
167
232
  else {
@@ -198,6 +263,7 @@ class NesquickComponent {
198
263
  break;
199
264
  }
200
265
  }
266
+ this._onUpdated();
201
267
  });
202
268
  break;
203
269
  }
@@ -237,6 +303,7 @@ class NesquickComponent {
237
303
  }
238
304
  this._renderChild(document, parent, ch, children);
239
305
  }
306
+ this._onUpdated();
240
307
  });
241
308
  }
242
309
  else {
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NesquickFragment = void 0;
4
4
  const NesquickComponent_1 = require("./NesquickComponent");
5
5
  class NesquickFragment extends NesquickComponent_1.NesquickComponent {
6
- constructor(children) {
7
- super("", { children });
6
+ constructor(children, jsxs = false) {
7
+ super("", { children }, jsxs);
8
8
  this._lastNode = null;
9
9
  this._fragment = null;
10
10
  }
package/lib/State.js CHANGED
@@ -4,6 +4,7 @@ exports.subscriptions = exports.Subscriptions = void 0;
4
4
  exports.useState = useState;
5
5
  exports.useEffect = useEffect;
6
6
  exports.useRender = useRender;
7
+ exports.afterRender = afterRender;
7
8
  exports.useMemo = useMemo;
8
9
  exports.useDispose = useDispose;
9
10
  let currentReactor = [];
@@ -19,6 +20,7 @@ var reactor;
19
20
  reactor_1.reset = reset;
20
21
  })(reactor || (reactor = {}));
21
22
  let pendingReactor = null;
23
+ const onRenders = [];
22
24
  function renderReactor(reactor, instant) {
23
25
  if (instant) {
24
26
  runSubscription(reactor);
@@ -49,6 +51,9 @@ function renderReactors() {
49
51
  next = n;
50
52
  } while (next);
51
53
  pendingReactor = null;
54
+ for (const cb of onRenders.splice(0)) {
55
+ cb();
56
+ }
52
57
  }
53
58
  class Subscriptions {
54
59
  constructor() {
@@ -109,6 +114,15 @@ function useRender(cb, reaction = null) {
109
114
  const sub = newSubscription(cb, reaction, false);
110
115
  runSubscription(sub);
111
116
  }
117
+ const NopSubscription = newSubscription(() => { }, null, false);
118
+ cancelSubscription(NopSubscription);
119
+ function afterRender(cb) {
120
+ onRenders.push(cb);
121
+ if (pendingReactor == null) {
122
+ NopSubscription.pending = false;
123
+ renderReactor(NopSubscription, false);
124
+ }
125
+ }
112
126
  function useMemo(cb) {
113
127
  const reactors = new Set();
114
128
  let state = 0;
@@ -7,6 +7,7 @@ function getSingleIdentifier(node) {
7
7
  node.forEachChild(node => {
8
8
  if (TS.isIdentifier(node)) {
9
9
  if (identifier != null) {
10
+ identifier = null;
10
11
  return node;
11
12
  }
12
13
  identifier = node;
@@ -53,6 +54,9 @@ function createSpreadCheckFunction(fName) {
53
54
  TS.factory.createReturnStatement(res)
54
55
  ], true));
55
56
  }
57
+ function arrowify(node) {
58
+ return TS.factory.createArrowFunction(void 0, void 0, [], void 0, TS.factory.createToken(TS.SyntaxKind.EqualsGreaterThanToken), node);
59
+ }
56
60
  const transformer = context => {
57
61
  return sourceFile => {
58
62
  let hasSpreadChecker = false;
@@ -67,7 +71,7 @@ const transformer = context => {
67
71
  return { node, hasCallExpression };
68
72
  };
69
73
  const visitGeneric = (node, options) => {
70
- let hasCallExpression = TS.isCallExpression(node);
74
+ let hasCallExpression = TS.isCallExpression(node) || TS.isElementAccessExpression(node) || TS.isPropertyAccessExpression(node);
71
75
  if (TS.isJsxSpreadAttribute(node)) {
72
76
  hasSpreadChecker = true;
73
77
  const expression = visitGeneric(node.expression, {}).node;
@@ -76,19 +80,12 @@ const transformer = context => {
76
80
  node = TS.factory.updateJsxSpreadAttribute(node, callExpression);
77
81
  }
78
82
  }
79
- else if (TS.isJsxElement(node)) {
80
- const firstLetter = node.openingElement.tagName.getText()[0];
83
+ else if (TS.isJsxElement(node) || TS.isJsxSelfClosingElement(node)) {
84
+ const firstLetter = TS.isJsxSelfClosingElement(node) ? node.tagName.getText()[0] : node.openingElement.tagName.getText()[0];
81
85
  const userComponent = firstLetter !== firstLetter.toLowerCase();
82
86
  const res = processNode(node, { userComponent });
83
87
  node = res.node;
84
88
  }
85
- else if (TS.isJsxOpeningLikeElement(node)) {
86
- const firstLetter = node.tagName.getText()[0];
87
- const userComponent = firstLetter !== firstLetter.toLowerCase();
88
- const res = processNode(node, { userComponent });
89
- node = res.node;
90
- hasCallExpression = hasCallExpression || res.hasCallExpression;
91
- }
92
89
  else if (TS.isJsxAttribute(node)) {
93
90
  node = TS.visitEachChild(node, node => {
94
91
  const res = visitGeneric(node, { ...options, isJsxAttribute: true });
@@ -108,6 +105,12 @@ const transformer = context => {
108
105
  node = TS.factory.createJsxExpression(void 0, returnNode);
109
106
  }
110
107
  }
108
+ else if (TS.isFunctionLike(node)) {
109
+ node = TS.visitEachChild(node, node => {
110
+ const res = visitGeneric(node, { ...options, isJsxAttribute: false });
111
+ return res.node;
112
+ }, context);
113
+ }
111
114
  else {
112
115
  node = TS.visitEachChild(node, node => {
113
116
  const res = visitGeneric(node, { ...options, isJsxAttribute: false });
@@ -115,6 +118,9 @@ const transformer = context => {
115
118
  return res.node;
116
119
  }, context);
117
120
  }
121
+ if (TS.isJsxChild(node) && !TS.isJsxText(node) && !TS.isJsxExpression(node) && options.userComponent) {
122
+ node = TS.factory.createJsxExpression(void 0, arrowify(node));
123
+ }
118
124
  return { node, hasCallExpression };
119
125
  };
120
126
  const visitorExpression = (node, options) => {
@@ -132,10 +138,8 @@ const transformer = context => {
132
138
  }
133
139
  const res = visitGeneric(node, {});
134
140
  node = res.node;
135
- if (TS.isExpression(node)) {
136
- if (options.userComponent || (res.hasCallExpression && !TS.isFunctionLike(node) && !TS.isJsxElement(node) && !TS.isJsxOpeningLikeElement(node))) {
137
- node = TS.factory.createArrowFunction(void 0, void 0, [], void 0, TS.factory.createToken(TS.SyntaxKind.EqualsGreaterThanToken), node);
138
- }
141
+ if (TS.isConciseBody(node) && (options.userComponent || res.hasCallExpression)) {
142
+ node = arrowify(node);
139
143
  }
140
144
  return node;
141
145
  };
@@ -7,22 +7,16 @@ const NesquickComponent_1 = require("./NesquickComponent");
7
7
  const NesquickFragment_1 = require("./NesquickFragment");
8
8
  exports.Fragment = Symbol();
9
9
  function jsxs(type, props, key) {
10
- if (type === exports.Fragment) {
11
- return new NesquickFragment_1.NesquickFragment(props.children);
12
- }
13
- if (key !== undefined) {
14
- props.key = key;
15
- }
16
- return new NesquickComponent_1.NesquickComponent(type, props);
10
+ return jsx(type, props, key, true);
17
11
  }
18
- function jsx(type, props, key) {
12
+ function jsx(type, props, key, jsxs = false) {
19
13
  if (type === exports.Fragment) {
20
- return new NesquickFragment_1.NesquickFragment([props.children]);
14
+ return new NesquickFragment_1.NesquickFragment([props.children], jsxs);
21
15
  }
22
16
  if (key !== undefined) {
23
17
  props.key = key;
24
18
  }
25
- return new NesquickComponent_1.NesquickComponent(type, props);
19
+ return new NesquickComponent_1.NesquickComponent(type, props, jsxs);
26
20
  }
27
21
  var JSX;
28
22
  (function (JSX) {
@@ -2,8 +2,8 @@ export type Child = NesquickComponent<any> | NesquickFragment | string | boolean
2
2
  export type Children = Child | Child[];
3
3
  export type ChildFunc = () => Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[];
4
4
  export type ComponentProps = Record<string, any>;
5
- export type FunctionComponent<P extends ComponentProps = {}> = (props: P) => NesquickComponent<P>;
6
- export type VeactDocument = Pick<Document, "createElement" | "createElementNS" | "createTextNode" | "createDocumentFragment" | "createComment">;
5
+ export type FunctionComponent<P extends ComponentProps = {}> = (props: P) => NesquickComponent<any>;
6
+ export type NesquickDocument = Pick<Document, "createElement" | "createElementNS" | "createTextNode" | "createDocumentFragment" | "createComment">;
7
7
  type NesquickChild = {
8
8
  node: Node | null;
9
9
  } & ({
@@ -25,23 +25,26 @@ type XmlNs = {
25
25
  export declare class NesquickComponent<P extends ComponentProps = {}> {
26
26
  private _render;
27
27
  protected props: P;
28
+ protected jsxs: boolean;
28
29
  private _subscriptions;
29
30
  private _styleSubscriptions;
30
31
  private _xmlns;
32
+ private _onUpdate;
31
33
  protected _children: NesquickChild[];
32
- constructor(_render: string | FunctionComponent<P>, props: P);
33
- render(document: VeactDocument): Node;
34
+ constructor(_render: string | FunctionComponent<P>, props: P, jsxs?: boolean);
35
+ render(document: NesquickDocument): Node;
34
36
  setXmlns(xmlns: XmlNs | null): void;
37
+ private _onUpdated;
35
38
  private _renderPropsNs;
36
39
  private _renderProps;
37
40
  private _renderStyles;
38
41
  private _renderStyle;
39
- protected _renderChildren(document: VeactDocument, parent: NesquickParent, children?: Children): void;
42
+ protected _renderChildren(document: NesquickDocument, parent: NesquickParent, children?: Children): void;
40
43
  protected _pushChild(): NesquickChild;
41
44
  protected _spliceChild(i: number): NesquickChild;
42
45
  protected _swapChilds(parent: NesquickParent, i1: number, i2: number): void;
43
46
  protected _removeChild(i: number): void;
44
- protected _renderChild(document: VeactDocument, parent: NesquickParent, nesquickChild: NesquickChild, child: Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[]): void;
47
+ protected _renderChild(document: NesquickDocument, parent: NesquickParent, nesquickChild: NesquickChild, child: Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[]): void;
45
48
  dispose(): void;
46
49
  }
47
50
  import { NesquickFragment } from "./NesquickFragment";
@@ -1,12 +1,12 @@
1
1
  import { JSX } from "./Nesquick";
2
- import { Child, NesquickComponent, NesquickParent, VeactDocument } from "./NesquickComponent";
2
+ import { Child, NesquickComponent, NesquickParent, NesquickDocument } from "./NesquickComponent";
3
3
  export declare class NesquickFragment extends NesquickComponent<{
4
4
  children: Child[];
5
5
  }> implements NesquickParent {
6
6
  private _lastNode;
7
7
  private _fragment;
8
- constructor(children: Child[]);
9
- render(document: VeactDocument): Node;
8
+ constructor(children: Child[], jsxs?: boolean);
9
+ render(document: NesquickDocument): Node;
10
10
  getDocument(): Document | null;
11
11
  getParent(): Node | null;
12
12
  appendChild(child: Node): void;
@@ -26,6 +26,7 @@ export declare namespace subscriptions {
26
26
  export declare function useState<T>(value: T): State<T>;
27
27
  export declare function useEffect<T>(cb: () => T, reaction?: ((data: T, lastReaction: boolean) => void) | null): void;
28
28
  export declare function useRender<T>(cb: () => T, reaction?: ((data: T, lastReaction: boolean) => void) | null): void;
29
+ export declare function afterRender(cb: () => void): void;
29
30
  export declare function useMemo<T>(cb: () => T): Getter<T>;
30
31
  export declare function useDispose(cb: () => void): void;
31
32
  export {};
@@ -2,7 +2,7 @@ import { FunctionComponent, ComponentProps, NesquickComponent } from "./Nesquick
2
2
  import { NesquickFragment } from "./NesquickFragment";
3
3
  export declare const Fragment: unique symbol;
4
4
  export declare function jsxs<P extends ComponentProps>(type: string | FunctionComponent<P> | typeof Fragment, props: P, key?: string | number | null): NesquickFragment | NesquickComponent<P>;
5
- export declare function jsx<P extends ComponentProps>(type: string | FunctionComponent<P> | typeof Fragment, props: P, key?: string | number | null): NesquickFragment | NesquickComponent<P>;
5
+ export declare function jsx<P extends ComponentProps>(type: string | FunctionComponent<P> | typeof Fragment, props: P, key?: string | number | null, jsxs?: boolean): NesquickFragment | NesquickComponent<P>;
6
6
  export type Component<P extends Record<any, any> = {}> = (props: P) => JSX.Element;
7
7
  export declare namespace JSX {
8
8
  export type JSXEvent<T extends Event, T2 extends EventTarget> = T & {
@@ -18,7 +18,8 @@ export declare namespace JSX {
18
18
  [k: string]: any;
19
19
  style?: Style;
20
20
  xmlns?: string | null;
21
- ref?: ((el: T) => void) | null;
21
+ "nq:ref"?: ((el: T) => void) | null;
22
+ "nq:update"?: ((el: T) => void) | null;
22
23
  }
23
24
  export type Style = StyleProps | string;
24
25
  export type StyleProps = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nesquick",
3
- "version": "2.0.2",
3
+ "version": "2.2.0",
4
4
  "description": "React-like library with focus on drawing performance",
5
5
  "types": "./lib/types/index.d.ts",
6
6
  "main": "./lib/index.js",