Compare commits

...

3 Commits

6 changed files with 34 additions and 11 deletions

View File

@ -190,8 +190,8 @@ them.
## Behavior
Anytime a property is set or a method is gotten and called, the container will check for an updated
state and trigger subscribers if it is updated. An hash function must be applied to determine
updated status.
state and trigger subscribers if it is updated. An optional hash function may be applied to
determine updated status. If the hash function is not supplied, every update will be propagated.
## Usage

View File

@ -1,6 +1,6 @@
{
"name": "frptools",
"version": "3.2.1",
"version": "3.2.3",
"description": "Observable Property and Computed data streams",
"main": "src/index.js",
"files": [

View File

@ -2,6 +2,17 @@ import { container, computed } from '../src/index.js';
import { dirtyMock, hashSet } from '../src/testUtil.js';
describe('A container', () => {
it('tracks properties', () => {
let i = 0;
const a = container({}, () => i++);
a.a = 1;
expect(a.a).toBe(a._.a);
a.b = false;
expect(a.b).toBe(a._.b);
});
it('notifies dependents of updates', () => {
let runCount = 0;
let currentValue = new Set();

View File

@ -2,7 +2,7 @@ import { registerSubscriptions, registerFire } from './util.js';
export function container(store, hash) {
let subscribers = [];
let id = hash(store);
let id = hash && hash(store);
const containerMethods = {
subscribe: registerSubscriptions(subscribers),
@ -13,8 +13,8 @@ export function container(store, hash) {
};
function checkUpdate(target) {
const newId = hash(target);
if (id !== newId) {
let newId = hash && hash(target);
if (!hash || id !== newId) {
id = newId;
containerMethods.fire(target);
}
@ -48,7 +48,9 @@ export function container(store, hash) {
target[name] = newVal;
checkUpdate(target);
return newVal;
// Returning a falsey value causes TypeError
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/set#Invariants
return newVal || true;
}
});

View File

@ -15,7 +15,7 @@
"backgroundtask": "~1.0.0",
"body-parser": "~1.18.3",
"date-fns": "~1.29.0",
"domvm": "~3.2.1",
"domvm": "~3.4.5",
"exif-parser": "~0.1.9",
"express": "~4.16.3",
"frptools": "~3.2.0",

View File

@ -1,8 +1,18 @@
import { log, group, groupEnd } from '../utils/console.js';
import { call } from 'frptools';
export const streamConfig = {
is: s => s && typeof s.subscribe === 'function',
val: s => s(),
sub: (s, fn) => s.subscribe(fn),
unsub: s => s()
off: subList => subList.forEach(call),
on: (accum, vm) => {
const redraw = () => vm.redraw();
return accum.map(s => (streamConfig.is(s) ? streamConfig.sub(s, redraw) : s()));
},
val: (s, accum) =>{
if (streamConfig.is(s)) {
accum.push(s);
return s();
}
return s;
}
};