craft-observable
small proxy driven observable objects to fit any framework or library
craft-observable uses
- Proxy (optional)
- ES6 syntax (optional)
- Set
- Array.from
- Object.assign
observables have the following properties
- .isObservable - true if it is an observable
- .set, .get - old school non proxy accessors
- .$set, .$get - listeners for access events
- .on , .once , .off , .emit, .stopall - event system
- .defineHandle - define event methods instead of using .on('xyz',function) all the time
observables code demo
basic instanciator observable(=[obj|function])
// node.js style requiring works but will be global if not available const observable eventsys = ; let farm = ; // listen for set events farm; farmfarmer = 'Bob Brown'; console; // -> { firstname : "Bob" , lastname : "Brown" } farmanimals = cows: 2 ; farmanimals; console; // -> the farm has no sheep // if proxy is not available in your browser || javascript environment use the traditional get and set accessor methods console; // -> the farm has no sheep console; // -> the farm has 2 cows
event system
the observables also include a build in event system to help make them as useful as possible
let notifier = ; notifier; // some seriously async code ; // to access methods on the listener assign it as a variable let uiChange = notifier; // lets say you want to stop recieving ui-change events uiChange; // but now you want to enable them again uiChange; // or uiChange;
separate use of event system
let messager = ; // .on, .once listeners have same interface messager; // .emit takes a string event identifier // any arguments that follow will be sent to listeners // there are no type biases any argument will do messager; messager; // this halts any events being emited // .stopall([true|false]) messager; // stopped // the bool is optional to turn event emitting on again // just call .stopall again with false messager; // working again
crazy meta objects
implement cool features by chaging the behavior of objects here is an example of what is possible, note this is probably a bad example but still it demonstrates the potential of observables to create new behaviour in objects
const fs = ; const observable = ; const metaobject = ; metaobject; metaobject; const getfile = ; getfile; const server = ; server;
Notes
This little doodle is subject to change but we'll try and keep it working as much as posible with no breaking changes. if you are experiencing issues or want to improve a feature do fork, raise issues tell us about bugs and so forth.
Thank you very much enjoy the observables