@owlmeans/state 0.1.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/LICENSE +21 -0
- package/README.md +456 -0
- package/build/.gitkeep +0 -0
- package/build/consts.d.ts +3 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +3 -0
- package/build/consts.js.map +1 -0
- package/build/errors.d.ts +10 -0
- package/build/errors.d.ts.map +1 -0
- package/build/errors.js +18 -0
- package/build/errors.js.map +1 -0
- package/build/index.d.ts +5 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +4 -0
- package/build/index.js.map +1 -0
- package/build/resource.d.ts +6 -0
- package/build/resource.d.ts.map +1 -0
- package/build/resource.js +208 -0
- package/build/resource.js.map +1 -0
- package/build/types.d.ts +39 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/build/utils/index.d.ts +2 -0
- package/build/utils/index.d.ts.map +1 -0
- package/build/utils/index.js +2 -0
- package/build/utils/index.js.map +1 -0
- package/build/utils/model.d.ts +4 -0
- package/build/utils/model.d.ts.map +1 -0
- package/build/utils/model.js +30 -0
- package/build/utils/model.js.map +1 -0
- package/package.json +34 -0
- package/src/consts.ts +3 -0
- package/src/errors.ts +23 -0
- package/src/index.ts +6 -0
- package/src/resource.ts +256 -0
- package/src/types.ts +49 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/model.ts +42 -0
- package/tsconfig.json +14 -0
- package/tsconfig.tsbuildinfo +1 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ListCriteria, Resource, ResourceRecord } from '@owlmeans/resource'
|
|
2
|
+
|
|
3
|
+
export interface StateResource<T extends ResourceRecord> extends Resource<T> {
|
|
4
|
+
/**
|
|
5
|
+
* @returns unsubscribe function
|
|
6
|
+
*/
|
|
7
|
+
subscribe: (params: StateSubscriptionOption<T>) => [() => void, StateModel<T>[]]
|
|
8
|
+
listen: (listener: StateListener<T>) => () => void
|
|
9
|
+
erase: () => Promise<void>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface StateSubscriptionOption<T extends ResourceRecord> {
|
|
13
|
+
id?: string | string[]
|
|
14
|
+
_systemId?: string
|
|
15
|
+
query?: ListCriteria
|
|
16
|
+
default?: Partial<T>
|
|
17
|
+
listener: StateListener<T>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface StateListener<T extends ResourceRecord> {
|
|
21
|
+
(record: StateModel<T>[]): void | Promise<void>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface StateModel<T extends ResourceRecord> {
|
|
25
|
+
record: T,
|
|
26
|
+
|
|
27
|
+
commit: (force?: boolean) => void
|
|
28
|
+
|
|
29
|
+
update: (data?: Partial<T>) => void
|
|
30
|
+
|
|
31
|
+
clear: () => void
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface StateResourceAppend {
|
|
35
|
+
getStateResource: <T extends ResourceRecord>(alias?: string) => StateResource<T>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface UseStoreHelper {
|
|
39
|
+
<T extends ResourceRecord>(id?: string | UseStoreHelperOptions<T>, opts?: string | boolean | UseStoreHelperOptions<T>): StateModel<T>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface UseStoreListHelper {
|
|
43
|
+
<T extends ResourceRecord>(id?: string | string[] | UseStoreHelperOptions<T>, opts?: string | boolean | UseStoreHelperOptions<T>): StateModel<T>[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface UseStoreHelperOptions<T extends ResourceRecord> extends Omit<StateSubscriptionOption<T>, "listener"> {
|
|
47
|
+
listen?: boolean
|
|
48
|
+
resource?: string
|
|
49
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ResourceRecord } from '@owlmeans/resource'
|
|
2
|
+
import { MisshapedRecord } from '@owlmeans/resource'
|
|
3
|
+
import type { StateModel, StateResource } from '../types.js'
|
|
4
|
+
|
|
5
|
+
export const createStateModel = <T extends ResourceRecord>(
|
|
6
|
+
record: T, resource: StateResource<T>
|
|
7
|
+
): StateModel<T> => {
|
|
8
|
+
let before: T = record
|
|
9
|
+
const model: StateModel<T> = {
|
|
10
|
+
record: { ...record },
|
|
11
|
+
|
|
12
|
+
commit: force => {
|
|
13
|
+
if (force !== true) {
|
|
14
|
+
if (!Object.entries(before).reduce(
|
|
15
|
+
(changed, [key, value]) => changed || model.record[key as keyof T] !== value, false
|
|
16
|
+
) && !Object.entries(model.record).reduce(
|
|
17
|
+
(changed, [key, value]) => changed || before[key as keyof T] !== value, false
|
|
18
|
+
)) {
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (record.id == null) {
|
|
23
|
+
throw new MisshapedRecord('id')
|
|
24
|
+
}
|
|
25
|
+
before = model.record
|
|
26
|
+
void resource.load(record.id).then(exists => {
|
|
27
|
+
exists != null && resource.update(model.record)
|
|
28
|
+
})
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
update: data => {
|
|
32
|
+
Object.assign(model.record, data)
|
|
33
|
+
model.commit()
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
clear: () => {
|
|
37
|
+
void resource.delete(record)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return model
|
|
42
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"../tsconfig.default.json"
|
|
4
|
+
],
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"rootDir": "./src/", /* Specify the root folder within your source files. */
|
|
7
|
+
"outDir": "./build/", /* Specify an output folder for all emitted files. */
|
|
8
|
+
},
|
|
9
|
+
"exclude": [
|
|
10
|
+
"./dist/**/*",
|
|
11
|
+
"./build/**/*",
|
|
12
|
+
"./*.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/consts.ts","./src/errors.ts","./src/index.ts","./src/resource.ts","./src/types.ts","./src/utils/index.ts","./src/utils/model.ts"],"version":"5.6.3"}
|