[folks: 1/4] Make BackendStore a singleton. Helps bgo#628970.



commit a5acb1e9fd0a5635c4d8692c2f58f1ca0184bf91
Author: Travis Reitter <travis reitter collabora co uk>
Date:   Wed Sep 8 10:24:55 2010 -0700

    Make BackendStore a singleton. Helps bgo#628970.

 NEWS                             |   11 +++++++++++
 folks/backend-store.vala         |   20 +++++++++++++++++++-
 folks/individual-aggregator.vala |    2 +-
 tools/import.vala                |    2 +-
 4 files changed, 32 insertions(+), 3 deletions(-)
---
diff --git a/NEWS b/NEWS
index e69de29..145b6b4 100644
--- a/NEWS
+++ b/NEWS
@@ -0,0 +1,11 @@
+============================================================================
+API breaks in 0.2.0
+============================================================================
+
+BackendStore is now a singleton.
+
+Vala:
+BackendStore default constructor removed. Use BackendStore.dup().
+
+C:
+folks_backend_store_new() removed. Use folks_backend_store_dup().
diff --git a/folks/backend-store.vala b/folks/backend-store.vala
index b6aff32..eabd8c8 100644
--- a/folks/backend-store.vala
+++ b/folks/backend-store.vala
@@ -39,6 +39,7 @@ public class Folks.BackendStore : Object {
 
   private HashMap<string,Backend> backend_hash;
   private GLib.List<ModuleFinalizeFunc> finalize_funcs = null;
+  private static weak BackendStore instance;
 
   /**
    * Emitted when a backend has been added to the BackendStore.
@@ -53,7 +54,21 @@ public class Folks.BackendStore : Object {
   /**
    * Create a new BackendStore.
    */
-  public BackendStore ()
+  public static BackendStore dup ()
+    {
+      if (instance == null)
+        {
+          /* use an intermediate variable to force a strong reference */
+          var new_instance = new BackendStore ();
+          instance = new_instance;
+
+          return new_instance;
+        }
+
+      return instance;
+    }
+
+  private BackendStore ()
     {
       this.backend_hash = new HashMap<string,Backend> (str_hash, str_equal);
     }
@@ -63,6 +78,9 @@ public class Folks.BackendStore : Object {
       /* Finalize all the loaded modules */
       foreach (ModuleFinalizeFunc func in this.finalize_funcs)
         func (this);
+
+      /* manually clear the singleton instance */
+      instance = null;
     }
 
   /**
diff --git a/folks/individual-aggregator.vala b/folks/individual-aggregator.vala
index 26353cd..7b658ea 100644
--- a/folks/individual-aggregator.vala
+++ b/folks/individual-aggregator.vala
@@ -123,7 +123,7 @@ public class Folks.IndividualAggregator : Object
       this.linking_enabled = (disable_linking == null ||
           disable_linking == "no" || disable_linking == "0");
 
-      this.backend_store = new BackendStore ();
+      this.backend_store = BackendStore.dup ();
       this.backend_store.backend_available.connect (this.backend_available_cb);
     }
 
diff --git a/tools/import.vala b/tools/import.vala
index aca2a97..451290f 100644
--- a/tools/import.vala
+++ b/tools/import.vala
@@ -90,7 +90,7 @@ public class Folks.ImportTool : Object
 
   private static async bool import ()
     {
-      BackendStore backend_store = new BackendStore ();
+      BackendStore backend_store = BackendStore.dup ();
 
       try
         {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]