Skip to content
Merged
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
5 changes: 5 additions & 0 deletions packages/use-store/src/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export {
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";
14 changes: 11 additions & 3 deletions packages/use-store/src/experimental/useStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type HookState<S, T> = {
* scheduled to catch us up with the rest of the app.
*/
export function useStoreSelector<S, T>(
store: Store<S, any>,
store: Store<S, never>,
selector: (state: S) => T,
): T {
const storeManager = useContext(storeManagerContext);
Expand Down Expand Up @@ -189,7 +189,7 @@ export function useStoreSelector<S, T>(
function setHookState(value: T) {
setState((prev) => {
// If nothing has changed...
if (prev.value === value && prev.selector === selector) {
if (is(prev.value, value) && prev.selector === selector) {
// Preserve object identity.
return prev;
}
Expand Down Expand Up @@ -252,6 +252,14 @@ function identity<T>(x: T): T {
return x;
}

export function useStore<S>(store: Store<S, any>): S {
function is(x: unknown, y: unknown) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just use Object.is? I think that's what React claims to use. If this is expected to provide that same behavior maybe we need to add a comment explaining why this particular behavior?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets revisit this as a followup. I think the rest of the pr is fine so I'm going to merge.


export function useStore<S>(store: Store<S, never>): S {
return useStoreSelector(store, identity);
}
2 changes: 1 addition & 1 deletion packages/use-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { ReactStore } from "./types";
export type { ReactStore, ISource, Reducer } from "./types";
import * as Experimental from "./experimental";

export { createStore, useStore } from "./useStore";
Expand Down
Loading