rlz-engine 1.0.23 → 1.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { create } from 'zustand';
2
2
  import { persist } from 'zustand/middleware';
3
+ import { cacheLastResult } from './cached';
3
4
  const AUTH_ID_KEY = 'AUTH_ID';
4
5
  const AUTH_NAME_KEY = 'AUTH_NAME';
5
6
  const AUTH_EMAIL_KEY = 'AUTH_EMAIL';
@@ -8,7 +9,13 @@ localStorage.removeItem(AUTH_ID_KEY);
8
9
  localStorage.removeItem(AUTH_NAME_KEY);
9
10
  localStorage.removeItem(AUTH_EMAIL_KEY);
10
11
  localStorage.removeItem(AUTH_TEMP_PASSWORD_KEY);
11
- let cachedAuthParam = null;
12
+ // eslint-disable-next-line @typescript-eslint/naming-convention
13
+ const makeAuthParam = cacheLastResult(function makeAuthParam(id, tempPassword) {
14
+ if (id === null || tempPassword === null) {
15
+ return null;
16
+ }
17
+ return { userId: id, tempPassword };
18
+ });
12
19
  // eslint-disable-next-line @typescript-eslint/naming-convention
13
20
  export const useAuthState = create()(persist((set, get) => ({
14
21
  id: null,
@@ -16,20 +23,8 @@ export const useAuthState = create()(persist((set, get) => ({
16
23
  email: null,
17
24
  tempPassword: null,
18
25
  getAuthParam: () => {
19
- const id = get().id;
20
- const tempPassword = get().tempPassword;
21
- if (id === null || tempPassword === null) {
22
- return null;
23
- }
24
- if (id === cachedAuthParam?.userId
25
- && tempPassword === cachedAuthParam?.tempPassword) {
26
- return cachedAuthParam;
27
- }
28
- cachedAuthParam = {
29
- userId: id,
30
- tempPassword
31
- };
32
- return cachedAuthParam;
26
+ const { id, tempPassword } = get();
27
+ return makeAuthParam(id, tempPassword);
33
28
  },
34
29
  login: (id, name, email, tempPassword) => set({ id, name, email, tempPassword }),
35
30
  logout: () => set({ id: null, name: null, email: null, tempPassword: null })
@@ -0,0 +1 @@
1
+ export declare function cacheLastResult<FnT extends (...args: any) => unknown>(fn: FnT): FnT;
@@ -0,0 +1,13 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export function cacheLastResult(fn) {
3
+ let lastArgs = null;
4
+ let lastResult = undefined;
5
+ return ((...args) => {
6
+ if (lastArgs !== null && lastArgs.every((arg, i) => arg === args[i])) {
7
+ return lastResult;
8
+ }
9
+ lastArgs = args;
10
+ lastResult = fn(...args);
11
+ return lastResult;
12
+ });
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rlz-engine",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "description": "Deps and tools for my style of development",
5
5
  "scripts": {
6
6
  "build": "tsc",