• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Proxy based implementation of Object.observe

proxy-observe

Object.deepObserve goes beyond the EcmaScript spec and implements the ability to observe an object and all its sub-objects with a single call.

Build Status Codacy Badge Code Climate Test Coverage Issue Count

NPM

A Proxy Based Implementation Of Object.observe, Array.observe plus Object.deepObserve. Object.observe and Array.observe have now been deprecated from Chrome and standards tracks, but some developers may still find them useful or require them for backward compatibility in Chrome applications.

Installation

npm install proxy-observe

The index.js and package.json files are compatible with https://github.com/anywhichway/node-require so that proxy-observe can be served directly to the browser from the node-modules/proxy-observe directory when using node Express. You can also use the files in the browser subdirectory directly.

In late April 2016 Object.observe disappeared from Chrome and Proxy appeared. As a result, the focus on supporting this library will go up so that can be used to replace what is now missing functionality in Chrome as well as support observers in other browsers.

Philosophy

This library exists because despite some people's reasonable philosophical concerns regarding the use of observers and the fact they have been deprecated from Chrome and were never fully supported in other JavaScript engines, some people still want to use them.

There is a slightly more complete EcmaScript implementation available at https://github.com/MaxArt2501/object-observe. This is probably the most popular Object.observe polyfill available at the moment. However, it has well documented and acknowledged shortcomings. It is based on polling which means some events get delivered out of order or even missed. It's author provides a reasonable rationale for not using Proxies to implement Object.observe.

The above being said, we had an application that could not afford to miss events or have them out of order and we also wanted something lighter and potentially faster. Hence, we built a Proxy based polyfill. As far as we know the sole shortcomings of our implementation are:

  1. There are some less used functions not yet implemented, e.g. deliverChangeRecords.

  2. The variables pointing to objects that are being observed must be re-assigned to point to a proxy returned by the call to Object.observe, e.g.

var object = { foo: null };
object = Object.observe(object, function(changeset) {
    console.log(changeset)
});
object.foo = "bar";

will result in {foo: "bar"} being printed to the console

Item one above can be re-mediated over time.

We believe item two is a small price to pay. Our implementation is also less than 200 lines of code and 4.5K minified including Array.observe and Object.deepObserve vs. over 500 lines of code and 28K for MaxArt2501 covering just Object.observe.

There is an additional implementation at https://github.com/joelgriffith/object-observe-es5. This implementation is synchronous and modifies all object properties to have custom getters and setters. This could really bind up your application if there are a lot of changes to objects. It also only monitors enumerable properties and like the MaxArt2501 implementation is several hundred lines of code.

Anyway, now you have a choice MaxArt2501, Joel Griffith or AnyWhichWay, and choice is good! They all have their pros and cons.

Usage and Enhancements

