Skip to content

Commit

Permalink
Fix types and use more shared utils
Browse files Browse the repository at this point in the history
  • Loading branch information
eolme committed Feb 6, 2021
1 parent b29861e commit a99c7e0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mntm/precoil",
"version": "1.4.0",
"version": "1.5.0",
"main": "dist/index.js",
"module": "dist/index.js",
"jsnext:main": "dist/index.js",
Expand Down
11 changes: 5 additions & 6 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import mitt from 'mitt';

import type { Store, Atom, AtomUpdater, AtomValOrUpdater, Selector } from './types';

import { weakUniqueId } from '@mntm/shared';

import mitt from 'mitt';
export const updater = mitt();

export const store: Store = {};
Expand All @@ -27,9 +28,7 @@ export const setter = <T>(key: string, value: AtomValOrUpdater<T>): T => {
return next;
};

let id = 0;
const generateKey = () => (++id) + Math.floor(Math.random() * 1E6).toString(32);
export const atom = <T>(defaultValue: T, key = generateKey()): Atom<T> => {
export const atom = <T>(defaultValue: T, key = weakUniqueId()): Atom<T> => {
store[key] = defaultValue;

return {
Expand All @@ -38,7 +37,7 @@ export const atom = <T>(defaultValue: T, key = generateKey()): Atom<T> => {
get() {
return getter(key);
},
set(value: T) {
set(value: AtomValOrUpdater<T>) {
return setter(key, value);
}
};
Expand Down

0 comments on commit a99c7e0

Please sign in to comment.