@viewfly/core 0.0.6 → 0.0.8

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.
@@ -29,7 +29,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
29
29
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
30
30
  PERFORMANCE OF THIS SOFTWARE.
31
31
  ***************************************************************************** */
32
- /* global Reflect, Promise */
32
+ /* global Reflect, Promise, SuppressedError, Symbol */
33
33
 
34
34
 
35
35
  function __decorate(decorators, target, key, desc) {
@@ -41,7 +41,12 @@ function __decorate(decorators, target, key, desc) {
41
41
 
42
42
  function __metadata(metadataKey, metadataValue) {
43
43
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
44
- }
44
+ }
45
+
46
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
47
+ var e = new Error(message);
48
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
49
+ };
45
50
 
46
51
  const refKey = 'ref';
47
52
  function getObjectChanges(newProps, oldProps) {
@@ -107,16 +112,6 @@ function styleToObject(style) {
107
112
  return obj;
108
113
  }
109
114
 
110
- class Memo {
111
- constructor(shouldUpdate, render) {
112
- this.shouldUpdate = shouldUpdate;
113
- this.render = render;
114
- }
115
- }
116
- function withMemo(shouldUpdate, render) {
117
- return new Memo(shouldUpdate, render);
118
- }
119
-
120
115
  const componentSetupStack = [];
121
116
  const signalDepsStack = [];
122
117
  const componentErrorFn = makeError('component');
@@ -193,11 +188,23 @@ class Component extends ReflectiveInjector {
193
188
  componentSetupStack.push(this);
194
189
  let isSetup = true;
195
190
  const render = this.type(props);
191
+ const isRenderFn = typeof render === 'function';
192
+ const componentInstance = isRenderFn ? { $render: render } : render;
193
+ let refs = toRefs(this.props.ref);
194
+ onMounted(() => {
195
+ for (const ref of refs) {
196
+ ref.bind(componentInstance);
197
+ }
198
+ });
199
+ onDestroy(() => {
200
+ for (const ref of refs) {
201
+ ref.unBind(componentInstance);
202
+ }
203
+ });
196
204
  isSetup = false;
197
205
  componentSetupStack.pop();
198
206
  signalDepsStack.push([]);
199
- const templateRender = render instanceof Memo ? render.render : render;
200
- let template = templateRender();
207
+ let template = componentInstance.$render();
201
208
  const deps = signalDepsStack.pop();
202
209
  this.unWatch = useEffect(deps, () => {
203
210
  this.markAsDirtied();
@@ -210,15 +217,27 @@ class Component extends ReflectiveInjector {
210
217
  if (add.length || remove.length || replace.length) {
211
218
  this.invokePropsChangedHooks(newProps);
212
219
  }
220
+ const newRefs = toRefs(newProps.ref);
221
+ for (const oldRef of refs) {
222
+ if (!newRefs.includes(oldRef)) {
223
+ oldRef.unBind(componentInstance);
224
+ }
225
+ }
226
+ for (const newRef of newRefs) {
227
+ if (!refs.includes(newRef)) {
228
+ newRef.bind(componentInstance);
229
+ }
230
+ }
231
+ refs = newRefs;
213
232
  }
214
- if (render instanceof Memo) {
215
- if (!render.shouldUpdate(newProps, oldProps)) {
233
+ if (typeof componentInstance.$shouldUpdate === 'function') {
234
+ if (!componentInstance.$shouldUpdate(newProps, oldProps)) {
216
235
  return template;
217
236
  }
218
237
  }
219
238
  this.unWatch();
220
239
  signalDepsStack.push([]);
221
- template = templateRender();
240
+ template = componentInstance.$render();
222
241
  const deps = signalDepsStack.pop();
223
242
  this.unWatch = useEffect(deps, () => {
224
243
  this.markAsDirtied();
@@ -302,6 +321,11 @@ class Component extends ReflectiveInjector {
302
321
  }
303
322
  }
304
323
  }
324
+ function toRefs(ref) {
325
+ return (Array.isArray(ref) ? ref : [ref]).filter(i => {
326
+ return i instanceof Ref;
327
+ });
328
+ }
305
329
  /**
306
330
  * 当组件第一次渲染完成时触发
307
331
  * @param callback
@@ -383,7 +407,7 @@ function onDestroy(callback) {
383
407
  class Ref {
384
408
  constructor(callback) {
385
409
  this.callback = callback;
386
- this.unBindMap = new WeakMap();
410
+ this.unBindMap = new Map();
387
411
  this.targetCaches = new Set();
388
412
  }
389
413
  bind(value) {
@@ -626,6 +650,13 @@ class JSXElement {
626
650
  }
627
651
  }
628
652
 
653
+ function withMemo(shouldUpdate, render) {
654
+ return {
655
+ $shouldUpdate: shouldUpdate,
656
+ $render: render
657
+ };
658
+ }
659
+
629
660
  /**
630
661
  * Viewfly 根组件,用于实现组件状态更新事件通知
631
662
  */
@@ -1293,4 +1324,4 @@ class Viewfly extends ReflectiveInjector {
1293
1324
  }
1294
1325
  }
1295
1326
 
1296
- export { Component, Fragment, JSXComponent, JSXElement, JSXText, Memo, NativeRenderer, Ref, Renderer, RootComponent, RootComponentRef, Viewfly, inject, jsx, jsxs, makeError, onDestroy, onMounted, onPropsChanged, onUpdated, provide, useDerived, useEffect, useRef, useSignal, withMemo };
1327
+ export { Component, Fragment, JSXComponent, JSXElement, JSXText, NativeRenderer, Ref, Renderer, RootComponent, RootComponentRef, Viewfly, inject, jsx, jsxs, makeError, onDestroy, onMounted, onPropsChanged, onUpdated, provide, useDerived, useEffect, useRef, useSignal, withMemo };
package/bundles/index.js CHANGED
@@ -30,7 +30,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
30
30
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31
31
  PERFORMANCE OF THIS SOFTWARE.
32
32
  ***************************************************************************** */
33
- /* global Reflect, Promise */
33
+ /* global Reflect, Promise, SuppressedError, Symbol */
34
34
 
35
35
 
36
36
  function __decorate(decorators, target, key, desc) {
@@ -42,7 +42,12 @@ function __decorate(decorators, target, key, desc) {
42
42
 
43
43
  function __metadata(metadataKey, metadataValue) {
44
44
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
45
- }
45
+ }
46
+
47
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
48
+ var e = new Error(message);
49
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
50
+ };
46
51
 
47
52
  const refKey = 'ref';
48
53
  function getObjectChanges(newProps, oldProps) {
@@ -108,16 +113,6 @@ function styleToObject(style) {
108
113
  return obj;
109
114
  }
110
115
 
111
- class Memo {
112
- constructor(shouldUpdate, render) {
113
- this.shouldUpdate = shouldUpdate;
114
- this.render = render;
115
- }
116
- }
117
- function withMemo(shouldUpdate, render) {
118
- return new Memo(shouldUpdate, render);
119
- }
120
-
121
116
  const componentSetupStack = [];
122
117
  const signalDepsStack = [];
123
118
  const componentErrorFn = makeError('component');
@@ -194,11 +189,23 @@ class Component extends di.ReflectiveInjector {
194
189
  componentSetupStack.push(this);
195
190
  let isSetup = true;
196
191
  const render = this.type(props);
192
+ const isRenderFn = typeof render === 'function';
193
+ const componentInstance = isRenderFn ? { $render: render } : render;
194
+ let refs = toRefs(this.props.ref);
195
+ onMounted(() => {
196
+ for (const ref of refs) {
197
+ ref.bind(componentInstance);
198
+ }
199
+ });
200
+ onDestroy(() => {
201
+ for (const ref of refs) {
202
+ ref.unBind(componentInstance);
203
+ }
204
+ });
197
205
  isSetup = false;
198
206
  componentSetupStack.pop();
199
207
  signalDepsStack.push([]);
200
- const templateRender = render instanceof Memo ? render.render : render;
201
- let template = templateRender();
208
+ let template = componentInstance.$render();
202
209
  const deps = signalDepsStack.pop();
203
210
  this.unWatch = useEffect(deps, () => {
204
211
  this.markAsDirtied();
@@ -211,15 +218,27 @@ class Component extends di.ReflectiveInjector {
211
218
  if (add.length || remove.length || replace.length) {
212
219
  this.invokePropsChangedHooks(newProps);
213
220
  }
221
+ const newRefs = toRefs(newProps.ref);
222
+ for (const oldRef of refs) {
223
+ if (!newRefs.includes(oldRef)) {
224
+ oldRef.unBind(componentInstance);
225
+ }
226
+ }
227
+ for (const newRef of newRefs) {
228
+ if (!refs.includes(newRef)) {
229
+ newRef.bind(componentInstance);
230
+ }
231
+ }
232
+ refs = newRefs;
214
233
  }
215
- if (render instanceof Memo) {
216
- if (!render.shouldUpdate(newProps, oldProps)) {
234
+ if (typeof componentInstance.$shouldUpdate === 'function') {
235
+ if (!componentInstance.$shouldUpdate(newProps, oldProps)) {
217
236
  return template;
218
237
  }
219
238
  }
220
239
  this.unWatch();
221
240
  signalDepsStack.push([]);
222
- template = templateRender();
241
+ template = componentInstance.$render();
223
242
  const deps = signalDepsStack.pop();
224
243
  this.unWatch = useEffect(deps, () => {
225
244
  this.markAsDirtied();
@@ -303,6 +322,11 @@ class Component extends di.ReflectiveInjector {
303
322
  }
304
323
  }
305
324
  }
325
+ function toRefs(ref) {
326
+ return (Array.isArray(ref) ? ref : [ref]).filter(i => {
327
+ return i instanceof Ref;
328
+ });
329
+ }
306
330
  /**
307
331
  * 当组件第一次渲染完成时触发
308
332
  * @param callback
@@ -384,7 +408,7 @@ function onDestroy(callback) {
384
408
  class Ref {
385
409
  constructor(callback) {
386
410
  this.callback = callback;
387
- this.unBindMap = new WeakMap();
411
+ this.unBindMap = new Map();
388
412
  this.targetCaches = new Set();
389
413
  }
390
414
  bind(value) {
@@ -627,6 +651,13 @@ class JSXElement {
627
651
  }
628
652
  }
629
653
 
654
+ function withMemo(shouldUpdate, render) {
655
+ return {
656
+ $shouldUpdate: shouldUpdate,
657
+ $render: render
658
+ };
659
+ }
660
+
630
661
  /**
631
662
  * Viewfly 根组件,用于实现组件状态更新事件通知
632
663
  */
@@ -1299,7 +1330,6 @@ exports.Fragment = Fragment;
1299
1330
  exports.JSXComponent = JSXComponent;
1300
1331
  exports.JSXElement = JSXElement;
1301
1332
  exports.JSXText = JSXText;
1302
- exports.Memo = Memo;
1303
1333
  exports.NativeRenderer = NativeRenderer;
1304
1334
  exports.Ref = Ref;
1305
1335
  exports.RootComponent = RootComponent;
@@ -1320,7 +1350,7 @@ exports.useRef = useRef;
1320
1350
  exports.useSignal = useSignal;
1321
1351
  exports.withMemo = withMemo;
1322
1352
  Object.keys(di).forEach(function (k) {
1323
- if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
1353
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1324
1354
  enumerable: true,
1325
1355
  get: function () { return di[k]; }
1326
1356
  });
@@ -1,14 +1,20 @@
1
1
  import { Provider, ReflectiveInjector, AbstractType, Type, InjectionToken, InjectFlags, Injector } from '@tanbo/di';
2
2
  import { Props, Key, JSXTypeof, JSXChildNode } from './jsx-element';
3
- import { Memo } from './memo';
4
3
  export declare class JSXComponent {
5
4
  props: Props;
6
5
  private factory;
7
6
  constructor(props: Props, factory: (injector: Component, props: Props) => Component);
8
7
  createInstance(injector: Component): Component;
9
8
  }
9
+ export interface ComponentInstance<T> {
10
+ $render(): JSXChildNode;
11
+ $shouldUpdate?(currentProps: T, prevProps: T): unknown;
12
+ }
13
+ export interface Renderable {
14
+ $render(): any;
15
+ }
10
16
  export interface ComponentSetup<T extends Props<any> = Props<any>> {
11
- (props?: T): (() => JSXChildNode) | Memo<T>;
17
+ (props?: T): (() => JSXChildNode) | ComponentInstance<T>;
12
18
  }
13
19
  /**
14
20
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
@@ -109,13 +115,17 @@ export declare function onDestroy(callback: () => void): void;
109
115
  export interface RefListener<T> {
110
116
  (current: T): void | (() => void);
111
117
  }
112
- export declare class Ref<T extends object> {
118
+ export type ExtractInstanceType<T, U = T extends (...args: any) => any ? ReturnType<T> : T> = U extends Renderable ? Omit<U, keyof ComponentInstance<any>> : U extends Function ? never : T;
119
+ export interface AbstractInstanceType<T extends Record<string, any>> {
120
+ (): T & Renderable;
121
+ }
122
+ export declare class Ref<T, U> {
113
123
  private callback;
114
124
  private unBindMap;
115
125
  private targetCaches;
116
- constructor(callback: RefListener<T>);
117
- bind(value: T): void;
118
- unBind(value: T): void;
126
+ constructor(callback: RefListener<U>);
127
+ bind(value: U): void;
128
+ unBind(value: U): void;
119
129
  }
120
130
  /**
121
131
  * 用于节点渲染完成时获取 DOM 节点
@@ -138,7 +148,7 @@ export declare class Ref<T extends object> {
138
148
  * }
139
149
  * ```
140
150
  */
141
- export declare function useRef<T extends object>(callback: RefListener<T>): Ref<T>;
151
+ export declare function useRef<T, U = ExtractInstanceType<T>>(callback: RefListener<U>): Ref<T, U>;
142
152
  declare const depsKey: unique symbol;
143
153
  /**
144
154
  * 组件状态实例,直接调用可以获取最新的状态,通过 set 方法可以更新状态
@@ -1,10 +1,6 @@
1
1
  import { JSXChildNode, Props } from './jsx-element';
2
+ import { ComponentInstance } from './component';
2
3
  export interface ShouldUpdate<T extends Props> {
3
4
  (currentProps: T, prevProps: T): unknown;
4
5
  }
5
- export declare class Memo<T extends Props> {
6
- shouldUpdate: ShouldUpdate<T>;
7
- render: () => JSXChildNode;
8
- constructor(shouldUpdate: ShouldUpdate<T>, render: () => JSXChildNode);
9
- }
10
- export declare function withMemo<T extends Props = Props>(shouldUpdate: ShouldUpdate<T>, render: () => JSXChildNode): Memo<T>;
6
+ export declare function withMemo<T extends Props = Props>(shouldUpdate: ShouldUpdate<T>, render: () => JSXChildNode): ComponentInstance<T>;
@@ -1,11 +1,12 @@
1
- import { Key, Ref, JSXComponent } from '@viewfly/core';
1
+ import { Key, Ref, JSXComponent, ExtractInstanceType } from '@viewfly/core';
2
2
  export declare namespace ViewTypes {
3
3
  type ClassNames = string | Record<string, unknown> | Array<string | Record<string, unknown>>;
4
4
  interface IntrinsicAttributes {
5
5
  key?: Key;
6
+ ref?: any;
6
7
  }
7
- interface RefAttributes<T extends object> extends IntrinsicAttributes {
8
- ref?: Ref<T> | Ref<T>[];
8
+ interface RefAttributes<T> extends IntrinsicAttributes {
9
+ ref?: Ref<T, ExtractInstanceType<T>> | Ref<T, ExtractInstanceType<T>>[];
9
10
  }
10
11
  interface ElementClass extends JSXComponent {
11
12
  }
@@ -0,0 +1,21 @@
1
+ import { jsx, jsxs, Fragment, ViewTypes } from '@viewfly/core';
2
+ import { NativeElements } from '@viewfly/platform-browser';
3
+ /**
4
+ * JSX namespace for usage with @jsxImportsSource directive
5
+ * when ts compilerOptions.jsx is 'react-jsx'
6
+ * https://www.typescriptlang.org/tsconfig#jsxImportSource
7
+ */
8
+ declare const jsxDEV: any;
9
+ export { jsx, jsxs, Fragment, jsxDEV };
10
+ export declare namespace JSX {
11
+ interface ElementClass extends ViewTypes.ElementClass {
12
+ }
13
+ interface IntrinsicElements extends NativeElements, ViewTypes.IntrinsicElements {
14
+ }
15
+ interface IntrinsicAttributes extends ViewTypes.IntrinsicAttributes {
16
+ }
17
+ interface ElementChildrenAttribute extends ViewTypes.ElementChildrenAttribute {
18
+ }
19
+ interface IntrinsicClassAttributes<T> extends ViewTypes.IntrinsicClassAttributes<T> {
20
+ }
21
+ }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "main": "./index.js",
3
3
  "module": "./index.esm.js",
4
- "types": "./index.d.ts"
4
+ "types": "./jsx.d.ts"
5
5
  }
@@ -1,5 +1,5 @@
1
- import type { jsx, jsxs, Fragment, ViewTypes } from '@viewfly/core'
2
- import type { NativeElements } from '@viewfly/platform-browser'
1
+ import { jsx, jsxs, Fragment, ViewTypes } from '@viewfly/core'
2
+ import { NativeElements } from '@viewfly/platform-browser'
3
3
 
4
4
  /**
5
5
  * JSX namespace for usage with @jsxImportsSource directive
@@ -7,7 +7,10 @@ import type { NativeElements } from '@viewfly/platform-browser'
7
7
  * https://www.typescriptlang.org/tsconfig#jsxImportSource
8
8
  */
9
9
 
10
- export { jsx, jsxs, Fragment }
10
+ const jsxDEV = jsx
11
+
12
+ export { jsx, jsxs, Fragment, jsxDEV }
13
+
11
14
 
12
15
  export namespace JSX {
13
16
  export interface ElementClass extends ViewTypes.ElementClass {
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@viewfly/core",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "Viewfly is a simple and easy-to-use JavaScript framework with an intuitive development experience.",
5
5
  "main": "./bundles/index.js",
6
6
  "module": "./bundles/index.esm.js",
7
7
  "typings": "./bundles/public-api.d.ts",
8
8
  "scripts": {
9
- "build:lib": "rimraf bundles && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
9
+ "build:lib": "rimraf bundles && npm run build:jsx && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
10
+ "build:jsx": "rimraf jsx-runtime/index.* && rollup --config rollup-jsx.config.ts --configPlugin @rollup/plugin-typescript",
10
11
  "publish:lib": "npm run build:lib && npm publish --access=public"
11
12
  },
12
13
  "license": "MIT",
@@ -17,11 +18,11 @@
17
18
  "reflect-metadata": "^0.1.13"
18
19
  },
19
20
  "devDependencies": {
20
- "@rollup/plugin-commonjs": "^23.0.2",
21
- "@rollup/plugin-typescript": "^9.0.2",
21
+ "@rollup/plugin-commonjs": "^25.0.3",
22
+ "@rollup/plugin-typescript": "^11.1.2",
22
23
  "rimraf": "^3.0.2",
23
- "rollup": "^3.2.5",
24
- "tslib": "^2.4.1"
24
+ "rollup": "^3.26.3",
25
+ "tslib": "^2.6.0"
25
26
  },
26
27
  "author": {
27
28
  "name": "Tanbo",
@@ -34,5 +35,5 @@
34
35
  "bugs": {
35
36
  "url": "https://github.com/viewfly/viewfly.git/issues"
36
37
  },
37
- "gitHead": "75d56812c92bfd5d099e0d7eb8d964251d88df28"
38
+ "gitHead": "cd3ad7ebfa4206f968844d0e3be6462b182b95b3"
38
39
  }
@@ -0,0 +1,25 @@
1
+ import commonjs from '@rollup/plugin-commonjs'
2
+ import typescript from '@rollup/plugin-typescript'
3
+
4
+ export default {
5
+ input: 'jsx.ts',
6
+ output: [
7
+ {
8
+ file: './jsx-runtime/index.js',
9
+ format: 'cjs'
10
+ },
11
+ {
12
+ file: './jsx-runtime/index.esm.js',
13
+ format: 'esm'
14
+ }
15
+ ],
16
+ plugins: [
17
+ commonjs(),
18
+ typescript({
19
+ tsconfig: './tsconfig-jsx.json',
20
+ compilerOptions: {
21
+ paths: {}
22
+ }
23
+ })
24
+ ]
25
+ }
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "declarationDir": "./jsx-runtime",
5
+ "useDefineForClassFields": false,
6
+ "emitDecoratorMetadata": true,
7
+ "experimentalDecorators": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "lib": [
10
+ "esnext",
11
+ "dom"
12
+ ],
13
+ "target": "es6",
14
+ "strict": true,
15
+ "module": "es2020",
16
+ "moduleResolution": "node",
17
+ "inlineSourceMap": true,
18
+ "inlineSources": true,
19
+ "noImplicitAny": false,
20
+ "outDir": "jsx-runtime/",
21
+ "downlevelIteration": true,
22
+ "paths": {
23
+ "@viewfly/core": [
24
+ "./src/public-api.ts"
25
+ ]
26
+ }
27
+ },
28
+ "include": [
29
+ "jsx.ts"
30
+ ]
31
+ }
@@ -1,7 +0,0 @@
1
- import { jsx, jsxs, Fragment } from '@viewfly/core'
2
-
3
- export {
4
- jsxs,
5
- jsx,
6
- Fragment
7
- }
@@ -1,17 +0,0 @@
1
- (function (factory) {
2
- if (typeof module === 'object' && typeof module.exports === 'object') {
3
- var v = factory(require, exports)
4
- if (v !== undefined) module.exports = v
5
- }
6
- else if (typeof define === 'function' && define.amd) {
7
- define(['require', 'exports', '@viewfly/core'], factory)
8
- }
9
- })(function (require, exports) {
10
- 'use strict'
11
- Object.defineProperty(exports, '__esModule', { value: true })
12
- exports.Fragment = exports.jsx = exports.jsxs = void 0
13
- const core_1 = require('@viewfly/core')
14
- Object.defineProperty(exports, 'jsx', { enumerable: true, get: function () { return core_1.jsx } })
15
- Object.defineProperty(exports, 'jsxs', { enumerable: true, get: function () { return core_1.jsxs } })
16
- Object.defineProperty(exports, 'Fragment', { enumerable: true, get: function () { return core_1.Fragment } })
17
- })