Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic parameter so TS users can use serialize: false #471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
{
"version": "0.1.0",
"version": "2.0.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-w", "-p", ".", "--noEmit"],
"isBackground": true,
"problemMatcher": "$tsc-watch"
"problemMatcher": "$tsc-watch",
"tasks": [
{
"label": "tsc",
"type": "shell",
"command": "tsc",
"args": [
"-w",
"-p",
".",
"--noEmit"
],
"isBackground": true,
"problemMatcher": "$tsc-watch",
"group": {
"_id": "build",
"isDefault": false
}
}
]
}
4 changes: 2 additions & 2 deletions src/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ApolloCache } from '@apollo/client/core';
import { ApolloPersistOptions, PersistedData } from './types';

export default class Cache<T> {
export default class CacheLog<T, U extends boolean = true> {
cache: ApolloCache<T>;
serialize: boolean;

constructor(options: ApolloPersistOptions<T>) {
constructor(options: ApolloPersistOptions<T, U>) {
const { cache, serialize = true } = options;

this.cache = cache;
Expand Down
4 changes: 2 additions & 2 deletions src/CachePersistor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import Trigger from './Trigger';

import { ApolloPersistOptions, LogLine } from './types';

export default class CachePersistor<T> {
export default class CachePersistor<T, U extends boolean = true> {
log: Log<T>;
cache: Cache<T>;
storage: Storage<T>;
persistor: Persistor<T>;
trigger: Trigger<T>;

constructor(options: ApolloPersistOptions<T>) {
constructor(options: ApolloPersistOptions<T, U>) {
if (!options.cache) {
throw new Error(
'In order to persist your Apollo Cache, you need to pass in a cache. ' +
Expand Down
4 changes: 2 additions & 2 deletions src/Log.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ApolloPersistOptions, LogLevel, LogLine } from './types';

export default class Log<T> {
export default class Log<T, U extends boolean = true> {
debug: boolean;
lines: Array<LogLine>;

static buffer = 30;
static prefix = '[apollo-cache-persist]';

constructor(options: ApolloPersistOptions<T>) {
constructor(options: ApolloPersistOptions<T, U>) {
const { debug = false } = options;

this.debug = debug;
Expand Down
4 changes: 2 additions & 2 deletions src/Persistor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface PersistorConfig<T> {
storage: Storage<T>;
}

export default class Persistor<T> {
export default class Persistor<T, U extends boolean = true> {
log: Log<T>;
cache: Cache<T>;
storage: Storage<T>;
Expand All @@ -20,7 +20,7 @@ export default class Persistor<T> {

constructor(
{ log, cache, storage }: PersistorConfig<T>,
options: ApolloPersistOptions<T>,
options: ApolloPersistOptions<T, U>,
) {
const {
maxSize = 1024 * 1024,
Expand Down
4 changes: 2 additions & 2 deletions src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
PersistedData,
} from './types';

export default class Storage<T> {
export default class Storage<T, U extends boolean = true> {
storage: PersistentStorage<PersistedData<T>>;
key: string;

constructor(options: ApolloPersistOptions<T>) {
constructor(options: ApolloPersistOptions<T, U>) {
const { storage, key = 'apollo-cache-persist' } = options;

this.storage = storage;
Expand Down
4 changes: 2 additions & 2 deletions src/Trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface TriggerConfig<T> {
persistor: Persistor<T>;
}

export default class Trigger<T> {
export default class Trigger<T, U extends boolean = true> {
debounce: number;
persistor: Persistor<T>;
paused: boolean;
Expand All @@ -21,7 +21,7 @@ export default class Trigger<T> {

constructor(
{ log, persistor }: TriggerConfig<T>,
options: ApolloPersistOptions<T>
options: ApolloPersistOptions<T, U>
) {
const { defaultDebounce } = Trigger;
const { cache, debounce, trigger = 'write' } = options;
Expand Down
18 changes: 9 additions & 9 deletions src/persistCacheSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import Storage from './Storage';

/**
* Add cache to persist engine using synchronous API
*
*
* @see SynchronousCachePersistor for advanced use cases
* @param options options for persist engine
*/
export const persistCacheSync = <T>(options: ApolloPersistOptions<T>) => {
export const persistCacheSync = <T, U extends boolean = true>(options: ApolloPersistOptions<T, U>) => {
const cachePersistor = new SynchronousCachePersistor(options);
cachePersistor.restoreSync();
};

/**
* Persistor engine that is going to use synchronous api
*/
export class SynchronousCachePersistor<T> extends CachePersistor<T> {
export class SynchronousCachePersistor<T, U extends boolean = true> extends CachePersistor<T, U> {
persistor: SynchronousPersistor<T>;

constructor(options: ApolloPersistOptions<T>) {
constructor(options: ApolloPersistOptions<T, U>) {
super(options);

this.storage = new SynchronousStorage(options);
Expand All @@ -35,12 +35,12 @@ export class SynchronousCachePersistor<T> extends CachePersistor<T> {
}
}

export class SynchronousPersistor<T> extends Persistor<T> {
storage: SynchronousStorage<T>;
export class SynchronousPersistor<T, U extends boolean = true> extends Persistor<T, U> {
storage: SynchronousStorage<T, U>;

constructor(
{ log, cache, storage }: PersistorConfig<T>,
options: ApolloPersistOptions<T>,
options: ApolloPersistOptions<T, U>,
) {
super({ log, cache, storage }, options);
}
Expand All @@ -50,8 +50,8 @@ export class SynchronousPersistor<T> extends Persistor<T> {
}
}

export class SynchronousStorage<T> extends Storage<T> {
constructor(options: ApolloPersistOptions<T>) {
export class SynchronousStorage<T, U extends boolean = true> extends Storage<T, U> {
constructor(options: ApolloPersistOptions<T, U>) {
super(options);
}

Expand Down