Skip to content

Commit

Permalink
refactor: move metadata into internals (#2062)
Browse files Browse the repository at this point in the history
This commit includes several updates:

1) Moves metadata interfaces and functions to the
`@ngxs/store/internals` subpackage.
2) Transfers interfaces and objects from `plugin_api.ts` to the
`@ngxs/store/plugins` subpackage.
3) Adds the `@deprecated` annotation to functions and types that were
exported from `public_to_deprecate.ts`.
  • Loading branch information
arturovt authored Mar 11, 2024
1 parent a836c51 commit 81baa9d
Show file tree
Hide file tree
Showing 69 changed files with 617 additions and 665 deletions.
3 changes: 2 additions & 1 deletion packages/devtools-plugin/src/devtools.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
EnvironmentProviders,
makeEnvironmentProviders
} from '@angular/core';
import { NGXS_PLUGINS, withNgxsPlugin } from '@ngxs/store';
import { withNgxsPlugin } from '@ngxs/store';
import { NGXS_PLUGINS } from '@ngxs/store/plugins';

import { NgxsDevtoolsOptions, NGXS_DEVTOOLS_OPTIONS } from './symbols';
import { NgxsReduxDevtoolsPlugin } from './devtools.plugin';
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools-plugin/src/devtools.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Inject, Injectable, Injector, NgZone, OnDestroy, ɵglobal } from '@angular/core';
import { Store } from '@ngxs/store';
import {
InitState,
getActionTypeFromInstance,
NgxsNextPluginFn,
NgxsPlugin,
Store
} from '@ngxs/store';
NgxsPlugin
} from '@ngxs/store/plugins';
import { tap, catchError } from 'rxjs/operators';

import {
Expand Down
3 changes: 2 additions & 1 deletion packages/form-plugin/src/directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeDetectorRef, Directive, Input, OnDestroy, OnInit } from '@angular/core';
import { FormGroup, FormGroupDirective } from '@angular/forms';
import { Actions, getValue, ofActionDispatched, Store } from '@ngxs/store';
import { Actions, ofActionDispatched, Store } from '@ngxs/store';
import { getValue } from '@ngxs/store/plugins';
import { Observable, ReplaySubject } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, takeUntil } from 'rxjs/operators';
import {
Expand Down
3 changes: 2 additions & 1 deletion packages/form-plugin/src/form.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
EnvironmentProviders,
makeEnvironmentProviders
} from '@angular/core';
import { NGXS_PLUGINS, withNgxsPlugin } from '@ngxs/store';
import { withNgxsPlugin } from '@ngxs/store';
import { NGXS_PLUGINS } from '@ngxs/store/plugins';

import { NgxsFormPlugin } from './form.plugin';
import { NgxsFormDirective } from './directive';
Expand Down
2 changes: 1 addition & 1 deletion packages/form-plugin/src/form.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
NgxsNextPluginFn,
NgxsPlugin,
setValue
} from '@ngxs/store';
} from '@ngxs/store/plugins';
import {
ResetForm,
SetFormDirty,
Expand Down
12 changes: 6 additions & 6 deletions packages/hmr-plugin/src/hmr-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApplicationRef, ComponentRef, NgModuleRef } from '@angular/core';
import { InitialState, NgxsBootstrapper } from '@ngxs/store/internals';
import { ɵInitialState, ɵNgxsAppBootstrappedState } from '@ngxs/store/internals';

import { HmrStorage } from './internal/hmr-storage';
import {
Expand Down Expand Up @@ -31,22 +31,22 @@ export class HmrManager<T extends Partial<NgxsHmrLifeCycle<S>>, S = NgxsHmrSnaps
return this.ngModule.injector.get(ApplicationRef);
}

private get bootstrap(): NgxsBootstrapper {
return this.ngModule.injector.get(NgxsBootstrapper);
private get appBootstrappedState() {
return this.ngModule.injector.get(ɵNgxsAppBootstrappedState);
}

public async hmrModule(
bootstrapFn: BootstrapModuleFn<T>,
tick: () => void
): Promise<NgModuleRef<T>> {
InitialState.set(this.storage.snapshot);
ɵInitialState.set(this.storage.snapshot);
this.ngModule = await bootstrapFn();
this.context = new HmrStateContextFactory(this.ngModule);
this.lifecycle = this.createLifecycle();

tick();

InitialState.pop();
ɵInitialState.pop();
return this.ngModule;
}

Expand All @@ -71,7 +71,7 @@ export class HmrManager<T extends Partial<NgxsHmrLifeCycle<S>>, S = NgxsHmrSnaps
private createLifecycle(): HmrLifecycle<T, S> {
return new HmrLifecycle(
this.ngModule.instance,
this.bootstrap,
this.appBootstrappedState,
this.storage,
this.context,
this.optionsBuilder
Expand Down
7 changes: 3 additions & 4 deletions packages/hmr-plugin/src/internal/hmr-lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgxsBootstrapper } from '@ngxs/store/internals';
import { ɵNgxsAppBootstrappedState } from '@ngxs/store/internals';
import { Observable, Subscription } from 'rxjs';
import { StateContext } from '@ngxs/store';

Expand All @@ -11,7 +11,7 @@ import { HmrStorage } from './hmr-storage';
export class HmrLifecycle<T extends Partial<NgxsHmrLifeCycle<S>>, S> {
constructor(
private ngAppModule: T,
private bootstrap: NgxsBootstrapper,
private appBootstrappedState: ɵNgxsAppBootstrappedState,
private storage: HmrStorage<S>,
private context: HmrStateContextFactory<T, S>,
private options: HmrOptionBuilder
Expand Down Expand Up @@ -52,10 +52,9 @@ export class HmrLifecycle<T extends Partial<NgxsHmrLifeCycle<S>>, S> {
private stateEventLoop(callback: HmrCallback<S>): void {
if (!this.storage.hasData()) return;

const appBootstrapped$: Observable<boolean> = this.bootstrap.appBootstrapped$;
const state$: Observable<any> = this.context.store.select(state => state);

appBootstrapped$.subscribe(() => {
this.appBootstrappedState.subscribe(() => {
let eventId: number;
const storeEventId: Subscription = state$.subscribe(() => {
// setTimeout used for zone detection after set hmr state
Expand Down
3 changes: 2 additions & 1 deletion packages/logger-plugin/src/action-logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getActionTypeFromInstance, Store } from '@ngxs/store';
import { Store } from '@ngxs/store';
import { getActionTypeFromInstance } from '@ngxs/store/plugins';

import { formatTime } from './internals';
import { LogWriter } from './log-writer';
Expand Down
4 changes: 3 additions & 1 deletion packages/logger-plugin/src/logger.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
NgModule,
makeEnvironmentProviders
} from '@angular/core';
import { NGXS_PLUGINS, withNgxsPlugin } from '@ngxs/store';
import { withNgxsPlugin } from '@ngxs/store';
import { NGXS_PLUGINS } from '@ngxs/store/plugins';

import { NgxsLoggerPlugin } from './logger.plugin';
import { NgxsLoggerPluginOptions, NGXS_LOGGER_PLUGIN_OPTIONS } from './symbols';

Expand Down
4 changes: 3 additions & 1 deletion packages/logger-plugin/src/logger.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Inject, Injectable, Injector } from '@angular/core';
import { NgxsNextPluginFn, NgxsPlugin, Store } from '@ngxs/store';
import { Store } from '@ngxs/store';
import { NgxsNextPluginFn, NgxsPlugin } from '@ngxs/store/plugins';
import { catchError, tap } from 'rxjs/operators';

import { ActionLogger } from './action-logger';
import { LogWriter } from './log-writer';
import { NgxsLoggerPluginOptions, NGXS_LOGGER_PLUGIN_OPTIONS } from './symbols';
Expand Down
3 changes: 2 additions & 1 deletion packages/storage-plugin/src/storage.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
EnvironmentProviders,
makeEnvironmentProviders
} from '@angular/core';
import { NGXS_PLUGINS, withNgxsPlugin } from '@ngxs/store';
import { withNgxsPlugin } from '@ngxs/store';
import { NGXS_PLUGINS } from '@ngxs/store/plugins';
import {
NgxsStoragePluginOptions,
STORAGE_ENGINE,
Expand Down
6 changes: 3 additions & 3 deletions packages/storage-plugin/src/storage.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PLATFORM_ID, Inject, Injectable } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { PlainObject } from '@ngxs/store/internals';
import { ɵPlainObject } from '@ngxs/store/internals';
import {
NgxsPlugin,
setValue,
Expand All @@ -9,7 +9,7 @@ import {
UpdateState,
actionMatcher,
NgxsNextPluginFn
} from '@ngxs/store';
} from '@ngxs/store/plugins';
import {
ɵDEFAULT_STATE_KEY,
ɵFinalNgxsStoragePluginOptions,
Expand Down Expand Up @@ -124,7 +124,7 @@ export class NgxsStoragePlugin implements NgxsPlugin {
}
return accumulator;
},
<PlainObject>{}
<ɵPlainObject>{}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function orderedQueueOperation<TArgs extends any[]>(operation: (...args: TArgs)
* When `subject` is a standard `Subject<T>` the second subscriber would recieve `end` and then `start`.
* When `subject` is a `OrderedSubject<T>` the second subscriber would recieve `start` and then `end`.
*/
export class OrderedSubject<T> extends Subject<T> {
export class ɵOrderedSubject<T> extends Subject<T> {
private _orderedNext = orderedQueueOperation((value?: T) => super.next(<T>value));

next(value?: T): void {
Expand All @@ -76,7 +76,7 @@ export class OrderedSubject<T> extends Subject<T> {
* When `subject` is a standard `BehaviorSubject<T>` the second subscriber would recieve `end` and then `start`.
* When `subject` is a `OrderedBehaviorSubject<T>` the second subscriber would recieve `start` and then `end`.
*/
export class OrderedBehaviorSubject<T> extends BehaviorSubject<T> {
export class ɵOrderedBehaviorSubject<T> extends BehaviorSubject<T> {
private _orderedNext = orderedQueueOperation((value: T) => super.next(value));
private _currentValue: T;

Expand Down
19 changes: 8 additions & 11 deletions packages/store/internals/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
export { NgxsBootstrapper } from './ngxs-bootstrapper';
export { memoize } from './memoize';
export { INITIAL_STATE_TOKEN, InitialState } from './initial-state';
export {
PlainObjectOf,
PlainObject,
ɵStateClass,
ɵMETA_KEY,
ɵMETA_OPTIONS_KEY,
ɵSELECTOR_META_KEY
} from './symbols';
export * from './symbols';
export * from './metadata';
export { ɵmemoize } from './memoize';
export { StateToken } from './state-token';
export { ɵINITIAL_STATE_TOKEN, ɵInitialState } from './initial-state';
export { ɵNgxsAppBootstrappedState } from './ngxs-app-bootstrapped-state';
export { ɵNGXS_STATE_CONTEXT_FACTORY, ɵNGXS_STATE_FACTORY } from './internal-tokens';
export { ɵOrderedSubject, ɵOrderedBehaviorSubject } from './custom-rxjs-subjects';
export { ɵStateStream } from './state-stream';
14 changes: 7 additions & 7 deletions packages/store/internals/src/initial-state.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { InjectionToken } from '@angular/core';

import { PlainObject } from './symbols';
import { ɵPlainObject } from './symbols';

declare const ngDevMode: boolean;

const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode;

export class InitialState {
private static _value: PlainObject = {};
export class ɵInitialState {
private static _value: ɵPlainObject = {};

static set(state: PlainObject) {
static set(state: ɵPlainObject) {
this._value = state;
}

static pop(): PlainObject {
static pop(): ɵPlainObject {
const state = this._value;
this._value = {};
return state;
}
}

export const INITIAL_STATE_TOKEN = new InjectionToken<PlainObject>(
export const ɵINITIAL_STATE_TOKEN = new InjectionToken<ɵPlainObject>(
NG_DEV_MODE ? 'INITIAL_STATE_TOKEN' : '',
{
providedIn: 'root',
factory: () => InitialState.pop()
factory: () => ɵInitialState.pop()
}
);
2 changes: 1 addition & 1 deletion packages/store/internals/src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function areArgumentsShallowlyEqual(
*
* @ignore
*/
export function memoize<T extends (...args: any[]) => any>(
export function ɵmemoize<T extends (...args: any[]) => any>(
func: T,
equalityCheck = defaultEqualityCheck
): T {
Expand Down
70 changes: 70 additions & 0 deletions packages/store/internals/src/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
ɵMETA_KEY,
ɵSELECTOR_META_KEY,
ɵMetaDataModel,
ɵStateClassInternal,
ɵSelectorMetaDataModel,
ɵRuntimeSelectorContext
} from './symbols';

/**
* Ensures metadata is attached to the class and returns it.
*
* @ignore
*/
export function ɵensureStoreMetadata(target: ɵStateClassInternal): ɵMetaDataModel {
if (!target.hasOwnProperty(ɵMETA_KEY)) {
const defaultMetadata: ɵMetaDataModel = {
name: null,
actions: {},
defaults: {},
path: null,
makeRootSelector(context: ɵRuntimeSelectorContext) {
return context.getStateGetter(defaultMetadata.name);
},
children: []
};

Object.defineProperty(target, ɵMETA_KEY, { value: defaultMetadata });
}
return ɵgetStoreMetadata(target);
}

/**
* Get the metadata attached to the state class if it exists.
*
* @ignore
*/
export function ɵgetStoreMetadata(target: ɵStateClassInternal): ɵMetaDataModel {
return target[ɵMETA_KEY]!;
}

/**
* Ensures metadata is attached to the selector and returns it.
*
* @ignore
*/
export function ɵensureSelectorMetadata(target: Function): ɵSelectorMetaDataModel {
if (!target.hasOwnProperty(ɵSELECTOR_META_KEY)) {
const defaultMetadata: ɵSelectorMetaDataModel = {
makeRootSelector: null,
originalFn: null,
containerClass: null,
selectorName: null,
getSelectorOptions: () => ({})
};

Object.defineProperty(target, ɵSELECTOR_META_KEY, { value: defaultMetadata });
}

return ɵgetSelectorMetadata(target);
}

/**
* Get the metadata attached to the selector if it exists.
*
* @ignore
*/
export function ɵgetSelectorMetadata(target: any): ɵSelectorMetaDataModel {
return target[ɵSELECTOR_META_KEY];
}
14 changes: 14 additions & 0 deletions packages/store/internals/src/ngxs-app-bootstrapped-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@angular/core';
import { ReplaySubject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class ɵNgxsAppBootstrappedState extends ReplaySubject<boolean> {
constructor() {
super(1);
}

bootstrap(): void {
this.next(true);
this.complete();
}
}
23 changes: 0 additions & 23 deletions packages/store/internals/src/ngxs-bootstrapper.ts

This file was deleted.

Loading

0 comments on commit 81baa9d

Please sign in to comment.