Skip to content

Commit

Permalink
Build latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
aleph1 committed Mar 16, 2023
1 parent cb5266b commit 088574b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 10 deletions.
6 changes: 4 additions & 2 deletions dist/simple-observable-proxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! simple observable proxy v1.0.0 | MIT License | © 2022 Aleph1 Technologies Inc */
/*! simple observable proxy v1.1.0 | MIT License | © 2022 Aleph1 Technologies Inc */
const observables = new Set(),
observablesByProxy = new Map(),
observersByProxy = new Map(),
Expand All @@ -9,7 +9,9 @@ const observables = new Set(),
observersByProxy.get(proxy).forEach((callback) => callback());
}),
proxiesToNotify.clear(),
window.requestAnimationFrame(tick);
"undefined" != typeof window && window.requestAnimationFrame
? window.requestAnimationFrame(tick)
: setTimeout(tick, 16);
},
makeObservableProxy = (data, rootProxy) => {
if (observables.has(data))
Expand Down
4 changes: 2 additions & 2 deletions dist/simple-observable-proxy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destroy = exports.unobserve = exports.observe = exports.observable = void 0;
// data that is being observed
const observables = new Set();
// original data for each proxy
Expand All @@ -14,7 +17,10 @@ const tick = () => {
observersByProxy.get(proxy).forEach((callback) => callback());
});
proxiesToNotify.clear();
window.requestAnimationFrame(tick);
if (typeof window !== "undefined" && window.requestAnimationFrame)
window.requestAnimationFrame(tick);
else
setTimeout(tick, 16);
};
const makeObservableProxy = (data, rootProxy) => {
if (observables.has(data))
Expand Down Expand Up @@ -59,18 +65,21 @@ const makeObservableProxy = (data, rootProxy) => {
observersByProxy.set(proxy, observers);
return proxy;
};
export const observable = (data) => {
const observable = (data) => {
return /*#__INLINE__*/ canBeObservable(data) && makeObservableProxy(data);
};
export const observe = (observableProxy, callback) => {
exports.observable = observable;
const observe = (observableProxy, callback) => {
const observers = observersByProxy.get(observableProxy);
return observers && typeof callback === 'function' ? observers.add(callback) : false;
};
export const unobserve = (observableProxy, callback) => {
exports.observe = observe;
const unobserve = (observableProxy, callback) => {
const observers = observersByProxy.get(observableProxy);
return observers && typeof callback === 'function' ? observers.delete(callback) : false;
};
export const destroy = (observableProxy) => {
exports.unobserve = unobserve;
const destroy = (observableProxy) => {
const observers = observersByProxy.get(observableProxy);
if (observers) {
observers.clear();
Expand All @@ -82,4 +91,5 @@ export const destroy = (observableProxy) => {
}
return false;
};
exports.destroy = destroy;
tick();
88 changes: 88 additions & 0 deletions lib/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// data that is being observed
const observables = new Set();
// original data for each proxy
const observablesByProxy = new Map();
// observers for each proxy
const observersByProxy = new Map();
// observers to call on tick
const proxiesToNotify = new Set();
const objectConstructor = {}.constructor;
const isPlainObject = (data) => !!data && typeof data === 'object' && data.constructor === objectConstructor;
const canBeObservable = (data) => Array.isArray(data) || /*#__INLINE__*/ isPlainObject(data);
const tick = () => {
proxiesToNotify.forEach(proxy => {
observersByProxy.get(proxy).forEach((callback) => callback());
});
proxiesToNotify.clear();
if (typeof window !== "undefined" && window.requestAnimationFrame)
window.requestAnimationFrame(tick);
else
setTimeout(tick, 16);
};
const makeObservableProxy = (data, rootProxy) => {
if (observables.has(data))
throw new Error('Can’t observe Object or Array again');
if (observablesByProxy.has(data))
throw new Error('rootProxy isn’t an observable');
let observers;
const proxy = new Proxy(data, {
get(target, key) {
return target[key];
},
set(target, key, value) {
if (target[key] !== value) {
if (observers) {
if (observablesByProxy.has(value))
throw new Error('Can’t nest observables');
proxiesToNotify.add(rootProxy || proxy);
}
target[key] = value;
}
return true;
},
deleteProperty(target, key) {
if (key in target) {
delete target[key];
proxiesToNotify.add(rootProxy || proxy);
return true;
}
return false;
}
});
(Array.isArray(data) ? [...Array(data.length).keys()] : Object.keys(data)).forEach(key => {
const value = data[key];
if (observablesByProxy.has(value))
throw new Error('Can’t nest observables');
if ( /*#__INLINE__*/canBeObservable(value))
proxy[key] = makeObservableProxy(value, rootProxy || proxy);
});
observers = new Set();
observables.add(data);
observablesByProxy.set(proxy, data);
observersByProxy.set(proxy, observers);
return proxy;
};
export const observable = (data) => {
return /*#__INLINE__*/ canBeObservable(data) && makeObservableProxy(data);
};
export const observe = (observableProxy, callback) => {
const observers = observersByProxy.get(observableProxy);
return observers && typeof callback === 'function' ? observers.add(callback) : false;
};
export const unobserve = (observableProxy, callback) => {
const observers = observersByProxy.get(observableProxy);
return observers && typeof callback === 'function' ? observers.delete(callback) : false;
};
export const destroy = (observableProxy) => {
const observers = observersByProxy.get(observableProxy);
if (observers) {
observers.clear();
observables.delete(observablesByProxy.get(observableProxy));
observablesByProxy.delete(observableProxy);
observersByProxy.delete(observableProxy);
proxiesToNotify.delete(observableProxy);
return true;
}
return false;
};
tick();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-observable-proxy",
"version": "1.0.0",
"version": "1.1.0",
"description": "Simple observable ES6 proxy.",
"browser": "dist/simple-observable-proxy.js",
"main": "lib/index.js",
Expand Down

0 comments on commit 088574b

Please sign in to comment.