@aire-ux/aire-condensation
Advanced tools
Comparing version 0.0.5 to 0.1.1
@@ -19,2 +19,3 @@ import TypeRegistry from "./type-registry"; | ||
export declare class Condensation { | ||
static context: Context; | ||
static registry: TypeRegistry; | ||
@@ -26,2 +27,3 @@ static remoteRegistry: RemoteRegistry; | ||
static newContext(): Context; | ||
static defaultContext(): Context; | ||
} | ||
@@ -28,0 +30,0 @@ export declare type RegistrationDefinition = { |
@@ -65,2 +65,8 @@ var __read = (this && this.__read) || function (o, n) { | ||
}; | ||
Condensation.defaultContext = function () { | ||
if (!Condensation.context) { | ||
Condensation.context = Condensation.newContext(); | ||
} | ||
return Condensation.context; | ||
}; | ||
Condensation.deserializerConfigurations = new Map(); | ||
@@ -72,3 +78,3 @@ return Condensation; | ||
function DefaultCondensationContext(region) { | ||
if (region === void 0) { region = new Region(); } | ||
if (region === void 0) { region = new Region("default"); } | ||
this.region = region; | ||
@@ -75,0 +81,0 @@ } |
@@ -34,2 +34,5 @@ /** | ||
export declare function allocate<T>(value: T, region?: Region): Pointer<T>; | ||
export declare class Regions { | ||
static regions: Map<string | number, Region>; | ||
} | ||
/** | ||
@@ -39,4 +42,5 @@ * a contiguous set of memory locations | ||
export declare class Region { | ||
static value: number; | ||
values: any[]; | ||
constructor(); | ||
constructor(name?: string); | ||
address(): number; | ||
@@ -43,0 +47,0 @@ addressOf<T>(t: T): Address; |
@@ -52,2 +52,9 @@ /** | ||
} | ||
var Regions = /** @class */ (function () { | ||
function Regions() { | ||
} | ||
Regions.regions = new Map(); | ||
return Regions; | ||
}()); | ||
export { Regions }; | ||
/** | ||
@@ -57,4 +64,10 @@ * a contiguous set of memory locations | ||
var Region = /** @class */ (function () { | ||
function Region() { | ||
function Region(name) { | ||
this.values = []; | ||
if (name) { | ||
Regions.regions.set(name, this); | ||
} | ||
else { | ||
Regions.regions.set(Region.value++, this); | ||
} | ||
} | ||
@@ -61,0 +74,0 @@ Region.prototype.address = function () { |
{ | ||
"name": "@aire-ux/aire-condensation", | ||
"version": "0.0.5", | ||
"version": "0.1.1", | ||
"description": "Client-side serialization library for Aire-UX", | ||
@@ -26,3 +26,4 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@babel/preset-typescript": "^7.16.0", | ||
"@babel/preset-env": "^7.16.11", | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@types/jest": "^27.0.3", | ||
@@ -33,2 +34,3 @@ "@zerollup/ts-transform-paths": "^1.7.18", | ||
"lint-staged": "^12.1.2", | ||
"lit": "2.0.0", | ||
"prettier": "2.5.0", | ||
@@ -41,2 +43,3 @@ "ts-jest": "^27.0.7", | ||
"preset": "ts-jest", | ||
"testEnvironment": "jsdom", | ||
"rootDir": "./", | ||
@@ -52,7 +55,32 @@ "testMatch": [ | ||
"src/test/typescript" | ||
] | ||
], | ||
"transform": { | ||
"^.+\\.(js|ts)$": "ts-jest" | ||
}, | ||
"moduleFileExtensions": [ | ||
"js", | ||
"jsx", | ||
"ts", | ||
"tsx" | ||
], | ||
"moduleDirectories": [ | ||
"node_modules", | ||
"src" | ||
], | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!\\@?lit)" | ||
], | ||
"globals": { | ||
"ts-jest": { | ||
"tsconfig": { | ||
"allowJs": true | ||
} | ||
}, | ||
"window": {} | ||
} | ||
}, | ||
"dependencies": { | ||
"reflect-metadata": "^0.1.13" | ||
} | ||
}, | ||
"gitHead": "37dea4f6338261ebd81b34a7339dec8b29eaf88e" | ||
} |
@@ -18,3 +18,2 @@ import TypeRegistry from "@condensation/type-registry"; | ||
} from "@condensation/deserializer"; | ||
import { add } from "husky"; | ||
@@ -32,2 +31,15 @@ export type Format = "json"; | ||
formalParams<T>( | ||
t: Class<T>, | ||
type: InvocationType, | ||
...args: string[] | ||
): any[]; | ||
invokeDirect<T, U>( | ||
value: T, | ||
op: string, | ||
...args: string[] | ||
): U | null; | ||
invoke<T, U>( | ||
@@ -48,2 +60,3 @@ address: Address | Pointer<T>, | ||
export class Condensation { | ||
static context: Context; | ||
static registry: TypeRegistry; | ||
@@ -71,6 +84,13 @@ static remoteRegistry: RemoteRegistry; | ||
} | ||
static defaultContext() { | ||
if (!Condensation.context) { | ||
Condensation.context = Condensation.newContext(); | ||
} | ||
return Condensation.context; | ||
} | ||
} | ||
class DefaultCondensationContext implements Context { | ||
constructor(readonly region = new Region()) {} | ||
constructor(readonly region = new Region("default")) {} | ||
@@ -89,3 +109,16 @@ create<T>(t: Class<T>, ...args: string[]): Pointer<T> { | ||
} | ||
invokeDirect<T, U>(value: T, op: string, ...args: string[]): U | null { | ||
const operation = (value as any)[op] as any; | ||
if (!operation) { | ||
throw new Error(`Type ${typeof value} has no method named '${op}'`); | ||
} | ||
const formals = this.formalParams( | ||
Object.getPrototypeOf(value).constructor, | ||
"method", | ||
...args | ||
); | ||
return operation.apply(value, formals); | ||
} | ||
invoke<T, U>( | ||
@@ -128,3 +161,3 @@ address: Address | Pointer<T>, | ||
private formalParams<T>( | ||
public formalParams<T>( | ||
t: Class<T>, | ||
@@ -151,2 +184,3 @@ type: InvocationType, | ||
} | ||
} | ||
@@ -153,0 +187,0 @@ |
@@ -1,9 +0,12 @@ | ||
import { Class } from "@condensation/types"; | ||
import { Condensation } from "@condensation/condensation"; | ||
import {Class} from "@condensation/types"; | ||
import {Condensation} from "@condensation/condensation"; | ||
export function Remotable<T>(type: Class<T>): Class<T> { | ||
export function Remotable<T>(type: Class<T>): void { | ||
Condensation.remoteRegistry.register(type); | ||
return type; | ||
// return type; | ||
} | ||
const ctx = Condensation.defaultContext(); | ||
export function Receive<T>(type: Class<T>) { | ||
@@ -28,1 +31,14 @@ return <U>(target: Class<U>, key: PropertyKey, index: number) => { | ||
} | ||
export function Remote( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: TypedPropertyDescriptor<any> | ||
) { | ||
const original = descriptor.value; | ||
descriptor.value = (...args: any[]) => { | ||
const formals = ctx.formalParams(target.constructor, 'method', ...args); | ||
return original.apply(target, formals); | ||
} | ||
} |
@@ -21,2 +21,3 @@ /** | ||
public readonly value: number; | ||
constructor(region: Region | number) { | ||
@@ -73,2 +74,6 @@ if (isRegion(region)) { | ||
export class Regions { | ||
static regions: Map<string | number, Region> = new Map(); | ||
} | ||
/** | ||
@@ -78,5 +83,12 @@ * a contiguous set of memory locations | ||
export class Region { | ||
static value: number; | ||
values: any[]; | ||
constructor() { | ||
constructor(name?: string) { | ||
this.values = []; | ||
if (name) { | ||
Regions.regions.set(name, this); | ||
} else { | ||
Regions.regions.set(Region.value++, this); | ||
} | ||
} | ||
@@ -83,0 +95,0 @@ |
@@ -1,12 +0,15 @@ | ||
import { Receive, Remotable } from "@condensation/remotable"; | ||
import { Property, RootElement } from "@condensation/root-element"; | ||
import { Condensation } from "@condensation/condensation"; | ||
import {Receive, Remotable, Remote} from "@condensation/remotable"; | ||
import {Property, RootElement} from "@condensation/root-element"; | ||
import {Condensation} from "@condensation/condensation"; | ||
import {customElement, LitElement} from "lit-element"; | ||
test("remotable should work with constructor arguments", () => { | ||
@RootElement | ||
class TestDTO {} | ||
class TestDTO { | ||
} | ||
@Remotable | ||
class TestReceiver { | ||
constructor(@Receive(TestDTO) dto: TestDTO) {} | ||
constructor(@Receive(TestDTO) dto: TestDTO) { | ||
} | ||
} | ||
@@ -20,2 +23,24 @@ | ||
test('remotable should work with subclasses', () => { | ||
@RootElement | ||
class TestDTO { | ||
} | ||
class Parent { | ||
id: string | undefined; | ||
} | ||
@Remotable | ||
class TestReceiver extends Parent { | ||
constructor(@Receive(TestDTO) dto: TestDTO) { | ||
super(); | ||
} | ||
} | ||
const defs = Condensation.remoteRegistry.resolve(TestReceiver).definitions; | ||
expect(defs.length).toBe(1); | ||
expect(defs[0].index).toBe(0); | ||
}); | ||
test("remotable should allow a value to be constructed", () => { | ||
@@ -42,9 +67,11 @@ @RootElement | ||
@Remotable | ||
class TestReceiver { | ||
@customElement('test-receiver') | ||
class TestReceiver extends LitElement { | ||
name: string | undefined; | ||
constructor( | ||
@Receive(Pet) public readonly pet: Pet, | ||
@Receive(Person) public readonly dto: Person | ||
@Receive(Pet) public readonly pet: Pet, | ||
@Receive(Person) public readonly dto: Person | ||
) { | ||
super(); | ||
this.name = dto.name; | ||
@@ -60,4 +87,4 @@ } | ||
const receiver = ctx.create<TestReceiver>( | ||
TestReceiver, | ||
` | ||
TestReceiver, | ||
` | ||
{ | ||
@@ -70,3 +97,3 @@ "name": "Flances", | ||
`, | ||
`{ | ||
`{ | ||
"name": "Josiah" | ||
@@ -136,9 +163,10 @@ }` | ||
constructor( | ||
@Receive(GraphConfiguration) readonly configuration: GraphConfiguration | ||
) {} | ||
@Receive(GraphConfiguration) readonly configuration: GraphConfiguration | ||
) { | ||
} | ||
} | ||
const mgr = Condensation.newContext().create<MxGraphManager>( | ||
MxGraphManager, | ||
`{ | ||
MxGraphManager, | ||
`{ | ||
"load-resources": "loading them resources" | ||
@@ -205,8 +233,8 @@ }` | ||
const ctx = Condensation.newContext(), | ||
mgr = ctx.create<MxGraphManager>(MxGraphManager); | ||
mgr = ctx.create<MxGraphManager>(MxGraphManager); | ||
ctx.invoke( | ||
mgr, | ||
"init", | ||
` | ||
mgr, | ||
"init", | ||
` | ||
{ | ||
@@ -220,1 +248,28 @@ "name": "Josiah" | ||
}); | ||
test("values should be invocable", () => { | ||
@RootElement | ||
class Person { | ||
@Property(String) | ||
name: string | undefined; | ||
} | ||
@Remotable | ||
class MxGraphManager { | ||
person: Person | undefined; | ||
@Remote | ||
public init(@Receive(Person) person: Person): void { | ||
this.person = person; | ||
} | ||
} | ||
const mgr = new MxGraphManager(); | ||
mgr.init(`{ | ||
"name": "Josiah" | ||
} | ||
` as any); | ||
expect(mgr?.person?.name).toBe("Josiah"); | ||
}); |
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"target": "es5", | ||
"module": "ESNext", | ||
"target": "ES2015", | ||
"module": "ES2015", | ||
"moduleResolution": "Node", | ||
@@ -10,3 +11,3 @@ "experimentalDecorators": true, | ||
"downlevelIteration": true, | ||
"lib": ["es2015"], | ||
"lib": ["es2015", "dom"], | ||
"strict": true, | ||
@@ -13,0 +14,0 @@ "skipLibCheck": true, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122788
3275
12
46