Skip to content
Open
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
16 changes: 12 additions & 4 deletions packages/use-store/src/experimental/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ function reactTransitionIsActive() {
return !!sharedReactInternals.T;
}

export class Store<S, A> extends Emitter<[]> {
private source: ISource<S, A>;
private state: S;
private committedState: S;
export interface ReactStore<S, A = never> {
getState(): S;
getCommittedState(): S;
handleUpdate(action: A): void;
subscribe(listener: () => void): () => void;
commit(state: S): void;
}

export class Store<S, A> extends Emitter<[]> implements ReactStore<S, A> {
source: ISource<S, A>;
state: S;
committedState: S;
constructor(source: ISource<S, A>) {
super();
this.source = source;
Expand Down
14 changes: 8 additions & 6 deletions packages/use-store/src/experimental/StoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ type RefCountedSubscription = {
unsubscribe: () => void;
};

type StoresSnapshot = Map<Store<unknown, unknown>, unknown>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyStore = Store<any, never>;

type StoresSnapshot = Map<AnyStore, unknown>;

/**
* StoreManager tracks all actively rendered stores in the tree and maintains a
Expand All @@ -15,8 +18,7 @@ type StoresSnapshot = Map<Store<unknown, unknown>, unknown>;
* state.
*/
export class StoreManager extends Emitter<[]> {
_storeRefCounts: Map<Store<unknown, unknown>, RefCountedSubscription> =
new Map();
_storeRefCounts: Map<AnyStore, RefCountedSubscription> = new Map();

getAllCommittedStates(): StoresSnapshot {
return new Map(
Expand All @@ -36,7 +38,7 @@ export class StoreManager extends Emitter<[]> {
);
}

addStore(store: Store<any, any>) {
addStore(store: AnyStore) {
const prev = this._storeRefCounts.get(store);
if (prev == null) {
this._storeRefCounts.set(store, {
Expand All @@ -57,11 +59,11 @@ export class StoreManager extends Emitter<[]> {
this.sweep();
}

removeStore(store: Store<any, any>) {
removeStore(store: AnyStore) {
const prev = this._storeRefCounts.get(store);
if (prev == null) {
throw new Error(
"Imblance in concurrent-safe store reference counting. This is a bug in react-use-store, please report it.",
"Imbalance in concurrent-safe store reference counting. This is a bug in react-use-store, please report it.",
);
}
// We decrement the count here, but don't actually do the cleanup. This is
Expand Down
6 changes: 6 additions & 0 deletions packages/use-store/src/experimental/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export {
useStore,
useStoreSelector,
useStoreSelectorWithEquality,
createStore,
createStoreFromSource,
StoreProvider,
} from "./useStore";

// Export types needed for public API
export type { ISource, Reducer } from "../types";
export type { ReactStore } from "./Store";
export { Store } from "./Store";
Loading