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

frill-core

v0.1.4

Published

Core part of FrillJS

Downloads

6

Readme

frill-core

Circle CI dependencies

Core part of frill.

NPM

Usage

$ npm install frill-core --save

Examples

Basic Actions

Simply extend BaseAction.

import {BaseAction} from 'frill-core';

class ExampleAction extends BaseAction {
  constructor() {
    super();
  }
  // count up
  countUpBy(count = 1) {
    // dispatch action to store
    this.dispatch('COUNT_UP_BY', count);
  }
}

Action Services

Services are helpers to use inside actions.

import {BaseAction} from 'frill-core';

// add a new service called 'newService'
BaseAction.addService('newService', (options) => {
  return 'something';
});

class ExampleAction extends BaseAction {
  constructor() {
    super();
    // use a service
    this.use('request', {prefix: '/api'});
    // this.use('socket', {url: 'http://localhost:3000'});
    // this.use('newService', options);
  }

  actionMethod() {
    // throws a request to '/api/test'
    this.request.prefix.get('/test', (err, res) => {
      if(err) console.error(err);
      console.log(res.text);
    });
  }

Built-in services

Additional options for built-in services

| Service | Options | Explanation | |:-------:|:-------:|:----------- | | socket | options.url (Required) | An url of the socket server. | | request | options.prefix (Optional) | Prefixes paths to send a request via this.request.prefix.get('/test'), etc. |

Basic Stores

Simply extend BaseStore.

import {BaseStore} from 'frill-core';

class ExampleStore extends BaseStore {
  constructor() {
    super();
    this._count = 0;

    // listens to an action
    this.actions = {
      'COUNT_UP_BY': 'countUpBy',
    };
  }

  // Triggered when COUNT_UP_BY is dispatched by an action.
  countUpBy(byCount = 1) {
    this._count = this._count + byCount;
    // emit change
    this.change();
  }

  // getter for current count
  get count() {
    return this._count;
  }
}

Context

Context ties up actions and stores together. To create a context, provide an object of actions and an object of stores to the attach method of frill-core.

You can access to stores using Context.store('storeName') or Context.stores.storeName.

You can access to actions using Context.action('actionName') or Context.actions.actionName.

You can access a dispatcher, via Context.dispatcher.

import frillCore from 'frill-core';

// object of actions
const actions = {
  ExampleAction: new ExampleAction(),
  // ExampleAction2: new ExampleAction2(),
  // ...
};

const stores = {
  ExampleStore: new ExampleStore(),
  // ExampleStore2: new ExampleStore2(),
  // ...
};

// Create context
const Context = frillCore.attach(stores, actions);

Components

BaseComponent

Simply extend BaseComponent.

BaseComponent will enable users to have access to this._bind(), and this.getFrill().

Method names specified in this._bind() will automatically bind methods with this, to use in the render() method without .bind(this).

this.getFrill() will return a context.

import React from 'react';
import {BaseComponent} from 'frill-core';

class CountButtonComponent extends BaseComponent {
  constructor(props) {
    super(props);

    // bind methods
    this._bind([
      'onClick',
    ]);
  }

  // click handler
  onClick(e) {
    e.preventDefault();
    // execute an action
    this.getFrill().action('Example').countUpBy(1);
  }

  render() {
    return (
      <button onClick="this.onClick">+1</button>
    );
  }
}

StoreWatchComponent

Extend a new instance of StoreWatchComponent, and give the name of store to watch.

A StoreWatchComponent will listen to stores, and execute getStateFromFrill() when a change is emitted from a store.

NOTE: An instance of StoreWatchComponent extends BaseComponent

import React from 'react';
import {StoreWatchComponent} from 'frill-core';

class TopComponent extends new StoreWatchComponent(['Example']) {
  constructor(props) {
    super(props);
  }

  // fetch state when a store emits change
  getStateFromFrill() {
    return {
      count: this.getFrill().store('Example').count,
    };
  }

  render() {
    return (
      <div>{this.state.count}</div>
    );
  }
}

Run tests

$ npm test

License

MIT