@viewfly/core 0.0.31 → 0.1.1

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.
@@ -11,7 +11,7 @@ export declare class Component extends ReflectiveInjector implements JSXTypeof<J
11
11
  props: Props;
12
12
  key?: Key | undefined;
13
13
  $$typeOf: JSXInternal.ComponentSetup<any>;
14
- destroyCallbacks: LifeCycleCallback[];
14
+ unmountedCallbacks: LifeCycleCallback[];
15
15
  mountCallbacks: LifeCycleCallback[];
16
16
  propsChangedCallbacks: PropsChangedCallback<any>[];
17
17
  updatedCallbacks: LifeCycleCallback[];
@@ -99,7 +99,7 @@ export declare function onPropsChanged<T extends Props>(callback: PropsChangedCa
99
99
  * 当组件销毁时调用回调函数
100
100
  * @param callback
101
101
  */
102
- export declare function onDestroy(callback: () => void): void;
102
+ export declare function onUnmounted(callback: () => void): void;
103
103
  export interface RefListener<T> {
104
104
  (current: T): void | (() => void);
105
105
  }
@@ -143,6 +143,7 @@ declare const depsKey: unique symbol;
143
143
  * ```
144
144
  */
145
145
  export interface Signal<T> {
146
+ $isSignal: true;
146
147
  /**
147
148
  * 直接调用一个 Signal 实例,可以获取最新状态
148
149
  */
@@ -663,7 +663,7 @@ class Component extends ReflectiveInjector {
663
663
  this.props = props;
664
664
  this.key = key;
665
665
  this.$$typeOf = this.type;
666
- this.destroyCallbacks = [];
666
+ this.unmountedCallbacks = [];
667
667
  this.mountCallbacks = [];
668
668
  this.propsChangedCallbacks = [];
669
669
  this.updatedCallbacks = [];
@@ -716,7 +716,7 @@ class Component extends ReflectiveInjector {
716
716
  ref.bind(this.instance);
717
717
  }
718
718
  });
719
- this.destroyCallbacks.push(() => {
719
+ this.unmountedCallbacks.push(() => {
720
720
  for (const ref of this.refs) {
721
721
  ref.unBind(this.instance);
722
722
  }
@@ -738,6 +738,9 @@ class Component extends ReflectiveInjector {
738
738
  if (add.length || remove.length || replace.length) {
739
739
  this.invokePropsChangedHooks(newProps);
740
740
  }
741
+ else if (!this.dirty) {
742
+ return this.template;
743
+ }
741
744
  const newRefs = toRefs(newProps.ref);
742
745
  for (const oldRef of this.refs) {
743
746
  if (!newRefs.includes(oldRef)) {
@@ -791,7 +794,7 @@ class Component extends ReflectiveInjector {
791
794
  fn();
792
795
  });
793
796
  this.propsChangedDestroyCallbacks = [];
794
- for (const fn of this.destroyCallbacks) {
797
+ for (const fn of this.unmountedCallbacks) {
795
798
  fn();
796
799
  }
797
800
  this.updatedCallbacks = [];
@@ -816,7 +819,7 @@ class Component extends ReflectiveInjector {
816
819
  for (const fn of this.mountCallbacks) {
817
820
  const destroyFn = fn();
818
821
  if (typeof destroyFn === 'function') {
819
- this.destroyCallbacks.push(destroyFn);
822
+ this.unmountedCallbacks.push(destroyFn);
820
823
  }
821
824
  }
822
825
  }
@@ -912,9 +915,9 @@ function onPropsChanged(callback) {
912
915
  * 当组件销毁时调用回调函数
913
916
  * @param callback
914
917
  */
915
- function onDestroy(callback) {
918
+ function onUnmounted(callback) {
916
919
  const component = getSetupContext();
917
- component.destroyCallbacks.push(callback);
920
+ component.unmountedCallbacks.push(callback);
918
921
  }
919
922
  class Ref {
920
923
  constructor(callback) {
@@ -1000,6 +1003,7 @@ function useSignal(state) {
1000
1003
  }
1001
1004
  return state;
1002
1005
  }
1006
+ signal.$isSignal = true;
1003
1007
  signal.set = function (newState) {
1004
1008
  if (newState === state) {
1005
1009
  return;
@@ -1011,6 +1015,14 @@ function useSignal(state) {
1011
1015
  fn();
1012
1016
  }
1013
1017
  };
1018
+ //
1019
+ // signal.toString = function () {
1020
+ // return String(state)
1021
+ // }
1022
+ //
1023
+ // signal.valueOf = function () {
1024
+ // return state
1025
+ // }
1014
1026
  signal[depsKey] = new Set();
1015
1027
  return signal;
1016
1028
  }
@@ -1075,7 +1087,7 @@ function useDerived(callback, isContinue) {
1075
1087
  const component = getSetupContext(false);
1076
1088
  const unListen = listen(signal, deps, callback, isContinue);
1077
1089
  if (component) {
1078
- component.destroyCallbacks.push(() => {
1090
+ component.unmountedCallbacks.push(() => {
1079
1091
  unListen();
1080
1092
  });
1081
1093
  }
@@ -1083,9 +1095,7 @@ function useDerived(callback, isContinue) {
1083
1095
  }
1084
1096
  /* eslint-enable max-len*/
1085
1097
  function useEffect(deps, effect) {
1086
- if (typeof deps === 'function' &&
1087
- typeof deps.set === 'undefined' &&
1088
- typeof deps[depsKey] === 'undefined') {
1098
+ if (typeof deps === 'function' && !deps.$isSignal) {
1089
1099
  deps = useDerived(deps);
1090
1100
  }
1091
1101
  const signals = Array.isArray(deps) ? deps : [deps];
@@ -1110,15 +1120,15 @@ function useEffect(deps, effect) {
1110
1120
  }
1111
1121
  isClean = true;
1112
1122
  if (component) {
1113
- const index = component.destroyCallbacks.indexOf(destroyFn);
1114
- component.destroyCallbacks.splice(index, 1);
1123
+ const index = component.unmountedCallbacks.indexOf(destroyFn);
1124
+ component.unmountedCallbacks.splice(index, 1);
1115
1125
  }
1116
1126
  for (const dep of signals) {
1117
1127
  dep[depsKey].delete(effectCallback);
1118
1128
  }
1119
1129
  };
1120
1130
  if (component) {
1121
- component.destroyCallbacks.push(destroyFn);
1131
+ component.unmountedCallbacks.push(destroyFn);
1122
1132
  }
1123
1133
  return destroyFn;
1124
1134
  }
@@ -1330,6 +1340,7 @@ function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context, expect
1330
1340
  newAtom.jsxNode = instance;
1331
1341
  if (newTemplate === oldTemplate) {
1332
1342
  reuseComponentView(nativeRenderer, newAtom, reusedAtom, context, expectIndex !== diffIndex - offset);
1343
+ updateView(nativeRenderer, instance);
1333
1344
  return;
1334
1345
  }
1335
1346
  if (newTemplate) {
@@ -1789,8 +1800,10 @@ class RootComponent extends Component {
1789
1800
  }
1790
1801
 
1791
1802
  const viewflyErrorFn = makeError('Viewfly');
1792
- function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1803
+ function viewfly(config) {
1804
+ const { context, nativeRenderer, autoUpdate, root } = Object.assign({ autoUpdate: true }, config);
1793
1805
  const appProviders = [];
1806
+ const modules = [];
1794
1807
  let destroyed = false;
1795
1808
  const rootComponent = new RootComponent(context || null, () => {
1796
1809
  provide(appProviders);
@@ -1820,13 +1833,29 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1820
1833
  }
1821
1834
  return app;
1822
1835
  },
1836
+ use(module) {
1837
+ if (Array.isArray(module)) {
1838
+ modules.push(...module);
1839
+ }
1840
+ else {
1841
+ modules.push(module);
1842
+ }
1843
+ return app;
1844
+ },
1823
1845
  mount(host) {
1846
+ var _a, _b;
1824
1847
  if (isStarted) {
1825
1848
  throw viewflyErrorFn('application has already started.');
1826
1849
  }
1850
+ for (const module of modules) {
1851
+ (_a = module.setup) === null || _a === void 0 ? void 0 : _a.call(module, app);
1852
+ }
1827
1853
  isStarted = true;
1828
1854
  appHost = host;
1829
1855
  render(host);
1856
+ for (const module of modules) {
1857
+ (_b = module.onAfterStartup) === null || _b === void 0 ? void 0 : _b.call(module, app);
1858
+ }
1830
1859
  if (!autoUpdate) {
1831
1860
  return app;
1832
1861
  }
@@ -1848,12 +1877,16 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1848
1877
  return app;
1849
1878
  },
1850
1879
  destroy() {
1880
+ var _a;
1851
1881
  destroyed = true;
1852
1882
  rootComponent.markAsDirtied();
1853
1883
  app.render();
1884
+ for (const module of modules) {
1885
+ (_a = module.onDestroy) === null || _a === void 0 ? void 0 : _a.call(module);
1886
+ }
1854
1887
  }
1855
1888
  };
1856
1889
  return app;
1857
1890
  }
1858
1891
 
1859
- export { Component, ForwardRef, Fragment, Inject, InjectFlags, Injectable, InjectionToken, Injector, JSXComponent, JSXElement, JSXText, NativeRenderer, NullInjector, Optional, Prop, Ref, ReflectiveInjector, RootComponent, Scope, Self, SkipSelf, THROW_IF_NOT_FOUND, Type, createRenderer, forwardRef, inject, jsx, jsxs, makeError, normalizeProvider, onDestroy, onMounted, onPropsChanged, onUpdated, provide, useDerived, useEffect, useRef, useSignal, viewfly, withMemo };
1892
+ export { Component, ForwardRef, Fragment, Inject, InjectFlags, Injectable, InjectionToken, Injector, JSXComponent, JSXElement, JSXText, NativeRenderer, NullInjector, Optional, Prop, Ref, ReflectiveInjector, RootComponent, Scope, Self, SkipSelf, THROW_IF_NOT_FOUND, Type, createRenderer, forwardRef, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, provide, useDerived, useEffect, useRef, useSignal, viewfly, withMemo };
package/bundles/index.js CHANGED
@@ -665,7 +665,7 @@ class Component extends ReflectiveInjector {
665
665
  this.props = props;
666
666
  this.key = key;
667
667
  this.$$typeOf = this.type;
668
- this.destroyCallbacks = [];
668
+ this.unmountedCallbacks = [];
669
669
  this.mountCallbacks = [];
670
670
  this.propsChangedCallbacks = [];
671
671
  this.updatedCallbacks = [];
@@ -718,7 +718,7 @@ class Component extends ReflectiveInjector {
718
718
  ref.bind(this.instance);
719
719
  }
720
720
  });
721
- this.destroyCallbacks.push(() => {
721
+ this.unmountedCallbacks.push(() => {
722
722
  for (const ref of this.refs) {
723
723
  ref.unBind(this.instance);
724
724
  }
@@ -740,6 +740,9 @@ class Component extends ReflectiveInjector {
740
740
  if (add.length || remove.length || replace.length) {
741
741
  this.invokePropsChangedHooks(newProps);
742
742
  }
743
+ else if (!this.dirty) {
744
+ return this.template;
745
+ }
743
746
  const newRefs = toRefs(newProps.ref);
744
747
  for (const oldRef of this.refs) {
745
748
  if (!newRefs.includes(oldRef)) {
@@ -793,7 +796,7 @@ class Component extends ReflectiveInjector {
793
796
  fn();
794
797
  });
795
798
  this.propsChangedDestroyCallbacks = [];
796
- for (const fn of this.destroyCallbacks) {
799
+ for (const fn of this.unmountedCallbacks) {
797
800
  fn();
798
801
  }
799
802
  this.updatedCallbacks = [];
@@ -818,7 +821,7 @@ class Component extends ReflectiveInjector {
818
821
  for (const fn of this.mountCallbacks) {
819
822
  const destroyFn = fn();
820
823
  if (typeof destroyFn === 'function') {
821
- this.destroyCallbacks.push(destroyFn);
824
+ this.unmountedCallbacks.push(destroyFn);
822
825
  }
823
826
  }
824
827
  }
@@ -914,9 +917,9 @@ function onPropsChanged(callback) {
914
917
  * 当组件销毁时调用回调函数
915
918
  * @param callback
916
919
  */
917
- function onDestroy(callback) {
920
+ function onUnmounted(callback) {
918
921
  const component = getSetupContext();
919
- component.destroyCallbacks.push(callback);
922
+ component.unmountedCallbacks.push(callback);
920
923
  }
921
924
  class Ref {
922
925
  constructor(callback) {
@@ -1002,6 +1005,7 @@ function useSignal(state) {
1002
1005
  }
1003
1006
  return state;
1004
1007
  }
1008
+ signal.$isSignal = true;
1005
1009
  signal.set = function (newState) {
1006
1010
  if (newState === state) {
1007
1011
  return;
@@ -1013,6 +1017,14 @@ function useSignal(state) {
1013
1017
  fn();
1014
1018
  }
1015
1019
  };
1020
+ //
1021
+ // signal.toString = function () {
1022
+ // return String(state)
1023
+ // }
1024
+ //
1025
+ // signal.valueOf = function () {
1026
+ // return state
1027
+ // }
1016
1028
  signal[depsKey] = new Set();
1017
1029
  return signal;
1018
1030
  }
@@ -1077,7 +1089,7 @@ function useDerived(callback, isContinue) {
1077
1089
  const component = getSetupContext(false);
1078
1090
  const unListen = listen(signal, deps, callback, isContinue);
1079
1091
  if (component) {
1080
- component.destroyCallbacks.push(() => {
1092
+ component.unmountedCallbacks.push(() => {
1081
1093
  unListen();
1082
1094
  });
1083
1095
  }
@@ -1085,9 +1097,7 @@ function useDerived(callback, isContinue) {
1085
1097
  }
1086
1098
  /* eslint-enable max-len*/
1087
1099
  function useEffect(deps, effect) {
1088
- if (typeof deps === 'function' &&
1089
- typeof deps.set === 'undefined' &&
1090
- typeof deps[depsKey] === 'undefined') {
1100
+ if (typeof deps === 'function' && !deps.$isSignal) {
1091
1101
  deps = useDerived(deps);
1092
1102
  }
1093
1103
  const signals = Array.isArray(deps) ? deps : [deps];
@@ -1112,15 +1122,15 @@ function useEffect(deps, effect) {
1112
1122
  }
1113
1123
  isClean = true;
1114
1124
  if (component) {
1115
- const index = component.destroyCallbacks.indexOf(destroyFn);
1116
- component.destroyCallbacks.splice(index, 1);
1125
+ const index = component.unmountedCallbacks.indexOf(destroyFn);
1126
+ component.unmountedCallbacks.splice(index, 1);
1117
1127
  }
1118
1128
  for (const dep of signals) {
1119
1129
  dep[depsKey].delete(effectCallback);
1120
1130
  }
1121
1131
  };
1122
1132
  if (component) {
1123
- component.destroyCallbacks.push(destroyFn);
1133
+ component.unmountedCallbacks.push(destroyFn);
1124
1134
  }
1125
1135
  return destroyFn;
1126
1136
  }
@@ -1332,6 +1342,7 @@ function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context, expect
1332
1342
  newAtom.jsxNode = instance;
1333
1343
  if (newTemplate === oldTemplate) {
1334
1344
  reuseComponentView(nativeRenderer, newAtom, reusedAtom, context, expectIndex !== diffIndex - offset);
1345
+ updateView(nativeRenderer, instance);
1335
1346
  return;
1336
1347
  }
1337
1348
  if (newTemplate) {
@@ -1791,8 +1802,10 @@ class RootComponent extends Component {
1791
1802
  }
1792
1803
 
1793
1804
  const viewflyErrorFn = makeError('Viewfly');
1794
- function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1805
+ function viewfly(config) {
1806
+ const { context, nativeRenderer, autoUpdate, root } = Object.assign({ autoUpdate: true }, config);
1795
1807
  const appProviders = [];
1808
+ const modules = [];
1796
1809
  let destroyed = false;
1797
1810
  const rootComponent = new RootComponent(context || null, () => {
1798
1811
  provide(appProviders);
@@ -1822,13 +1835,29 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1822
1835
  }
1823
1836
  return app;
1824
1837
  },
1838
+ use(module) {
1839
+ if (Array.isArray(module)) {
1840
+ modules.push(...module);
1841
+ }
1842
+ else {
1843
+ modules.push(module);
1844
+ }
1845
+ return app;
1846
+ },
1825
1847
  mount(host) {
1848
+ var _a, _b;
1826
1849
  if (isStarted) {
1827
1850
  throw viewflyErrorFn('application has already started.');
1828
1851
  }
1852
+ for (const module of modules) {
1853
+ (_a = module.setup) === null || _a === void 0 ? void 0 : _a.call(module, app);
1854
+ }
1829
1855
  isStarted = true;
1830
1856
  appHost = host;
1831
1857
  render(host);
1858
+ for (const module of modules) {
1859
+ (_b = module.onAfterStartup) === null || _b === void 0 ? void 0 : _b.call(module, app);
1860
+ }
1832
1861
  if (!autoUpdate) {
1833
1862
  return app;
1834
1863
  }
@@ -1850,9 +1879,13 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1850
1879
  return app;
1851
1880
  },
1852
1881
  destroy() {
1882
+ var _a;
1853
1883
  destroyed = true;
1854
1884
  rootComponent.markAsDirtied();
1855
1885
  app.render();
1886
+ for (const module of modules) {
1887
+ (_a = module.onDestroy) === null || _a === void 0 ? void 0 : _a.call(module);
1888
+ }
1856
1889
  }
1857
1890
  };
1858
1891
  return app;
@@ -1887,9 +1920,9 @@ exports.jsx = jsx;
1887
1920
  exports.jsxs = jsxs;
1888
1921
  exports.makeError = makeError;
1889
1922
  exports.normalizeProvider = normalizeProvider;
1890
- exports.onDestroy = onDestroy;
1891
1923
  exports.onMounted = onMounted;
1892
1924
  exports.onPropsChanged = onPropsChanged;
1925
+ exports.onUnmounted = onUnmounted;
1893
1926
  exports.onUpdated = onUpdated;
1894
1927
  exports.provide = provide;
1895
1928
  exports.useDerived = useDerived;
@@ -17,7 +17,13 @@ export interface Config {
17
17
  export interface Application<T extends NativeNode = NativeNode> {
18
18
  provide(providers: Provider | Provider[]): Application<T>;
19
19
  mount(host: T, autoUpdate?: boolean): Application<T>;
20
+ use(module: Module | Module[]): Application<T>;
20
21
  render(): Application<T>;
21
22
  destroy(): void;
22
23
  }
23
- export declare function viewfly<T extends NativeNode>({ context, nativeRenderer, autoUpdate, root }: Config): Application<T>;
24
+ export interface Module {
25
+ setup?(app: Application): void;
26
+ onAfterStartup?(app: Application): void;
27
+ onDestroy?(): void;
28
+ }
29
+ export declare function viewfly<T extends NativeNode>(config: Config): Application<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/core",
3
- "version": "0.0.31",
3
+ "version": "0.1.1",
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",
@@ -46,5 +46,5 @@
46
46
  "bugs": {
47
47
  "url": "https://github.com/viewfly/viewfly.git/issues"
48
48
  },
49
- "gitHead": "cb3bcc1d390c6ee85411c1713f966b3d50f6e066"
49
+ "gitHead": "d6c54831a96266979af7b95c7152fe7ef2b62a4f"
50
50
  }