Skip to content

Commit

Permalink
Added Files and Documentations - Release
Browse files Browse the repository at this point in the history
  • Loading branch information
prkeshri committed Oct 17, 2021
1 parent 7507689 commit 343e165
Show file tree
Hide file tree
Showing 17 changed files with 940 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Future.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { EventEmitter } = require('events');
const { Func, noFunc } = require("./Internal");

class Future extends EventEmitter {
constructor(f = undefined) {
if (typeof f === 'function') {
f = new Func;
Object.assign(f, EventEmitter.prototype);
} else {
super();
f = this;
}
f.o = null;
f.resolve = f.addCall = noFunc; // Will be defined in trap method
return f;
}

await(promise) {
(async _ => {
this.resolve(await promise);
})();
return this;
}
}

exports.Future = Future;
38 changes: 38 additions & 0 deletions Internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { VALUE } = require('./Symbols');

class Internal {
constructor(value) {
this[VALUE] = value;
}
}

class IfNotExist extends Internal {
constructor(value, skip = false, valueWhenExists = undefined) {
super(value);
this.skip = Boolean(skip);
if (skip && valueWhenExists) {
this.resolve = valueWhenExists;
}
}
}

class Both extends Internal {
constructor(value, callback) {
super(value);
this.callback = callback;
}
}

class Func extends Function {
}

const noFunc = function() {};

const noObj = {};

exports.Internal = Internal;
exports.IfNotExist = IfNotExist;
exports.Both = Both;
exports.Func = Func;
exports.noFunc = noFunc;
exports.noObj = noObj;
95 changes: 95 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<h1 align="center">Welcome to future-proxy 👋</h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
<a href="#" target="_blank">
<img alt="License: ISC" src="https://img.shields.io/badge/License-ISC-yellow.svg" />
</a>
</p>

> A proxy which wraps a potential &#34;future&#34; object, that does not exist Yet!
### 🏠 [Homepage](https://github.com/prkeshri/node-future-proxy)

## Install

```sh
npm install future-proxy
```

> A JS Proxy wraps an object where we can intercept (and do whatever we wish with the object!). But what if the object does not exist yet? One example being a mock http request (I am working on this ;)). So, here it goes. Here are a few examples that will illustrate the usage:

<u>Warning:</u> Only limitation is: By default, 'get' trap outputs a function. If you wish anything else, you will have to provide in interceptors!
<br/>

<b>Minimal Setup:</b>

const proxy = require('./proxy');

const t = new proxy.Future(); // Create a reference to some future object.
const x = proxy.trap(t);

// Do whatever you would do as if the object 't' actually exists

// Later after sometime...
t.resolve(futureObject); // Now, all the calls (get, set, etc.) to 'futureObject' will be made.

If you wish to trap a function, instead of new proxy.Future, call:

new proxy.Future(proxy.Function);

The documentation is a progress to include the other (advanced) features. Meanwhile, files inside test/ can be referenced.


## API

### trap

This function outputs the Proxy.
The actual methods are called when the target is set to proper value by
calling target.resolve(value);

##### Parameters

* `target` **Future** : o : Must be an instance of Future.
* `interceptors` **any?** : An interceptor object (Optional) See [below](interceptors).

##### Returns `

`Proxy` : This records all the calls and calls them later when the target is resolved!

[interceptors]: #interceptors
#### An interceptor can be either of the following:
1. ` function(trapKey, arguments)` Here, trapKey is any of the proxy handler key i.e. get, set, etc.
arguments is the arguments to the handler trap.
2. `{
... same key value pairs as Proxy Handler.
See [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Proxy]
}`

##### Interceptor return values and actions:
1. If no value returned, same call will be made when the target is resolved.
2. If any value other than the below, are returned, same call will be made when the target is resolved.2.a.

<... More and advanced usage documentation in progress ... >


## Run tests

```sh
npm run test
```

## Author

👤 **Praveen Ranjan Keshri**

* Github: [@prkeshri](https://github.com/prkeshri)
* LinkedIn: [@prkeshri](https://linkedin.com/in/prkeshri)

## Show your support

Give a ⭐️ if this project helped you!

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_
17 changes: 17 additions & 0 deletions Symbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const VALUE = Symbol("VALUE"),
FUNCTION = Symbol("FUNCTION"),
__call = Symbol("__call"),
TRAP = Symbol("TRAP"),
__resolve = Symbol("__resolve"),
IF_NOT_EXIST = Symbol("IF_NOT_EXIST"),
RESOLVE = Symbol("RESOLVE"),
BOTH = Symbol("BOTH");

exports.VALUE = VALUE;
exports.FUNCTION = FUNCTION;
exports.__call = __call;
exports.TRAP = TRAP;
exports.__resolve = __resolve;
exports.IF_NOT_EXIST = IF_NOT_EXIST;
exports.RESOLVE = RESOLVE;
exports.BOTH = BOTH;
23 changes: 23 additions & 0 deletions defaultValueMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { FUNCTION } = require('./Symbols');

const defaultValueMap = {
"apply": undefined,
"construct": {},
"defineProperty": true,
"deleteProperty": true,
"get": FUNCTION,
"getOwnPropertyDescriptor": undefined,
"getPrototypeOf": {},
"has": false,
"isExtensible": false,
"ownKeys": {},
"preventExtensions": false,
"set": true,
"setPrototypeOf": true,
};
const overrideDefaultValueMap = function (newValueMap) {
return Object.assign(overrideDefaultValueMap, newValueMap);
};

exports.defaultValueMap = defaultValueMap;
exports.overrideDefaultValueMap = overrideDefaultValueMap;
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "future-proxy",
"version": "1.0.0",
"description": "A proxy which wraps a potential \"future\" object, which does not exist Yet!",
"main": "proxy.js",
"scripts": {
"test": "node ./test/proxy.test"
},
"keywords": [
"proxy",
"future",
"wrap"
],
"author": "Praveen Ranjan Keshri",
"license": "ISC",
"directories": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/prkeshri/node-future-proxy.git"
},
"bugs": {
"url": "https://github.com/prkeshri/node-future-proxy/issues"
},
"homepage": "https://github.com/prkeshri/node-future-proxy#readme"
}
Loading

0 comments on commit 343e165

Please sign in to comment.