From 633c30870a97e025dcbddf74939d3af03c654511 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sat, 18 Sep 2021 16:21:12 +0200 Subject: [PATCH 1/2] separated createState and createLightState to prevent tree-shaking issue --- packages/core/src/state/index.ts | 73 +------------------ .../core/src/state/public/createLightState.ts | 33 +++++++++ packages/core/src/state/public/createState.ts | 37 ++++++++++ packages/core/src/state/public/index.ts | 9 +++ packages/event/src/index.ts | 2 + 5 files changed, 83 insertions(+), 71 deletions(-) create mode 100644 packages/core/src/state/public/createLightState.ts create mode 100644 packages/core/src/state/public/createState.ts create mode 100644 packages/core/src/state/public/index.ts diff --git a/packages/core/src/state/index.ts b/packages/core/src/state/index.ts index 4caa8d5a..3195df90 100644 --- a/packages/core/src/state/index.ts +++ b/packages/core/src/state/index.ts @@ -1,77 +1,8 @@ -import { defineConfig, removeProperties } from '@agile-ts/utils'; -import { CreateAgileSubInstanceInterface, shared } from '../shared'; -import { State, StateConfigInterface } from './state'; -import { EnhancedState } from './state.enhanced'; - export * from './state'; export * from './state.observer'; export * from './state.enhanced'; export * from './state.persistent'; export * from './state.runtime.job'; -/** - * Returns a newly created State. - * - * A State manages a piece of Information - * that we need to remember globally at a later point in time. - * While providing a toolkit to use and mutate this piece of Information. - * - * You can create as many global States as you need. - * - * [Learn more..](https://agile-ts.org/docs/core/agile-instance/methods#createstate) - * - * @public - * @param initialValue - Initial value of the State. - * @param config - Configuration object - */ -export function createLightState( - initialValue: ValueType, - config: CreateStateConfigInterfaceWithAgile = {} -): State { - config = defineConfig(config, { - agileInstance: shared, - }); - return new State( - config.agileInstance as any, - initialValue, - removeProperties(config, ['agileInstance']) - ); -} - -// TODO 'createState' doesn't get entirely treeshaken away (React project) -/** - * Returns a newly created enhanced State. - * - * An enhanced State manages, like a normal State, a piece of Information - * that we need to remember globally at a later point in time. - * While providing a toolkit to use and mutate this piece of Information. - * - * The main difference to a normal State is however - * that an enhanced State provides a wider variety of inbuilt utilities (like a persist, undo, watch functionality) - * but requires a larger bundle size in return. - * - * You can create as many global enhanced States as you need. - * - * [Learn more..](https://agile-ts.org/docs/core/agile-instance/methods#createstate) - * - * @public - * @param initialValue - Initial value of the State. - * @param config - Configuration object - */ -export function createState( - initialValue: ValueType, - config: CreateStateConfigInterfaceWithAgile = {} -): EnhancedState { - config = defineConfig(config, { - agileInstance: shared, - }); - return new EnhancedState( - config.agileInstance as any, - initialValue, - removeProperties(config, ['agileInstance']) - ); -} - -export interface CreateStateConfigInterfaceWithAgile - extends CreateAgileSubInstanceInterface, - StateConfigInterface {} +// Outsourced from here because of tree shaking issues (See: https://github.com/agile-ts/agile/issues/196) +export * from './public'; diff --git a/packages/core/src/state/public/createLightState.ts b/packages/core/src/state/public/createLightState.ts new file mode 100644 index 00000000..4055f734 --- /dev/null +++ b/packages/core/src/state/public/createLightState.ts @@ -0,0 +1,33 @@ +import { defineConfig, removeProperties } from '@agile-ts/utils'; +import { shared } from '../../shared'; +import { State } from '../state'; +import { CreateStateConfigInterfaceWithAgile } from './index'; + +/** + * Returns a newly created State. + * + * A State manages a piece of Information + * that we need to remember globally at a later point in time. + * While providing a toolkit to use and mutate this piece of Information. + * + * You can create as many global States as you need. + * + * [Learn more..](https://agile-ts.org/docs/core/agile-instance/methods#createstate) + * + * @public + * @param initialValue - Initial value of the State. + * @param config - Configuration object + */ +export function createLightState( + initialValue: ValueType, + config: CreateStateConfigInterfaceWithAgile = {} +): State { + config = defineConfig(config, { + agileInstance: shared, + }); + return new State( + config.agileInstance as any, + initialValue, + removeProperties(config, ['agileInstance']) + ); +} diff --git a/packages/core/src/state/public/createState.ts b/packages/core/src/state/public/createState.ts new file mode 100644 index 00000000..8963bf0f --- /dev/null +++ b/packages/core/src/state/public/createState.ts @@ -0,0 +1,37 @@ +import { defineConfig, removeProperties } from '@agile-ts/utils'; +import { shared } from '../../shared'; +import { EnhancedState } from '../state.enhanced'; +import { CreateStateConfigInterfaceWithAgile } from './index'; + +/** + * Returns a newly created enhanced State. + * + * An enhanced State manages, like a normal State, a piece of Information + * that we need to remember globally at a later point in time. + * While providing a toolkit to use and mutate this piece of Information. + * + * The main difference to a normal State is however + * that an enhanced State provides a wider variety of inbuilt utilities (like a persist, undo, watch functionality) + * but requires a larger bundle size in return. + * + * You can create as many global enhanced States as you need. + * + * [Learn more..](https://agile-ts.org/docs/core/agile-instance/methods#createstate) + * + * @public + * @param initialValue - Initial value of the State. + * @param config - Configuration object + */ +export function createState( + initialValue: ValueType, + config: CreateStateConfigInterfaceWithAgile = {} +): EnhancedState { + config = defineConfig(config, { + agileInstance: shared, + }); + return new EnhancedState( + config.agileInstance as any, + initialValue, + removeProperties(config, ['agileInstance']) + ); +} diff --git a/packages/core/src/state/public/index.ts b/packages/core/src/state/public/index.ts new file mode 100644 index 00000000..34fdf484 --- /dev/null +++ b/packages/core/src/state/public/index.ts @@ -0,0 +1,9 @@ +import { CreateAgileSubInstanceInterface } from '../../shared'; +import { StateConfigInterface } from '../state'; + +export * from './createState'; +export * from './createLightState'; + +export interface CreateStateConfigInterfaceWithAgile + extends CreateAgileSubInstanceInterface, + StateConfigInterface {} diff --git a/packages/event/src/index.ts b/packages/event/src/index.ts index a5206c74..09aba335 100644 --- a/packages/event/src/index.ts +++ b/packages/event/src/index.ts @@ -1,4 +1,6 @@ import { Event } from './event'; export * from './event'; +export * from './react'; + export default Event; From ef10f0cf572d06f34b8c18f2946fdd38982a2639 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sat, 18 Sep 2021 17:09:59 +0200 Subject: [PATCH 2/2] renamed createState --- .../src/state/public/createEnhancedState.ts | 37 +++++++++++++++++++ packages/core/src/state/public/createState.ts | 16 +++----- packages/core/src/state/public/index.ts | 1 + 3 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 packages/core/src/state/public/createEnhancedState.ts diff --git a/packages/core/src/state/public/createEnhancedState.ts b/packages/core/src/state/public/createEnhancedState.ts new file mode 100644 index 00000000..6e64a675 --- /dev/null +++ b/packages/core/src/state/public/createEnhancedState.ts @@ -0,0 +1,37 @@ +import { defineConfig, removeProperties } from '@agile-ts/utils'; +import { shared } from '../../shared'; +import { EnhancedState } from '../state.enhanced'; +import { CreateStateConfigInterfaceWithAgile } from './index'; + +/** + * Returns a newly created enhanced State. + * + * An enhanced State manages, like a normal State, a piece of Information + * that we need to remember globally at a later point in time. + * While providing a toolkit to use and mutate this piece of Information. + * + * The main difference to a normal State is however + * that an enhanced State provides a wider variety of inbuilt utilities (like a persist, undo, watch functionality) + * but requires a larger bundle size in return. + * + * You can create as many global enhanced States as you need. + * + * [Learn more..](https://agile-ts.org/docs/core/agile-instance/methods#createstate) + * + * @public + * @param initialValue - Initial value of the State. + * @param config - Configuration object + */ +export function createEnhancedState( + initialValue: ValueType, + config: CreateStateConfigInterfaceWithAgile = {} +): EnhancedState { + config = defineConfig(config, { + agileInstance: shared, + }); + return new EnhancedState( + config.agileInstance as any, + initialValue, + removeProperties(config, ['agileInstance']) + ); +} diff --git a/packages/core/src/state/public/createState.ts b/packages/core/src/state/public/createState.ts index 8963bf0f..3a75772a 100644 --- a/packages/core/src/state/public/createState.ts +++ b/packages/core/src/state/public/createState.ts @@ -1,7 +1,8 @@ -import { defineConfig, removeProperties } from '@agile-ts/utils'; -import { shared } from '../../shared'; +import { + createEnhancedState, + CreateStateConfigInterfaceWithAgile, +} from './index'; import { EnhancedState } from '../state.enhanced'; -import { CreateStateConfigInterfaceWithAgile } from './index'; /** * Returns a newly created enhanced State. @@ -26,12 +27,5 @@ export function createState( initialValue: ValueType, config: CreateStateConfigInterfaceWithAgile = {} ): EnhancedState { - config = defineConfig(config, { - agileInstance: shared, - }); - return new EnhancedState( - config.agileInstance as any, - initialValue, - removeProperties(config, ['agileInstance']) - ); + return createEnhancedState(initialValue, config); } diff --git a/packages/core/src/state/public/index.ts b/packages/core/src/state/public/index.ts index 34fdf484..42ac2aed 100644 --- a/packages/core/src/state/public/index.ts +++ b/packages/core/src/state/public/index.ts @@ -2,6 +2,7 @@ import { CreateAgileSubInstanceInterface } from '../../shared'; import { StateConfigInterface } from '../state'; export * from './createState'; +export * from './createEnhancedState'; export * from './createLightState'; export interface CreateStateConfigInterfaceWithAgile