-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sync): add initial implementation
- Loading branch information
Showing
10 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
!/bin/**/* | ||
!/dist/**/* | ||
/src/ | ||
tsconfig.json | ||
tslint.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 Ihor Chulinda | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
[npm-badge-png]: https://nodei.co/npm/metaf-sync.png?downloads=true&downloadRank=true&stars=true | ||
[package-url]: https://npmjs.com/package/metaf-sync | ||
|
||
[![npm badge][npm-badge-png]][package-url] | ||
|
||
[![Known Vulnerabilities](https://snyk.io/test/npm/metaf-core/badge.svg)](https://snyk.io/test/npm/metaf-sync) | ||
|
||
|
||
# MetaF Sync | ||
> Sync package for **MetaF**ramework. | ||
> **WARNING:** it's early beta, so documentation may have mistakes, if you face any problems feel free to create [issues](https://github.com/Igmat/metaf/issues). | ||
## Table of Contents | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
|
||
- [What is it?](#what-is-it) | ||
- [Motivation](#motivation) | ||
- [How it works?](#how-it-works) | ||
- [Why do I have to use it?](#why-do-i-have-to-use-it) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
## What is it? | ||
**TBD** | ||
|
||
## Motivation | ||
**TBD** | ||
|
||
## How it works? | ||
**TBD** | ||
|
||
## Why do I have to use it? | ||
**TBD** | ||
|
||
## Installation | ||
Run: | ||
``` | ||
npm install metaf-core | ||
``` | ||
|
||
## Usage | ||
**TBD** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "metaf-sync", | ||
"version": "0.2.34", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"private": true, | ||
"types": "dist/index.d.ts", | ||
"author": "Ihor Chulinda <ichulinda@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:Igmat/metaf.git" | ||
}, | ||
"scripts": { | ||
"doctoc": "doctoc README.md", | ||
"prepublish": "npm run doctoc" | ||
}, | ||
"dependencies": { | ||
"metaf-resolvable": "^0.2.33" | ||
}, | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './runInSync'; | ||
export * from './sync'; | ||
export * from './syncService'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { PromiseCache } from './sync'; | ||
|
||
export const context = { | ||
cache: new WeakMap<Function, { [args: string]: unknown }>(), | ||
}; | ||
|
||
export function runInSync(app: () => void, cache = new WeakMap<Function, { [args: string]: unknown }>()) { | ||
context.cache = cache; | ||
try { | ||
app(); | ||
} catch (err) { | ||
if (!(err instanceof PromiseCache)) throw err; | ||
err.resolved | ||
.then(() => runInSync(app, cache)) | ||
.catch(innerError => { throw innerError; }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const methodsToThrow = [ | ||
'getPrototypeOf', | ||
'setPrototypeOf', | ||
'isExtensible', | ||
'preventExtensions', | ||
'getOwnPropertyDescriptor', | ||
'has', | ||
'get', | ||
'set', | ||
'deleteProperty', | ||
'defineProperty', | ||
'enumerate', | ||
'ownKeys', | ||
'apply', | ||
'construct', | ||
]; | ||
export class PromiseCache { | ||
content = new WeakMap<Promise<unknown>, unknown>(); | ||
proxies = new WeakMap<Promise<unknown>, unknown>(); | ||
resolved = Promise.resolve<unknown>(undefined); | ||
} | ||
const promiseCache = new PromiseCache(); | ||
const trap = () => { | ||
throw promiseCache; | ||
}; | ||
const pendingHandler = methodsToThrow.reduce((result, method) => ({...result, [method]: trap}), {}); | ||
type Primitives = string | number | boolean | symbol | undefined | null; | ||
export type PrimitivesWrapper<T> = T extends Primitives ? { value: T } : T; | ||
export type AsyncPrimitivesWrapper<T> = T extends Promise<infer R> | ||
? PrimitivesWrapper<R> | ||
: T; | ||
export function sync<P>(promise: P): AsyncPrimitivesWrapper<P> { | ||
if (!(promise instanceof Promise)) return promise as AsyncPrimitivesWrapper<P>; | ||
if (promiseCache.content.has(promise)) return promiseCache.content.get(promise) as AsyncPrimitivesWrapper<P>; | ||
if (promiseCache.proxies.has(promise)) return promiseCache.proxies.get(promise) as AsyncPrimitivesWrapper<P>; | ||
promiseCache.resolved = Promise.all([ | ||
promiseCache.resolved, | ||
promise.then(value => promiseCache.content.set(promise, wrapPrimitive(value))), | ||
]); | ||
const pendingProxy = new Proxy({}, pendingHandler) as AsyncPrimitivesWrapper<P>; | ||
promiseCache.proxies.set(promise, pendingProxy); | ||
|
||
return pendingProxy; | ||
} | ||
|
||
function wrapPrimitive<T extends unknown>(value: T): PrimitivesWrapper<T> { | ||
if (typeof value === 'string' || | ||
typeof value === 'number' || | ||
typeof value === 'boolean' || | ||
typeof value === 'symbol' || | ||
typeof value === 'undefined' || | ||
value === null) { | ||
return { value } as PrimitivesWrapper<T>; | ||
} | ||
|
||
return value as PrimitivesWrapper<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Callable, Constructable, isConstructor } from 'metaf-resolvable'; | ||
import { context } from './runInSync'; | ||
import { AsyncPrimitivesWrapper, sync } from './sync'; | ||
type TypedFunction<T, ARGS extends unknown[]> = (...args: ARGS) => T; | ||
function isFunction<T, ARGS extends unknown[]>(arg: unknown): arg is TypedFunction<T, ARGS> { | ||
return typeof arg === 'function'; | ||
} | ||
export type SynchronizedProperty<T> = | ||
T extends () => infer R | ||
? () => AsyncPrimitivesWrapper<R> | ||
// FIXME: remove this hack for JSX syntax when TS will properly use createElement signature | ||
: T extends (props: infer PROPS, ...args: infer ARGS) => infer R | ||
? (props: PROPS, ...args: ARGS) => AsyncPrimitivesWrapper<R> | ||
: T extends (...args: infer ARGS) => infer R | ||
? TypedFunction<AsyncPrimitivesWrapper<R>, ARGS> | ||
: AsyncPrimitivesWrapper<T>; | ||
export type Synchronous<I extends object> = { | ||
[P in keyof I]: SynchronizedProperty<I[P]>; | ||
}; | ||
function synchronizeFunction<R, ARGS extends unknown[]>(method: TypedFunction<R, ARGS>): TypedFunction<AsyncPrimitivesWrapper<R>, ARGS> { | ||
return function (this: unknown, ...args: ARGS) { | ||
if (!context.cache.has(method)) context.cache.set(method, {}); | ||
const functionCache = context.cache.get(method) as { [arg: string]: R }; | ||
const key = JSON.stringify(args); | ||
if (!functionCache.hasOwnProperty(key)) functionCache[key] = method.call(this, ...args); | ||
|
||
return sync(functionCache[key]); | ||
}; | ||
} | ||
export function syncService<I extends object>(serviceConstructor: Constructable<[], I> | Callable<I>): Synchronous<I> { | ||
const instance = isConstructor(serviceConstructor) | ||
? new serviceConstructor() | ||
: serviceConstructor(); | ||
const result: Partial<Synchronous<I>> = {}; | ||
// We want to wrap all properties, including inherited, | ||
// so we don't need tslint warning as about not filtering | ||
// forin statement to only own properties | ||
// tslint:disable-next-line:forin | ||
for (const key in instance) { | ||
const property = instance[key]; | ||
result[key] = (isFunction(property) | ||
? synchronizeFunction(property) | ||
: (property instanceof Promise) | ||
? sync(property) | ||
: property) as Synchronous<I>[Extract<keyof Synchronous<I>, string>]; | ||
} | ||
|
||
return result as Synchronous<I>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src" | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": [ | ||
"../../tslint.json" | ||
], | ||
"rules": { | ||
"no-object-literal-type-assertion": false, | ||
"interface-name": false, | ||
"no-default-export": false, | ||
"variable-name": false | ||
} | ||
} |