Skip to content

Commit

Permalink
Some adjustments + added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joeriks committed Aug 22, 2013
1 parent 40d34e1 commit 8d4c567
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
68 changes: 68 additions & 0 deletions amplifyjs/amplifyjs-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Subscribe and publish with no data

amplify.subscribe("nodataexample", function () {
alert("nodataexample topic published!");
});

// Subscribe and publish with data

amplify.publish("nodataexample");

amplify.subscribe("dataexample", function (data) {
alert(data.foo); // bar
});


amplify.publish("dataexample", { foo: "bar" });

amplify.subscribe("dataexample2", function (param1, param2) {
alert(param1 + param2); // barbaz
});

//...

amplify.publish("dataexample2", "bar", "baz");

// Subscribe and publish with context and data

amplify.subscribe("datacontextexample", $("p:first"), function (data) {
this.text(data.exampleText); // first p element would have "foo bar baz" as text
});

amplify.publish("datacontextexample", { exampleText: "foo bar baz" });

// Subscribe to a topic with high priority

amplify.subscribe("priorityexample", function (data) {
alert(data.foo);
});

amplify.subscribe("priorityexample", function (data) {
if (data.foo === "oops") {
return false;
}
}, 1);


// Store data with amplify storage picking the default storage technology:

amplify.publish("priorityexample", { foo: "bar" });
amplify.publish("priorityexample", { foo: "oops" });

amplify.store("storeExample1", { foo: "bar" });
amplify.store("storeExample2", "baz");
// retrieve the data later via the key
var myStoredValue = amplify.store("storeExample1"),
myStoredValue2 = amplify.store("storeExample2"),
myStoredValues = amplify.store();
myStoredValue.foo; // bar
myStoredValue2; // baz
myStoredValues.storeExample1.foo; // bar
myStoredValues.storeExample2; // baz

// Store data explicitly with session storage

amplify.store.sessionStorage("explicitExample", { foo2: "baz" });
// retrieve the data later via the key
var myStoredValue2 = amplify.store.sessionStorage("explicitExample");
myStoredValue2.foo2; // baz
29 changes: 21 additions & 8 deletions amplifyjs/amplifyjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

interface amplifyRequestSettings {
resourceId: string;
data?: Object;
data?: any;
success?: Function;
error?: Function;
}
Expand All @@ -18,7 +18,7 @@ interface amplifyRequest {
* data: A set of key/value pairs of data to be sent to the resource.
* callback: A function to invoke if the resource is retrieved successfully.
*/
(resourceId: string, hash?: Object, callback?: Function): void;
(resourceId: string, hash?: any, callback?: Function): void;

/***
* Request a resource.
Expand All @@ -39,7 +39,7 @@ interface amplifyRequest {
* cache: See the cache section for more details.
* decoder: See the decoder section for more details.
*/
define(resourceId: string, requestType: string, settings?: Object): void;
define(resourceId: string, requestType: string, settings?: any): void;

/***
* Define a custom request.
Expand All @@ -58,11 +58,24 @@ interface amplifySubscribe {
/***
* Subscribe to a message.
* topic: Name of the message to subscribe to.
* [context]: What this will be when the callback is invoked.
* callback: Function to invoke when the message is published.
*/
(topic: string, callback: Function): void;
/***
* Subscribe to a message.
* topic: Name of the message to subscribe to.
* context: What this will be when the callback is invoked.
* callback: Function to invoke when the message is published.
* [priority]: Priority relative to other subscriptions for the same message. Lower values have higher priority. Default is 10.
*/
(topic: string, context: any, callback: Function, priority?: number): void;
/***
* Subscribe to a message.
* topic: Name of the message to subscribe to.
* callback: Function to invoke when the message is published.
* [priority]: Priority relative to other subscriptions for the same message. Lower values have higher priority. Default is 10.
*/
(topic: string, context?: Object, callback?: Function, priority?: number): void;
(topic: string, callback: Function, priority?: number): void;
}
interface amplifyStorageTypeStore {
/***
Expand All @@ -72,17 +85,17 @@ interface amplifyStorageTypeStore {
* value: The value to store. The value can be anything that can be serialized as JSON.
* [options]: A set of key/value pairs that relate to settings for storing the value.
*/
(key: string, value: Object, options?: Object): void;
(key: string, value: any, options?: any): void;

/***
* Gets a stored value based on the key.
*/
(key: string): Object;
(key: string): any;

/***
* Gets a hash of all stored values.
*/
(): Object;
(): any;
}

interface amplifyStore extends amplifyStorageTypeStore{
Expand Down

0 comments on commit 8d4c567

Please sign in to comment.