@laser-ui/hooks 0.4.1 → 0.6.0
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.
- package/CHANGELOG.md +8 -0
- package/index.d.ts +0 -2
- package/index.js +0 -2
- package/package.json +3 -6
- package/storage/index.d.ts +0 -4
- package/storage/index.js +0 -4
- package/storage/localStorage.d.ts +0 -8
- package/storage/localStorage.js +0 -16
- package/storage/parser.d.ts +0 -10
- package/storage/parser.js +0 -14
- package/storage/storage.d.ts +0 -7
- package/storage/storage.js +0 -2
- package/storage/store.d.ts +0 -18
- package/storage/store.js +0 -64
- package/storage/useStorage.d.ts +0 -18
- package/storage/useStorage.js +0 -36
- package/useACL.d.ts +0 -14
- package/useACL.js +0 -79
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
# [0.6.0](https://github.com/laser-ui/laser-ui/compare/v0.5.0...v0.6.0) (2024-03-06)
|
|
6
|
+
|
|
7
|
+
**Note:** Version bump only for package @laser-ui/hooks
|
|
8
|
+
|
|
9
|
+
# [0.5.0](https://github.com/laser-ui/laser-ui/compare/v0.4.1...v0.5.0) (2024-01-31)
|
|
10
|
+
|
|
11
|
+
**Note:** Version bump only for package @laser-ui/hooks
|
|
12
|
+
|
|
5
13
|
## [0.4.1](https://github.com/laser-ui/laser-ui/compare/v0.4.0...v0.4.1) (2023-12-12)
|
|
6
14
|
|
|
7
15
|
**Note:** Version bump only for package @laser-ui/hooks
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laser-ui/hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Shared hooks used by Laser UI packages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ui",
|
|
@@ -29,14 +29,11 @@
|
|
|
29
29
|
"lodash": "^4.17.21"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"immer": ">=2.0.0"
|
|
33
|
-
"react": "^18.0.0",
|
|
34
|
-
"react-dom": "^18.0.0",
|
|
35
|
-
"tslib": "^2.0.0"
|
|
32
|
+
"immer": ">=2.0.0"
|
|
36
33
|
},
|
|
37
34
|
"publishConfig": {
|
|
38
35
|
"access": "public",
|
|
39
36
|
"directory": "../../dist/libs/hooks"
|
|
40
37
|
},
|
|
41
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "b131bc22712979a5de35f237d1b0ef2d490ae62e"
|
|
42
39
|
}
|
package/storage/index.d.ts
DELETED
package/storage/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { AbstractStorage } from './storage';
|
|
2
|
-
export declare class LocalStorageService extends AbstractStorage<string, string> {
|
|
3
|
-
getItem(key: string): string | null;
|
|
4
|
-
getItem(key: string, defaultValue: string): string;
|
|
5
|
-
setItem(key: string, value: string): void;
|
|
6
|
-
removeItem(key: string): void;
|
|
7
|
-
clear(): void;
|
|
8
|
-
}
|
package/storage/localStorage.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { AbstractStorage } from './storage';
|
|
2
|
-
export class LocalStorageService extends AbstractStorage {
|
|
3
|
-
getItem(key, defaultValue) {
|
|
4
|
-
var _a, _b;
|
|
5
|
-
return (_b = (_a = localStorage.getItem(key)) !== null && _a !== void 0 ? _a : defaultValue) !== null && _b !== void 0 ? _b : null;
|
|
6
|
-
}
|
|
7
|
-
setItem(key, value) {
|
|
8
|
-
localStorage.setItem(key, value);
|
|
9
|
-
}
|
|
10
|
-
removeItem(key) {
|
|
11
|
-
localStorage.removeItem(key);
|
|
12
|
-
}
|
|
13
|
-
clear() {
|
|
14
|
-
localStorage.clear();
|
|
15
|
-
}
|
|
16
|
-
}
|
package/storage/parser.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export interface Parser<V, O> {
|
|
2
|
-
deserializer: (value: V) => O;
|
|
3
|
-
serializer: (value: O) => V;
|
|
4
|
-
}
|
|
5
|
-
export interface AbstractParserOptions<V> {
|
|
6
|
-
plain: Parser<V, string>;
|
|
7
|
-
number: Parser<V, number>;
|
|
8
|
-
json: Parser<V, any>;
|
|
9
|
-
}
|
|
10
|
-
export declare const STRING_PARSER: AbstractParserOptions<string>;
|
package/storage/parser.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export const STRING_PARSER = {
|
|
2
|
-
plain: {
|
|
3
|
-
serializer: (value) => value,
|
|
4
|
-
deserializer: (value) => value,
|
|
5
|
-
},
|
|
6
|
-
number: {
|
|
7
|
-
serializer: (value) => String(value),
|
|
8
|
-
deserializer: (value) => Number(value),
|
|
9
|
-
},
|
|
10
|
-
json: {
|
|
11
|
-
serializer: (value) => JSON.stringify(value),
|
|
12
|
-
deserializer: (value) => JSON.parse(value),
|
|
13
|
-
},
|
|
14
|
-
};
|
package/storage/storage.d.ts
DELETED
package/storage/storage.js
DELETED
package/storage/store.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { AbstractStorage } from './storage';
|
|
2
|
-
declare class Store {
|
|
3
|
-
private _listeners;
|
|
4
|
-
private _key;
|
|
5
|
-
private _storage;
|
|
6
|
-
constructor(key: any, storage: AbstractStorage<any, any>);
|
|
7
|
-
subscribe(onStoreChange: () => void): () => void;
|
|
8
|
-
getSnapshot(): any;
|
|
9
|
-
emitChange(): void;
|
|
10
|
-
}
|
|
11
|
-
export declare const stores: Map<any, Store>;
|
|
12
|
-
export declare function useStore(key: any, defaultValue: any, storage: AbstractStorage<any, any>): {
|
|
13
|
-
subscribe: (onStoreChange: () => void) => () => void;
|
|
14
|
-
getSnapshot: () => any;
|
|
15
|
-
getServerSnapshot: () => any;
|
|
16
|
-
emitChange: () => void;
|
|
17
|
-
};
|
|
18
|
-
export {};
|
package/storage/store.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
2
|
-
class Store {
|
|
3
|
-
constructor(key, storage) {
|
|
4
|
-
Object.defineProperty(this, "_listeners", {
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true,
|
|
8
|
-
value: []
|
|
9
|
-
});
|
|
10
|
-
Object.defineProperty(this, "_key", {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
configurable: true,
|
|
13
|
-
writable: true,
|
|
14
|
-
value: void 0
|
|
15
|
-
});
|
|
16
|
-
Object.defineProperty(this, "_storage", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
configurable: true,
|
|
19
|
-
writable: true,
|
|
20
|
-
value: void 0
|
|
21
|
-
});
|
|
22
|
-
this._key = key;
|
|
23
|
-
this._storage = storage;
|
|
24
|
-
}
|
|
25
|
-
subscribe(onStoreChange) {
|
|
26
|
-
this._listeners = this._listeners.concat([onStoreChange]);
|
|
27
|
-
return () => {
|
|
28
|
-
this._listeners = this._listeners.filter((f) => f !== onStoreChange);
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
getSnapshot() {
|
|
32
|
-
return this._storage.getItem(this._key);
|
|
33
|
-
}
|
|
34
|
-
emitChange() {
|
|
35
|
-
for (const listener of this._listeners) {
|
|
36
|
-
listener();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
export const stores = new Map();
|
|
41
|
-
export function useStore(key, defaultValue, storage) {
|
|
42
|
-
return useMemo(() => {
|
|
43
|
-
let store = stores.get(key);
|
|
44
|
-
if (!store) {
|
|
45
|
-
store = new Store(key, storage);
|
|
46
|
-
stores.set(key, store);
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
subscribe: store.subscribe.bind(store),
|
|
50
|
-
getSnapshot: store.getSnapshot.bind(store),
|
|
51
|
-
getServerSnapshot: () => {
|
|
52
|
-
try {
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
54
|
-
return store.getSnapshot().bind(store);
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
return defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
emitChange: store.emitChange.bind(store),
|
|
61
|
-
};
|
|
62
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
63
|
-
}, [key, storage]);
|
|
64
|
-
}
|
package/storage/useStorage.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { AbstractParserOptions } from './parser';
|
|
2
|
-
import type { AbstractStorage } from './storage';
|
|
3
|
-
interface UseStorageMethod<V> {
|
|
4
|
-
set: (value: V) => void;
|
|
5
|
-
remove: () => void;
|
|
6
|
-
}
|
|
7
|
-
export declare function useStorage<V, K = string>(key: K | null, defaultValue?: undefined, parser?: keyof AbstractParserOptions<any>): {
|
|
8
|
-
readonly value: V | null;
|
|
9
|
-
} & UseStorageMethod<V>;
|
|
10
|
-
export declare function useStorage<V, K = string>(key: K, defaultValue: V, parser?: keyof AbstractParserOptions<any>): {
|
|
11
|
-
readonly value: V;
|
|
12
|
-
} & UseStorageMethod<V>;
|
|
13
|
-
export declare namespace useStorage {
|
|
14
|
-
var SERVICE: AbstractStorage<any, any>;
|
|
15
|
-
var PARSER: AbstractParserOptions<any>;
|
|
16
|
-
var clear: () => void;
|
|
17
|
-
}
|
|
18
|
-
export {};
|
package/storage/useStorage.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { isNull, isUndefined } from 'lodash';
|
|
2
|
-
import { useMemo, useSyncExternalStore } from 'react';
|
|
3
|
-
import { LocalStorageService } from './localStorage';
|
|
4
|
-
import { STRING_PARSER } from './parser';
|
|
5
|
-
import { useStore, stores } from './store';
|
|
6
|
-
export function useStorage(key, defaultValue, parser = 'plain') {
|
|
7
|
-
const { SERVICE, PARSER } = useStorage;
|
|
8
|
-
const { serializer, deserializer } = isUndefined(parser) ? PARSER.plain : PARSER[parser];
|
|
9
|
-
const store = useStore(key, defaultValue, SERVICE);
|
|
10
|
-
const value = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getServerSnapshot);
|
|
11
|
-
return useMemo(() => ({
|
|
12
|
-
get value() {
|
|
13
|
-
if (isNull(value)) {
|
|
14
|
-
return defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
|
|
15
|
-
}
|
|
16
|
-
return deserializer(value);
|
|
17
|
-
},
|
|
18
|
-
set: (value) => {
|
|
19
|
-
const originValue = serializer(value);
|
|
20
|
-
SERVICE.setItem(key, originValue);
|
|
21
|
-
store.emitChange();
|
|
22
|
-
},
|
|
23
|
-
remove: () => {
|
|
24
|
-
SERVICE.removeItem(key);
|
|
25
|
-
store.emitChange();
|
|
26
|
-
},
|
|
27
|
-
}), [SERVICE, defaultValue, deserializer, key, serializer, store, value]);
|
|
28
|
-
}
|
|
29
|
-
useStorage.SERVICE = new LocalStorageService();
|
|
30
|
-
useStorage.PARSER = STRING_PARSER;
|
|
31
|
-
useStorage.clear = () => {
|
|
32
|
-
useStorage.SERVICE.clear();
|
|
33
|
-
for (const [, store] of stores) {
|
|
34
|
-
store.emitChange();
|
|
35
|
-
}
|
|
36
|
-
};
|
package/useACL.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export type Control = string | number;
|
|
2
|
-
export type ControlMode = 'one' | 'all';
|
|
3
|
-
export declare class ACL {
|
|
4
|
-
private _full;
|
|
5
|
-
private _controls;
|
|
6
|
-
get full(): boolean;
|
|
7
|
-
get controls(): Control[];
|
|
8
|
-
setFull(full: boolean): void;
|
|
9
|
-
set(control: Control[]): void;
|
|
10
|
-
add(control: Control | Control[]): void;
|
|
11
|
-
remove(control: Control | Control[]): void;
|
|
12
|
-
can(control: Control | Control[], mode?: ControlMode): boolean;
|
|
13
|
-
}
|
|
14
|
-
export declare function useACL(): ACL;
|
package/useACL.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { isArray } from 'lodash';
|
|
2
|
-
import { useSyncExternalStore } from 'react';
|
|
3
|
-
export class ACL {
|
|
4
|
-
constructor() {
|
|
5
|
-
Object.defineProperty(this, "_full", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
configurable: true,
|
|
8
|
-
writable: true,
|
|
9
|
-
value: false
|
|
10
|
-
});
|
|
11
|
-
Object.defineProperty(this, "_controls", {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
configurable: true,
|
|
14
|
-
writable: true,
|
|
15
|
-
value: new Set()
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
get full() {
|
|
19
|
-
return this._full;
|
|
20
|
-
}
|
|
21
|
-
get controls() {
|
|
22
|
-
return Array.from(this._controls);
|
|
23
|
-
}
|
|
24
|
-
setFull(full) {
|
|
25
|
-
this._full = full;
|
|
26
|
-
emitChange();
|
|
27
|
-
}
|
|
28
|
-
set(control) {
|
|
29
|
-
this._controls = new Set(control);
|
|
30
|
-
emitChange();
|
|
31
|
-
}
|
|
32
|
-
add(control) {
|
|
33
|
-
for (const v of isArray(control) ? control : [control]) {
|
|
34
|
-
this._controls.add(v);
|
|
35
|
-
}
|
|
36
|
-
emitChange();
|
|
37
|
-
}
|
|
38
|
-
remove(control) {
|
|
39
|
-
for (const v of isArray(control) ? control : [control]) {
|
|
40
|
-
this._controls.delete(v);
|
|
41
|
-
}
|
|
42
|
-
emitChange();
|
|
43
|
-
}
|
|
44
|
-
can(control, mode = 'one') {
|
|
45
|
-
if (this._full) {
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
const arr = isArray(control) ? control : [control];
|
|
49
|
-
let n = 0;
|
|
50
|
-
for (const v of arr) {
|
|
51
|
-
if (this._controls.has(v)) {
|
|
52
|
-
n += 1;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (n > 0 && (mode === 'one' || (mode === 'all' && n === arr.length))) {
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
const acl = new ACL();
|
|
62
|
-
let listeners = [];
|
|
63
|
-
function subscribe(onStoreChange) {
|
|
64
|
-
listeners = listeners.concat([onStoreChange]);
|
|
65
|
-
return () => {
|
|
66
|
-
listeners = listeners.filter((f) => f !== onStoreChange);
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
function getSnapshot() {
|
|
70
|
-
return acl;
|
|
71
|
-
}
|
|
72
|
-
function emitChange() {
|
|
73
|
-
for (const listener of listeners) {
|
|
74
|
-
listener();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
export function useACL() {
|
|
78
|
-
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
79
|
-
}
|