Skip to content

Commit

Permalink
cache also returns the value in local cache in the promise
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Oct 28, 2023
1 parent fe7c31a commit 7c4c7cf
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function isInRemoteChange() {
return internalState.globalState.isLoadingRemote$.get();
}

import type { ObservablePersistenceConfig } from './src/observableInterfaces';
import type { CacheReturnValue, ObservablePersistenceConfig } from './src/observableInterfaces';
import { observablePersistConfiguration } from './src/persist/configureObservablePersistence';
export const internal: {
observablePersistConfiguration: ObservablePersistenceConfig;
Expand All @@ -21,7 +21,7 @@ persistActivateNode();

declare module '@legendapp/state' {
interface ActivateParams<T> {
cache: (cacheOptions: CacheOptions<T>) => Promise<{ dateModified: number }>;
cache: (cacheOptions: CacheOptions<T>) => Promise<CacheReturnValue>;
updateLastSync: (lastSync: number) => void;
retry: (options?: RetryOptions) => void;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import type {
ObservablePersistStateInternal,
OnSetExtra,
SubscribeOptions,
CacheReturnValue,
} from './observableInterfaces';
import { observe } from './observe';
import { onChange } from './onChange';
Expand Down Expand Up @@ -882,12 +883,12 @@ function createNodeActivationParams(node: NodeValue): ActivateProxyParams {
if (!state.cacheOptions) {
state.cacheOptions = isFunction(fn) ? fn() : fn;
}
return new Promise<{ dateModified: number }>((resolve) => {
return new Promise<CacheReturnValue>((resolve) => {
const wait = () => {
if (node.state) {
when(node.state!.isLoadedLocal, () => {
const dateModified = node.state!.dateModified.get();
resolve({ dateModified });
resolve({ dateModified, value: peek(node) });
});
} else {
queueMicrotask(wait);
Expand Down
4 changes: 4 additions & 0 deletions src/observableInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,7 @@ export interface SubscribeOptions {
update: UpdateFn;
refresh: () => void;
}
export interface CacheReturnValue {
dateModified: number;
value: any;
}
3 changes: 2 additions & 1 deletion tests/computed-persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ describe('caching with new computed', () => {
localStorage.setItem('nodes__m', JSON.stringify({ modified: 1000 }));

const nodes = observable(async ({ cache }: ActivateParams) => {
const { dateModified } = await cache({
const { dateModified, value } = await cache({
pluginLocal: ObservablePersistLocalStorage,
local: 'nodes',
});
expect(dateModified).toEqual(1000);
expect(value).toEqual({ key0: { key: 'key0' } });
const nodes = await new Promise<{ key: string }[]>((resolve) =>
setTimeout(() => resolve([{ key: 'key1' }]), 2),
);
Expand Down

0 comments on commit 7c4c7cf

Please sign in to comment.