Skip to content

Commit

Permalink
add run at a different place
Browse files Browse the repository at this point in the history
  • Loading branch information
ibnlanre committed Oct 2, 2023
1 parent ca407ab commit f38cb5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/component/atom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AtomConfig } from "definition";
import { Atom, AtomConfig } from "definition";
import { AtomSubject } from "subject";
import { isAtomStateFunction } from "utilities";

Expand All @@ -18,9 +18,11 @@ export function atom<
Context extends {
[key: string]: any;
} = {}
>(config: AtomConfig<State, Used, Use, Data, Context>) {
>(
config: AtomConfig<State, Used, Use, Data, Context>
): Atom<State, Used, Use, Data, Context> {
const { state, actions, context = {} as Context } = config;
const { set, get } = { ...actions };
const { set, get, run, use } = { ...actions };

const observable = new AtomSubject(
isAtomStateFunction<State, Context>(state) ? state(context) : state
Expand Down Expand Up @@ -92,7 +94,7 @@ export function atom<
then: observable.value,
now: value,
used: usage.used,
use,
use: makeUse,
};

// The set function allows optional transformations and returns the new state.
Expand All @@ -110,9 +112,9 @@ export function atom<
get: (value: State) => {
const params = {
then: observable.value,
used: usage.used,
now: value,
use,
used: usage.used,
use: makeUse,
};

// The get function allows optional transformations and returns the transformed value.
Expand Down Expand Up @@ -160,19 +162,17 @@ export function atom<
/**
* Execute the `use` function with optional arguments and update `used`.
*
* @param {...Use} args Optional arguments to pass to the `run` function.
* @param {...Use} args Optional arguments to pass to the `use` function.
* @returns {Props}
*/
run: (...args: Use) => {
usage.used = actions?.use?.(props, ...args);
return props;
},
use: makeUse,
};

function use(...args: Use): Used | undefined {
const result = actions?.use?.(fields, ...args);
function makeUse(...args: Use): Used | undefined {
const result = use?.(fields, ...args);
return (usage.used = result);
}


if (run) makeUse(...run);
return props;
}
10 changes: 8 additions & 2 deletions src/component/useAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ import { isSetStateFunction } from "utilities";
*
* @returns {[Data, (value: State | SetStateAction<State>) => void]} An array containing the atom's data and a function to set its state.
*/
export function useAtom<State, Use extends ReadonlyArray<any>, Data, Context>(
store: Atom<State, Use, Data, Context>,
export function useAtom<
State,
Used,
Use extends ReadonlyArray<any>,
Data,
Context
>(
store: Atom<State, Used, Use, Data, Context>,
singleton?: (ctx: Context) => void
) {
const { get, set, next, subscribe } = store;
Expand Down
19 changes: 14 additions & 5 deletions src/definition/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,13 @@ export interface Actions<
Data,
Context
> {
use?: (fields: Fields<State, Data, Context>, ...args: Use) => Used;
get?: (values: Values<State, Used, Use>, context: Context) => Data;
run?: Use;
set?: (values: Values<State, Used, Use>, context: Context) => State;
get?: (values: Values<State, Used, Use>, context: Context) => Data;
use?: <Value = Data>(
fields: Fields<State, Value, Context>,
...args: Use
) => Used;
}

export type AtomConfig<
Expand All @@ -205,9 +209,14 @@ export type AtomConfig<
context?: Context;
};

export interface Atom<State, Use extends ReadonlyArray<any>, Data, Context>
extends Fields<State, Data, Context> {
run: (...args: Use) => Atom<State, Use, Data, Context>;
export interface Atom<
State,
Used,
Use extends ReadonlyArray<any>,
Data,
Context
> extends Fields<State, Data, Context> {
use: (...args: Use) => Used | undefined;
}

export * from "./cookieOptions";
Expand Down

0 comments on commit f38cb5e

Please sign in to comment.