-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters