Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/BaseController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
*/
name = 'BaseController';

private readonly initialConfig: C;
readonly #initialConfig: C;

private readonly initialState: S;
readonly #initialState: S;

private internalConfig: C = this.defaultConfig;
#internalConfig: C = this.defaultConfig;

private internalState: S = this.defaultState;
#internalState: S = this.defaultState;

private internalListeners: Listener<S>[] = [];
#internalListeners: Listener<S>[] = [];

/**
* Creates a BaseController instance. Both initial state and initial
Expand All @@ -66,8 +66,8 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
*/
constructor(config: Partial<C> = {} as C, state: Partial<S> = {} as S) {
// Use assign since generics can't be spread: https://git.io/vpRhY
this.initialState = state as S;
this.initialConfig = config as C;
this.#initialState = state as S;
this.#initialConfig = config as C;
}

/**
Expand All @@ -78,10 +78,10 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
* @returns This controller instance.
*/
protected initialize() {
this.internalState = this.defaultState;
this.internalConfig = this.defaultConfig;
this.configure(this.initialConfig);
this.update(this.initialState);
this.#internalState = this.defaultState;
this.#internalConfig = this.defaultConfig;
this.configure(this.#initialConfig);
this.update(this.#initialState);
return this;
}

Expand All @@ -91,7 +91,7 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
* @returns The current configuration.
*/
get config() {
return this.internalConfig;
return this.#internalConfig;
}

/**
Expand All @@ -100,7 +100,7 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
* @returns The current state.
*/
get state() {
return this.internalState;
return this.#internalState;
}

/**
Expand All @@ -112,20 +112,20 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
*/
configure(config: Partial<C>, overwrite = false, fullUpdate = true) {
if (fullUpdate) {
this.internalConfig = overwrite
this.#internalConfig = overwrite
? (config as C)
: Object.assign(this.internalConfig, config);
: Object.assign(this.#internalConfig, config);

for (const key in this.internalConfig) {
if (typeof this.internalConfig[key] !== 'undefined') {
(this as any)[key as string] = this.internalConfig[key];
for (const key in this.#internalConfig) {
if (typeof this.#internalConfig[key] !== 'undefined') {
(this as any)[key as string] = this.#internalConfig[key];
}
}
} else {
for (const key in config) {
/* istanbul ignore else */
if (typeof this.internalConfig[key] !== 'undefined') {
this.internalConfig[key] = config[key] as any;
if (typeof this.#internalConfig[key] !== 'undefined') {
this.#internalConfig[key] = config[key] as any;
(this as any)[key as string] = config[key];
}
}
Expand All @@ -140,8 +140,8 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
return;
}

this.internalListeners.forEach((listener) => {
listener(this.internalState);
this.#internalListeners.forEach((listener) => {
listener(this.#internalState);
});
}

Expand All @@ -151,7 +151,7 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
* @param listener - The callback triggered when state changes.
*/
subscribe(listener: Listener<S>) {
this.internalListeners.push(listener);
this.#internalListeners.push(listener);
}

/**
Expand All @@ -161,8 +161,8 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
* @returns `true` if a listener is found and unsubscribed.
*/
unsubscribe(listener: Listener<S>) {
const index = this.internalListeners.findIndex((cb) => listener === cb);
index > -1 && this.internalListeners.splice(index, 1);
const index = this.#internalListeners.findIndex((cb) => listener === cb);
index > -1 && this.#internalListeners.splice(index, 1);
return index > -1;
}

Expand All @@ -173,9 +173,9 @@ export class BaseController<C extends BaseConfig, S extends BaseState> {
* @param overwrite - Overwrite state instead of merging.
*/
update(state: Partial<S>, overwrite = false) {
this.internalState = overwrite
this.#internalState = overwrite
? Object.assign({}, state as S)
: Object.assign({}, this.internalState, state);
: Object.assign({}, this.#internalState, state);
this.notify();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/BaseControllerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class BaseController<
S extends Record<string, Json>,
messenger extends RestrictedControllerMessenger<N, any, any, string, string>,
> {
private internalState: S;
#internalState: S;

protected messagingSystem: messenger;

Expand Down Expand Up @@ -120,7 +120,7 @@ export class BaseController<
}) {
this.messagingSystem = messenger;
this.name = name;
this.internalState = state;
this.#internalState = state;
this.metadata = metadata;

this.messagingSystem.registerActionHandler(
Expand All @@ -135,7 +135,7 @@ export class BaseController<
* @returns The current state.
*/
get state() {
return this.internalState;
return this.#internalState;
}

set state(_) {
Expand All @@ -162,9 +162,9 @@ export class BaseController<
state: S,
cb: typeof callback,
) => [S, Patch[], Patch[]]
)(this.internalState, callback);
)(this.#internalState, callback);

this.internalState = nextState;
this.#internalState = nextState;
this.messagingSystem.publish(
`${this.name}:stateChange` as Namespaced<N, any>,
nextState,
Expand Down
16 changes: 8 additions & 8 deletions src/ComposableController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export type ControllerList = (
* Controller that can be used to compose multiple controllers together
*/
export class ComposableController extends BaseController<never, any> {
private controllers: ControllerList = [];
#controllers: ControllerList = [];

private messagingSystem?: RestrictedControllerMessenger<
#messagingSystem?: RestrictedControllerMessenger<
'ComposableController',
never,
any,
Expand Down Expand Up @@ -58,16 +58,16 @@ export class ComposableController extends BaseController<never, any> {
}, {} as any),
);
this.initialize();
this.controllers = controllers;
this.messagingSystem = messenger;
this.controllers.forEach((controller) => {
this.#controllers = controllers;
this.#messagingSystem = messenger;
this.#controllers.forEach((controller) => {
const { name } = controller;
if ((controller as BaseController<any, any>).subscribe !== undefined) {
(controller as BaseController<any, any>).subscribe((state) => {
this.update({ [name]: state });
});
} else if (this.messagingSystem) {
(this.messagingSystem.subscribe as any)(
} else if (this.#messagingSystem) {
(this.#messagingSystem.subscribe as any)(
`${name}:stateChange`,
(state: any) => {
this.update({ [name]: state });
Expand All @@ -90,7 +90,7 @@ export class ComposableController extends BaseController<never, any> {
*/
get flatState() {
let flatState = {};
for (const controller of this.controllers) {
for (const controller of this.#controllers) {
flatState = { ...flatState, ...controller.state };
}
return flatState;
Expand Down
Loading