@codeleap/store 4.2.18
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/package.json +25 -0
- package/package.json.bak +25 -0
- package/src/globalState.ts +42 -0
- package/src/index.ts +3 -0
- package/src/types.ts +19 -0
- package/src/utils.ts +37 -0
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codeleap/store",
|
|
3
|
+
"version": "4.2.18",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
|
|
8
|
+
"type": "git",
|
|
9
|
+
"directory": "packages/store"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@codeleap/config": "4.3.0",
|
|
13
|
+
"ts-node-dev": "1.1.8"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "echo 'No build needed'"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"typescript": "5.0.4",
|
|
20
|
+
"react": "18.1.0",
|
|
21
|
+
"nanostores": "*",
|
|
22
|
+
"@nanostores/react": "*",
|
|
23
|
+
"@nanostores/persistent": "*"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/package.json.bak
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codeleap/store",
|
|
3
|
+
"version": "4.2.18",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
|
|
8
|
+
"type": "git",
|
|
9
|
+
"directory": "packages/store"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@codeleap/config": "workspace:*",
|
|
13
|
+
"ts-node-dev": "1.1.8"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "echo 'No build needed'"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"typescript": "5.0.4",
|
|
20
|
+
"react": "18.1.0",
|
|
21
|
+
"nanostores": "*",
|
|
22
|
+
"@nanostores/react": "*",
|
|
23
|
+
"@nanostores/persistent": "*"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useStore } from '@nanostores/react'
|
|
2
|
+
import { setPersistentEngine, persistentAtom } from '@nanostores/persistent'
|
|
3
|
+
import { atom } from 'nanostores'
|
|
4
|
+
import { GlobalState, GlobalStateConfig, StateSelector } from './types'
|
|
5
|
+
import { stateAssign, useStateSelector } from './utils'
|
|
6
|
+
|
|
7
|
+
const defaultConfig: GlobalStateConfig = {
|
|
8
|
+
persistKey: null,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const setGlobalStatePersistor = setPersistentEngine
|
|
12
|
+
|
|
13
|
+
export function globalState<T>(value: T, config: GlobalStateConfig = defaultConfig): GlobalState<T> {
|
|
14
|
+
const { persistKey } = config
|
|
15
|
+
|
|
16
|
+
const isPersistState = typeof persistKey === 'string'
|
|
17
|
+
|
|
18
|
+
const store = isPersistState ? persistentAtom<T>(persistKey, value, {
|
|
19
|
+
encode: JSON.stringify,
|
|
20
|
+
decode: JSON.parse,
|
|
21
|
+
}) : atom(value)
|
|
22
|
+
|
|
23
|
+
return new Proxy(store, {
|
|
24
|
+
get(target, prop, receiver) {
|
|
25
|
+
if (prop === 'use') {
|
|
26
|
+
return (selector?: StateSelector<T, any>) => {
|
|
27
|
+
if (!selector) return useStore(target)
|
|
28
|
+
return useStateSelector(target, selector)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (prop === 'set') {
|
|
33
|
+
return (newValue: Partial<T>) => {
|
|
34
|
+
const value = stateAssign(newValue, target.get())
|
|
35
|
+
target.set(value)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Reflect.get(target, prop, receiver)
|
|
40
|
+
}
|
|
41
|
+
}) as unknown as GlobalState<T>
|
|
42
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { WritableAtom } from 'nanostores'
|
|
2
|
+
import { PersistentStore, PersistentEvents, PersistentEvent } from '@nanostores/persistent'
|
|
3
|
+
|
|
4
|
+
export type StateSelector<S, R> = (state: S) => R
|
|
5
|
+
|
|
6
|
+
export type GlobalState<T> = Omit<WritableAtom<T>, 'set'> & {
|
|
7
|
+
use: <Selected = T>(selector?: StateSelector<T, Selected>) => Selected
|
|
8
|
+
set: (newValue: Partial<T>) => T
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type GlobalStateConfig = {
|
|
12
|
+
persistKey?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type GlobalStatePersistor = PersistentStore
|
|
16
|
+
|
|
17
|
+
export type GlobalStatePersistorEvents = PersistentEvents
|
|
18
|
+
|
|
19
|
+
export type GlobalStatePersistorEvent = PersistentEvent
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useStore } from '@nanostores/react'
|
|
2
|
+
import { Store } from 'nanostores'
|
|
3
|
+
|
|
4
|
+
export const createStateSelector = <T, R>(
|
|
5
|
+
store: Store<T>,
|
|
6
|
+
selector: (state: T) => R
|
|
7
|
+
) => ({
|
|
8
|
+
get: () => selector(store.get()),
|
|
9
|
+
listen: (listener: (value: R) => void) => {
|
|
10
|
+
return store.listen((state) => {
|
|
11
|
+
listener(selector(state))
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
} as Store)
|
|
15
|
+
|
|
16
|
+
export function useStateSelector<T, R>(
|
|
17
|
+
store: Store<T>,
|
|
18
|
+
selector: (state: T) => R
|
|
19
|
+
): R {
|
|
20
|
+
return useStore(createStateSelector(store, selector))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function stateAssign<T>(newValue: Partial<T>, stateValue: T): T {
|
|
24
|
+
if (Array.isArray(stateValue)) {
|
|
25
|
+
return [
|
|
26
|
+
...stateValue,
|
|
27
|
+
...(Array.isArray(newValue) ? newValue : [newValue]),
|
|
28
|
+
] as T
|
|
29
|
+
} else if (typeof stateValue === "object" && stateValue !== null) {
|
|
30
|
+
return {
|
|
31
|
+
...stateValue,
|
|
32
|
+
...newValue,
|
|
33
|
+
} as T
|
|
34
|
+
} else {
|
|
35
|
+
return newValue as T
|
|
36
|
+
}
|
|
37
|
+
}
|