Permalink
Browse files

Added Task and Deferred

  • Loading branch information...
1 parent 8af63e3 commit ff9667afbc730fef76f92b7a8ae7341f5e43b465 @nikhilk committed Aug 11, 2012
@@ -77,6 +77,9 @@
<Compile Include="ComponentModel\IObserver.cs" />
<Compile Include="ComponentModel\Observable.cs" />
<Compile Include="Diagnostics\SyntaxValidationAttribute.cs" />
+ <Compile Include="Threading\Deferred.cs" />
+ <Compile Include="Threading\TaskStatus.cs" />
+ <Compile Include="Threading\Task.cs" />
<Compile Include="Tuple.cs" />
<Compile Include="Nullable.cs" />
<Compile Include="Diagnostics\ConditionalAttribute.cs" />
@@ -0,0 +1,47 @@
+// Deferred.cs
+// Script#/Libraries/CoreLib
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+using System.Runtime.CompilerServices;
+
+namespace System.Threading {
+
+ [Imported]
+ [ScriptNamespace("ss")]
+ public class Deferred {
+
+ [IntrinsicProperty]
+ public Task Task {
+ get {
+ return null;
+ }
+ }
+
+ public void Reject() {
+ }
+
+ public void Reject(Exception error) {
+ }
+
+ public void Resolve() {
+ }
+ }
+
+ [Imported]
+ [ScriptNamespace("ss")]
+ [ScriptName("Deferred")]
+ public sealed class Deferred<T> : Deferred {
+
+ [IntrinsicProperty]
+ public new Task<T> Task {
+ get {
+ return null;
+ }
+ }
+
+ public void Resolve(T result) {
+ }
+ }
+}
@@ -0,0 +1,98 @@
+// Task.cs
+// Script#/Libraries/CoreLib
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+using System.Runtime.CompilerServices;
+
+namespace System.Threading {
+
+ [Imported]
+ [ScriptNamespace("ss")]
+ public class Task {
+
+ internal Task() {
+ }
+
+ public bool Completed {
+ get {
+ return false;
+ }
+ }
+
+ [IntrinsicProperty]
+ public Exception Error {
+ get {
+ return null;
+ }
+ }
+
+ [IntrinsicProperty]
+ public TaskStatus Status {
+ get {
+ return TaskStatus.Pending;
+ }
+ }
+
+ public static Task<bool> All(params Task[] tasks) {
+ return null;
+ }
+
+ public static Task<bool> All(int timeout, params Task[] tasks) {
+ return null;
+ }
+
+ public static Task<Task> Any(params Task[] tasks) {
+ return null;
+ }
+
+ public static Task<Task> Any(int timeout, params Task[] tasks) {
+ return null;
+ }
+
+ public Task ContinueWith(Action<Task> continuation) {
+ return null;
+ }
+
+ public static Task Delay(int timeout) {
+ return null;
+ }
+
+ public Task Done(Action callback) {
+ return null;
+ }
+
+ public Task Fail(Action<Exception> callback) {
+ return null;
+ }
+
+ public Task Then(Action doneCallback, Action<Exception> failCallback) {
+ return null;
+ }
+ }
+
+ [Imported]
+ [ScriptNamespace("ss")]
+ [ScriptName("Task")]
+ public sealed class Task<T> : Task {
+
+ internal Task() {
+ }
+
+ [IntrinsicProperty]
+ public T Result {
+ get {
+ return default(T);
+ }
+ }
+
+ public Task<T> ContinueWith(Action<Task<T>> continuation) {
+ return null;
+ }
+
+ public Task<T> Done(Action<T> callback) {
+ return null;
+ }
+ }
+}
@@ -0,0 +1,22 @@
+// TaskStatus.cs
+// Script#/Libraries/CoreLib
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+using System.Runtime.CompilerServices;
+
+namespace System {
+
+ [Imported]
+ [IgnoreNamespace]
+ [NamedValues]
+ public enum TaskStatus {
+
+ Pending,
+
+ Done,
+
+ Failed
+ }
+}
@@ -0,0 +1,145 @@
+///////////////////////////////////////////////////////////////////////////////
+// Task
+
+ss.Task = function(result) {
+ this._continuations = ss.isValue(result) ?
+ (this.status = 'done', null) :
+ (this.status = 'pending', []);
+ this.result = result;
+ this.error = null;
+}
+ss.Task.prototype = {
+ get_completed: function() {
+ return this.status != 'pending';
+ },
+ continueWith: function(continuation) {
+ if (this._continuations) {
+ this._continuations.push(continuation);
+ }
+ else {
+ var self = this;
+ setTimeout(function() { continuation(self); }, 0);
+ }
+ return this;
+ },
+ done: function(callback) {
+ return this.continueWith(function(t) {
+ if (t.status == 'done') {
+ callback(t.result);
+ }
+ });
+ },
+ fail: function(callback) {
+ return this.continueWith(function(t) {
+ if (t.status == 'failed') {
+ callback(t.error);
+ }
+ });
+ },
+ then: function(doneCallback, failCallback) {
+ return this.continueWith(function(t) {
+ t.status == 'done' ? doneCallback(t.result) : failCallback(t.error);
+ });
+ },
+ _update: function(result, error) {
+ if (this.status == 'pending') {
+ if (error) {
+ this.error = error;
+ this.status = 'failed';
+ }
+ else {
+ this.result = result;
+ this.status = 'done';
+ }
+
+ var continuations = this._continuations;
+ this._continuations = null;
+
+ for (var i = 0, c = continuations.length; i < c; i++) {
+ continuations[i](this);
+ }
+ }
+ }
+};
+ss.Task._join = function(tasks, any) {
+ tasks = Array.toArray(tasks);
+#if DEBUG
+ ss.Debug.assert(tasks.length > 1);
+#endif // DEBUG
+
+ var count = tasks.length;
+
+ var interval = 0;
+ if ((count > 1) && (typeof tasks[0] == 'number')) {
+ interval = tasks[0];
+ tasks = tasks.slice(1);
+ count--;
+ }
+
+ var joinTask = new ss.Task();
+ var seen = 0;
+
+ function continuation(t) {
+ if (joinTask.status == 'pending') {
+ seen++;
+ if (any) {
+ joinTask._update(t);
+ }
+ else if (seen == count) {
+ joinTask._update(true);
+ }
+ }
+ }
+
+ function timeout() {
+ if (joinTask.status == 'pending') {
+ if (any) {
+ joinTask._update(null);
+ }
+ else {
+ joinTask._update(false);
+ }
+ }
+ }
+
+ if (interval != 0) {
+ setTimeout(timeout, interval);
+ }
+
+ for (var i = 0; i < count; i++) {
+ tasks[i].continueWith(continuation);
+ }
+
+ return joinTask;
+}
+ss.Task.all = function() {
+ return ss.Task._join(arguments, false);
+}
+ss.Task.any = function() {
+ return ss.Task._join(arguments, true);
+}
+ss.Task.delay = function(timeout) {
+ var timerTask = new ss.Task();
+
+ setTimeout(function() {
+ timerTask._update(true);
+ }, timeout);
+
+ return timerTask;
+}
+
+
+ss.Deferred = function(result) {
+ this.task = new ss.Task(result);
+}
+ss.Deferred.prototype = {
+ resolve: function(result) {
+ this.task._update(result);
+ },
+ reject: function(error) {
+ this.task._update(null, error || new Error());
+ }
+};
+
+ss.Deferred.registerClass('Deferred');
+ss.Task.registerClass('Task');
@@ -15,6 +15,7 @@
<OutputPath>..\..\..\bin\Release\</OutputPath>
</PropertyGroup>
<ItemGroup>
+ <Content Include="BCL\Task.js" />
<Content Include="loader.js" />
<Content Include="mscorlib.js" />
<Content Include="Extensions\Array.js" />
@@ -146,4 +146,6 @@ ss.parseXml = function(markup) {
#include "BCL\Observable.js"
+#include "BCL\Task.js"
+
#include "BCL\App.js"
Oops, something went wrong.

0 comments on commit ff9667a

Please sign in to comment.