Object.observe = function(object,[callback[,acceptlist[,pausable[,pause[,delay]]])

Proxy-observe makes the second arguments to Object.unobserve and Array.unobserve optional so that an object can be completely un-observed in one call. Additionally, <instance>.unobserve returns the original observed object or array "deproxied".

Object.observe

Proxy-observer supports a deepObserve capability on nested objects. It also allows the pausing and starting of observers using two additional optional arguments to Object.observe.

pausable - a boolean that indicates to create the observer so it can be paused. The argument pausable is optional to reduce the chance of shadowing a property or method on any existing code.

pause - a boolean that indicates to create the observer in paused state

If pausable is true then an additional method deliver(ignorePrevious) is available to start delivery.

To pause delivery, set a property called pause on the function deliver to true. Re-set it to false and call deliver(ignorePrevious) to re-start change handling. If ignorePrevious is set to true, then queued changes will not cause the invocation of observer callbacks.

What's Implemented and Not and Other Issues

Array.observe does not behave well in Chrome. It is the native implementation that does not behave well. We are looking to patch this in the future. Unit tests pass in Firefox, which uses our observe implementation.

You can observe for ["add", "update", "delete", "reconfigure", "setPrototype","preventExtensions"] using Object.observe.

You can observe for ["add", "update", "delete", "splice"] using Array.observe.

Object.getNotifier and Object.deliverChangeRecords are not implemented.

Currently Object.deepObserve does not support event type selectivity. All events are monitored. There is also no Object.deepUnobserve.

v0.0.12 setPrototypeOf observing does not work in Firefox.

Release History

v0.0.21 2017-02-06 Enhancements: check the type of objects (object, array, date, etc), check if the new object already have an proxy based on ('observers') Thanks @vitormsilva!

v0.0.20 2017-01-21 Fixed issue #16. Thanks @vitormsilva!

v0.0.19 2016-11-13 Fixed issue 15 where "delay" was undefined.

v0.0.18 2016-07-22 Ehanced to take a 6th argument, delay, to set delivery timeout on changes in milliseconds. Also reduced CPU load by making the delivery function "smart". Delivery no longer creates timeout calls if there is nothing to deliver. The next change to an object will restart the delivery function with the previous delay setting.

v0.0.17 2016-06-15 Fixed Issue 10 thanks to goodells. Updated unit test should support response to pop accordingly.

v0.0.16 2016-06-02 Modified delivery timeout from 0ms to 10ms to reduce CPU loading.

v0.0.15 2016-05-15 README updates. Unobserve added to Array corrected issue with Object.unobserve not working. Also made callback argument to unobserve optional, which eliminates all observations. Finally, added an unobserve method on on observed instances which returns the original object, i.e. de-proxies, Added more unit tests.

v0.0.13,14 2016-05-12 README and code style improvements.

v0.0.12 2016-05-11 Addressed issue 5. Added unit tests. Coverage over 90%. Array.unobserve still not implemented. setPrototypeOf observing does not work in Firefox.

v0.0.11 2016-05-06 Addressed issues 2,3,4 ... added some unit tests to address issue 4. Unsure how long issue 2 has been undiscovered. Issue 3 has been in all versions. Issue 4 is an enhancement. Started testing in NodeJS v6.0 now that Proxy is supported by NodeJS. Array.unobserve still not implemented.

v0.0.10 2016-01-26 Added browserified versions. Re-wrote unit tests to use blanket.js since unit tests will always pass on Chrome since it has native support for Object.observe and unit testing must be done in the browser until node.js support MS Chakra. However, the test coverage reporting is not working.

v0.0.9 2015-12-13 Added some unit tests and updated documentation. Consider this a BETA.

v0.0.8 2015-12-13 Codacy driven enhancements. Consider this a BETA.

v0.0.6 2015-11-07 Fixed another issue with testing for existence of Proxy object. Consider this a BETA.

v0.0.5 2015-11-07 Fixed issue with testing for existence of Proxy object. Consider this a BETA.

v0.0.4 2015-10-03 Fixed issue with deepObserve discovered during code walkthrough. Issue would have impacted non-Chrome browsers. Consider this a BETA.

v0.0.3 2015-10-01 Updated README. No unit tests yet. Consider this a BETA.

v0.0.2 2015-10-01 Added Object.deepObserve and Array.observe. No unit tests yet. Consider this a BETA.

v0.0.1 2015-10-01 Initial release. No unit tests yet. Consider this a BETA.

License

MIT License - see LICENSE file

More Repositories

1

thunderclap

A key-value, indexed JSON, and graph database plus function oriented server designed for Cloudflare
JavaScript
297
star
2

reasondb

A 100% JavaScript object database: SQL like syntax, full-text search, auto object sync, swapable persistence engines, asynchronous cursors, 30+ built-in plus in-line fat arrow predicates, predicate extensibility,joins, nested matching.
JavaScript
272
star
3

nano-memoize

Faster than fast, smaller than micro ... a nano speed and size (780 Brotili bytes) memoize for single and multiple argument functions.
JavaScript
212
star
4

tlx

TLX is a small (< 5K minimized and gzipped) multi-paradigm front-end library supporting: template literals in place of JSX; multi-root templates in HTML, JavaScript, or remote URL references; t-for, t-foreach, t-forvalues with iterable protocol support; t-if attribute directive; custom attribute directives; optional two-way data binding; automatic or manual creation of standards compliant custom elements and components; standards compliant event handlers; a default router; extended lifecycle callbacks; automatic HTML injection protection.
JavaScript
71
star
5

txi

Small, focused full-text indexing and search library for any JavaScript application
JavaScript
68
star
6

switchcase

Declarative and functional switch supporting literals, functional tests, regular expressions, object pattern matching, and routing.
JavaScript
66
star
7

timewave

A tiny clock and date, period, or duration math library < 2k (minified/gzipped)
JavaScript
48
star
8

blockstore

100% native JavaScript, block allocated high speed key/value file store
JavaScript
34
star
9

nano-pipe

A tiny library (<450 bytes gzipped) to create chainable functions/pipelines including support for async generators.
JavaScript
30
star
10

nano-copy

Tiny, super fast JavaScript deep copy
JavaScript
25
star
11

fos

Function Oriented Server: The easy way to expose JavaScript APIs to clients as micro-services.
JavaScript
24
star
12

benchtest

Integrated performance testing for Mocha based unit testing
JavaScript
24
star
13

jsxdirect

A browser based JSX transpiler with automatic support for React, preact, and hyperapp.
JavaScript
23
star
14

cryptozoa

Dead simple isomorphic encryption wrapper around window.crypto and Node crypto.
JavaScript
22
star
15

joqular

JOQULAR (JavaScript Object Query Language Representation) for JSON data matching, transformation, validation and extraction
JavaScript
21
star
16

scriptswitch

Super simple (and tiny, 700 bytes gzipped) conditional script loading manager for browsers and NodeJS.
JavaScript
19
star
17

intersector

Superfast intersection supporting primitives and objects. In the age of big data, you need it.
JavaScript
18
star
18

generx

JavaScript generators extended with forEach, map, reduce ... most standard Array methods.
JavaScript
14
star
19

denodata

Generalized indexing, search, and change subscriptions for Deno KV
TypeScript
13
star
20

array-set-ops

Extremely fast set and map/reduce operations for Arrays, Sets, and Cartesian Product
JavaScript
11
star
21

doxl

Kind of like GraphQL except for Javascript objects. Extracts and optionally transforms sub-objects from Javascript objects.
JavaScript
11
star
22

watchlight

A light-weight, comprehensive, reactive framework for business logic and when things change.
JavaScript
11
star
23

assentials

Essential polymorphic async functions ... every, forEach, map, reduce, some, flow, pipe, when, route and more!
JavaScript
10
star
24

lmdb-oql

A high level query mechanism for indexed LMDB databases
JavaScript
9
star
25

lmdb-indexeddb

LMDB API wrapped around IndexedDB to make LMDB available in the browser
JavaScript
9
star
26

tlx-chart

Charts and gauges without JavaScript ... well just a tiny bit.
JavaScript
8
star
27

unionizor

Superfast union supporting primitives and objects. In the age of big data, you need it.
JavaScript
8
star
28

jasonette-html

An HTML/Web renderer for Jasonette
JavaScript
7
star
29

node-fetch-event

JavaScript
7
star
30

iterable-pipe

Treat iterables/generators like arrays without converting them. Make all array functions chainable.
JavaScript
7
star
31

lazui

Single page apps and lazy loading sites with minimal JavaScript or client build processes
JavaScript
7
star
32

lmdb-query

A higher level query mechanism for LMDB supporting functional and RegExp filters
JavaScript
6
star
33

lmdb-index

Higher level object operations for LMDB values, e.g. indexing
JavaScript
6
star
34

dot-async-data

Asynchronous dot notation to radically simplify JSON database access.
JavaScript
6
star
35

modulastic

Express middleware to expose select Node modules to browser
5
star
36

js-generics

A generic function definition and dispatch library for Javascript.
JavaScript
5
star
37

little-cleaner

A small utility for reducing the chance of XSS and HTML injection
JavaScript
5
star
38

cxproduct

Cartesian cross-product as a first class object.
JavaScript
5
star
39

little-diff

I tiny string and array diff and apply diff library
JavaScript
5
star
40

josedb

JSON Object Signing and Encryption Database
JavaScript
5
star
41

js-codex

Encode and decode modern JavaScript, e.g. Map, Set, NaN, Infinity, typed Arrays for JSON.stringify and JSON.parse.
HTML
5
star
42

lightview

Small, simple, powerful web UI creation ...
JavaScript
4
star
43

joex

Javascript Object Extensions
JavaScript
4
star
44

about-time

Javascript library for managing and comparing Time, Duration, TimeSpan objects in manners similar to Date
JavaScript
4
star
45

tlx-editor

A single HTML component supporting all input types, select, textarea, radio groups, and star ratings.
JavaScript
4
star
46

secst

SECST is a semantic, extensible, computational, styleable tagged markup language. You can use it to joyfully create compelling, interactive documents backed by HTML.
JavaScript
4
star
47

examplify

Amplify your Markdown documentation with executable examples
JavaScript
4
star
48

hcx

HTML Compiler - Routeless, targetless, remote, runnable routes and more ...
JavaScript
3
star
49

ipvfs

Creates and manages diff versioned files in an IPFS store
JavaScript
3
star
50

cachestore

A standalone memory cache or wrapper for storage supporting localStore, blockstore, Redis or similar APIs
JavaScript
3
star
51

relation-manager

Automatic relationship management for JavaScript objects.
JavaScript
3
star
52

gun-block

Super fast NodeJS file based block storage for the Gun database
3
star
53

vdomx

Adds interpolation and custom directives to your VDOM and regular HTML
JavaScript
2
star
54

jovial

Javascript Object Validation Interception Augmentation Library
JavaScript
2
star
55

lazy-elements

Single page apps with minimal custom JavaScript or client build processes
JavaScript
2
star
56

node-require

An Express addin to export node modules to the client and a supporting CommonJS browser require
JavaScript
2
star
57

lmdb-localstorage

A browser localStorage implementation of the NodeJS lmdb API
JavaScript
2
star
58

trui

Tiny Reactive UI
JavaScript
2
star
59

all

Collects all values for an iterable as an array
JavaScript
1
star
60

inline-function

A web component for safely evaluating complex and symbolic math expressions and other functions and displaying the result.
JavaScript
1
star
61

lmdb-patch

Provides a patch method for LMDB to do partial object updates.
JavaScript
1
star
62

autohelm

A library to support standardized navigation in HTML files generated from Markdown with headings
JavaScript
1
star
63

lmdb-extend

A small utility to support extending the functionality of LMDB databases
JavaScript
1
star
64

deferscript

Defers inline JavaScript tags that are not modules.
JavaScript
1
star
65

cf-fs

fs wrapper around Cloudflare KV store
1
star
66

lmdb-cluster

A clustered version of lmdb that supports a REST API, a unified POST API, and sockets.
JavaScript
1
star
67

arg-waiter

Ensures all arguments to a function that are Promises are resolved
JavaScript
1
star
68

lmdb-copy

Copies one LMDB key value to another
JavaScript
1
star
69

lmdb-move

Moves one LMDB key value to another, i.e. renames, within the scope of a transaction
JavaScript
1
star
70

foundationdb-index

Generalized indexing and search (including vectors) for FoundationDB
1
star
71

quick-worker

A library to quickly create WebWorkers and treat them almost like regular objects.
JavaScript
1
star
72

map

Map for any iterable
JavaScript
1
star
73

math-science-formula

A web component for rendering math and chemical formulas
HTML
1
star