atom-state
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -10,7 +10,8 @@ /// <reference types="react" /> | ||
getAtomValue: typeof notInAContextWithReturn; | ||
getAtomPromise: () => undefined; | ||
getAtomPromise: () => Promise<void>; | ||
setAtomValue: typeof notInAContext; | ||
removeAtom: typeof notInAContextWithReturn; | ||
registerAtomGroup: () => () => void; | ||
} | ||
declare const AtomContext: import("react").Context<IAtomStore>; | ||
export default AtomContext; |
@@ -15,2 +15,4 @@ import Batcher from './Batcher'; | ||
}; | ||
export declare type GetDefaultValueType = (...params: any[]) => any; | ||
export declare type GetAtomValueOptionType = (...params: any) => AtomValueOption; | ||
export interface IAtomStore { | ||
@@ -21,5 +23,6 @@ subscribeAtom: (key: any, listener: AtomStoreListener) => () => void; | ||
getAtomValue: (key: any) => any; | ||
getAtomPromise(key: any): Promise<any> | undefined; | ||
getAtomPromise: (key: any) => Promise<any>; | ||
setAtomValue: (key: any, newValue: any, option?: AtomValueOption) => void; | ||
removeAtom: (key: any) => boolean; | ||
registerAtomGroup: (getKey: (...params: any[]) => any, getDefaultValue: GetDefaultValueType | any, getOption?: AtomValueOption | GetAtomValueOptionType) => (...params: any[]) => void; | ||
} | ||
@@ -62,3 +65,3 @@ export default class AtomStore implements IAtomStore { | ||
*/ | ||
getAtomPromise(key: any): Promise<any> | undefined; | ||
getAtomPromise(key: any): Promise<any>; | ||
/** | ||
@@ -95,2 +98,3 @@ * Get the fallback value for a promise | ||
removeAtom(key: any): boolean; | ||
registerAtomGroup(getKey: (...params: any[]) => any, getDefaultValue: GetDefaultValueType | any, getOption?: AtomValueOption | GetAtomValueOptionType): (...params: any[]) => void; | ||
} |
@@ -21,6 +21,10 @@ import React, { createContext, useContext, useCallback, useState, useEffect } from 'react'; | ||
notInAContext(); | ||
return undefined; | ||
return Promise.resolve(); | ||
}; | ||
this.setAtomValue = notInAContext; | ||
this.removeAtom = notInAContextWithReturn; | ||
this.registerAtomGroup = function () { | ||
notInAContext(); | ||
return function () { }; | ||
}; | ||
} | ||
@@ -127,12 +131,14 @@ return DefaultAtomStore; | ||
var atomPromise = this._atomPromises.get(key); | ||
if (!atomPromise) { | ||
return Promise.resolve(); | ||
} | ||
// The atom value is NOT a promise or has already gotten the result | ||
// return the content with a Promise | ||
if ((!this.isAtomPromise(key) && this.containsAtom(key)) || | ||
(atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.status) === 'success') { | ||
if (!this.isAtomPromise(key) || atomPromise.status === 'success') { | ||
return Promise.resolve(content); | ||
} | ||
if ((atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.status) === 'failed') { | ||
return Promise.reject(atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.error); | ||
if (atomPromise.status === 'failed') { | ||
return Promise.reject(atomPromise.error); | ||
} | ||
return atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.promise; | ||
return Promise.resolve(atomPromise.promise); | ||
}; | ||
@@ -234,2 +240,35 @@ /** | ||
}; | ||
AtomStore.prototype.registerAtomGroup = function (getKey, getDefaultValue, getOption) { | ||
var _this = this; | ||
var get = this.getAtomValue.bind(this); | ||
return function () { | ||
var params = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
params[_i] = arguments[_i]; | ||
} | ||
var key = getKey.apply(void 0, params); | ||
var value; | ||
if (typeof getDefaultValue === 'function') { | ||
value = getDefaultValue.apply(void 0, params); | ||
} | ||
else { | ||
value = getDefaultValue; | ||
} | ||
// getDefaultValue might be (...parms) => ({ get }) => any | ||
if (typeof value === 'function') { | ||
value = value({ get: get }); | ||
} | ||
var option; | ||
if (typeof getOption === 'function') { | ||
option = getOption.apply(void 0, params); | ||
} | ||
else { | ||
option = getOption; | ||
} | ||
if (!_this.containsAtom(key)) { | ||
_this.setAtomValue(key, value, option); | ||
} | ||
return key; | ||
}; | ||
}; | ||
return AtomStore; | ||
@@ -236,0 +275,0 @@ }()); |
@@ -28,6 +28,10 @@ 'use strict'; | ||
notInAContext(); | ||
return undefined; | ||
return Promise.resolve(); | ||
}; | ||
this.setAtomValue = notInAContext; | ||
this.removeAtom = notInAContextWithReturn; | ||
this.registerAtomGroup = function () { | ||
notInAContext(); | ||
return function () { }; | ||
}; | ||
} | ||
@@ -134,12 +138,14 @@ return DefaultAtomStore; | ||
var atomPromise = this._atomPromises.get(key); | ||
if (!atomPromise) { | ||
return Promise.resolve(); | ||
} | ||
// The atom value is NOT a promise or has already gotten the result | ||
// return the content with a Promise | ||
if ((!this.isAtomPromise(key) && this.containsAtom(key)) || | ||
(atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.status) === 'success') { | ||
if (!this.isAtomPromise(key) || atomPromise.status === 'success') { | ||
return Promise.resolve(content); | ||
} | ||
if ((atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.status) === 'failed') { | ||
return Promise.reject(atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.error); | ||
if (atomPromise.status === 'failed') { | ||
return Promise.reject(atomPromise.error); | ||
} | ||
return atomPromise === null || atomPromise === void 0 ? void 0 : atomPromise.promise; | ||
return Promise.resolve(atomPromise.promise); | ||
}; | ||
@@ -241,2 +247,35 @@ /** | ||
}; | ||
AtomStore.prototype.registerAtomGroup = function (getKey, getDefaultValue, getOption) { | ||
var _this = this; | ||
var get = this.getAtomValue.bind(this); | ||
return function () { | ||
var params = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
params[_i] = arguments[_i]; | ||
} | ||
var key = getKey.apply(void 0, params); | ||
var value; | ||
if (typeof getDefaultValue === 'function') { | ||
value = getDefaultValue.apply(void 0, params); | ||
} | ||
else { | ||
value = getDefaultValue; | ||
} | ||
// getDefaultValue might be (...parms) => ({ get }) => any | ||
if (typeof value === 'function') { | ||
value = value({ get: get }); | ||
} | ||
var option; | ||
if (typeof getOption === 'function') { | ||
option = getOption.apply(void 0, params); | ||
} | ||
else { | ||
option = getOption; | ||
} | ||
if (!_this.containsAtom(key)) { | ||
_this.setAtomValue(key, value, option); | ||
} | ||
return key; | ||
}; | ||
}; | ||
return AtomStore; | ||
@@ -243,0 +282,0 @@ }()); |
@@ -0,1 +1,11 @@ | ||
### 0.4.0 (2020-06-26) | ||
##### Chores | ||
* checkout master and pull before creating release ([afc95818](https://github.com/JimLiu/atom-state/commit/afc958181f9e3f4179c85b420f2171aba6e0b8e1)) | ||
##### New Features | ||
* Add new method `registerAtomGroup` for store ([361a2e77](https://github.com/JimLiu/atom-state/commit/361a2e77e5ce694b1dcc888656eaebf2179f027d)) | ||
#### 0.3.1 (2020-06-20) | ||
@@ -5,3 +15,3 @@ | ||
* Support options for creating default atom ([95217b57](https://github.com/JimLiu/atom-state/commit/95217b574f04e23c3dd23d860f3811810aa9bc7b)) | ||
- Support options for creating default atom ([95217b57](https://github.com/JimLiu/atom-state/commit/95217b574f04e23c3dd23d860f3811810aa9bc7b)) | ||
@@ -8,0 +18,0 @@ ### 0.3.0 (2020-06-20) |
{ | ||
"name": "atom-state", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "A state management library for React", | ||
@@ -13,5 +13,5 @@ "main": "build/index.js", | ||
"test": "jest --coverage", | ||
"release:major": "changelog -M && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major && git push origin && git push origin --tags", | ||
"release:minor": "changelog -m && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version minor && git push origin && git push origin --tags", | ||
"release:patch": "changelog -p && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch && git push origin && git push origin --tags" | ||
"release:major": "git checkout master && git pull && changelog -M && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major && git push origin && git push origin --tags", | ||
"release:minor": "git checkout master && git pull && changelog -m && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version minor && git push origin && git push origin --tags", | ||
"release:patch": "git checkout master && git pull && changelog -p && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch && git push origin && git push origin --tags" | ||
}, | ||
@@ -18,0 +18,0 @@ "repository": { |
@@ -25,6 +25,10 @@ import { createContext } from 'react' | ||
notInAContext() | ||
return undefined | ||
return Promise.resolve() | ||
} | ||
setAtomValue = notInAContext | ||
removeAtom = notInAContextWithReturn | ||
registerAtomGroup = () => { | ||
notInAContext() | ||
return () => {} | ||
} | ||
} | ||
@@ -31,0 +35,0 @@ |
@@ -66,10 +66,18 @@ import AtomStore from '../AtomStore' | ||
test('should return the original promise for a promise', () => { | ||
const store = createStore() | ||
const promise = Promise.resolve('bar') | ||
store.setAtomValue('foo', promise) | ||
expect(store.getAtomPromise('foo')).toBe(promise) | ||
}) | ||
test("should return undefined if key doesn's exist", () => { | ||
const store = createStore() | ||
expect(store.getAtomPromise('foo1')).toBeUndefined() | ||
expect(store.getAtomPromise('foo1')).resolves.toBeUndefined() | ||
}) | ||
test('should resolve a result', () => { | ||
test('should resolve a result', async () => { | ||
const store = createStore() | ||
store.setAtomValue('foo', Promise.resolve('bar')) | ||
await nextTick() | ||
expect(store.getAtomPromise('foo')).resolves.toBe('bar') | ||
@@ -84,2 +92,9 @@ }) | ||
test('should reject an error', async () => { | ||
const store = createStore() | ||
store.setAtomValue('foo', Promise.reject('error')) | ||
await nextTick() | ||
expect(store.getAtomPromise('foo')).rejects.toBe('error') | ||
}) | ||
test('should reject an error with fallback error message', async () => { | ||
@@ -283,1 +298,57 @@ const store = createStore() | ||
}) | ||
describe('registerAtomGroup()', () => { | ||
test('should register an atom group with key mygroup-${id}', () => { | ||
const store = createStore() | ||
const group = store.registerAtomGroup(id => `mygroup-${id}`, 1) | ||
const groupKey = group(1) | ||
expect(groupKey).toBe('mygroup-1') | ||
expect(store.containsAtom(groupKey)).toBe(true) | ||
expect(store.getAtomValue(groupKey)).toBe(1) | ||
}) | ||
test('should register an atom group with dynamic value', () => { | ||
const store = createStore() | ||
const group = store.registerAtomGroup( | ||
(id: any) => `mygroup-${id}`, | ||
(id: any) => id | ||
) | ||
expect(store.getAtomValue(group(2))).toBe(2) | ||
}) | ||
test('should register an atom group with dynamic value and get function', () => { | ||
const store = createStore({ | ||
seed: 2 | ||
}) | ||
const group = store.registerAtomGroup( | ||
(id: any) => `mygroup-${id}`, | ||
(id: any) => ({ get }: { get: (key: any) => any }) => get('seed') * id | ||
) | ||
expect(store.getAtomValue(group(2))).toBe(4) | ||
}) | ||
test('should register an atom group with option', () => { | ||
const store = createStore() | ||
const group = store.registerAtomGroup( | ||
(id: any) => `mygroup-${id}`, | ||
1, | ||
() => ({ isAsync: true }) | ||
) | ||
expect(store.isAtomPromise(group(2))).toBe(true) | ||
}) | ||
test('should NOT create new atom for an exists key', () => { | ||
const store = createStore({ | ||
'mygroup-1': -1 | ||
}) | ||
const group = store.registerAtomGroup(id => `mygroup-${id}`, 1) | ||
const groupKey = group(1) | ||
expect(store.getAtomValue(groupKey)).toBe(-1) | ||
}) | ||
}) |
@@ -23,2 +23,6 @@ import Batcher from './Batcher' | ||
export type GetDefaultValueType = (...params: any[]) => any | ||
export type GetAtomValueOptionType = (...params: any) => AtomValueOption | ||
export interface IAtomStore { | ||
@@ -29,5 +33,10 @@ subscribeAtom: (key: any, listener: AtomStoreListener) => () => void | ||
getAtomValue: (key: any) => any | ||
getAtomPromise(key: any): Promise<any> | undefined | ||
getAtomPromise: (key: any) => Promise<any> | ||
setAtomValue: (key: any, newValue: any, option?: AtomValueOption) => void | ||
removeAtom: (key: any) => boolean | ||
registerAtomGroup: ( | ||
getKey: (...params: any[]) => any, | ||
getDefaultValue: GetDefaultValueType | any, | ||
getOption?: AtomValueOption | GetAtomValueOptionType | ||
) => (...params: any[]) => void | ||
} | ||
@@ -108,20 +117,20 @@ | ||
*/ | ||
getAtomPromise (key: any): Promise<any> | undefined { | ||
const content: any = this.getAtomValue(key) | ||
getAtomPromise (key: any): Promise<any> { | ||
const content = this.getAtomValue(key) | ||
const atomPromise = this._atomPromises.get(key) | ||
if (!atomPromise) { | ||
return Promise.resolve() | ||
} | ||
// The atom value is NOT a promise or has already gotten the result | ||
// return the content with a Promise | ||
if ( | ||
(!this.isAtomPromise(key) && this.containsAtom(key)) || | ||
atomPromise?.status === 'success' | ||
) { | ||
if (!this.isAtomPromise(key) || atomPromise.status === 'success') { | ||
return Promise.resolve(content) | ||
} | ||
if (atomPromise?.status === 'failed') { | ||
return Promise.reject(atomPromise?.error) | ||
if (atomPromise.status === 'failed') { | ||
return Promise.reject(atomPromise.error) | ||
} | ||
return atomPromise?.promise | ||
return Promise.resolve(atomPromise.promise) | ||
} | ||
@@ -254,2 +263,37 @@ | ||
} | ||
registerAtomGroup ( | ||
getKey: (...params: any[]) => any, | ||
getDefaultValue: GetDefaultValueType | any, | ||
getOption?: AtomValueOption | GetAtomValueOptionType | ||
): (...params: any[]) => void { | ||
const get = this.getAtomValue.bind(this) | ||
return (...params: any[]) => { | ||
const key = getKey(...params) | ||
let value | ||
if (typeof getDefaultValue === 'function') { | ||
value = getDefaultValue(...params) | ||
} else { | ||
value = getDefaultValue | ||
} | ||
// getDefaultValue might be (...parms) => ({ get }) => any | ||
if (typeof value === 'function') { | ||
value = value({ get }) | ||
} | ||
let option | ||
if (typeof getOption === 'function') { | ||
option = getOption(...params) | ||
} else { | ||
option = getOption | ||
} | ||
if (!this.containsAtom(key)) { | ||
this.setAtomValue(key, value, option) | ||
} | ||
return key | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150277
2050