Skip to content

Commit

Permalink
Release dynamic atoms & SSR support
Browse files Browse the repository at this point in the history
  • Loading branch information
eolme committed Apr 16, 2022
1 parent c5f80a8 commit a64ec96
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
29 changes: 18 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"name": "@mntm/precoil",
"version": "5.0.0-rc.0",
"version": "5.0.0",
"type": "module",
"main": "./lib/index.js",
"module": "./lib/index.js",
"jsnext:main": "./lib/index.js",
"types": "./lib/index.d.ts",
"exports": {
"import": "./lib/index.js",
"types": "./lib/index.d.ts"
},
"sideEffects": false,
"author": {
"name": "Anton Petrov",
Expand All @@ -31,24 +35,27 @@
"build": "tsc"
},
"dependencies": {
"@mntm/shared": "^3.6.1",
"@mntm/shared": "^5.0.0",
"mitt": "^3.0.0"
},
"devDependencies": {
"@mntm/eslint-config": "^0.4.4",
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"eslint": "^7.32.0",
"shx": "^0.3.3",
"typescript": "^4.5.2"
"@mntm/eslint-config": "^1.1.2",
"@types/react": "^18.0.5",
"@types/react-dom": "^18.0.1",
"eslint": "^8.13.0",
"shx": "^0.3.4",
"typescript": "^4.6.3"
},
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^18 || ^17",
"react-dom": "^18 || ^17"
},
"peerDependenciesMeta": {
"react": {
"optional": true
"optional": false
},
"react-dom": {
"optional": false
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
} from './store.js';

export {
resetAtom,
resetAtoms,
hydrateAtom
} from './ssr.js';
Expand Down
17 changes: 15 additions & 2 deletions src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import type { Atom } from './types.js';

import { store } from './store.js';

/**
* Sets the state of the passed atom.
*/
export const hydrateAtom = <T>(atom: Readonly<Atom<T>>, value: Readonly<T>) => {
store.set(atom.key, value);
};

export const resetAtoms = (atoms: Readonly<Array<Readonly<Atom<any>>>>) => {
atoms.forEach((atom) => store.set(atom.key, atom.def));
/**
* Resets a state of passed atom.
*/
export const resetAtom = <T>(atom: Readonly<Atom<T>>) => {
store.set(atom.key, atom.def);
};

/**
* Resets states of passed atoms.
*/
export const resetAtoms = (atoms: ReadonlyArray<Readonly<Atom<any>>>) => {
atoms.forEach(resetAtom);
};
33 changes: 27 additions & 6 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const store: Store = new Map();
*
* @param defaultValue Initial atom state.
*
* @param key A unique string that allows you to indetify the atom.
* @param key A unique string that allows you to identify the atom.
*
* @returns A new atom that lets you read and update its state.
*
Expand Down Expand Up @@ -95,12 +95,33 @@ export const atom = <T>(defaultValue: T, key = weakUniqueId()): Atom<T> => {
};

/**
* @todo TODO
* An dynamic atom represents state in precoil.
*
* @param get
* @param set
* @param key
* @returns
* @description Dynamic atoms depend on other atoms.
*
* @template T Dynamic atom state type.
*
* @param get Function to get states of other atoms.
*
* @param set Function to set new states to other atoms.
*
* @param key A unique string that allows you to identify the atom.
*
* @returns A new dynamic atom that lets you read and update dependent
* atoms state.
*
* @example
*
* const dynamicCounterAtom = dynamicAtom(
* (get) => get(counterAtom),
* (get, set, arg) => set(counterAtom, arg),
* 'dynamicCount'
* );
*
* console.log(dynamicCounterAtom.get()); // log: 0
* console.log(dynamicCounterAtom.set(1)); // log: 1
*
* @noinline
*/
export const dynamicAtom = <T>(
get: (
Expand Down

0 comments on commit a64ec96

Please sign in to comment.