Skip to content

Commit

Permalink
Adds new serialize decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Dec 4, 2021
1 parent 315bda0 commit 9a9b9ce
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/system.ts
Expand Up @@ -23,6 +23,7 @@ export * as Dates from './system/date';
export * from './system/decorators/gate';
export * from './system/decorators/log';
export * from './system/decorators/memoize';
export * from './system/decorators/serialize';
export * from './system/decorators/timeout';
export * as Encoding from './system/encoding';
export * as Functions from './system/function';
Expand Down
6 changes: 3 additions & 3 deletions src/system/decorators/gate.ts
@@ -1,4 +1,5 @@
'use strict';
import { Uri } from 'vscode';
import { is as isPromise } from '../promise';

const emptyStr = '';
Expand All @@ -8,9 +9,8 @@ function defaultResolver(...args: any[]): string {
const arg0 = args[0];
if (arg0 == null) return emptyStr;
if (typeof arg0 === 'string') return arg0;
if (typeof arg0 === 'number' || typeof arg0 === 'boolean') {
return String(arg0);
}
if (typeof arg0 === 'number' || typeof arg0 === 'boolean' || arg0 instanceof Error) return String(arg0);
if (arg0 instanceof Uri) return arg0.toString();

return JSON.stringify(arg0);
}
Expand Down
61 changes: 61 additions & 0 deletions src/system/decorators/serialize.ts
@@ -0,0 +1,61 @@
'use strict';
import { Uri } from 'vscode';

const emptyStr = '';

function defaultResolver(...args: any[]): string {
if (args.length === 1) {
const arg0 = args[0];
if (arg0 === undefined) return emptyStr;
if (typeof arg0 === 'string') return arg0;
if (typeof arg0 === 'number' || typeof arg0 === 'boolean' || arg0 instanceof Error) return String(arg0);
if (arg0 instanceof Uri) return arg0.toString();

return JSON.stringify(arg0);
}

return JSON.stringify(args);
}

export function serialize<T extends (...arg: any) => any>(
resolver?: (...args: Parameters<T>) => string,
): (target: any, key: string, descriptor: PropertyDescriptor) => void {
return (target: any, key: string, descriptor: PropertyDescriptor) => {
let fn: Function | undefined;
if (typeof descriptor.value === 'function') {
fn = descriptor.value;
} else if (typeof descriptor.get === 'function') {
fn = descriptor.get;
}
if (fn === undefined) throw new Error('Not supported');

const serializeKey = `$serialize$${key}`;

descriptor.value = function (this: any, ...args: any[]) {
const prop =
args.length === 0
? serializeKey
: `${serializeKey}$${(resolver ?? defaultResolver)(...(args as Parameters<T>))}`;

if (!Object.prototype.hasOwnProperty.call(this, prop)) {
Object.defineProperty(this, prop, {
configurable: false,
enumerable: false,
writable: true,
value: undefined,
});
}

let promise = this[prop];
const run = () => fn!.apply(this, args);
if (promise === undefined) {
promise = run();
} else {
promise = promise.then(run, run);
}

this[prop] = promise;
return promise;
};
};
}

0 comments on commit 9a9b9ce

Please sign in to comment.