resourcerer 2.0.0-beta-3 → 2.0.1
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/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 +3 -3
- package/resourcerer.d.ts +2 -1
- package/test/model-cache.test.js +59 -1
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resourcerer",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "github.com/noahgrant/resourcerer"
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"vitest": "^1.6.0"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"react": "^16.14.0 || ^17.0.0 || ^18.0.0",
|
|
54
|
-
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0"
|
|
53
|
+
"react": "^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
54
|
+
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
55
55
|
}
|
|
56
56
|
}
|
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
|
|
package/test/model-cache.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Collection from "../lib/collection";
|
|
2
2
|
import Model from "../lib/model";
|
|
3
|
-
import ModelCache from "../lib/model-cache";
|
|
3
|
+
import ModelCache, { invalidate } from "../lib/model-cache";
|
|
4
4
|
import { vi } from "vitest";
|
|
5
5
|
|
|
6
6
|
const CACHE_WAIT = 150000,
|
|
@@ -205,4 +205,62 @@ describe("ModelCache", () => {
|
|
|
205
205
|
expect(ModelCache.get("users~key=value")).toBeDefined();
|
|
206
206
|
expect(ModelCache.get("decisions")).toBeDefined();
|
|
207
207
|
});
|
|
208
|
+
|
|
209
|
+
it("when calling 'removeAllExcept' removes all models except those with a specific key", () => {
|
|
210
|
+
const cacheKeys = [
|
|
211
|
+
"user~userId=zorah",
|
|
212
|
+
"user~source=hbase_userId=noah",
|
|
213
|
+
"user",
|
|
214
|
+
"users",
|
|
215
|
+
"users~key=value",
|
|
216
|
+
"aliens",
|
|
217
|
+
"decisions",
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
cacheKeys.forEach((key) => ModelCache.put(key, {}, {}));
|
|
221
|
+
|
|
222
|
+
ModelCache.removeAllExcept(["users", "aliens"]);
|
|
223
|
+
expect(ModelCache.get("user")).not.toBeDefined();
|
|
224
|
+
expect(ModelCache.get("user~userId=zorah")).not.toBeDefined();
|
|
225
|
+
expect(ModelCache.get("user~source=hbase_userId=noah")).not.toBeDefined();
|
|
226
|
+
expect(ModelCache.get("users")).toBeDefined();
|
|
227
|
+
expect(ModelCache.get("users~key=value")).toBeDefined();
|
|
228
|
+
expect(ModelCache.get("aliens")).toBeDefined();
|
|
229
|
+
expect(ModelCache.get("decisions")).not.toBeDefined();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe("invalidate", () => {
|
|
234
|
+
it("removes models from the cache immediately", () => {
|
|
235
|
+
ModelCache.put("foo", {}, {});
|
|
236
|
+
ModelCache.put("bar", {}, {});
|
|
237
|
+
ModelCache.put("baz", {}, {});
|
|
238
|
+
invalidate("foo");
|
|
239
|
+
expect(ModelCache.get("foo")).not.toBeDefined();
|
|
240
|
+
expect(ModelCache.get("bar")).toBeDefined();
|
|
241
|
+
expect(ModelCache.get("baz")).toBeDefined();
|
|
242
|
+
|
|
243
|
+
// safe
|
|
244
|
+
invalidate();
|
|
245
|
+
expect(ModelCache.get("bar")).toBeDefined();
|
|
246
|
+
expect(ModelCache.get("baz")).toBeDefined();
|
|
247
|
+
|
|
248
|
+
invalidate(["bar", "baz"]);
|
|
249
|
+
expect(ModelCache.get("bar")).not.toBeDefined();
|
|
250
|
+
expect(ModelCache.get("baz")).not.toBeDefined();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("removes models except those specified when the 'except' option is true", () => {
|
|
254
|
+
ModelCache.put("foo", {}, {});
|
|
255
|
+
ModelCache.put("bar", {}, {});
|
|
256
|
+
ModelCache.put("baz", {}, {});
|
|
257
|
+
invalidate(["foo", "bar"], { except: true });
|
|
258
|
+
expect(ModelCache.get("foo")).toBeDefined();
|
|
259
|
+
expect(ModelCache.get("bar")).toBeDefined();
|
|
260
|
+
expect(ModelCache.get("baz")).not.toBeDefined();
|
|
261
|
+
|
|
262
|
+
invalidate([], { except: true });
|
|
263
|
+
expect(ModelCache.get("foo")).not.toBeDefined();
|
|
264
|
+
expect(ModelCache.get("bar")).not.toBeDefined();
|
|
265
|
+
});
|
|
208
266
|
});
|