@viewfly/core 0.0.30 → 0.1.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.
@@ -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
  */
@@ -33,6 +33,7 @@ export declare class JSXComponent implements JSXTypeof<JSXInternal.ComponentSetu
33
33
  props: Props;
34
34
  factory: (parentComponent: Component) => Component;
35
35
  key?: Key | undefined;
36
+ static createInstance(type: JSXInternal.ComponentSetup, props: Props, factory: (parentComponent: Component) => Component, key?: Key): JSXComponent;
36
37
  $$typeOf: JSXInternal.ComponentSetup<any>;
37
38
  constructor(type: JSXInternal.ComponentSetup, props: Props, factory: (parentComponent: Component) => Component, key?: Key | undefined);
38
39
  createInstance(parentComponent: Component): Component;
@@ -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
  }
@@ -791,7 +791,7 @@ class Component extends ReflectiveInjector {
791
791
  fn();
792
792
  });
793
793
  this.propsChangedDestroyCallbacks = [];
794
- for (const fn of this.destroyCallbacks) {
794
+ for (const fn of this.unmountedCallbacks) {
795
795
  fn();
796
796
  }
797
797
  this.updatedCallbacks = [];
@@ -816,7 +816,7 @@ class Component extends ReflectiveInjector {
816
816
  for (const fn of this.mountCallbacks) {
817
817
  const destroyFn = fn();
818
818
  if (typeof destroyFn === 'function') {
819
- this.destroyCallbacks.push(destroyFn);
819
+ this.unmountedCallbacks.push(destroyFn);
820
820
  }
821
821
  }
822
822
  }
@@ -912,9 +912,9 @@ function onPropsChanged(callback) {
912
912
  * 当组件销毁时调用回调函数
913
913
  * @param callback
914
914
  */
915
- function onDestroy(callback) {
915
+ function onUnmounted(callback) {
916
916
  const component = getSetupContext();
917
- component.destroyCallbacks.push(callback);
917
+ component.unmountedCallbacks.push(callback);
918
918
  }
919
919
  class Ref {
920
920
  constructor(callback) {
@@ -1000,6 +1000,7 @@ function useSignal(state) {
1000
1000
  }
1001
1001
  return state;
1002
1002
  }
1003
+ signal.$isSignal = true;
1003
1004
  signal.set = function (newState) {
1004
1005
  if (newState === state) {
1005
1006
  return;
@@ -1011,6 +1012,14 @@ function useSignal(state) {
1011
1012
  fn();
1012
1013
  }
1013
1014
  };
1015
+ //
1016
+ // signal.toString = function () {
1017
+ // return String(state)
1018
+ // }
1019
+ //
1020
+ // signal.valueOf = function () {
1021
+ // return state
1022
+ // }
1014
1023
  signal[depsKey] = new Set();
1015
1024
  return signal;
1016
1025
  }
@@ -1075,7 +1084,7 @@ function useDerived(callback, isContinue) {
1075
1084
  const component = getSetupContext(false);
1076
1085
  const unListen = listen(signal, deps, callback, isContinue);
1077
1086
  if (component) {
1078
- component.destroyCallbacks.push(() => {
1087
+ component.unmountedCallbacks.push(() => {
1079
1088
  unListen();
1080
1089
  });
1081
1090
  }
@@ -1083,9 +1092,7 @@ function useDerived(callback, isContinue) {
1083
1092
  }
1084
1093
  /* eslint-enable max-len*/
1085
1094
  function useEffect(deps, effect) {
1086
- if (typeof deps === 'function' &&
1087
- typeof deps.set === 'undefined' &&
1088
- typeof deps[depsKey] === 'undefined') {
1095
+ if (typeof deps === 'function' && !deps.$isSignal) {
1089
1096
  deps = useDerived(deps);
1090
1097
  }
1091
1098
  const signals = Array.isArray(deps) ? deps : [deps];
@@ -1110,15 +1117,15 @@ function useEffect(deps, effect) {
1110
1117
  }
1111
1118
  isClean = true;
1112
1119
  if (component) {
1113
- const index = component.destroyCallbacks.indexOf(destroyFn);
1114
- component.destroyCallbacks.splice(index, 1);
1120
+ const index = component.unmountedCallbacks.indexOf(destroyFn);
1121
+ component.unmountedCallbacks.splice(index, 1);
1115
1122
  }
1116
1123
  for (const dep of signals) {
1117
1124
  dep[depsKey].delete(effectCallback);
1118
1125
  }
1119
1126
  };
1120
1127
  if (component) {
1121
- component.destroyCallbacks.push(destroyFn);
1128
+ component.unmountedCallbacks.push(destroyFn);
1122
1129
  }
1123
1130
  return destroyFn;
1124
1131
  }
@@ -1151,7 +1158,7 @@ function jsx(setup, props, key) {
1151
1158
  if (typeof setup === 'string') {
1152
1159
  return JSXElement.createInstance(setup, props, key);
1153
1160
  }
1154
- return new JSXComponent(setup, props, function (context) {
1161
+ return JSXComponent.createInstance(setup, props, function (context) {
1155
1162
  return new Component(context, setup, props, key);
1156
1163
  }, key);
1157
1164
  }
@@ -1175,6 +1182,9 @@ class JSXElement {
1175
1182
  }
1176
1183
  }
1177
1184
  class JSXComponent {
1185
+ static createInstance(type, props, factory, key) {
1186
+ return new JSXComponent(type, props, factory, key);
1187
+ }
1178
1188
  constructor(type, props, factory, key) {
1179
1189
  this.type = type;
1180
1190
  this.props = props;
@@ -1786,8 +1796,10 @@ class RootComponent extends Component {
1786
1796
  }
1787
1797
 
1788
1798
  const viewflyErrorFn = makeError('Viewfly');
1789
- function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1799
+ function viewfly(config) {
1800
+ const { context, nativeRenderer, autoUpdate, root } = Object.assign({ autoUpdate: true }, config);
1790
1801
  const appProviders = [];
1802
+ const modules = [];
1791
1803
  let destroyed = false;
1792
1804
  const rootComponent = new RootComponent(context || null, () => {
1793
1805
  provide(appProviders);
@@ -1817,13 +1829,29 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1817
1829
  }
1818
1830
  return app;
1819
1831
  },
1832
+ use(module) {
1833
+ if (Array.isArray(module)) {
1834
+ modules.push(...module);
1835
+ }
1836
+ else {
1837
+ modules.push(module);
1838
+ }
1839
+ return app;
1840
+ },
1820
1841
  mount(host) {
1842
+ var _a, _b;
1821
1843
  if (isStarted) {
1822
1844
  throw viewflyErrorFn('application has already started.');
1823
1845
  }
1846
+ for (const module of modules) {
1847
+ (_a = module.setup) === null || _a === void 0 ? void 0 : _a.call(module, app);
1848
+ }
1824
1849
  isStarted = true;
1825
1850
  appHost = host;
1826
1851
  render(host);
1852
+ for (const module of modules) {
1853
+ (_b = module.onAfterStartup) === null || _b === void 0 ? void 0 : _b.call(module, app);
1854
+ }
1827
1855
  if (!autoUpdate) {
1828
1856
  return app;
1829
1857
  }
@@ -1845,12 +1873,16 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1845
1873
  return app;
1846
1874
  },
1847
1875
  destroy() {
1876
+ var _a;
1848
1877
  destroyed = true;
1849
1878
  rootComponent.markAsDirtied();
1850
1879
  app.render();
1880
+ for (const module of modules) {
1881
+ (_a = module.onDestroy) === null || _a === void 0 ? void 0 : _a.call(module);
1882
+ }
1851
1883
  }
1852
1884
  };
1853
1885
  return app;
1854
1886
  }
1855
1887
 
1856
- 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 };
1888
+ 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
  }
@@ -793,7 +793,7 @@ class Component extends ReflectiveInjector {
793
793
  fn();
794
794
  });
795
795
  this.propsChangedDestroyCallbacks = [];
796
- for (const fn of this.destroyCallbacks) {
796
+ for (const fn of this.unmountedCallbacks) {
797
797
  fn();
798
798
  }
799
799
  this.updatedCallbacks = [];
@@ -818,7 +818,7 @@ class Component extends ReflectiveInjector {
818
818
  for (const fn of this.mountCallbacks) {
819
819
  const destroyFn = fn();
820
820
  if (typeof destroyFn === 'function') {
821
- this.destroyCallbacks.push(destroyFn);
821
+ this.unmountedCallbacks.push(destroyFn);
822
822
  }
823
823
  }
824
824
  }
@@ -914,9 +914,9 @@ function onPropsChanged(callback) {
914
914
  * 当组件销毁时调用回调函数
915
915
  * @param callback
916
916
  */
917
- function onDestroy(callback) {
917
+ function onUnmounted(callback) {
918
918
  const component = getSetupContext();
919
- component.destroyCallbacks.push(callback);
919
+ component.unmountedCallbacks.push(callback);
920
920
  }
921
921
  class Ref {
922
922
  constructor(callback) {
@@ -1002,6 +1002,7 @@ function useSignal(state) {
1002
1002
  }
1003
1003
  return state;
1004
1004
  }
1005
+ signal.$isSignal = true;
1005
1006
  signal.set = function (newState) {
1006
1007
  if (newState === state) {
1007
1008
  return;
@@ -1013,6 +1014,14 @@ function useSignal(state) {
1013
1014
  fn();
1014
1015
  }
1015
1016
  };
1017
+ //
1018
+ // signal.toString = function () {
1019
+ // return String(state)
1020
+ // }
1021
+ //
1022
+ // signal.valueOf = function () {
1023
+ // return state
1024
+ // }
1016
1025
  signal[depsKey] = new Set();
1017
1026
  return signal;
1018
1027
  }
@@ -1077,7 +1086,7 @@ function useDerived(callback, isContinue) {
1077
1086
  const component = getSetupContext(false);
1078
1087
  const unListen = listen(signal, deps, callback, isContinue);
1079
1088
  if (component) {
1080
- component.destroyCallbacks.push(() => {
1089
+ component.unmountedCallbacks.push(() => {
1081
1090
  unListen();
1082
1091
  });
1083
1092
  }
@@ -1085,9 +1094,7 @@ function useDerived(callback, isContinue) {
1085
1094
  }
1086
1095
  /* eslint-enable max-len*/
1087
1096
  function useEffect(deps, effect) {
1088
- if (typeof deps === 'function' &&
1089
- typeof deps.set === 'undefined' &&
1090
- typeof deps[depsKey] === 'undefined') {
1097
+ if (typeof deps === 'function' && !deps.$isSignal) {
1091
1098
  deps = useDerived(deps);
1092
1099
  }
1093
1100
  const signals = Array.isArray(deps) ? deps : [deps];
@@ -1112,15 +1119,15 @@ function useEffect(deps, effect) {
1112
1119
  }
1113
1120
  isClean = true;
1114
1121
  if (component) {
1115
- const index = component.destroyCallbacks.indexOf(destroyFn);
1116
- component.destroyCallbacks.splice(index, 1);
1122
+ const index = component.unmountedCallbacks.indexOf(destroyFn);
1123
+ component.unmountedCallbacks.splice(index, 1);
1117
1124
  }
1118
1125
  for (const dep of signals) {
1119
1126
  dep[depsKey].delete(effectCallback);
1120
1127
  }
1121
1128
  };
1122
1129
  if (component) {
1123
- component.destroyCallbacks.push(destroyFn);
1130
+ component.unmountedCallbacks.push(destroyFn);
1124
1131
  }
1125
1132
  return destroyFn;
1126
1133
  }
@@ -1153,7 +1160,7 @@ function jsx(setup, props, key) {
1153
1160
  if (typeof setup === 'string') {
1154
1161
  return JSXElement.createInstance(setup, props, key);
1155
1162
  }
1156
- return new JSXComponent(setup, props, function (context) {
1163
+ return JSXComponent.createInstance(setup, props, function (context) {
1157
1164
  return new Component(context, setup, props, key);
1158
1165
  }, key);
1159
1166
  }
@@ -1177,6 +1184,9 @@ class JSXElement {
1177
1184
  }
1178
1185
  }
1179
1186
  class JSXComponent {
1187
+ static createInstance(type, props, factory, key) {
1188
+ return new JSXComponent(type, props, factory, key);
1189
+ }
1180
1190
  constructor(type, props, factory, key) {
1181
1191
  this.type = type;
1182
1192
  this.props = props;
@@ -1788,8 +1798,10 @@ class RootComponent extends Component {
1788
1798
  }
1789
1799
 
1790
1800
  const viewflyErrorFn = makeError('Viewfly');
1791
- function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1801
+ function viewfly(config) {
1802
+ const { context, nativeRenderer, autoUpdate, root } = Object.assign({ autoUpdate: true }, config);
1792
1803
  const appProviders = [];
1804
+ const modules = [];
1793
1805
  let destroyed = false;
1794
1806
  const rootComponent = new RootComponent(context || null, () => {
1795
1807
  provide(appProviders);
@@ -1819,13 +1831,29 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1819
1831
  }
1820
1832
  return app;
1821
1833
  },
1834
+ use(module) {
1835
+ if (Array.isArray(module)) {
1836
+ modules.push(...module);
1837
+ }
1838
+ else {
1839
+ modules.push(module);
1840
+ }
1841
+ return app;
1842
+ },
1822
1843
  mount(host) {
1844
+ var _a, _b;
1823
1845
  if (isStarted) {
1824
1846
  throw viewflyErrorFn('application has already started.');
1825
1847
  }
1848
+ for (const module of modules) {
1849
+ (_a = module.setup) === null || _a === void 0 ? void 0 : _a.call(module, app);
1850
+ }
1826
1851
  isStarted = true;
1827
1852
  appHost = host;
1828
1853
  render(host);
1854
+ for (const module of modules) {
1855
+ (_b = module.onAfterStartup) === null || _b === void 0 ? void 0 : _b.call(module, app);
1856
+ }
1829
1857
  if (!autoUpdate) {
1830
1858
  return app;
1831
1859
  }
@@ -1847,9 +1875,13 @@ function viewfly({ context, nativeRenderer, autoUpdate, root }) {
1847
1875
  return app;
1848
1876
  },
1849
1877
  destroy() {
1878
+ var _a;
1850
1879
  destroyed = true;
1851
1880
  rootComponent.markAsDirtied();
1852
1881
  app.render();
1882
+ for (const module of modules) {
1883
+ (_a = module.onDestroy) === null || _a === void 0 ? void 0 : _a.call(module);
1884
+ }
1853
1885
  }
1854
1886
  };
1855
1887
  return app;
@@ -1884,9 +1916,9 @@ exports.jsx = jsx;
1884
1916
  exports.jsxs = jsxs;
1885
1917
  exports.makeError = makeError;
1886
1918
  exports.normalizeProvider = normalizeProvider;
1887
- exports.onDestroy = onDestroy;
1888
1919
  exports.onMounted = onMounted;
1889
1920
  exports.onPropsChanged = onPropsChanged;
1921
+ exports.onUnmounted = onUnmounted;
1890
1922
  exports.onUpdated = onUpdated;
1891
1923
  exports.provide = provide;
1892
1924
  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.30",
3
+ "version": "0.1.0",
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": "57f845762f1e40ca48ff9328d73358035d293aba"
49
+ "gitHead": "790bb591abb02c500f54c579c7783da66a999b87"
50
50
  }