Skip to content
This repository was archived by the owner on Jul 31, 2020. It is now read-only.

Commit e0c51b2

Browse files
committed
feat: updateAPI to use options obj for everything
BREAKING CHANGE: Not backwards compatible with previous API
1 parent 5dca25c commit e0c51b2

File tree

3 files changed

+87
-45
lines changed

3 files changed

+87
-45
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import { when } from 'mobx';
1919
import { createAsyncStore } from 'mst-async-store';
2020

2121
// Generate store model
22-
const MyAsyncStore = createAsyncStore(
23-
'MyAsyncStore', // Name of store
24-
MyModel, // Your MST model representing one item
25-
(self) => (
22+
const MyAsyncStore = createAsyncStore({
23+
name: 'MyAsyncStore',
24+
itemModel: MyModel,
25+
ttl: 10000,
26+
failstateTtl: 5000
27+
fetchActions: (self) => (
2628
{
2729
// Logic to fetch one item
2830
async fetchOne(id: string) {
@@ -39,11 +41,9 @@ const MyAsyncStore = createAsyncStore(
3941
const data = await axios.get(`/all`);
4042
return data.response.map((d) => MyModel.create(d));
4143
},
42-
},
43-
// Store options
44-
{ ttl: 10000, failstateTtl: 5000 }
44+
}
4545
)
46-
);
46+
});
4747

4848
// Instantiate store
4949
const myAsyncStore = MyAsyncStore.create();

src/__tests__/createAsyncStore.spec.ts

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,33 @@ const DummyModel = types.model('DummyModel', { id: types.identifier });
66

77
describe('createAsyncStore', () => {
88
it('should create AsyncStore model', () => {
9-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel);
9+
const AsyncStore = createAsyncStore({
10+
name: 'AsyncStore',
11+
itemModel: DummyModel,
12+
});
1013
expect(AsyncStore.name).toBe('AsyncStore');
1114
});
1215

1316
it('should create asyncStore instance', () => {
14-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel);
17+
const AsyncStore = createAsyncStore({
18+
name: 'AsyncStore',
19+
itemModel: DummyModel,
20+
});
1521
const asyncStore = AsyncStore.create();
1622
expect(asyncStore.isReady).toBe(false);
1723
});
1824

1925
it('should fetch one item', async () => {
2026
const dummyItem = DummyModel.create({ id: 'foo' });
21-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel, () => ({
22-
async fetchOne() {
23-
return dummyItem;
24-
},
25-
}));
27+
const AsyncStore = createAsyncStore({
28+
name: 'AsyncStore',
29+
itemModel: DummyModel,
30+
fetchActions: () => ({
31+
async fetchOne() {
32+
return dummyItem;
33+
},
34+
}),
35+
});
2636
const asyncStore = AsyncStore.create();
2737
const container = asyncStore.getOne('foo');
2838
await when(() => container.isReady);
@@ -34,11 +44,15 @@ describe('createAsyncStore', () => {
3444
it('should fetch many items', async () => {
3545
const dummyItem1 = DummyModel.create({ id: 'foo' });
3646
const dummyItem2 = DummyModel.create({ id: 'bar' });
37-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel, () => ({
38-
async fetchMany() {
39-
return [dummyItem1, dummyItem2];
40-
},
41-
}));
47+
const AsyncStore = createAsyncStore({
48+
name: 'AsyncStore',
49+
itemModel: DummyModel,
50+
fetchActions: () => ({
51+
async fetchMany() {
52+
return [dummyItem1, dummyItem2];
53+
},
54+
}),
55+
});
4256
const asyncStore = AsyncStore.create();
4357
const containers = asyncStore.getMany(['foo', 'bar']);
4458
await when(() => asyncStore.isReady);
@@ -50,11 +64,15 @@ describe('createAsyncStore', () => {
5064
it('should fetch all items', async () => {
5165
const dummyItem1 = DummyModel.create({ id: 'foo' });
5266
const dummyItem2 = DummyModel.create({ id: 'bar' });
53-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel, () => ({
54-
async fetchAll() {
55-
return [dummyItem1, dummyItem2];
56-
},
57-
}));
67+
const AsyncStore = createAsyncStore({
68+
name: 'AsyncStore',
69+
itemModel: DummyModel,
70+
fetchActions: () => ({
71+
async fetchAll() {
72+
return [dummyItem1, dummyItem2];
73+
},
74+
}),
75+
});
5876
const asyncStore = AsyncStore.create();
5977
asyncStore.getAll();
6078
await when(() => asyncStore.isReady);
@@ -65,11 +83,15 @@ describe('createAsyncStore', () => {
6583
});
6684

6785
it('should fail to fetch one item', async () => {
68-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel, () => ({
69-
async fetchOne() {
70-
throw Error('Failed to fetch item');
71-
},
72-
}));
86+
const AsyncStore = createAsyncStore({
87+
name: 'AsyncStore',
88+
itemModel: DummyModel,
89+
fetchActions: () => ({
90+
async fetchOne() {
91+
throw Error('Failed to fetch item');
92+
},
93+
}),
94+
});
7395
const asyncStore = AsyncStore.create();
7496
const container = asyncStore.getOne('foo');
7597
await when(() => container.isReady);
@@ -79,11 +101,15 @@ describe('createAsyncStore', () => {
79101
});
80102

81103
it('should fail to fetch many items', async () => {
82-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel, () => ({
83-
async fetchMany() {
84-
throw Error('Failed to fetch items');
85-
},
86-
}));
104+
const AsyncStore = createAsyncStore({
105+
name: 'AsyncStore',
106+
itemModel: DummyModel,
107+
fetchActions: () => ({
108+
async fetchMany() {
109+
throw Error('Failed to fetch items');
110+
},
111+
}),
112+
});
87113
const asyncStore = AsyncStore.create();
88114
const containers = asyncStore.getMany(['foo', 'bar']);
89115
await when(() => asyncStore.isReady);
@@ -95,23 +121,32 @@ describe('createAsyncStore', () => {
95121
});
96122

97123
it('should throw exception when not supporting fetchOne', async () => {
98-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel);
124+
const AsyncStore = createAsyncStore({
125+
name: 'AsyncStore',
126+
itemModel: DummyModel,
127+
});
99128
const asyncStore = AsyncStore.create();
100129
expect(asyncStore._fetchOne('foo')).rejects.toEqual(
101130
Error("Store doesn't support fetchOne")
102131
);
103132
});
104133

105134
it('should throw exception when not supporting fetchMany', async () => {
106-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel);
135+
const AsyncStore = createAsyncStore({
136+
name: 'AsyncStore',
137+
itemModel: DummyModel,
138+
});
107139
const asyncStore = AsyncStore.create();
108140
expect(asyncStore._fetchMany(['foo', 'bar'])).rejects.toEqual(
109141
Error("Store doesn't support fetchMany")
110142
);
111143
});
112144

113145
it('should throw exception when not supporting fetchAll', async () => {
114-
const AsyncStore = createAsyncStore('AsyncStore', DummyModel);
146+
const AsyncStore = createAsyncStore({
147+
name: 'AsyncStore',
148+
itemModel: DummyModel,
149+
});
115150
const asyncStore = AsyncStore.create();
116151
expect(asyncStore._fetchAll()).rejects.toEqual(
117152
Error("Store doesn't support fetchAll")

src/createAsyncStore.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,27 @@ export interface VolatileAsyncStoreState {
3030
error?: Error;
3131
}
3232

33-
export interface AsyncStoreOptions {
33+
export interface AsyncStoreOptions<T extends IAnyModelType> {
34+
name?: string;
35+
itemModel: T;
3436
ttl?: number;
3537
failstateTtl?: number;
3638
batch?: number;
39+
fetchActions?: AsyncFetchActions<T>;
3740
}
3841

3942
export function createAsyncStore<T extends IAnyModelType>(
40-
name: string,
41-
ItemModel: T,
42-
fetchActions?: AsyncFetchActions<T>,
43-
options?: AsyncStoreOptions
43+
options: AsyncStoreOptions<T>
4444
) {
45-
const { ttl = 0, failstateTtl = 10000, batch = 40 } = options || {};
46-
const AsyncContainer = createAsyncContainer<T>(ItemModel, {
45+
const {
46+
name = 'AnonymouseAsyncStore',
47+
itemModel,
48+
ttl = 0,
49+
failstateTtl = 10000,
50+
batch = 40,
51+
fetchActions,
52+
} = options;
53+
const AsyncContainer = createAsyncContainer<T>(itemModel, {
4754
ttl,
4855
failstateTtl,
4956
});

0 commit comments

Comments
 (0)