@trypattern/sdk 0.0.0-stage → 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/README.md +127 -2
- package/dist/access.d.ts +31 -0
- package/dist/access.js +72 -0
- package/dist/actions.d.ts +57 -0
- package/dist/actions.js +198 -0
- package/dist/api.d.ts +20 -0
- package/dist/api.js +34 -0
- package/dist/app.d.ts +106 -0
- package/dist/app.js +224 -0
- package/dist/appearance.d.ts +7 -0
- package/dist/appearance.js +2 -0
- package/dist/auth/sessionApi.d.ts +41 -0
- package/dist/auth/sessionApi.js +158 -0
- package/dist/auth/sessionStore.d.ts +20 -0
- package/dist/auth/sessionStore.js +40 -0
- package/dist/auth/user.d.ts +11 -0
- package/dist/auth/user.js +9 -0
- package/dist/builtins/Account.d.ts +25 -0
- package/dist/builtins/Account.js +15 -0
- package/dist/builtins/Palette.d.ts +29 -0
- package/dist/builtins/Palette.js +63 -0
- package/dist/builtins/SignIn.d.ts +28 -0
- package/dist/builtins/SignIn.js +60 -0
- package/dist/builtins/Toasts.d.ts +32 -0
- package/dist/builtins/Toasts.js +24 -0
- package/dist/builtins/actions.d.ts +2 -0
- package/dist/builtins/actions.js +45 -0
- package/dist/cache.d.ts +24 -0
- package/dist/cache.js +112 -0
- package/dist/cli/app.d.ts +2 -0
- package/dist/cli/app.js +1329 -0
- package/dist/cli/screenshot.d.ts +1 -0
- package/dist/cli/screenshot.js +96 -0
- package/dist/cli/tools.d.ts +12 -0
- package/dist/cli/tools.js +35 -0
- package/dist/components/AppFrame.d.ts +14 -0
- package/dist/components/AppFrame.js +64 -0
- package/dist/components/AppearancePicker.d.ts +1 -0
- package/dist/components/AppearancePicker.js +12 -0
- package/dist/components/Avatar.d.ts +9 -0
- package/dist/components/Avatar.js +63 -0
- package/dist/components/Badge.d.ts +11 -0
- package/dist/components/Badge.js +87 -0
- package/dist/components/Button.d.ts +23 -0
- package/dist/components/Button.js +249 -0
- package/dist/components/Card.d.ts +12 -0
- package/dist/components/Card.js +38 -0
- package/dist/components/CommandMenu.d.ts +23 -0
- package/dist/components/CommandMenu.js +214 -0
- package/dist/components/CommandPalette.d.ts +1 -0
- package/dist/components/CommandPalette.js +7 -0
- package/dist/components/Content.d.ts +7 -0
- package/dist/components/Content.js +24 -0
- package/dist/components/ContextMenu.d.ts +8 -0
- package/dist/components/ContextMenu.js +23 -0
- package/dist/components/Dialog.d.ts +11 -0
- package/dist/components/Dialog.js +81 -0
- package/dist/components/Divider.d.ts +6 -0
- package/dist/components/Divider.js +15 -0
- package/dist/components/EmptyState.d.ts +8 -0
- package/dist/components/EmptyState.js +24 -0
- package/dist/components/Form.d.ts +9 -0
- package/dist/components/Form.js +23 -0
- package/dist/components/Icon.d.ts +10 -0
- package/dist/components/Icon.js +22 -0
- package/dist/components/IconButton.d.ts +14 -0
- package/dist/components/IconButton.js +53 -0
- package/dist/components/Kbd.d.ts +6 -0
- package/dist/components/Kbd.js +67 -0
- package/dist/components/Link.d.ts +9 -0
- package/dist/components/Link.js +35 -0
- package/dist/components/ListRow.d.ts +12 -0
- package/dist/components/ListRow.js +62 -0
- package/dist/components/LoadingRows.d.ts +5 -0
- package/dist/components/LoadingRows.js +6 -0
- package/dist/components/Menu.d.ts +12 -0
- package/dist/components/Menu.js +33 -0
- package/dist/components/MenuItems.d.ts +21 -0
- package/dist/components/MenuItems.js +32 -0
- package/dist/components/Placement.d.ts +21 -0
- package/dist/components/Placement.js +1 -0
- package/dist/components/Popup.d.ts +8 -0
- package/dist/components/Popup.js +43 -0
- package/dist/components/PopupItem.d.ts +8 -0
- package/dist/components/PopupItem.js +51 -0
- package/dist/components/PropertyRow.d.ts +7 -0
- package/dist/components/PropertyRow.js +23 -0
- package/dist/components/Row.d.ts +16 -0
- package/dist/components/Row.js +27 -0
- package/dist/components/Screen.d.ts +13 -0
- package/dist/components/Screen.js +56 -0
- package/dist/components/Scrim.d.ts +8 -0
- package/dist/components/Scrim.js +40 -0
- package/dist/components/ScrollArea.d.ts +15 -0
- package/dist/components/ScrollArea.js +13 -0
- package/dist/components/SegmentedControl.d.ts +17 -0
- package/dist/components/SegmentedControl.js +68 -0
- package/dist/components/Select.d.ts +17 -0
- package/dist/components/Select.js +107 -0
- package/dist/components/SettingsRow.d.ts +8 -0
- package/dist/components/SettingsRow.js +32 -0
- package/dist/components/Sidebar.d.ts +20 -0
- package/dist/components/Sidebar.js +138 -0
- package/dist/components/SignInScreen.d.ts +1 -0
- package/dist/components/SignInScreen.js +40 -0
- package/dist/components/Size.d.ts +1 -0
- package/dist/components/Size.js +1 -0
- package/dist/components/Skeleton.d.ts +8 -0
- package/dist/components/Skeleton.js +48 -0
- package/dist/components/Spinner.d.ts +6 -0
- package/dist/components/Spinner.js +54 -0
- package/dist/components/Stack.d.ts +14 -0
- package/dist/components/Stack.js +19 -0
- package/dist/components/Switch.d.ts +10 -0
- package/dist/components/Switch.js +71 -0
- package/dist/components/Text.d.ts +21 -0
- package/dist/components/Text.js +77 -0
- package/dist/components/TextField.d.ts +25 -0
- package/dist/components/TextField.js +68 -0
- package/dist/components/Toaster.d.ts +1 -0
- package/dist/components/Toaster.js +83 -0
- package/dist/components/Tooltip.d.ts +8 -0
- package/dist/components/Tooltip.js +43 -0
- package/dist/components/controlLook.d.ts +60 -0
- package/dist/components/controlLook.js +67 -0
- package/dist/components/frame.d.ts +4 -0
- package/dist/components/frame.js +2 -0
- package/dist/components/icons.d.ts +58 -0
- package/dist/components/icons.js +105 -0
- package/dist/components/interaction.d.ts +12 -0
- package/dist/components/interaction.js +22 -0
- package/dist/components/spacing.d.ts +118 -0
- package/dist/components/spacing.js +46 -0
- package/dist/components/tones.d.ts +28 -0
- package/dist/components/tones.js +12 -0
- package/dist/entityStore.d.ts +46 -0
- package/dist/entityStore.js +160 -0
- package/dist/flows/accessFlows.d.ts +9 -0
- package/dist/flows/accessFlows.js +151 -0
- package/dist/flows/browser.d.ts +32 -0
- package/dist/flows/browser.js +164 -0
- package/dist/flows/contactSheet.d.ts +1 -0
- package/dist/flows/contactSheet.js +63 -0
- package/dist/flows/flowBrowser.d.ts +23 -0
- package/dist/flows/flowBrowser.js +21 -0
- package/dist/flows/flowSetup.d.ts +3 -0
- package/dist/flows/flowSetup.js +35 -0
- package/dist/flows/flows.d.ts +41 -0
- package/dist/flows/flows.js +148 -0
- package/dist/flows/screenshotName.d.ts +1 -0
- package/dist/flows/screenshotName.js +6 -0
- package/dist/gate.d.ts +8 -0
- package/dist/gate.js +52 -0
- package/dist/hooks/appContext.d.ts +14 -0
- package/dist/hooks/appContext.js +5 -0
- package/dist/hooks/appHooks.d.ts +21 -0
- package/dist/hooks/appHooks.js +24 -0
- package/dist/hooks/useAnimatesOnChange.d.ts +1 -0
- package/dist/hooks/useAnimatesOnChange.js +14 -0
- package/dist/hooks/useStoreState.d.ts +6 -0
- package/dist/hooks/useStoreState.js +5 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +19 -0
- package/dist/memoryApi.d.ts +19 -0
- package/dist/memoryApi.js +42 -0
- package/dist/model.d.ts +94 -0
- package/dist/model.js +243 -0
- package/dist/page.d.ts +11 -0
- package/dist/page.js +9 -0
- package/dist/platform.d.ts +1 -0
- package/dist/platform.js +5 -0
- package/dist/react.d.ts +46 -0
- package/dist/react.js +38 -0
- package/dist/registered.d.ts +54 -0
- package/dist/registered.js +17 -0
- package/dist/routes.d.ts +38 -0
- package/dist/routes.js +21 -0
- package/dist/rules/checkAllFiles.d.ts +1 -0
- package/dist/rules/checkAllFiles.js +741 -0
- package/dist/rules/checkApiRouting.d.ts +1 -0
- package/dist/rules/checkApiRouting.js +112 -0
- package/dist/rules/lintPlugin.d.ts +252 -0
- package/dist/rules/lintPlugin.js +1157 -0
- package/dist/rules/messages.d.ts +48 -0
- package/dist/rules/messages.js +48 -0
- package/dist/rules/themeTokens.d.ts +6 -0
- package/dist/rules/themeTokens.js +64 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.js +1 -0
- package/dist/server/auth.d.ts +115 -0
- package/dist/server/auth.js +131 -0
- package/dist/server/authService.d.ts +9 -0
- package/dist/server/authService.js +50 -0
- package/dist/server/createServer.d.ts +357 -0
- package/dist/server/createServer.js +62 -0
- package/dist/server/env.d.ts +15 -0
- package/dist/server/env.js +1 -0
- package/dist/server/index.d.ts +8 -0
- package/dist/server/index.js +5 -0
- package/dist/server/session.d.ts +28 -0
- package/dist/server/session.js +54 -0
- package/dist/server/sessions.d.ts +92 -0
- package/dist/server/sessions.js +47 -0
- package/dist/server/signInWire.d.ts +52 -0
- package/dist/server/signInWire.js +22 -0
- package/dist/server/tables.d.ts +242 -0
- package/dist/server/tables.js +246 -0
- package/dist/server/users.d.ts +189 -0
- package/dist/server/users.js +49 -0
- package/dist/shortcuts.d.ts +3 -0
- package/dist/shortcuts.js +40 -0
- package/dist/start.d.ts +20 -0
- package/dist/start.js +121 -0
- package/dist/state.d.ts +14 -0
- package/dist/state.js +17 -0
- package/dist/store.d.ts +43 -0
- package/dist/store.js +198 -0
- package/dist/subscribers.d.ts +5 -0
- package/dist/subscribers.js +13 -0
- package/dist/table.d.ts +143 -0
- package/dist/table.js +91 -0
- package/dist/tables.d.ts +77 -0
- package/dist/tables.js +216 -0
- package/dist/theme/reset.css +84 -0
- package/dist/theme/scales.d.ts +76 -0
- package/dist/theme/scales.js +82 -0
- package/dist/theme/tokens.stylex.d.ts +218 -0
- package/dist/theme/tokens.stylex.js +233 -0
- package/dist/vite/commandBridge.d.ts +2 -0
- package/dist/vite/commandBridge.js +46 -0
- package/dist/vite/designManifest.d.ts +25 -0
- package/dist/vite/designManifest.js +191 -0
- package/dist/vite/localWorker.d.ts +4 -0
- package/dist/vite/localWorker.js +59 -0
- package/dist/vite/rootAlias.d.ts +2 -0
- package/dist/vite/rootAlias.js +15 -0
- package/dist/vite/vite.d.ts +7 -0
- package/dist/vite/vite.js +88 -0
- package/dist/wire.d.ts +7 -0
- package/dist/wire.js +1 -0
- package/package.json +99 -3
- package/presets/lintPlugin.js +7 -0
- package/presets/oxlint.json +167 -0
- package/presets/register.d.ts +19 -0
- package/presets/server.d.ts +2 -0
- package/presets/tsconfig.app.json +29 -0
- package/presets/tsconfig.server.json +27 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { command } from "../model.js";
|
|
3
|
+
import { Model } from "../registered.js";
|
|
4
|
+
export class SignIn extends Model({
|
|
5
|
+
state: {
|
|
6
|
+
step: z.enum(['choose', 'code']).default('choose'),
|
|
7
|
+
email: z.string().default(''),
|
|
8
|
+
signingIn: z.enum(['google', 'email', 'dev']).nullable().default(null),
|
|
9
|
+
notice: z.string().nullable().default(null)
|
|
10
|
+
}
|
|
11
|
+
}) {
|
|
12
|
+
continueWithGoogle = command(() => {
|
|
13
|
+
this.set({ signingIn: 'google', notice: null });
|
|
14
|
+
this.stores.session.continueWithGoogle();
|
|
15
|
+
});
|
|
16
|
+
sendEmailCode = command({ input: { email: z.email() } }, async ({ email }) => {
|
|
17
|
+
this.set({ signingIn: 'email', notice: null });
|
|
18
|
+
try {
|
|
19
|
+
const outcome = await this.stores.session.sendEmailCode(email);
|
|
20
|
+
this.set(outcome === 'sent'
|
|
21
|
+
? { signingIn: null, step: 'code', email }
|
|
22
|
+
: {
|
|
23
|
+
signingIn: null,
|
|
24
|
+
notice: `Couldn’t send a code to ${email}. Try again in a few minutes.`
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
this.set({ signingIn: null, notice: UNREACHABLE });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
verifyEmailCode = command({ input: { code: z.string() } }, async ({ code }) => {
|
|
32
|
+
this.set({ signingIn: 'email', notice: null });
|
|
33
|
+
try {
|
|
34
|
+
const isSignedIn = await this.stores.session.verifyEmailCode(code);
|
|
35
|
+
this.set(isSignedIn
|
|
36
|
+
? { signingIn: null, step: 'choose', email: '' }
|
|
37
|
+
: { signingIn: null, notice: 'That code is wrong or has expired.' });
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
this.set({ signingIn: null, notice: UNREACHABLE });
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
startOver = command(() => {
|
|
44
|
+
this.set({ step: 'choose', email: '', signingIn: null, notice: null });
|
|
45
|
+
});
|
|
46
|
+
signInAsDevUser = command({ input: { email: z.email() } }, async ({ email }) => {
|
|
47
|
+
this.set({ signingIn: 'dev', notice: null });
|
|
48
|
+
try {
|
|
49
|
+
await this.stores.session.devSignIn(email);
|
|
50
|
+
this.set({ signingIn: null });
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
this.set({
|
|
54
|
+
signingIn: null,
|
|
55
|
+
notice: `Couldn’t sign in as ${email}. Is the dev server running?`
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const UNREACHABLE = 'Couldn’t reach the server. Check your connection and try again.';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const toast: z.ZodObject<{
|
|
3
|
+
id: z.ZodString;
|
|
4
|
+
tone: z.ZodEnum<{
|
|
5
|
+
success: "success";
|
|
6
|
+
error: "error";
|
|
7
|
+
neutral: "neutral";
|
|
8
|
+
}>;
|
|
9
|
+
title: z.ZodString;
|
|
10
|
+
description: z.ZodNullable<z.ZodString>;
|
|
11
|
+
}, z.core.$strip>;
|
|
12
|
+
export type Toast = z.output<typeof toast>;
|
|
13
|
+
type ToastInput = Pick<Toast, 'tone' | 'title'> & {
|
|
14
|
+
description?: string;
|
|
15
|
+
};
|
|
16
|
+
declare const Toasts_base: import("../store.ts").StoreClass<{
|
|
17
|
+
items: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
18
|
+
id: z.ZodString;
|
|
19
|
+
tone: z.ZodEnum<{
|
|
20
|
+
success: "success";
|
|
21
|
+
error: "error";
|
|
22
|
+
neutral: "neutral";
|
|
23
|
+
}>;
|
|
24
|
+
title: z.ZodString;
|
|
25
|
+
description: z.ZodNullable<z.ZodString>;
|
|
26
|
+
}, z.core.$strip>>>;
|
|
27
|
+
}, []>;
|
|
28
|
+
export declare class Toasts extends Toasts_base {
|
|
29
|
+
show({ description, ...shown }: ToastInput): void;
|
|
30
|
+
dismiss(id: string): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { Store } from "../store.js";
|
|
3
|
+
const toast = z.object({
|
|
4
|
+
id: z.string(),
|
|
5
|
+
tone: z.enum(['neutral', 'success', 'error']),
|
|
6
|
+
title: z.string(),
|
|
7
|
+
description: z.string().nullable()
|
|
8
|
+
});
|
|
9
|
+
const TOAST_MS = 4000;
|
|
10
|
+
export class Toasts extends Store({
|
|
11
|
+
state: { items: z.array(toast).default([]) }
|
|
12
|
+
}) {
|
|
13
|
+
show({ description, ...shown }) {
|
|
14
|
+
const id = crypto.randomUUID();
|
|
15
|
+
const added = { id, description: description ?? null, ...shown };
|
|
16
|
+
this.set({ items: [...this.state.items, added] });
|
|
17
|
+
setTimeout(() => this.dismiss(id), TOAST_MS);
|
|
18
|
+
}
|
|
19
|
+
dismiss(id) {
|
|
20
|
+
const items = this.state.items.filter((item) => item.id !== id);
|
|
21
|
+
if (items.length !== this.state.items.length)
|
|
22
|
+
this.set({ items });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { appearance } from "../appearance.js";
|
|
2
|
+
import { action, defineActions } from "../registered.js";
|
|
3
|
+
export const BUILT_IN_GROUPS = ['Preferences', 'Account'];
|
|
4
|
+
const appearanceEntries = {
|
|
5
|
+
system: { label: 'Match the system appearance', icon: 'monitor' },
|
|
6
|
+
light: { label: 'Switch to light appearance', icon: 'sun' },
|
|
7
|
+
dark: { label: 'Switch to dark appearance', icon: 'moon' }
|
|
8
|
+
};
|
|
9
|
+
const paletteActions = defineActions(() => [
|
|
10
|
+
action({
|
|
11
|
+
id: 'palette.toggle',
|
|
12
|
+
label: 'Open command menu',
|
|
13
|
+
group: 'Preferences',
|
|
14
|
+
icon: 'search',
|
|
15
|
+
keys: ['mod+k'],
|
|
16
|
+
isHidden: true,
|
|
17
|
+
send: [{ type: 'palette.toggle' }]
|
|
18
|
+
})
|
|
19
|
+
]);
|
|
20
|
+
const appearanceActions = defineActions(({ models }) => appearance.options
|
|
21
|
+
.filter((option) => option !== models.account.appearance)
|
|
22
|
+
.map((option) => action({
|
|
23
|
+
id: `appearance.${option}`,
|
|
24
|
+
label: appearanceEntries[option].label,
|
|
25
|
+
group: 'Preferences',
|
|
26
|
+
icon: appearanceEntries[option].icon,
|
|
27
|
+
keywords: 'theme mode color scheme',
|
|
28
|
+
send: [{ type: 'account.setAppearance', appearance: option }]
|
|
29
|
+
})));
|
|
30
|
+
const accountActions = defineActions(({ models }) => [
|
|
31
|
+
models.account.user !== null &&
|
|
32
|
+
action({
|
|
33
|
+
id: 'account.signOut',
|
|
34
|
+
label: 'Log out',
|
|
35
|
+
group: 'Account',
|
|
36
|
+
icon: 'logOut',
|
|
37
|
+
keywords: 'sign out',
|
|
38
|
+
send: [{ type: 'account.signOut' }]
|
|
39
|
+
})
|
|
40
|
+
]);
|
|
41
|
+
export const builtInActions = [
|
|
42
|
+
paletteActions,
|
|
43
|
+
appearanceActions,
|
|
44
|
+
accountActions
|
|
45
|
+
];
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { Persisted } from './state.ts';
|
|
3
|
+
export type CacheStorage = {
|
|
4
|
+
get: (key: string) => string | undefined;
|
|
5
|
+
set: (key: string, value: string) => void;
|
|
6
|
+
keys: () => readonly string[];
|
|
7
|
+
remove: (key: string) => void;
|
|
8
|
+
};
|
|
9
|
+
export type CacheKey = string | (() => string);
|
|
10
|
+
export declare class Cached<Value> {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(storage: CacheStorage, key: CacheKey, schema: z.ZodType<Value>);
|
|
13
|
+
read(): Value | undefined;
|
|
14
|
+
write(value: Value): void;
|
|
15
|
+
}
|
|
16
|
+
export declare class Cache {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(storage: CacheStorage);
|
|
19
|
+
static local(): Cache;
|
|
20
|
+
static memory(): Cache;
|
|
21
|
+
scope<Value>(key: CacheKey, schema: z.ZodType<Value>): Cached<Value>;
|
|
22
|
+
persisted(model: string): Persisted;
|
|
23
|
+
clear(): void;
|
|
24
|
+
}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
const PREFIX = 'pattern:';
|
|
2
|
+
export class Cached {
|
|
3
|
+
#storage;
|
|
4
|
+
#key;
|
|
5
|
+
#schema;
|
|
6
|
+
constructor(storage, key, schema) {
|
|
7
|
+
this.#storage = storage;
|
|
8
|
+
this.#key = key;
|
|
9
|
+
this.#schema = schema;
|
|
10
|
+
}
|
|
11
|
+
get #fullKey() {
|
|
12
|
+
return PREFIX + (typeof this.#key === 'string' ? this.#key : this.#key());
|
|
13
|
+
}
|
|
14
|
+
read() {
|
|
15
|
+
try {
|
|
16
|
+
const text = this.#storage.get(this.#fullKey);
|
|
17
|
+
if (text === undefined)
|
|
18
|
+
return undefined;
|
|
19
|
+
const parsed = this.#schema.safeParse(JSON.parse(text));
|
|
20
|
+
return parsed.success ? parsed.data : undefined;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
write(value) {
|
|
27
|
+
try {
|
|
28
|
+
this.#storage.set(this.#fullKey, JSON.stringify(value));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class Cache {
|
|
36
|
+
#storage;
|
|
37
|
+
constructor(storage) {
|
|
38
|
+
this.#storage = storage;
|
|
39
|
+
}
|
|
40
|
+
static local() {
|
|
41
|
+
return new Cache(new LocalStorage());
|
|
42
|
+
}
|
|
43
|
+
static memory() {
|
|
44
|
+
return new Cache(new MemoryStorage());
|
|
45
|
+
}
|
|
46
|
+
scope(key, schema) {
|
|
47
|
+
return new Cached(this.#storage, key, schema);
|
|
48
|
+
}
|
|
49
|
+
persisted(model) {
|
|
50
|
+
const storage = this.#storage;
|
|
51
|
+
const keyOf = (key) => `${PREFIX}${model}.${key}`;
|
|
52
|
+
return {
|
|
53
|
+
read(key) {
|
|
54
|
+
try {
|
|
55
|
+
const text = storage.get(keyOf(key));
|
|
56
|
+
return text === undefined ? undefined : JSON.parse(text);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
write(key, value) {
|
|
63
|
+
try {
|
|
64
|
+
storage.set(keyOf(key), JSON.stringify(value));
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
clear() {
|
|
73
|
+
try {
|
|
74
|
+
for (const key of this.#storage.keys()) {
|
|
75
|
+
if (key.startsWith(PREFIX))
|
|
76
|
+
this.#storage.remove(key);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
class LocalStorage {
|
|
85
|
+
get(key) {
|
|
86
|
+
return localStorage.getItem(key) ?? undefined;
|
|
87
|
+
}
|
|
88
|
+
set(key, value) {
|
|
89
|
+
localStorage.setItem(key, value);
|
|
90
|
+
}
|
|
91
|
+
keys() {
|
|
92
|
+
return Array.from({ length: localStorage.length }, (_, index) => localStorage.key(index)).filter((key) => key !== null);
|
|
93
|
+
}
|
|
94
|
+
remove(key) {
|
|
95
|
+
localStorage.removeItem(key);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
class MemoryStorage {
|
|
99
|
+
#entries = new Map();
|
|
100
|
+
get(key) {
|
|
101
|
+
return this.#entries.get(key);
|
|
102
|
+
}
|
|
103
|
+
set(key, value) {
|
|
104
|
+
this.#entries.set(key, value);
|
|
105
|
+
}
|
|
106
|
+
keys() {
|
|
107
|
+
return [...this.#entries.keys()];
|
|
108
|
+
}
|
|
109
|
+
remove(key) {
|
|
110
|
+
this.#entries.delete(key);
|
|
111
|
+
}
|
|
112
|
+
}
|