• This repository has been archived on 03/Jan/2019
  • Stars
    star
    107
  • Rank 321,746 (Top 7 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created over 12 years ago
  • Updated over 5 years ago

Reviews

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

Repository Details

[DEPRECATED] Small JS class that provides async control flow, property listeners, barrier pattern, and more.

Please note: This project is deprecated at Zynga and is no longer maintained.


Overview

Atom.js is a small, easy to use JavaScript class that provides asynchronous control flow, event/property listeners, barriers, and more.

Features

  • Small: 3.4kB minified, 1.5kB gzipped.
  • No dependencies: works in a browser, or in node.
  • Enables programming patterns that reduce the need for deeply nested callbacks and conditionals.

Install

npm install atom-js

Unit Tests

To run from command line using node.js:

node test.js      // brief
node test.js -v   // verbose

To run in a browser, open test.html, or go here.

Tutorial

This is a.

	var a

a is an atom.

	var a = atom();

Properties

An atom has properties. The .get() and .set() methods may be employed to read and write values of any type.

	a.set('key', 'value');
	console.log('Value of key: ' + a.get('key'));

	a.set({
		pi: 3.141592653,
		r: 5,
		circumference: function () {
			return 2 * a.get('pi') * a.get('r');
		}
	});
	console.log('Circumference: ' + a.get('circumference')());

Parameters to the constructor will also be set as properties.

	a = atom('key', 'value');

	a = atom({ pi: 3.141592653, r: 5 });

Use .has() to query for existence of a property, and .keys() to get a list of all properties that have been set.

	if (a.has('game')) {
		console.log('What "a" brings to the table: ' + a.keys());
	}

The .each() method lets you execute a function on a series of properties.

	a.set({ r: 0xBA, g: 0xDA, b: 0x55 });
	a.each(['r', 'g', 'b'], function (key, value) {
		console.log(key + ': ' + value);
	});

Listeners

Listeners may be attached to atoms in a variety of ways.

To be notified as soon as a property is set, use the .once() method. The callback will be called immediately if the property is already set.

	a.once('userInfo', function (userInfo) {
		alert('Welcome, ' + userInfo.name + '!');
	});

Many atom methods can work with more than one property at a time.

	a.once(['userInfo', 'appInfo'], function (user, app) {
		alert('Welcome to ' + app.name + ', ' + user.name + '!');
	});

When you just want to know about the next change, even if the property is already set, use .next().

	a.next('click', function (click) {
		alert('Are you done clicking on ' + click.button + ' yet?');
	});

To watch for any future changes to a property, use the .on() (alias .bind()) method.

	function myErrorHandler(error) {
		console.log('There was a grevious calamity of code in ' + a.get('module'));
		console.log(error);
	}
	a.on('error', myErrorHandler);

Note that setting a property with a primitive (string/number/boolean) value will only trigger listeners if the value is different. On the other hand, setting an array or object value will always trigger listeners.

You can unregister any listener using .off() (alias .unbind()).

	a.off(myErrorHandler);

If you only want to remove the listener associated with a particular key or keys, you can specify those too:

	a.off(['a', 'b'], myErrorHandler);

Needs and Providers

You can register a provider for a property.

	a.provide('privacyPolicy', function (done) {
		httpRequest(baseUrl + '/privacy.txt', function (content) {
			done(content);
		});
	});

Providers only get invoked if there is a need, and if the property is not already set. Use the .need() method to declare a need for a particular property. If a corresponding provider is registered, it will be invoked. Otherwise, .need() behaves just like .once().

	a.on('clickPrivacy', function () {
		a.need('privacyPolicy', function (text) {
			element.innerText = text;
		});
	});

Entanglement

Properties of two or more atoms can be entangled, using the .entangle() method. When an entangled property gets set on one atom, the value will instantly propagate to the other.

	var b = atom();
	a.entangle(b, 'email');
	a.set('email', '[email protected]');
	console.log('Entangled email: ' + b.get('email'));

.entangle() also works when called with a list of properties.

	a.entangle(b, ['firstname', 'lastname']);

If called with a map of property names, then property 'X' on one atom can be entangled with property 'Y' on the other atom.

	a.entangle(b, { firstname: 'first', lastname: 'last' });
	a.set('firstname', 'Joe');
	console.log('Welcome, ' + b.get('first'));

Note that entangled properties are not actually synchronized until the first change after entanglement.

Asynchronous Queueing

String together a series of asynchronous functions using the .chain() method.

	a.chain(
		function (nextLink) {
			callAjaxMethod('callThisFirst', function (firstResult) {
				nextLink(firstResult);
			});
		},
		function (nextLink, firstResult) {
			callAjaxMethod('callThisSecond', function (secondResult) {
				nextLink(secondResult);
			});
		}
	);

Method Chaining

Not to be confused with the .chain() method specifically, "method chaining" actually refers to the practice of stringing together multiple method calls in a single expression.

	a = atom('start', new Date())
		.once('loaded', function () {
			console.log('Finished loading.');
		})
		.once('shutdown', function () {
			console.log('Shutting down.');
		})
		.set('loaded', true);

The .chain(), .each(), .entangle(), .mixin(), .need(), .next(), .off(), .on(), .once(), .provide() and .set() methods are all chainable.

Cleanup

Release references to all data and callback functions with the .destroy() method.

	a.destroy();

After being destroyed, most of an atom's functions will throw exceptions when called.

Additional Resources

More Repositories

1

zperfmon

[DEPRECATED] zPerfmon
PHP
425
star
2

jsbindings

[DEPRECATED] JavaScript bindings for Objective-C and C
Objective-C++
393
star
3

jasy

[DEPRECATED] A powerful Python3 based Web Tooling Framework.
Python
129
star
4

rl-bakery

RL-Bakery makes it easy to build production, large scale, batch Deep Reinforcement Learning applications.
Python
88
star
5

hiccup

[DEPRECATED] Hiccup is a framework that allows the Burp Suite (a web application security testing tool, http://portswigger.net/burp/) to be extended and customized, through the interface provided by Burp Extender (http://portswigger.net/burp/extender/). Its aim is to allow for the development and integration of custom testing functionality into the Burp tool using Python request/response handler plugins.
Python
42
star
6

core

[DEPRECATED] Core - A Fine Foundation for JavaScript Development
JavaScript
36
star
7

swf2ccb

[DEPRECATED] A tool that converts existing assets to ccb format
ActionScript
29
star
8

style

[DEPRECATED] Tiny but useful JS utility that dynamically generates CSS from JS object notation.
JavaScript
25
star
9

loadScript

[DEPRECATED] Small JS script loader utility; allows client-side script substitution.
JavaScript
21
star
10

zynga-hacklang-framework

[DEPRECATED] Zynga Hack Framework
Hack
18
star
11

BossAlienMediaPlayer

A mopidy-based server that allows to play music from a public playlist. Democracy for music.
Python
17
star
12

gravity

[DEPRECATED] A light-weight but powerful JS build tool/server
JavaScript
16
star
13

saigon

[DEPRECATED] Saigon - Zynga's Central Nagios Configuration Service
PHP
14
star
14

classes

[DEPRECATED] JS class pattern; supports multi-inheritance, static & instance methods, public/protected access...
JavaScript
7
star
15

protoc-gen-zsharp

[DEPRECATED] C# Protobuf Plugin with support for Event Sourcing
C#
7
star
16

ssh-cmd-proxy

A Gradle plugin that fixes some technical deficiencies and expands the capabilities of source dependencies.
Java
4
star
17

apibrowser

[DEPRECATED] Api Browser to dsplay generated JSON docs from Jasy
JavaScript
3
star
18

zynga-hhvm-phpunit

HHVM support for the PHP Unit Testing framework
Hack
3
star
19

Zynga.Akka.Cluster

[DEPRECATED] Extensions to the Akka.Cluster C# Framework
C#
2
star
20

jasy-compat

[DEPRECATED] Demonstrates usage of Jasy with unmodified 3rd party content
Python
2
star