Skip to content

Commit

Permalink
feat: add return State from loaded callback
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrashenko committed Dec 27, 2021
1 parent 7d771c8 commit b43375c
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 21 deletions.
6 changes: 5 additions & 1 deletion dist/vue-eternal-loading.common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/vue-eternal-loading.common.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion dist/vue-eternal-loading.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/vue-eternal-loading.umd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vue-eternal-loading.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vue-eternal-loading.umd.min.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/api/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ load(
// Optional params:
// - count - how many items has been loaded
// - pageSize - items per page count
// Returns:
// - State ( 'loading', 'no-more', 'no-results' )
loaded,

// Call when you have no more item
Expand Down
9 changes: 8 additions & 1 deletion docs/api/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## LoadAction
```ts
type LoadAction = {
loaded(count?: number, pageSize?: number): void;
loaded(count?: number, pageSize?: number): State;
noMore(): void;
noResults(): void;
error(): void;
Expand All @@ -24,3 +24,10 @@ Describes payload what we get in `loaded` prop callback.
type Position = 'top' | 'left' | 'default';
```
Describes possible loader positions.

## State
Added in `v1.2.0`
```ts
type State = 'loading' | 'error' | 'no-more' | 'no-results';
```
Describes possible state for loader.
14 changes: 14 additions & 0 deletions docs/guide/loading-states.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,17 @@ function load({ loaded }) {
---

We have one more state `error`, but we can't rich it automatically, just using `loaded` callback. It's because **vue-eternal-loading** have no idea about loading errors, and it can switch states based on information which you pass to `loaded` and it's not enough information to set `error` state. How to set `error` state manually we will learn further in the next sections.

---

If you want to know which state now is inside `load` function - `loaded()` callback returns it for you.
```js
function load({ loaded }) {
// Load data from server
// ...
const state = loaded();
if (state === 'no-more') {
alert('Boom! You have reached the end.')
}
}
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
],
"publishConfig": {
"access": "public"
}
},
"dependencies": {}
}
6 changes: 5 additions & 1 deletion src/components/VueEternalLoading/VueEternalLoading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,27 @@ export default defineComponent({
});
}
function loaded(count?: number, pageSize?: number) {
function loaded(count?: number, pageSize?: number): State {
if (count === 0) {
if (isFirstLoad.value) {
noResults();
return 'no-results';
} else {
noMore();
return 'no-more';
}
} else if (
count !== undefined &&
pageSize !== undefined &&
count < pageSize
) {
noMore();
return 'no-more';
} else {
isFirstLoad.value = false;
restoreScroll();
observe();
return 'loading';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ describe('VueEternalLoading', () => {
expect(observe).toHaveBeenCalledTimes(1);
expect(unobserve).toHaveBeenCalledTimes(1);
// @ts-ignore
action.loaded(5, 5);
let state = action.loaded(5, 5);
expect(state).toBe('loading');
expect(observe).toHaveBeenCalledTimes(2);
expect(unobserve).toHaveBeenCalledTimes(1);
await nextTick();
expect(wrapper.html()).toContain('STATE_LOADING');
// @ts-ignore
action.loaded(4, 5);
state = action.loaded(4, 5);
expect(state).toBe('no-more');
await nextTick();
expect(wrapper.html()).toContain('STATE_NO_MORE');
});
Expand All @@ -132,7 +134,8 @@ describe('VueEternalLoading', () => {
const wrapper = getComponent();
runCallback(true);
// @ts-ignore
action.loaded(0, 5);
const state = action.loaded(0, 5);
expect(state).toBe('no-results');
expect(observe).toHaveBeenCalledTimes(1);
expect(unobserve).toHaveBeenCalledTimes(1);
await nextTick();
Expand Down Expand Up @@ -243,15 +246,15 @@ describe('VueEternalLoading', () => {

test('container is null', async () => {
const wrapper = getComponent(true, null);
expect(observe).not.toHaveBeenCalled();
expect(observe).toHaveBeenCalled();
expect(unobserve).not.toHaveBeenCalled();

await wrapper.setProps({ container: document.documentElement });
expect(observe).toHaveBeenCalledTimes(1);
expect(unobserve).not.toHaveBeenCalled();

await wrapper.setProps({ container: document.body });
expect(observe).toHaveBeenCalledTimes(2);
expect(unobserve).toHaveBeenCalledTimes(1);

await wrapper.setProps({ container: document.body });
expect(observe).toHaveBeenCalledTimes(3);
expect(unobserve).toHaveBeenCalledTimes(2);
});
});
2 changes: 1 addition & 1 deletion src/components/VueEternalLoading/helpers/type/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type LoadAction = {
loaded(count?: number, pageSize?: number): void;
loaded(count?: number, pageSize?: number): State;
noMore(): void;
noResults(): void;
error(): void;
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type {
LoadAction,
LoadPayload,
Position,
State,
} from './components/VueEternalLoading/helpers/type/type';
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3273,9 +3273,9 @@ caniuse-api@^3.0.0:
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001219:
version "1.0.30001243"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001243.tgz#d9250155c91e872186671c523f3ae50cfc94a3aa"
integrity sha512-vNxw9mkTBtkmLFnJRv/2rhs1yufpDfCkBZexG3Y0xdOH2Z/eE/85E4Dl5j1YUN34nZVsSp6vVRFQRrez9wJMRA==
version "1.0.30001292"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001292.tgz"
integrity sha512-jnT4Tq0Q4ma+6nncYQVe7d73kmDmE9C3OGTx3MvW7lBM/eY1S1DZTMBON7dqV481RhNiS5OxD7k9JQvmDOTirw==

capture-exit@^2.0.0:
version "2.0.0"
Expand Down

0 comments on commit b43375c

Please sign in to comment.