Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[♻️]: Refactor to cleanup types and update documentation #6

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor to cleanup subscribe entry type
  • Loading branch information
mimshins committed May 17, 2024
commit d8ad6eccd0d01bdbc124ceb61981b7205f91c922
28 changes: 15 additions & 13 deletions src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import * as React from "react";
import type {
CallableFunction,
ContainerInitializer,
DefaultSubscribeEntry,
EqualityCheckFunction,
SelectedSubscribeEntry,
SubscribeCallback,
SubscribeEntry,
Unsubscribe,
ValueSelector,
} from "./types";
Expand All @@ -17,10 +19,7 @@ import {

class Container<T> {
private _value: T;
private _subscribers: Set<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
SubscribeCallback<T> | SelectedSubscribeEntry<T, any>
>;
private _subscribers: Set<SubscribeEntry<T>>;

constructor(initializer: ContainerInitializer<T>) {
const initialValue =
Expand All @@ -37,10 +36,15 @@ class Container<T> {
* and returns the unsubscribe function.
*/
public subscribe(subscribeCallback: SubscribeCallback<T>): Unsubscribe {
this._subscribers.add(subscribeCallback);
const entry: DefaultSubscribeEntry<T> = {
type: "default",
subscribeCallback,
};

this._subscribers.add(entry);

const unsubscribe: Unsubscribe = () => {
this._subscribers.delete(subscribeCallback);
this._subscribers.delete(entry);
};

return unsubscribe;
Expand All @@ -61,7 +65,7 @@ class Container<T> {
isEqual?: EqualityCheckFunction<P>,
): Unsubscribe {
const entry: SelectedSubscribeEntry<T, P> = {
type: "SELECTED_SUBSCRIBE_ENTRY",
type: "selected",
selector,
subscribeCallback,
isEqual,
Expand Down Expand Up @@ -95,9 +99,11 @@ class Container<T> {
this._value = newValue;

this._subscribers.forEach(entry => {
if ("type" in entry) {
if (entry.type !== "SELECTED_SUBSCRIBE_ENTRY") return;
if (entry.type === "default") {
if (Object.is(prevValue, newValue)) return;

entry.subscribeCallback(newValue);
} else if (entry.type === "selected") {
const { selector, subscribeCallback, isEqual } = entry;

const selectedValue = selector(newValue) as unknown;
Expand All @@ -111,10 +117,6 @@ class Container<T> {
if (!shouldEmit) return;

subscribeCallback(selectedValue);
} else {
if (Object.is(prevValue, newValue)) return;

entry(newValue);
}
});
}
Expand Down
20 changes: 14 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export type CallableFunction<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
TArgs extends any[] = [],
TReturn = void,
> = (...args: TArgs) => TReturn;
/* eslint-disable @typescript-eslint/no-explicit-any */
export type CallableFunction<TArgs extends any[] = [], TReturn = void> = (
...args: TArgs
) => TReturn;

export type SubscribeCallback<T> = (value: T) => void;
export type Unsubscribe = () => void;
Expand All @@ -12,10 +11,19 @@ export type ValueSelector<T, P> = (value: T) => P;
export type EqualityCheckFunction<P> = (a: P, b: P) => boolean;

export type SelectedSubscribeEntry<T, P> = {
type: "SELECTED_SUBSCRIBE_ENTRY";
type: "selected";
subscribeCallback: SubscribeCallback<P>;
selector: ValueSelector<T, P>;
isEqual?: EqualityCheckFunction<P>;
};

export type DefaultSubscribeEntry<T> = {
type: "default";
subscribeCallback: SubscribeCallback<T>;
};

export type SubscribeEntry<T> =
| DefaultSubscribeEntry<T>
| SelectedSubscribeEntry<T, any>;

export type ContainerInitializer<T> = T | CallableFunction<[], T>;