npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

craft-observable

v3.0.0

Published

small proxy driven observable objects to fit any framework or library

Downloads

7

Readme

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 } = require('craft-observable');

  let farm = observable();

  // listen for set events
  farm.$set('farmer', (key, value, observable, isnew) => {
      console.log('a new farmer is set');
      if (value.includes(' ')) {
          value = value.split(' ');
          return {
              firstname: value[0],
              lastname: value[1],
          }
      }
  });

  farm.farmer = 'Bob Brown';

  console.log(farm.farmer); // -> { firstname : "Bob" , lastname : "Brown" }

  farm.animals = {
    cows: 2
  };

  farm.animals.$get((key, obj) => {
      return `the farm has ${obj[key] == undefined ? 'no ' + key : obj[key] + ' ' + key}`;
  });

  console.log(farm.animals.sheep); // -> the farm has no sheep
  // if proxy is not available in your browser || javascript environment use the traditional get and set accessor methods
  console.log(farm.animals.get('sheep')); // -> the farm has no sheep

  console.log(farm.animals.cows); // -> 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 = observable();

  notifier.on('ui-change',(html,...otherargs) => {
    let article = document.createElement('article');
    article.innerHTML = html;
    document.querySelector('div.articles').appendChild(article);
  });

  // some seriously async code
  fetch('/doodle.html')
  .then(checkerrors)
  .then(response => response.text())
  .then(text => {
    notifier.emit('ui-change',text);
  });

  // to access methods on the listener assign it as a variable
  let uiChange = notifier.on('ui-change',(html,...otherargs) => {
    let article = document.createElement('article');
    article.innerHTML = html;
    document.querySelector('div.articles').appendChild(article);
  });
  // lets say you want to stop recieving ui-change events
  uiChange.off();
  // but now you want to enable them again
  uiChange.on();
  // or
  uiChange.once();

separate use of event system

  let messager = eventsys({});

  // .on, .once listeners have same interface
  messager.on('msg', event => {
    console.log('new message',event);
  });

  // .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.emit('msg',{
    body : 'new piece of information'
  });

  messager.emit('msg','new piece of info','code 3' /*...*/);
  // this halts any events being emited
  // .stopall([true|false])
  messager.stopall(); // stopped
  // the bool is optional to turn event emitting on again
  // just call .stopall again with false
  messager.stopall(false); // 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 = require('fs');
  const {observable} = require('craft-observable');
  const metaobject = observable({
    logloc : './log.txt'
  });


  metaobject.$set('log',(key,val,obj) => {
    fs.appendFile(obj.logloc, val, err => {
        if (err) throw err;
        console.log(val);
    });
  });

  metaobject.$get('log',(key,obj) => fs.readFileSync(obj.logloc,'utf8'));

  const getfile = observable();

  getfile.$get((key,obj) => {
    const path = __dirname+'/'+key;
    const stats = fs.statSync(path);
    if(stats.isFile()) return fs.readFileSync(path,'utf8');
  });

  const server = require('http').createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    res.end(getfile['index.html']);
    metaobject.log = `${Date.now()} : request made \n`;
  });

  server.listen(3000, '127.0.0.1', () => {
    console.log(`Server running at http://127.0.0.1:3000/`);
  });

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