Commit 1900d971 authored by Vadym Gidulian's avatar Vadym Gidulian

Initial commit

parents
dist/
# Dependency directories
node_modules/
# Logs
logs/
*.log
npm-debug.log*
# Optional npm cache directory
.npm/
# Optional REPL history
.node_repl_history/
stages:
- test
before_script:
- yarn versions
test:
stage: test
script:
- yarn
- yarn run test
tags:
- npm
artifacts:
paths:
- dist/
.*
src
test
gulpfile.js
yarn.lock
# Promised value
A wrapper for values got asynchronously
## Usage
```js
const data = new PromisedValue('Getting data...');
console.log(data.value); // 'Getting data...'
console.log(data.isReady); // false
data.willBeReady.then(data => {
console.log(data); // 'Some data'
});
// ...
data.value = 'Some data';
console.log(data.value); // 'Some data'
console.log(data.isReady); // true
data.willBeReady.then(data => {
console.log(data); // 'Some data'
});
```
const gulp = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const paths = new function() {
this.srcDir = 'src';
this.distDir = 'dist';
this.js = this.srcDir + '/index.js';
this.jsOut = this.distDir;
};
// Scripts
gulp.task('js', function () {
return gulp.src(paths.js)
.pipe(babel({
presets: ['env']
}))
.pipe(uglify({
compress: true,
mangle: {
toplevel: true
}
}))
.pipe(gulp.dest(paths.jsOut));
});
// Stages
gulp.task('watch', function () {
gulp.watch(paths.js, gulp.task('js'));
});
gulp.task('build', gulp.task('js'));
gulp.task('default', gulp.task('build'));
{
"name": "@gviagroup/promised-value",
"version": "1.0.0",
"description": "A wrapper for values got asynchronously",
"main": "dist/index.js",
"scripts": {
"start": "gulp && concurrently \"gulp watch\" \"ava --fail-fast -w test\"",
"test": "gulp && ava --verbose test"
},
"devDependencies": {
"ava": "^0.25.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"concurrently": "^3.6.0",
"gulp": "^4.0.0",
"gulp-babel": "^7.0.1",
"gulp-uglify": "^3.0.1"
}
}
'use strict';
module.exports = PromisedValue;
function PromisedValue(initialValue) {
if (!(this instanceof PromisedValue)) {
throw new TypeError("Cannot call a class as a function");
}
let initialized = false;
let value = initialValue;
let resolvePromise;
let willBeReady = new Promise(resolve => {
resolvePromise = resolve;
});
Object.defineProperties(this, {
isReady: {
get() {
return initialized;
}
},
value: {
get() {
return value;
},
set(newValue) {
value = newValue;
if (!initialized) {
resolvePromise(value);
initialized = true;
} else {
willBeReady = Promise.resolve(value);
}
}
},
willBeReady: {
get() {
return willBeReady;
}
}
});
}
const test = require('ava');
const PV = require('../dist/index');
test.cb('Promised value', t => {
t.plan(6);
const v = new PV(42);
v.willBeReady.then(value => {
t.is(value, 123);
});
t.is(v.isReady, false);
t.is(v.value, 42);
v.value = 123;
t.is(v.isReady, true);
t.is(v.value, 123);
v.willBeReady.then(value => {
t.is(value, 123);
t.end();
});
});
test('Use new only', t => {
try {
PV();
t.fail('An exception should be thrown');
} catch (e) {
t.pass();
}
});
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment