@ebflat9/fp
Advanced tools
Comparing version 1.2.23 to 1.2.24
{ | ||
"name": "@ebflat9/fp", | ||
"version": "1.2.23", | ||
"version": "1.2.24", | ||
"description": "my fp utils", | ||
@@ -5,0 +5,0 @@ "source": "src/index.js", |
@@ -198,2 +198,26 @@ /** | ||
/** | ||
* IsAsyncFunction, checks if f is an async function | ||
* @param {any} f | ||
* @returns {boolean} | ||
*/ | ||
export const isAsyncFunction = f => | ||
isFunction(f) && f[Symbol.toStringTag] === 'AsyncFunction' | ||
/** | ||
* IsGeneratorFunction, checks if f is a generator function | ||
* @param {any} f | ||
* @returns {boolean} | ||
*/ | ||
export const isGeneratorFunction = f => | ||
isFunction(f) && f[Symbol.toStringTag] === 'GeneratorFunction' | ||
/** | ||
* isAsyncGeneratorFunction, checks if f is an async generator | ||
* @param {any} | ||
* @returns {boolean} | ||
*/ | ||
export const isAsyncGeneratorFunction = f => | ||
isFunction(f) && f[Symbol.toStringTag] === 'AsyncGeneratorFunction' | ||
/** | ||
* IsSet, checks if s is a Set | ||
@@ -200,0 +224,0 @@ * @param {any} s |
@@ -1,2 +0,7 @@ | ||
import { isFunction } from '../combinators.js' | ||
import { | ||
isFunction, | ||
isAsyncFunction, | ||
isGeneratorFunction, | ||
isAsyncGeneratorFunction, | ||
} from '../combinators.js' | ||
@@ -41,2 +46,41 @@ /** | ||
if (isAsyncFunction(target[prop])) { | ||
return async function wrappedMethod() { | ||
const result = await target[prop]?.apply(target, arguments) | ||
const currentArgs = JSON.stringify(arguments) | ||
const lastArgs = cache[prop] | ||
if (currentArgs !== lastArgs) { | ||
cache[prop] = currentArgs | ||
dispatchChanged(target, prop) | ||
} | ||
return result | ||
} | ||
} | ||
if (isGeneratorFunction(target[prop])) { | ||
return function* wrappedMethod() { | ||
const generator = target[prop]?.apply(target, arguments) | ||
do { | ||
const { done, value } = generator.next() | ||
dispatchChanged(target, prop) | ||
yield { done, value } | ||
} while (!done) | ||
} | ||
} | ||
if (isAsyncGeneratorFunction(target[prop])) { | ||
return async function* wrappedMethod() { | ||
const generator = target[prop]?.apply(target, arguments) | ||
do { | ||
const { done, value } = await generator.next() | ||
dispatchChanged(target, prop) | ||
yield { done, value } | ||
} while (!done) | ||
} | ||
} | ||
if (isFunction(target[prop])) { | ||
@@ -43,0 +87,0 @@ return function wrappedMethod() { |
@@ -1342,3 +1342,62 @@ import { EventEmitter } from '../src/reactivize.js' | ||
}) | ||
it('should allow observing generator functions', function (done) { | ||
let called = 0 | ||
const obj = { | ||
name: 'tim', | ||
*nameGen() { | ||
called++ | ||
yield (this.name = Math.random() * 1000) | ||
}, | ||
} | ||
const observed = Observable.makeObservable(obj) | ||
observed.observe().subscribe(value => { | ||
assert.notDeepEqual(value.name, 'tim') | ||
assert.equal(called, 1) | ||
done() | ||
}) | ||
;[...observed.nameGen()] | ||
}) | ||
it('should allow observing async functions', function (done) { | ||
let called = 0 | ||
const obj = { | ||
name: 'tim', | ||
async changeName(newName) { | ||
called++ | ||
return new Promise(resolve => | ||
setTimeout(() => resolve((this.name = newName)), 1) | ||
) | ||
}, | ||
} | ||
const observed = Observable.makeObservable(obj) | ||
observed.observe().subscribe(value => { | ||
assert.deepEqual(value.name, 'john') | ||
assert.equal(called, 1) | ||
done() | ||
}) | ||
observed.changeName('john') | ||
}) | ||
it('should allow observing async generator functions', function (done) { | ||
let called = 0 | ||
const obj = { | ||
name: 'tim', | ||
async *genName() { | ||
called++ | ||
const newName = await new Promise(resolve => | ||
setTimeout(() => resolve(Math.random() * 10000)) | ||
) | ||
while (true) yield (this.name = newName) | ||
}, | ||
} | ||
const observed = Observable.makeObservable(obj) | ||
observed.observe().subscribe(value => { | ||
assert.notEqual(value.name, 'tim') | ||
assert.equal(called, 1) | ||
done() | ||
}) | ||
observed.genName().next() | ||
}) | ||
}) | ||
}) |
225366
7617