resourcerer 2.0.0 → 2.0.2
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/README.md +8 -1
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/lib/collection.d.ts +23 -23
- package/build/lib/model-cache.d.ts +7 -2
- package/build/lib/model-cache.js +16 -1
- package/build/lib/model.d.ts +1 -1
- package/build/lib/resourcerer.d.ts +1 -1
- package/build/lib/resourcerer.js +2 -2
- package/build/lib/types.d.ts +2 -1
- package/package.json +1 -1
- package/resourcerer.d.ts +2 -1
package/README.md
CHANGED
|
@@ -932,7 +932,14 @@ function MyComponent(props) {
|
|
|
932
932
|
return <Button onClick={() => invalidate(["todos"])}>Invalidate me</Button>;
|
|
933
933
|
```
|
|
934
934
|
|
|
935
|
-
* Unlike [`refetching`](#refetching), the ResourceKeys passed to `invalidate` do not need to be from those returned by the executor function. They can any resource key.
|
|
935
|
+
* Unlike [`refetching`](#refetching), the ResourceKeys passed to `invalidate` do not need to be from those returned by the executor function. They can any resource key. This function is also available as a static import from the `resourcerer` package.
|
|
936
|
+
|
|
937
|
+
Rather than invalidating the cache for select keys, you can also invalidate the entire cache with the exception of some keys by passing the `{except: true}` option:
|
|
938
|
+
|
|
939
|
+
```js
|
|
940
|
+
// this will remove all items from the cache except those from the "todos" resource
|
|
941
|
+
invalidate(["todos"], {except: true});
|
|
942
|
+
```
|
|
936
943
|
|
|
937
944
|
## Tracking Request Times
|
|
938
945
|
|
package/build/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { default as Model } from "./lib/model.js";
|
|
|
3
3
|
export { default as sync, ajax, type SyncOptions } from "./lib/sync.js";
|
|
4
4
|
export { default as prefetch } from "./lib/prefetch.js";
|
|
5
5
|
export { default as request } from "./lib/request.js";
|
|
6
|
-
export { default as ModelCache } from "./lib/model-cache.js";
|
|
6
|
+
export { default as ModelCache, invalidate } from "./lib/model-cache.js";
|
|
7
7
|
export * as Utils from "./lib/utils.js";
|
|
8
8
|
export { register, ModelMap, ResourcesConfig } from "./lib/config.js";
|
|
9
9
|
export { useResources, withResources } from "./lib/resourcerer.js";
|
package/build/index.js
CHANGED
|
@@ -3,7 +3,7 @@ export { default as Model } from "./lib/model.js";
|
|
|
3
3
|
export { default as sync, ajax } from "./lib/sync.js";
|
|
4
4
|
export { default as prefetch } from "./lib/prefetch.js";
|
|
5
5
|
export { default as request } from "./lib/request.js";
|
|
6
|
-
export { default as ModelCache } from "./lib/model-cache.js";
|
|
6
|
+
export { default as ModelCache, invalidate } from "./lib/model-cache.js";
|
|
7
7
|
export * as Utils from "./lib/utils.js";
|
|
8
8
|
export { register, ModelMap, ResourcesConfig } from "./lib/config.js";
|
|
9
9
|
export { useResources, withResources } from "./lib/resourcerer.js";
|
|
@@ -53,45 +53,45 @@ export default class Collection<T extends Record<string, any> = object, O extend
|
|
|
53
53
|
} & SyncOptions & CSetOptions): Promise<[InstanceType<this["Model"]>, Response]>;
|
|
54
54
|
parse(response: any, options: SyncOptions & CSetOptions): T[];
|
|
55
55
|
_getModelClass<T extends Record<string, any>, O extends Record<string, any>>(): {
|
|
56
|
-
new (attributes?:
|
|
56
|
+
new (attributes?: T | undefined, options?: O & SetOptions & ConstructorOptions): {
|
|
57
57
|
cid: string;
|
|
58
58
|
id: string | number;
|
|
59
|
-
attributes:
|
|
60
|
-
readonly urlOptions:
|
|
61
|
-
collection?: Collection
|
|
62
|
-
lazy?: boolean
|
|
63
|
-
refetching?: boolean
|
|
64
|
-
measure?: boolean | ((config: ResourceConfigObj) => boolean)
|
|
65
|
-
isEmptyModel?: boolean
|
|
66
|
-
toJSON():
|
|
59
|
+
attributes: T;
|
|
60
|
+
readonly urlOptions: O;
|
|
61
|
+
collection?: Collection;
|
|
62
|
+
lazy?: boolean;
|
|
63
|
+
refetching?: boolean;
|
|
64
|
+
measure?: boolean | ((config: ResourceConfigObj) => boolean);
|
|
65
|
+
isEmptyModel?: boolean;
|
|
66
|
+
toJSON(): T;
|
|
67
67
|
sync(model: Model<Record<string, any>, Record<string, any>> | Collection<object, object>, options?: SyncOptions | undefined): Promise<[any, Response]>;
|
|
68
|
-
get<K extends keyof
|
|
69
|
-
pick<
|
|
70
|
-
has(attr: keyof
|
|
71
|
-
set(attrs?: Partial<
|
|
72
|
-
unset(attr: keyof
|
|
73
|
-
clear(options
|
|
68
|
+
get<K extends keyof T>(attr: K): T[K];
|
|
69
|
+
pick<K extends keyof T>(...attrs: K[]): Pick<T, K>;
|
|
70
|
+
has(attr: keyof T): boolean;
|
|
71
|
+
set(attrs?: Partial<T>, options?: SetOptions): any;
|
|
72
|
+
unset(attr: keyof T, options?: SetOptions): any;
|
|
73
|
+
clear(options?: SetOptions): any;
|
|
74
74
|
fetch(options?: SyncOptions & SetOptions): Promise<readonly [any, Response]>;
|
|
75
|
-
save(attrs: Partial<
|
|
76
|
-
wait?: boolean
|
|
77
|
-
patch?: boolean
|
|
75
|
+
save(attrs: Partial<T>, options?: {
|
|
76
|
+
wait?: boolean;
|
|
77
|
+
patch?: boolean;
|
|
78
78
|
} & SyncOptions & SetOptions): Promise<[any, Response]>;
|
|
79
79
|
destroy(options?: {
|
|
80
|
-
wait?: boolean
|
|
80
|
+
wait?: boolean;
|
|
81
81
|
} & SyncOptions & SetOptions): Promise<[any, Response]>;
|
|
82
|
-
url(options?:
|
|
83
|
-
parse(response: any, options: SyncOptions & SetOptions):
|
|
82
|
+
url(options?: O_1): string;
|
|
83
|
+
parse(response: any, options: SyncOptions & SetOptions): T;
|
|
84
84
|
isNew(): boolean;
|
|
85
85
|
_callbacks: {
|
|
86
86
|
callback: (...args: any[]) => void;
|
|
87
|
-
context:
|
|
87
|
+
context: NonNullable<unknown>;
|
|
88
88
|
}[];
|
|
89
89
|
triggerUpdate(): void;
|
|
90
90
|
onUpdate(callback: (...args: any[]) => void, context: {}): void;
|
|
91
91
|
offUpdate(ctx: {}): void;
|
|
92
92
|
};
|
|
93
93
|
idAttribute: string;
|
|
94
|
-
dependencies:
|
|
94
|
+
dependencies: Array<string | ((attrs: Record<string, any>) => Record<string, any>)>;
|
|
95
95
|
cacheTimeout: number;
|
|
96
96
|
defaults: {};
|
|
97
97
|
measure: boolean | ((config: ResourceConfigObj) => boolean);
|
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import Collection from "./collection.js";
|
|
2
2
|
import Model from "./model.js";
|
|
3
|
+
import { ResourceKeys } from "./types.js";
|
|
3
4
|
type Component = NonNullable<unknown>;
|
|
4
|
-
declare const
|
|
5
|
+
declare const ModelCache: {
|
|
5
6
|
get(cacheKey: string): Model<Record<string, any>, Record<string, any>> | Collection<object, object> | undefined;
|
|
6
7
|
put(cacheKey: string, model: Model | Collection, component?: Component): void;
|
|
7
8
|
register(cacheKey: string, component?: Component): void;
|
|
8
9
|
unregister(component: Component, ...cacheKeys: string[]): void;
|
|
9
10
|
remove(cacheKey: string): void;
|
|
10
11
|
removeAllWithModel(resourceKey: string): void;
|
|
12
|
+
removeAllExcept(resourceKeys: string[]): void;
|
|
11
13
|
__removeAll__(): void;
|
|
12
14
|
};
|
|
13
|
-
export default
|
|
15
|
+
export default ModelCache;
|
|
16
|
+
export declare function invalidate(keys: ResourceKeys | ResourceKeys[], { except }?: {
|
|
17
|
+
except?: boolean;
|
|
18
|
+
}): void;
|
package/build/lib/model-cache.js
CHANGED
|
@@ -2,7 +2,7 @@ import { ResourcesConfig } from "./config.js";
|
|
|
2
2
|
const modelCache = new Map();
|
|
3
3
|
const componentManifest = new Map();
|
|
4
4
|
const timeouts = {};
|
|
5
|
-
|
|
5
|
+
const ModelCache = {
|
|
6
6
|
get(cacheKey) {
|
|
7
7
|
return modelCache.get(cacheKey);
|
|
8
8
|
},
|
|
@@ -45,11 +45,20 @@ export default {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
},
|
|
48
|
+
removeAllExcept(resourceKeys) {
|
|
49
|
+
for (const key of modelCache.keys()) {
|
|
50
|
+
if (!resourceKeys.includes(key) &&
|
|
51
|
+
!resourceKeys.some((resourceKey) => key.startsWith(`${resourceKey}~`))) {
|
|
52
|
+
this.remove(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
48
56
|
__removeAll__() {
|
|
49
57
|
modelCache.forEach((val, key) => clearModel(key));
|
|
50
58
|
componentManifest.forEach((val, key) => componentManifest.delete(key));
|
|
51
59
|
},
|
|
52
60
|
};
|
|
61
|
+
export default ModelCache;
|
|
53
62
|
function scheduleForRemoval(cacheKey) {
|
|
54
63
|
const Constructor = modelCache.get(cacheKey)?.constructor;
|
|
55
64
|
const timeout = Constructor?.cacheTimeout || ResourcesConfig.cacheGracePeriod;
|
|
@@ -60,3 +69,9 @@ function clearModel(cacheKey) {
|
|
|
60
69
|
delete timeouts[cacheKey];
|
|
61
70
|
modelCache.delete(cacheKey);
|
|
62
71
|
}
|
|
72
|
+
export function invalidate(keys, { except } = {}) {
|
|
73
|
+
keys = Array.isArray(keys) ? keys : [keys];
|
|
74
|
+
except ?
|
|
75
|
+
ModelCache.removeAllExcept(keys)
|
|
76
|
+
: keys.forEach((key) => ModelCache.removeAllWithModel(key));
|
|
77
|
+
}
|
package/build/lib/model.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export default class Model<T extends Record<string, any> = Record<string, any>,
|
|
|
33
33
|
has(attr: keyof T): boolean;
|
|
34
34
|
set(attrs?: Partial<T>, options?: SetOptions): this;
|
|
35
35
|
unset(attr: keyof T, options?: SetOptions): this;
|
|
36
|
-
clear(options
|
|
36
|
+
clear(options?: SetOptions): this;
|
|
37
37
|
fetch(options?: SyncOptions & SetOptions): Promise<readonly [this, Response]>;
|
|
38
38
|
save(attrs: Partial<T>, options?: {
|
|
39
39
|
wait?: boolean;
|
|
@@ -12,6 +12,6 @@ export declare function useResources<T extends ResourceKeys, O extends Record<st
|
|
|
12
12
|
[Key in T as `${T}Status`]: number;
|
|
13
13
|
};
|
|
14
14
|
export declare const withResources: (getResources: ExecutorFunction) => (Component: ComponentClass<Record<string, any>>) => (props: Record<string, any>) => React.CElement<{
|
|
15
|
-
children: React.ReactElement
|
|
15
|
+
children: React.ReactElement;
|
|
16
16
|
}, ErrorBoundary>;
|
|
17
17
|
export declare function getCacheKey({ resourceKey, params, path, data, }: InternalResourceConfigObj): string;
|
package/build/lib/resourcerer.js
CHANGED
|
@@ -4,7 +4,7 @@ import React, { useEffect, useReducer, useRef, useState, } from "react";
|
|
|
4
4
|
import Collection from "./collection.js";
|
|
5
5
|
import ErrorBoundary from "./error-boundary.js";
|
|
6
6
|
import Model from "./model.js";
|
|
7
|
-
import ModelCache from "./model-cache.js";
|
|
7
|
+
import ModelCache, { invalidate } from "./model-cache.js";
|
|
8
8
|
import ReactDOM from "react-dom";
|
|
9
9
|
import request from "./request.js";
|
|
10
10
|
export function useResources(getResources, _props) {
|
|
@@ -164,7 +164,7 @@ export function useResources(getResources, _props) {
|
|
|
164
164
|
});
|
|
165
165
|
});
|
|
166
166
|
},
|
|
167
|
-
invalidate
|
|
167
|
+
invalidate,
|
|
168
168
|
setResourceState,
|
|
169
169
|
...loadingStates,
|
|
170
170
|
...requestStatuses,
|
package/build/lib/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Model from "./model.js";
|
|
2
2
|
import Collection from "./collection.js";
|
|
3
|
+
import { invalidate } from "./model-cache.js";
|
|
3
4
|
export type LoadingStates = "error" | "loading" | "loaded" | "pending";
|
|
4
5
|
export type Resource = [string, InternalResourceConfigObj];
|
|
5
6
|
export type Props = Record<string, any>;
|
|
@@ -55,7 +56,7 @@ export type UseResourcesResponse = {
|
|
|
55
56
|
hasLoaded: boolean;
|
|
56
57
|
hasInitiallyLoaded: boolean;
|
|
57
58
|
refetch: (keys: ResourceKeys[]) => void;
|
|
58
|
-
invalidate:
|
|
59
|
+
invalidate: typeof invalidate;
|
|
59
60
|
setResourceState(newState: {
|
|
60
61
|
[key: string]: any;
|
|
61
62
|
}): void;
|
package/package.json
CHANGED
package/resourcerer.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Model from "./build/lib/model.js";
|
|
2
2
|
import Collection from "./build/lib/collection.js";
|
|
3
|
+
import { invalidate } from "./build/lib/model-cache.js";
|
|
3
4
|
|
|
4
5
|
export * from "./build/index.js";
|
|
5
6
|
|
|
@@ -43,7 +44,7 @@ declare module "resourcerer" {
|
|
|
43
44
|
hasLoaded: boolean;
|
|
44
45
|
hasInitiallyLoaded: boolean;
|
|
45
46
|
refetch: (keys: ResourceKeys | ResourceKeys[]) => void;
|
|
46
|
-
invalidate:
|
|
47
|
+
invalidate: typeof invalidate;
|
|
47
48
|
setResourceState(newState: { [key: string]: any }): void;
|
|
48
49
|
};
|
|
49
50
|
|